Skip to content

Commit d43535b

Browse files
maximilian-shclaude
andcommitted
Fix portfolio mode by adding secAccNo parameter to compactPortfolio
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b32ff3b commit d43535b

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

pytr/api.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class TradeRepublicApi:
6666
_subscription_id_counter = 1
6767
_previous_responses: Dict[str, str] = {}
6868
subscriptions: Dict[str, Dict[str, Any]] = {}
69+
_secAccNo = None
6970

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

411412
async def compact_portfolio(self):
412-
return await self.subscribe({"type": "compactPortfolio"})
413+
# Ensure secAccNo is available
414+
if self._secAccNo is None:
415+
self.settings()
416+
if self._secAccNo is None:
417+
raise ValueError("secAccNo not available. Unable to retrieve securities account number.")
418+
return await self.subscribe({"type": "compactPortfolio", "secAccNo": self._secAccNo})
419+
420+
async def compact_portfolio_by_type(self):
421+
# Ensure secAccNo is available
422+
if self._secAccNo is None:
423+
self.settings()
424+
if self._secAccNo is None:
425+
raise ValueError("secAccNo not available. Unable to retrieve securities account number.")
426+
return await self.subscribe({"type": "compactPortfolioByType", "secAccNo": self._secAccNo})
413427

414428
async def watchlist(self):
415429
return await self.subscribe({"type": "watchlist"})
@@ -749,7 +763,11 @@ def settings(self):
749763
else:
750764
r = self._sign_request("/api/v1/auth/account", method="GET")
751765
r.raise_for_status()
752-
return r.json()
766+
settings_data = r.json()
767+
# Extract secAccNo from settings if available
768+
if self._secAccNo is None and "securitiesAccountNumber" in settings_data:
769+
self._secAccNo = settings_data["securitiesAccountNumber"]
770+
return settings_data
753771

754772
def order_cost(self, isin, exchange, order_mode, order_type, size, sell_fractions):
755773
url = (

0 commit comments

Comments
 (0)