Skip to content
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
22 changes: 20 additions & 2 deletions pytr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class TradeRepublicApi:
_subscription_id_counter = 1
_previous_responses: Dict[str, str] = {}
subscriptions: Dict[str, Dict[str, Any]] = {}
_secAccNo = None

_credentials_file = CREDENTIALS_FILE
_cookies_file = COOKIES_FILE
Expand Down Expand Up @@ -409,7 +410,20 @@ async def portfolio_status(self):
return await self.subscribe({"type": "portfolioStatus"})

async def compact_portfolio(self):
return await self.subscribe({"type": "compactPortfolio"})
# Ensure secAccNo is available
if self._secAccNo is None:
self.settings()
if self._secAccNo is None:
raise ValueError("secAccNo not available. Unable to retrieve securities account number.")
return await self.subscribe({"type": "compactPortfolio", "secAccNo": self._secAccNo})

async def compact_portfolio_by_type(self):
# Ensure secAccNo is available
if self._secAccNo is None:
self.settings()
if self._secAccNo is None:
raise ValueError("secAccNo not available. Unable to retrieve securities account number.")
return await self.subscribe({"type": "compactPortfolioByType", "secAccNo": self._secAccNo})

async def watchlist(self):
return await self.subscribe({"type": "watchlist"})
Expand Down Expand Up @@ -749,7 +763,11 @@ def settings(self):
else:
r = self._sign_request("/api/v1/auth/account", method="GET")
r.raise_for_status()
return r.json()
settings_data = r.json()
# Extract secAccNo from settings if available
if self._secAccNo is None and "securitiesAccountNumber" in settings_data:
self._secAccNo = settings_data["securitiesAccountNumber"]
return settings_data

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all these locations still relevant to try?

In my case the securities account number is stored in settings_data["securitiesAccountNumber"]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — simplified to only use settings_data["securitiesAccountNumber"]. The other fallbacks were speculative. Same field works for me as well.

def order_cost(self, isin, exchange, order_mode, order_type, size, sell_fractions):
url = (
Expand Down