06/03 Optimize codes, fix Schwab Account

This commit is contained in:
2026-06-03 14:04:39 -04:00
parent b75f18d6c8
commit 5c8854a65a
3 changed files with 67 additions and 9 deletions
+14 -3
View File
@@ -239,15 +239,26 @@ def sync_account_snapshot(schwab_account):
# ── 2. Positions ──────────────────────────────────────────────────────────
positions_synced = 0
for pos in sec.get('positions', []):
instrument = pos.get('instrument', {})
raw_positions = sec.get('positions') or [] # guard: API may send null
log.info('[schwab] %s has %d position(s) in API response',
schwab_account.account_name, len(raw_positions))
for pos in raw_positions:
instrument = pos.get('instrument') or {}
asset_key = instrument.get('assetType', '')
symbol = (instrument.get('symbol') or '').upper().strip()
long_qty = float(pos.get('longQuantity') or 0)
pfm_type = ASSET_TYPE_MAP.get(asset_key)
if not pfm_type or not symbol or long_qty <= 0:
log.debug('[schwab] position: symbol=%s assetType=%s longQty=%s pfm_type=%s',
symbol, asset_key, long_qty, pfm_type)
# Skip unknowns, empty symbols, and zero-quantity positions
if not symbol or long_qty <= 0:
continue
# Fall back to 'other' if the asset type isn't in our map
if not pfm_type:
pfm_type = 'other'
avg_price = float(pos.get('averagePrice') or pos.get('averageLongPrice') or 0)
market_value = float(pos.get('marketValue') or 0)