diff --git a/pytr/portfolio.py b/pytr/portfolio.py index 7b92a08..b585b48 100644 --- a/pytr/portfolio.py +++ b/pytr/portfolio.py @@ -38,41 +38,44 @@ 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']) - 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() - 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)}")