Skip to content

Commit

Permalink
fix portfolio command
Browse files Browse the repository at this point in the history
  • Loading branch information
marzzzello committed Oct 29, 2023
1 parent 0390f3a commit 6110926
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions pytr/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def portfolio_loop(self):
await self.tr.unsubscribe(subscription_id)
pos = subscriptions[subscription_id]
subscriptions.pop(subscription_id, None)
pos['netValue'] = response['last']['price'] * pos['netSize']
pos['netValue'] = float(response['last']['price']) * float(pos['netSize'])
else:
print(f"unmatched subscription of type '{subscription['type']}':\n{preview(response)}")

Expand All @@ -81,27 +81,31 @@ def overview(self):
# print(f'{x:24}: {self.portfolio[x]:>10.2f}')
# print()

print('Name ISIN avgCost * quantity = buyCost -> netValue diff %-diff')
print(
'Name ISIN avgCost * quantity = buyCost -> netValue diff %-diff'
)
totalBuyCost = 0.0
totalNetValue = 0.0
positions = self.portfolio['positions']
for pos in sorted(positions, key=lambda x: x['netSize'], reverse=True):
# pos['netValue'] = 0 # TODO: Update the value from each Stock request
buyCost = pos['averageBuyIn'] * pos['netSize']
diff = pos['netValue'] - buyCost
buyCost = float(pos['averageBuyIn']) * float(pos['netSize'])
diff = float(pos['netValue']) - buyCost
if buyCost == 0:
diffP = 0.0
else:
diffP = ((pos['netValue'] / buyCost) - 1) * 100
totalBuyCost += buyCost
totalNetValue += pos['netValue']
totalNetValue += float(pos['netValue'])

print(
f"{pos['name']:<25} {pos['instrumentId']} {pos['averageBuyIn']:>10.2f} * {pos['netSize']:>10.2f}"
+ f" = {buyCost:>10.2f} -> {pos['netValue']:>10.2f} {diff:>10.2f} {diffP:>7.1f}%"
f"{pos['name']:<25} {pos['instrumentId']} {float(pos['averageBuyIn']):>10.2f} * {float(pos['netSize']):>10.2f}"
+ f" = {float(buyCost):>10.2f} -> {float(pos['netValue']):>10.2f} {diff:>10.2f} {diffP:>7.1f}%"
)

print('Name ISIN avgCost * quantity = buyCost -> netValue diff %-diff')
print(
'Name ISIN avgCost * quantity = buyCost -> netValue diff %-diff'
)
print()

diff = totalNetValue - totalBuyCost
Expand All @@ -111,7 +115,7 @@ def overview(self):
diffP = ((totalNetValue / totalBuyCost) - 1) * 100
print(f'Depot {totalBuyCost:>43.2f} -> {totalNetValue:>10.2f} {diff:>10.2f} {diffP:>7.1f}%')

cash = self.cash[0]['amount']
cash = float(self.cash[0]['amount'])
currency = self.cash[0]['currencyId']
print(f'Cash {currency} {cash:>40.2f} -> {cash:>10.2f}')
print(f'Total {cash+totalBuyCost:>43.2f} -> {cash+totalNetValue:>10.2f}')
Expand Down

0 comments on commit 6110926

Please sign in to comment.