price_monitor 汇总值重算 + total_assets修正
问题:price_monitor每2分钟更新个股价,但不更新 total_market_value/total_assets/cash/position_pct, 这些字段停留在import_holding_xls快照值,已严重过期。 导致报告显示错误的总资产和仓位。 修复: - 每次更新个股价后,实时重算 total_market_value = sum(shares*price) - cash 从 stale_report(Dad截图确认的可用现金)同步 - total_assets = market_value + available_cash + freeze - 避免价格无变化时不触发更新(timeout fallback保留)
This commit is contained in:
@@ -193,6 +193,40 @@ def refresh_data_prices():
|
||||
wl['updated_at'] = datetime.now().isoformat()
|
||||
json.dump(wl, open(WATCHLIST_PATH, 'w'), ensure_ascii=False, indent=2)
|
||||
|
||||
# --- 汇总值重算(2026-06-29 bugfix: 之前price_monitor只更新个股价,不更新汇总)---
|
||||
# total_market_value / total_assets / cash / position_pct 来自import_holding_xls快照
|
||||
# 价格更新后必须同步刷新汇总,否则报告使用过期汇总 → 现金/资产/仓位全错
|
||||
try:
|
||||
live_market_value = sum(
|
||||
h.get('shares', 0) * h.get('price', 0)
|
||||
for h in pf.get('holdings', [])
|
||||
)
|
||||
# 从 stale_report 读可用现金(Dad截图确认值)
|
||||
stale_cash = 0
|
||||
try:
|
||||
sr = json.load(open('/home/hmo/web-dashboard/data/strategy_staleness_report.json'))
|
||||
fallback_cash = pf.get('cash', 0) or 0
|
||||
stale_cash = sr.get('portfolio', {}).get('cash', fallback_cash)
|
||||
except:
|
||||
stale_cash = pf.get('cash', 0) or 0
|
||||
|
||||
old_mv = pf.get('total_market_value', 0)
|
||||
if abs(old_mv - live_market_value) > 0.01:
|
||||
pf['total_market_value'] = round(live_market_value, 2)
|
||||
|
||||
old_cash = pf.get('cash', 0)
|
||||
if abs(old_cash - stale_cash) > 0.01:
|
||||
pf['cash'] = stale_cash
|
||||
|
||||
pf['total_assets'] = round(live_market_value + stale_cash, 2)
|
||||
if pf['total_assets'] > 0:
|
||||
pf['position_pct'] = round(live_market_value / pf['total_assets'] * 100, 2)
|
||||
pf['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M')
|
||||
json.dump(pf, open(PORTFOLIO_PATH, 'w'), ensure_ascii=False, indent=2)
|
||||
except Exception as e:
|
||||
print(f" [汇总重算失败] {e}", flush=True)
|
||||
# --- 结束汇总重算 ---
|
||||
|
||||
return updated
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user