Improve navigating trades and positions by cross-linking
This commit is contained in:
@@ -49,7 +49,7 @@ class OpenPositions(BaseModel):
|
||||
def parse_prices(x):
|
||||
if float(x["long"]["units"]) > 0:
|
||||
return x["long"]["averagePrice"]
|
||||
elif float(x["short"]["units"]) > 0:
|
||||
elif float(x["short"]["units"]) < 0:
|
||||
return x["short"]["averagePrice"]
|
||||
else:
|
||||
return 0
|
||||
@@ -58,7 +58,7 @@ def parse_prices(x):
|
||||
def parse_units(x):
|
||||
if float(x["long"]["units"]) > 0:
|
||||
return x["long"]["units"]
|
||||
elif float(x["short"]["units"]) > 0:
|
||||
elif float(x["short"]["units"]) < 0:
|
||||
return x["short"]["units"]
|
||||
else:
|
||||
return 0
|
||||
@@ -67,7 +67,7 @@ def parse_units(x):
|
||||
def parse_value(x):
|
||||
if float(x["long"]["units"]) > 0:
|
||||
return D(x["long"]["units"]) * D(x["long"]["averagePrice"])
|
||||
elif float(x["short"]["units"]) > 0:
|
||||
elif float(x["short"]["units"]) < 0:
|
||||
return D(x["short"]["units"]) * D(x["short"]["averagePrice"])
|
||||
else:
|
||||
return 0
|
||||
@@ -76,12 +76,21 @@ def parse_value(x):
|
||||
def parse_side(x):
|
||||
if float(x["long"]["units"]) > 0:
|
||||
return "long"
|
||||
elif float(x["short"]["units"]) > 0:
|
||||
elif float(x["short"]["units"]) < 0:
|
||||
return "short"
|
||||
else:
|
||||
return "unknown"
|
||||
|
||||
|
||||
def parse_trade_ids(x, sum=-1):
|
||||
if float(x["long"]["units"]) > 0:
|
||||
return [str(int(y) + sum) for y in x["long"]["tradeIDs"]]
|
||||
elif float(x["short"]["units"]) < 0:
|
||||
return [str(int(y) + sum) for y in x["short"]["tradeIDs"]]
|
||||
else:
|
||||
return "unknown"
|
||||
|
||||
|
||||
OpenPositionsSchema = {
|
||||
"itemlist": (
|
||||
"positions",
|
||||
@@ -89,6 +98,7 @@ OpenPositionsSchema = {
|
||||
{
|
||||
"symbol": "instrument",
|
||||
"unrealized_pl": "unrealizedPL",
|
||||
"trade_ids": parse_trade_ids, # actual value is lower by 1
|
||||
"price": parse_prices,
|
||||
"units": parse_units,
|
||||
"side": parse_side,
|
||||
@@ -284,6 +294,9 @@ PositionDetailsSchema = {
|
||||
"units": lambda x: parse_units(x["position"]),
|
||||
"side": lambda x: parse_side(x["position"]),
|
||||
"value": lambda x: parse_value(x["position"]),
|
||||
"trade_ids": lambda x: parse_trade_ids(
|
||||
x["position"], sum=0
|
||||
), # this value is correct
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user