s-ttp commited on
Commit
1e316dd
·
verified ·
1 Parent(s): d6eceaf

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +12 -2
app.py CHANGED
@@ -65,17 +65,27 @@ async def nav():
65
  # Alpaca's portfolio/history posts a daily point that lags the LIVE equity (so the chart
66
  # would end on yesterday's close while the Day% KPI already shows today's move). Append a
67
  # current-equity point so the line matches the live KPIs.
 
68
  try:
69
  acct = await run_in_threadpool(alpaca.account)
70
  eq = acct.get("equity")
71
  if eq and (not pts or abs(eq - pts[-1]["equity"]) > 0.01):
72
- import time as _t
73
- pts.append({"t": int(_t.time()), "equity": float(eq)})
74
  except Exception:
75
  pass
76
  start = (datetime.fromtimestamp(pts[0]["t"], timezone.utc).strftime("%Y-%m-%d")
77
  if pts else "2024-01-01")
78
  spy = await run_in_threadpool(alpaca.spy_series, start)
 
 
 
 
 
 
 
 
 
79
  return {"fund": pts, "base_value": hist["base_value"], "spy": spy}
80
  except alpaca.AlpacaError as e:
81
  raise HTTPException(503, str(e))
 
65
  # Alpaca's portfolio/history posts a daily point that lags the LIVE equity (so the chart
66
  # would end on yesterday's close while the Day% KPI already shows today's move). Append a
67
  # current-equity point so the line matches the live KPIs.
68
+ live_added = False
69
  try:
70
  acct = await run_in_threadpool(alpaca.account)
71
  eq = acct.get("equity")
72
  if eq and (not pts or abs(eq - pts[-1]["equity"]) > 0.01):
73
+ pts.append({"t": int(datetime.now(timezone.utc).timestamp()), "equity": float(eq)})
74
+ live_added = True
75
  except Exception:
76
  pass
77
  start = (datetime.fromtimestamp(pts[0]["t"], timezone.utc).strftime("%Y-%m-%d")
78
  if pts else "2024-01-01")
79
  spy = await run_in_threadpool(alpaca.spy_series, start)
80
+ # Extend SPY to "now" too (its daily bars lag the same way) so both lines terminate together.
81
+ if live_added:
82
+ try:
83
+ q = (await run_in_threadpool(alpaca.quotes, ["SPY"])).get("SPY")
84
+ if q and q.get("price"):
85
+ spy = list(spy) + [{"t": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
86
+ "c": float(q["price"])}]
87
+ except Exception:
88
+ pass
89
  return {"fund": pts, "base_value": hist["base_value"], "spy": spy}
90
  except alpaca.AlpacaError as e:
91
  raise HTTPException(503, str(e))