From d319769fe086f94183626d1d7615df380647ee73 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 2 Nov 2022 19:09:50 +0000 Subject: [PATCH] Finish porting data structures for positions --- core/lib/schemas/alpaca_s.py | 3 ++ core/lib/schemas/oanda_s.py | 34 ++++++++++++++++++++-- core/templates/partials/position-list.html | 4 +-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/core/lib/schemas/alpaca_s.py b/core/lib/schemas/alpaca_s.py index 6f10c64..08d7de4 100644 --- a/core/lib/schemas/alpaca_s.py +++ b/core/lib/schemas/alpaca_s.py @@ -105,6 +105,9 @@ get_all_positions_schema = { "symbol": "symbol", "unrealized_pl": "unrealized_pl", "price": "current_price", + "units": "qty", + "side": "side", + "value": "market_value", } ], ) diff --git a/core/lib/schemas/oanda_s.py b/core/lib/schemas/oanda_s.py index f571c16..d421d84 100644 --- a/core/lib/schemas/oanda_s.py +++ b/core/lib/schemas/oanda_s.py @@ -118,14 +118,41 @@ class OpenPositions(BaseModel): def parse_prices(x): - if int(x["long"]["units"]) > 0: + if float(x["long"]["units"]) > 0: return x["long"]["averagePrice"] - elif int(x["short"]["units"]) > 0: + elif float(x["short"]["units"]) > 0: return x["short"]["averagePrice"] else: return 0 +def parse_units(x): + if float(x["long"]["units"]) > 0: + return x["long"]["units"] + elif float(x["short"]["units"]) > 0: + return x["short"]["units"] + else: + return 0 + + +def parse_value(x): + if float(x["long"]["units"]) > 0: + return float(x["long"]["units"]) * float(x["long"]["averagePrice"]) + elif float(x["short"]["units"]) > 0: + return float(x["short"]["units"]) * float(x["short"]["averagePrice"]) + else: + return 0 + + +def parse_side(x): + if float(x["long"]["units"]) > 0: + return "long" + elif float(x["short"]["units"]) > 0: + return "short" + else: + return "unknown" + + OpenPositions_schema = { "itemlist": ( "positions", @@ -134,6 +161,9 @@ OpenPositions_schema = { "symbol": "instrument", "unrealized_pl": "unrealizedPL", "price": parse_prices, + "units": parse_units, + "side": parse_side, + "value": parse_value, } ], ) diff --git a/core/templates/partials/position-list.html b/core/templates/partials/position-list.html index 63d1248..38247f8 100644 --- a/core/templates/partials/position-list.html +++ b/core/templates/partials/position-list.html @@ -18,8 +18,8 @@ {{ item.account }} {{ item.symbol }} {{ item.price }} - {{ item.qty }} - {{ item.market_value }} + {{ item.units }} + {{ item.value }} {{ item.unrealized_pl }} {{ item.side }}