Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle bonds calculation in portfolio #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pytr/portfolio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import re

from pytr.utils import preview

Expand Down Expand Up @@ -67,6 +68,17 @@ async def portfolio_loop(self):
await self.tr.unsubscribe(subscription_id)
pos = subscriptions.pop(subscription_id)
pos["netValue"] = float(response["last"]["price"]) * float(pos["netSize"])

# We need to calculate the netValue for bonds differently
# We need to identify if it is a bond name - bonds have "year like 2025" in their name:
bond_pattern = re.compile(
r'(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|January|February|March|April|May|June|July|August|September|October|November|December)\.?\s+20\d{2}',
re.IGNORECASE)

if bond_pattern.search(pos["name"]):
# Bond calculation - price is per $100 face value
pos["netValue"] = float(response["last"]["price"]) * float(pos["netSize"]) / 100

else:
print(f"unmatched subscription of type '{subscription['type']}':\n{preview(response)}")

Expand Down
Loading