Raise an error if a position has both short and long sides

This commit is contained in:
Mark Veidemanis 2022-12-02 07:20:37 +00:00
parent 1ce6c3fafa
commit c5d289ce85
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 15 additions and 0 deletions

View File

@ -46,7 +46,18 @@ class OpenPositions(BaseModel):
lastTransactionID: str
def prevent_hedging(x):
"""
Our implementation breaks if a position has both.
We implemented it this way in order to more easily support other exchanges.
The actual direction is put into the root object with Grom.
"""
if float(x["long"]["units"]) > 0 and float(x["short"]["units"]) < 0:
raise ValueError("Hedging not allowed")
def parse_prices(x):
prevent_hedging(x)
if float(x["long"]["units"]) > 0:
return x["long"]["averagePrice"]
elif float(x["short"]["units"]) < 0:
@ -56,6 +67,7 @@ def parse_prices(x):
def parse_units(x):
prevent_hedging(x)
if float(x["long"]["units"]) > 0:
return x["long"]["units"]
elif float(x["short"]["units"]) < 0:
@ -65,6 +77,7 @@ def parse_units(x):
def parse_value(x):
prevent_hedging(x)
if float(x["long"]["units"]) > 0:
return D(x["long"]["units"]) * D(x["long"]["averagePrice"])
elif float(x["short"]["units"]) < 0:
@ -74,6 +87,7 @@ def parse_value(x):
def parse_side(x):
prevent_hedging(x)
if float(x["long"]["units"]) > 0:
return "long"
elif float(x["short"]["units"]) < 0:
@ -83,6 +97,7 @@ def parse_side(x):
def parse_trade_ids(x, sum=0):
prevent_hedging(x)
if float(x["long"]["units"]) > 0:
return [str(int(y) + sum) for y in x["long"]["tradeIDs"]]
elif float(x["short"]["units"]) < 0: