From f32d66b852ecc18878759d497cc8a2c6764f9744 Mon Sep 17 00:00:00 2001 From: Cyril Pawelko Date: Mon, 6 May 2024 21:13:50 +0200 Subject: [PATCH 1/2] Get products from correct exchange (issue #48) --- pytr/portfolio.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/pytr/portfolio.py b/pytr/portfolio.py index 7b92a08..4e97ed8 100644 --- a/pytr/portfolio.py +++ b/pytr/portfolio.py @@ -38,41 +38,42 @@ async def portfolio_loop(self): await self.tr.unsubscribe(subscription_id) - # Populate netValue for each ISIN - positions = self.portfolio['positions'] + # Populate name for each ISIN subscriptions = {} + positions = self.portfolio['positions'] for pos in sorted(positions, key=lambda x: x['netSize'], reverse=True): isin = pos['instrumentId'] - # subscription_id = await self.tr.instrument_details(pos['instrumentId']) - subscription_id = await self.tr.ticker(isin, exchange='LSX') + subscription_id = await self.tr.instrument_details(pos['instrumentId']) subscriptions[subscription_id] = pos while len(subscriptions) > 0: subscription_id, subscription, response = await self.tr.recv() - if subscription['type'] == 'ticker': + if subscription['type'] == 'instrument': await self.tr.unsubscribe(subscription_id) pos = subscriptions[subscription_id] subscriptions.pop(subscription_id, None) - pos['netValue'] = float(response['last']['price']) * float(pos['netSize']) + pos['name'] = response['shortName'] + pos['exchangeIds'] = response['exchangeIds'] else: print(f"unmatched subscription of type '{subscription['type']}':\n{preview(response)}") - # Populate name for each ISIN + # Populate netValue for each ISIN subscriptions = {} for pos in sorted(positions, key=lambda x: x['netSize'], reverse=True): isin = pos['instrumentId'] - subscription_id = await self.tr.instrument_details(pos['instrumentId']) + # subscription_id = await self.tr.instrument_details(pos['instrumentId']) + subscription_id = await self.tr.ticker(isin, exchange=pos['exchangeIds'][0]) subscriptions[subscription_id] = pos while len(subscriptions) > 0: subscription_id, subscription, response = await self.tr.recv() - if subscription['type'] == 'instrument': + if subscription['type'] == 'ticker': await self.tr.unsubscribe(subscription_id) pos = subscriptions[subscription_id] subscriptions.pop(subscription_id, None) - pos['name'] = response['shortName'] + pos['netValue'] = float(response['last']['price']) * float(pos['netSize']) else: print(f"unmatched subscription of type '{subscription['type']}':\n{preview(response)}") From f67f7c781e18e7d309a9af28fcfb12d07fec70d0 Mon Sep 17 00:00:00 2001 From: Katzmann1983 Date: Fri, 24 May 2024 07:27:42 +0200 Subject: [PATCH 2/2] Fix for no exchange, e.g. dividends --- pytr/portfolio.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pytr/portfolio.py b/pytr/portfolio.py index 4e97ed8..b585b48 100644 --- a/pytr/portfolio.py +++ b/pytr/portfolio.py @@ -62,9 +62,11 @@ async def portfolio_loop(self): subscriptions = {} for pos in sorted(positions, key=lambda x: x['netSize'], reverse=True): isin = pos['instrumentId'] - # subscription_id = await self.tr.instrument_details(pos['instrumentId']) - subscription_id = await self.tr.ticker(isin, exchange=pos['exchangeIds'][0]) - subscriptions[subscription_id] = pos + if len(pos['exchangeIds']) > 0: + subscription_id = await self.tr.ticker(isin, exchange=pos['exchangeIds'][0]) + subscriptions[subscription_id] = pos + else: + pos['netValue'] = float(pos['averageBuyIn'] )* float(pos['netSize']) while len(subscriptions) > 0: subscription_id, subscription, response = await self.tr.recv()