22
33from typing import Any , Callable , Iterable
44
5- from .market_data import fetch_last_price
5+ from .market_data import fetch_last_prices
66
77
88def fetch_strategy_account_state (
@@ -31,11 +31,14 @@ def fetch_strategy_account_state(
3131 sellable_quantities = {symbol : 0.0 for symbol in assets }
3232 filter_enabled = bool (assets )
3333
34+ position_rows : list [tuple [str , str , Any , Any ]] = []
3435 positions_response = t_ctx .stock_positions ()
3536 if positions_response and hasattr (positions_response , "channels" ):
3637 for channel in positions_response .channels :
3738 for position in getattr (channel , "positions" , []):
38- full_symbol = getattr (position , "symbol" , "" )
39+ full_symbol = str (getattr (position , "symbol" , "" ) or "" ).strip ().upper ()
40+ if not full_symbol :
41+ continue
3942 root_symbol = full_symbol .split ("." )[0 ].strip ().upper ()
4043 if filter_enabled and root_symbol not in market_values :
4144 continue
@@ -57,15 +60,19 @@ def fetch_strategy_account_state(
5760 f"quantity={ raw_quantity } available_quantity={ raw_available_quantity } "
5861 )
5962
60- last_price = fetch_last_price (q_ctx , full_symbol )
61- if last_price is None :
62- continue
63+ position_rows .append ((root_symbol , full_symbol , raw_quantity , raw_available_quantity ))
64+
65+ prices = fetch_last_prices (q_ctx , [full_symbol for _root_symbol , full_symbol , _quantity , _available in position_rows ])
66+ for root_symbol , full_symbol , raw_quantity , raw_available_quantity in position_rows :
67+ last_price = prices .get (full_symbol )
68+ if last_price is None :
69+ continue
6370
64- quantity = float (raw_quantity )
65- available_quantity = float (raw_available_quantity )
66- market_values [root_symbol ] += quantity * last_price
67- quantities [root_symbol ] += quantity
68- sellable_quantities [root_symbol ] += available_quantity
71+ quantity = float (raw_quantity )
72+ available_quantity = float (raw_available_quantity )
73+ market_values [root_symbol ] += quantity * last_price
74+ quantities [root_symbol ] += quantity
75+ sellable_quantities [root_symbol ] += available_quantity
6976
7077 if position_log_fn is not None :
7178 for symbol in assets or tuple (sorted (quantities )):
0 commit comments