Raise an error if a position has both short and long sides
This commit is contained in:
parent
1ce6c3fafa
commit
c5d289ce85
|
@ -46,7 +46,18 @@ class OpenPositions(BaseModel):
|
||||||
lastTransactionID: str
|
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):
|
def parse_prices(x):
|
||||||
|
prevent_hedging(x)
|
||||||
if float(x["long"]["units"]) > 0:
|
if float(x["long"]["units"]) > 0:
|
||||||
return x["long"]["averagePrice"]
|
return x["long"]["averagePrice"]
|
||||||
elif float(x["short"]["units"]) < 0:
|
elif float(x["short"]["units"]) < 0:
|
||||||
|
@ -56,6 +67,7 @@ def parse_prices(x):
|
||||||
|
|
||||||
|
|
||||||
def parse_units(x):
|
def parse_units(x):
|
||||||
|
prevent_hedging(x)
|
||||||
if float(x["long"]["units"]) > 0:
|
if float(x["long"]["units"]) > 0:
|
||||||
return x["long"]["units"]
|
return x["long"]["units"]
|
||||||
elif float(x["short"]["units"]) < 0:
|
elif float(x["short"]["units"]) < 0:
|
||||||
|
@ -65,6 +77,7 @@ def parse_units(x):
|
||||||
|
|
||||||
|
|
||||||
def parse_value(x):
|
def parse_value(x):
|
||||||
|
prevent_hedging(x)
|
||||||
if float(x["long"]["units"]) > 0:
|
if float(x["long"]["units"]) > 0:
|
||||||
return D(x["long"]["units"]) * D(x["long"]["averagePrice"])
|
return D(x["long"]["units"]) * D(x["long"]["averagePrice"])
|
||||||
elif float(x["short"]["units"]) < 0:
|
elif float(x["short"]["units"]) < 0:
|
||||||
|
@ -74,6 +87,7 @@ def parse_value(x):
|
||||||
|
|
||||||
|
|
||||||
def parse_side(x):
|
def parse_side(x):
|
||||||
|
prevent_hedging(x)
|
||||||
if float(x["long"]["units"]) > 0:
|
if float(x["long"]["units"]) > 0:
|
||||||
return "long"
|
return "long"
|
||||||
elif float(x["short"]["units"]) < 0:
|
elif float(x["short"]["units"]) < 0:
|
||||||
|
@ -83,6 +97,7 @@ def parse_side(x):
|
||||||
|
|
||||||
|
|
||||||
def parse_trade_ids(x, sum=0):
|
def parse_trade_ids(x, sum=0):
|
||||||
|
prevent_hedging(x)
|
||||||
if float(x["long"]["units"]) > 0:
|
if float(x["long"]["units"]) > 0:
|
||||||
return [str(int(y) + sum) for y in x["long"]["tradeIDs"]]
|
return [str(int(y) + sum) for y in x["long"]["tradeIDs"]]
|
||||||
elif float(x["short"]["units"]) < 0:
|
elif float(x["short"]["units"]) < 0:
|
||||||
|
|
Loading…
Reference in New Issue