diff --git a/core/lib/schemas/oanda_s.py b/core/lib/schemas/oanda_s.py index 54d55df..6b401ee 100644 --- a/core/lib/schemas/oanda_s.py +++ b/core/lib/schemas/oanda_s.py @@ -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: