Spaces:
Running
Running
Upload 2 files
Browse files- app.py +314 -312
- runtime.py +13 -2
app.py
CHANGED
|
@@ -7,40 +7,40 @@ from datetime import date, datetime, time, timedelta
|
|
| 7 |
import sys
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
-
from fastapi import BackgroundTasks, HTTPException, Query
|
| 11 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
-
from fastapi import FastAPI
|
| 13 |
-
from pydantic import BaseModel
|
| 14 |
|
| 15 |
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
| 16 |
-
from nifty_backend.runtime import (
|
| 17 |
-
CLOSE_REFRESH_READY,
|
| 18 |
-
IST,
|
| 19 |
-
STALE_CHECK_INTERVAL_SECONDS,
|
| 20 |
-
TPLUS1_READY,
|
| 21 |
-
close_refresh_due,
|
| 22 |
-
dashboard_payload,
|
| 23 |
-
is_trading_day,
|
| 24 |
-
latest_saved_prediction,
|
| 25 |
-
latest_tplus1_prediction,
|
| 26 |
-
next_trading_day,
|
| 27 |
-
refresh_daily_data,
|
| 28 |
-
refresh_first5_prediction,
|
| 29 |
-
refresh_market_close_data,
|
| 30 |
-
refresh_stale_data_once,
|
| 31 |
-
refresh_tplus1_prediction,
|
| 32 |
-
seconds_until_next_ist_run,
|
| 33 |
-
warm_dashboard_payload_cache,
|
| 34 |
-
)
|
| 35 |
-
from kotak_neo import (
|
| 36 |
-
KotakNeoConfigError,
|
| 37 |
-
KotakNeoError,
|
| 38 |
-
KotakNeoSessionRequired,
|
| 39 |
-
kotak_neo_manager,
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
app = FastAPI(title="NIFTY 50 Forecaster Backend")
|
| 44 |
app.add_middleware(
|
| 45 |
CORSMiddleware,
|
| 46 |
allow_origins=["*"],
|
|
@@ -50,19 +50,19 @@ app.add_middleware(
|
|
| 50 |
)
|
| 51 |
|
| 52 |
|
| 53 |
-
market_status = "Waiting for next session"
|
| 54 |
-
close_refresh_lock = threading.Lock()
|
| 55 |
-
tplus1_refresh_lock = threading.Lock()
|
| 56 |
-
MARKET_OPEN = time(9, 15)
|
| 57 |
-
FIRST5_READY = time(9, 20)
|
| 58 |
-
MARKET_CLOSE = time(15, 30)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
class TotpRequest(BaseModel):
|
| 62 |
-
totp: str
|
| 63 |
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
|
|
|
| 66 |
if not close_refresh_due():
|
| 67 |
return {"status": "skipped", "reason": "close refresh is not due"}
|
| 68 |
if not close_refresh_lock.acquire(blocking=False):
|
|
@@ -70,39 +70,39 @@ def refresh_market_close_data_if_due() -> dict:
|
|
| 70 |
try:
|
| 71 |
info = refresh_market_close_data()
|
| 72 |
return {"status": "refreshed", **info}
|
| 73 |
-
finally:
|
| 74 |
-
close_refresh_lock.release()
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
def latest_tplus1_prediction_date(payload: dict | None = None) -> date | None:
|
| 78 |
-
try:
|
| 79 |
-
latest = payload if payload is not None else latest_tplus1_prediction()
|
| 80 |
-
raw = latest.get("input_date")
|
| 81 |
-
return date.fromisoformat(str(raw)[:10]) if raw else None
|
| 82 |
-
except Exception:
|
| 83 |
-
return None
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
def tplus1_refresh_due(now: datetime | None = None, latest_date: date | None = None) -> bool:
|
| 87 |
-
now = now or datetime.now(IST)
|
| 88 |
-
if not is_trading_day(now.date()) or not (TPLUS1_READY <= now.time() < MARKET_CLOSE):
|
| 89 |
-
return False
|
| 90 |
-
latest_date = latest_date if latest_date is not None else latest_tplus1_prediction_date()
|
| 91 |
-
return latest_date != now.date()
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
def refresh_tplus1_if_due() -> dict:
|
| 95 |
-
now = datetime.now(IST)
|
| 96 |
-
latest_date = latest_tplus1_prediction_date()
|
| 97 |
-
if not tplus1_refresh_due(now=now, latest_date=latest_date):
|
| 98 |
-
return {"status": "skipped", "reason": "tplus1 refresh is not due"}
|
| 99 |
-
if not tplus1_refresh_lock.acquire(blocking=False):
|
| 100 |
-
return {"status": "skipped", "reason": "tplus1 refresh already running"}
|
| 101 |
-
try:
|
| 102 |
-
prediction = refresh_tplus1_prediction(session_date=now.date())
|
| 103 |
-
return {"status": "refreshed", "prediction": prediction}
|
| 104 |
-
finally:
|
| 105 |
-
tplus1_refresh_lock.release()
|
| 106 |
|
| 107 |
|
| 108 |
def latest_prediction_date(payload: dict | None = None) -> date | None:
|
|
@@ -114,7 +114,7 @@ def latest_prediction_date(payload: dict | None = None) -> date | None:
|
|
| 114 |
return None
|
| 115 |
|
| 116 |
|
| 117 |
-
def current_market_state(now: datetime | None = None) -> dict:
|
| 118 |
global market_status
|
| 119 |
now = now or datetime.now(IST)
|
| 120 |
today = now.date()
|
|
@@ -124,8 +124,8 @@ def current_market_state(now: datetime | None = None) -> dict:
|
|
| 124 |
market_is_open_for_t5 = trading_day and FIRST5_READY <= current_time < MARKET_CLOSE
|
| 125 |
market_is_open_for_tplus1 = trading_day and TPLUS1_READY <= current_time < MARKET_CLOSE
|
| 126 |
has_current_first5 = market_is_open_for_t5 and latest_date == today
|
| 127 |
-
tplus1_latest_date = latest_tplus1_prediction_date()
|
| 128 |
-
has_current_tplus1 = market_is_open_for_tplus1 and tplus1_latest_date == today
|
| 129 |
next_session = today if trading_day and current_time < MARKET_CLOSE else next_trading_day(today + timedelta(days=1))
|
| 130 |
|
| 131 |
if not trading_day:
|
|
@@ -150,48 +150,48 @@ def current_market_state(now: datetime | None = None) -> dict:
|
|
| 150 |
else:
|
| 151 |
status = "Prediction Pending"
|
| 152 |
detail = "No current-session prediction has been generated yet."
|
| 153 |
-
else:
|
| 154 |
-
status = "Market Closed"
|
| 155 |
-
detail = "Trading session has ended."
|
| 156 |
-
|
| 157 |
-
if not trading_day:
|
| 158 |
-
tplus1_status = "Market Closed"
|
| 159 |
-
tplus1_detail = f"Next trading session is {next_session.isoformat()}."
|
| 160 |
-
elif current_time < TPLUS1_READY:
|
| 161 |
-
tplus1_status = "Waiting for 2:30 PM"
|
| 162 |
-
tplus1_detail = "The T+1 forecast becomes available at 2:30 PM IST."
|
| 163 |
-
elif current_time < MARKET_CLOSE:
|
| 164 |
-
if has_current_tplus1:
|
| 165 |
-
tplus1_status = "Ready"
|
| 166 |
-
tplus1_detail = "Today's T+1 prediction is available."
|
| 167 |
-
else:
|
| 168 |
-
tplus1_status = "Pending"
|
| 169 |
-
tplus1_detail = "No current-session T+1 prediction has been generated yet."
|
| 170 |
-
else:
|
| 171 |
-
tplus1_status = "Market Closed"
|
| 172 |
-
tplus1_detail = "Trading session has ended."
|
| 173 |
-
|
| 174 |
-
if not trading_day:
|
| 175 |
-
t5_status = "Market Closed"
|
| 176 |
-
t5_detail = f"Next trading session is {next_session.isoformat()}."
|
| 177 |
-
elif current_time < FIRST5_READY:
|
| 178 |
-
t5_status = "Waiting for 9:20 AM"
|
| 179 |
-
t5_detail = "The T+5 forecast becomes available after the first five one-minute bars."
|
| 180 |
-
elif current_time < MARKET_CLOSE:
|
| 181 |
-
if market_status in {"Fetching T+5 Prediction Data...", "Prediction Failed"}:
|
| 182 |
-
t5_status = market_status
|
| 183 |
-
t5_detail = "The first-five-minute prediction job is still resolving."
|
| 184 |
-
elif has_current_first5:
|
| 185 |
-
t5_status = "Ready"
|
| 186 |
-
t5_detail = "Today's first-five-minute prediction is available."
|
| 187 |
-
else:
|
| 188 |
-
t5_status = "Pending"
|
| 189 |
-
t5_detail = "No current-session prediction has been generated yet."
|
| 190 |
-
else:
|
| 191 |
-
t5_status = "Market Closed"
|
| 192 |
-
t5_detail = "Trading session has ended."
|
| 193 |
-
|
| 194 |
-
return {
|
| 195 |
"market_status": status,
|
| 196 |
"market_detail": detail,
|
| 197 |
"server_time_ist": now.isoformat(),
|
|
@@ -199,41 +199,41 @@ def current_market_state(now: datetime | None = None) -> dict:
|
|
| 199 |
"session_date": today.isoformat(),
|
| 200 |
"next_session_date": next_session.isoformat(),
|
| 201 |
"latest_prediction_date": latest_date.isoformat() if latest_date else None,
|
| 202 |
-
"t5_available": has_current_first5,
|
| 203 |
-
"t5_status": t5_status,
|
| 204 |
-
"t5_detail": t5_detail,
|
| 205 |
-
"market_is_open_for_t5": market_is_open_for_t5,
|
| 206 |
-
"tplus1_available": has_current_tplus1,
|
| 207 |
-
"tplus1_status": tplus1_status,
|
| 208 |
-
"tplus1_detail": tplus1_detail,
|
| 209 |
-
"market_is_open_for_tplus1": market_is_open_for_tplus1,
|
| 210 |
-
"latest_tplus1_prediction_date": tplus1_latest_date.isoformat() if tplus1_latest_date else None,
|
| 211 |
-
}
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
def attach_market_state(payload: dict) -> dict:
|
| 215 |
-
state = current_market_state()
|
| 216 |
-
payload.setdefault("data_status", {})
|
| 217 |
-
payload["data_status"].update(state)
|
| 218 |
-
try:
|
| 219 |
-
payload["nifty_quote"] = kotak_neo_manager.fetch_nifty50_quote()
|
| 220 |
-
payload["nifty_quote_error"] = None
|
| 221 |
-
except KotakNeoSessionRequired as exc:
|
| 222 |
-
payload["nifty_quote"] = None
|
| 223 |
-
payload["nifty_quote_error"] = {"status": 401, "message": str(exc)}
|
| 224 |
-
except KotakNeoConfigError as exc:
|
| 225 |
-
payload["nifty_quote"] = None
|
| 226 |
-
payload["nifty_quote_error"] = {"status": 503, "message": str(exc)}
|
| 227 |
-
except KotakNeoError as exc:
|
| 228 |
-
payload["nifty_quote"] = None
|
| 229 |
-
payload["nifty_quote_error"] = {"status": 502, "message": str(exc)}
|
| 230 |
-
|
| 231 |
-
t5_latest = payload.get("latest") or {}
|
| 232 |
tomorrow_latest = payload.get("tomorrow_latest") or {}
|
| 233 |
tplus1_latest = payload.get("tplus1_latest") or {}
|
| 234 |
t5_available = bool(state["t5_available"] and t5_latest.get("prediction"))
|
| 235 |
tplus1_available = bool(state["tplus1_available"] and tplus1_latest.get("prediction"))
|
| 236 |
-
tomorrow_available = bool(tomorrow_latest.get("prediction"))
|
| 237 |
refresh_phase = payload.get("data_status", {}).get("refresh_phase")
|
| 238 |
if refresh_phase in {"waiting_second_payload", "refreshing"}:
|
| 239 |
tomorrow_status = "WAITING FOR SECOND PAYLOAD"
|
|
@@ -257,40 +257,40 @@ def attach_market_state(payload: dict) -> dict:
|
|
| 257 |
"validation_accuracy": tomorrow_latest.get("validation_accuracy"),
|
| 258 |
"test_accuracy": tomorrow_latest.get("test_accuracy"),
|
| 259 |
},
|
| 260 |
-
"t5": {
|
| 261 |
-
"available": t5_available,
|
| 262 |
-
"status": "Ready" if t5_available else state["t5_status"],
|
| 263 |
-
"reason": None if t5_available else state["t5_detail"],
|
| 264 |
-
"input_date": t5_latest.get("input_date"),
|
| 265 |
-
"prediction": t5_latest.get("prediction") if t5_available else None,
|
| 266 |
-
"prob_up": t5_latest.get("prob_up") if t5_available else None,
|
| 267 |
-
"confidence": t5_latest.get("confidence") if t5_available else None,
|
| 268 |
-
"threshold": t5_latest.get("threshold") if t5_available else None,
|
| 269 |
-
"is_overridden": bool(t5_latest.get("is_overridden")) if t5_available else False,
|
| 270 |
-
"model_name": t5_latest.get("model_name"),
|
| 271 |
-
"validation_accuracy": (payload.get("summary") or {}).get("validation_accuracy"),
|
| 272 |
-
"test_accuracy": (payload.get("summary") or {}).get("test_accuracy"),
|
| 273 |
},
|
| 274 |
-
"tplus1": {
|
| 275 |
-
"available": tplus1_available,
|
| 276 |
-
"status": "Ready" if tplus1_available else state["tplus1_status"],
|
| 277 |
-
"reason": None if tplus1_available else state["tplus1_detail"],
|
| 278 |
-
"target_date": tplus1_latest.get("target_date") or state["next_session_date"],
|
| 279 |
-
"input_date": tplus1_latest.get("input_date"),
|
| 280 |
"prediction": tplus1_latest.get("prediction") if tplus1_available else None,
|
| 281 |
-
"prob_up": tplus1_latest.get("prob_up") if tplus1_available else None,
|
| 282 |
-
"confidence": tplus1_latest.get("confidence") if tplus1_available else None,
|
| 283 |
-
"threshold": tplus1_latest.get("threshold") if tplus1_available else None,
|
| 284 |
-
"is_overridden": bool(tplus1_latest.get("overlay_changed")) if tplus1_available else False,
|
| 285 |
-
"model_name": tplus1_latest.get("model_name"),
|
| 286 |
-
"validation_accuracy": (payload.get("tplus1_summary") or {}).get("validation_accuracy"),
|
| 287 |
-
"test_accuracy": (payload.get("tplus1_summary") or {}).get("test_accuracy"),
|
| 288 |
},
|
| 289 |
}
|
| 290 |
return payload
|
| 291 |
|
| 292 |
|
| 293 |
-
async def daily_ist_refresh_loop() -> None:
|
| 294 |
global market_status
|
| 295 |
while True:
|
| 296 |
# Wait until 9:00 AM IST
|
|
@@ -318,22 +318,22 @@ async def daily_ist_refresh_loop() -> None:
|
|
| 318 |
print(f"[scheduler] first5 refresh failed: {exc}", flush=True)
|
| 319 |
market_status = "Prediction Failed"
|
| 320 |
|
| 321 |
-
try:
|
| 322 |
-
await asyncio.to_thread(refresh_daily_data)
|
| 323 |
-
except Exception as exc:
|
| 324 |
-
print(f"[scheduler] daily refresh failed: {exc}", flush=True)
|
| 325 |
-
|
| 326 |
-
await asyncio.sleep(seconds_until_next_ist_run(TPLUS1_READY))
|
| 327 |
-
print("[scheduler] 2:30 PM IST - Refreshing T+1 prediction", flush=True)
|
| 328 |
-
try:
|
| 329 |
-
info = await asyncio.to_thread(refresh_tplus1_if_due)
|
| 330 |
-
print(f"[scheduler] tplus1 refresh result: {info}", flush=True)
|
| 331 |
-
except Exception as exc:
|
| 332 |
-
print(f"[scheduler] tplus1 refresh failed: {exc}", flush=True)
|
| 333 |
-
|
| 334 |
-
await asyncio.sleep(seconds_until_next_ist_run(CLOSE_REFRESH_READY))
|
| 335 |
-
print("[scheduler] 3:45 PM IST - Refreshing close data", flush=True)
|
| 336 |
-
try:
|
| 337 |
info = await asyncio.to_thread(refresh_market_close_data_if_due)
|
| 338 |
print(f"[scheduler] close refresh result: {info}", flush=True)
|
| 339 |
except Exception as exc:
|
|
@@ -361,44 +361,44 @@ async def refresh_current_session_once() -> None:
|
|
| 361 |
print(f"[startup] daily refresh failed: {exc}", flush=True)
|
| 362 |
|
| 363 |
|
| 364 |
-
async def refresh_market_close_once_if_due() -> None:
|
| 365 |
try:
|
| 366 |
info = await asyncio.to_thread(refresh_market_close_data_if_due)
|
| 367 |
if info.get("status") == "refreshed":
|
| 368 |
print(f"[startup] close refresh result: {info}", flush=True)
|
| 369 |
except Exception as exc:
|
| 370 |
-
print(f"[startup] close refresh failed: {exc}", flush=True)
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
async def refresh_tplus1_once_if_due() -> None:
|
| 374 |
-
try:
|
| 375 |
-
info = await asyncio.to_thread(refresh_tplus1_if_due)
|
| 376 |
-
if info.get("status") == "refreshed":
|
| 377 |
-
print(f"[startup] tplus1 refresh result: {info}", flush=True)
|
| 378 |
-
except Exception as exc:
|
| 379 |
-
print(f"[startup] tplus1 refresh failed: {exc}", flush=True)
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
async def warm_dashboard_payload_cache_once() -> None:
|
| 383 |
-
try:
|
| 384 |
-
await asyncio.to_thread(warm_dashboard_payload_cache)
|
| 385 |
-
except Exception as exc:
|
| 386 |
-
print(f"[startup] dashboard payload warmup failed: {exc}", flush=True)
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
async def stale_data_watch_loop() -> None:
|
| 390 |
-
while True:
|
| 391 |
-
try:
|
| 392 |
-
info = await asyncio.to_thread(refresh_stale_data_once)
|
| 393 |
-
if info.get("status") == "refreshed":
|
| 394 |
-
print(f"[stale-watch] refreshed stale data: {info}", flush=True)
|
| 395 |
-
except Exception as exc:
|
| 396 |
-
print(f"[stale-watch] stale refresh failed: {exc}", flush=True)
|
| 397 |
-
await asyncio.sleep(STALE_CHECK_INTERVAL_SECONDS)
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
@app.on_event("startup")
|
| 401 |
-
async def start_scheduler() -> None:
|
| 402 |
global market_status
|
| 403 |
# Initialize correct status on startup based on current time
|
| 404 |
now = datetime.now(IST).time()
|
|
@@ -416,12 +416,12 @@ async def start_scheduler() -> None:
|
|
| 416 |
else:
|
| 417 |
market_status = "Prediction Pending"
|
| 418 |
|
| 419 |
-
asyncio.create_task(refresh_current_session_once())
|
| 420 |
-
asyncio.create_task(refresh_tplus1_once_if_due())
|
| 421 |
-
asyncio.create_task(refresh_market_close_once_if_due())
|
| 422 |
-
asyncio.create_task(warm_dashboard_payload_cache_once())
|
| 423 |
-
asyncio.create_task(stale_data_watch_loop())
|
| 424 |
-
asyncio.create_task(daily_ist_refresh_loop())
|
| 425 |
|
| 426 |
|
| 427 |
@app.get("/health")
|
|
@@ -434,87 +434,89 @@ def root() -> dict[str, str]:
|
|
| 434 |
return {"service": "NIFTY 50 Forecaster Backend", "status": "ok"}
|
| 435 |
|
| 436 |
|
| 437 |
-
@app.get("/dashboard")
|
| 438 |
-
def dashboard() -> dict:
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
except
|
| 458 |
-
raise HTTPException(status_code=
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
except
|
| 468 |
-
raise HTTPException(status_code=
|
| 469 |
-
except
|
| 470 |
-
raise HTTPException(status_code=
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
except
|
| 480 |
-
raise HTTPException(status_code=
|
| 481 |
-
except
|
| 482 |
-
raise HTTPException(status_code=
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
"
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
except
|
| 497 |
-
raise HTTPException(status_code=
|
| 498 |
-
except
|
| 499 |
-
raise HTTPException(status_code=
|
|
|
|
|
|
|
| 500 |
|
| 501 |
|
| 502 |
@app.get("/cron/keepalive")
|
| 503 |
-
def cron_keepalive(background_tasks: BackgroundTasks) -> dict:
|
| 504 |
-
close_refresh = {"status": "not_checked"}
|
| 505 |
-
tplus1_refresh = {"status": "not_checked"}
|
| 506 |
-
if tplus1_refresh_due():
|
| 507 |
-
background_tasks.add_task(refresh_tplus1_if_due)
|
| 508 |
-
tplus1_refresh = {"status": "scheduled"}
|
| 509 |
-
if close_refresh_due():
|
| 510 |
-
background_tasks.add_task(refresh_market_close_data_if_due)
|
| 511 |
-
close_refresh = {"status": "scheduled"}
|
| 512 |
-
return {
|
| 513 |
-
"status": "awake",
|
| 514 |
-
"market": current_market_state(),
|
| 515 |
-
"tplus1_refresh": tplus1_refresh,
|
| 516 |
-
"close_refresh": close_refresh,
|
| 517 |
-
}
|
| 518 |
|
| 519 |
|
| 520 |
@app.get("/prediction/latest")
|
|
|
|
| 7 |
import sys
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
+
from fastapi import BackgroundTasks, HTTPException, Query, Response
|
| 11 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
+
from fastapi import FastAPI
|
| 13 |
+
from pydantic import BaseModel
|
| 14 |
|
| 15 |
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
| 16 |
+
from nifty_backend.runtime import (
|
| 17 |
+
CLOSE_REFRESH_READY,
|
| 18 |
+
IST,
|
| 19 |
+
STALE_CHECK_INTERVAL_SECONDS,
|
| 20 |
+
TPLUS1_READY,
|
| 21 |
+
close_refresh_due,
|
| 22 |
+
dashboard_payload,
|
| 23 |
+
is_trading_day,
|
| 24 |
+
latest_saved_prediction,
|
| 25 |
+
latest_tplus1_prediction,
|
| 26 |
+
next_trading_day,
|
| 27 |
+
refresh_daily_data,
|
| 28 |
+
refresh_first5_prediction,
|
| 29 |
+
refresh_market_close_data,
|
| 30 |
+
refresh_stale_data_once,
|
| 31 |
+
refresh_tplus1_prediction,
|
| 32 |
+
seconds_until_next_ist_run,
|
| 33 |
+
warm_dashboard_payload_cache,
|
| 34 |
+
)
|
| 35 |
+
from kotak_neo import (
|
| 36 |
+
KotakNeoConfigError,
|
| 37 |
+
KotakNeoError,
|
| 38 |
+
KotakNeoSessionRequired,
|
| 39 |
+
kotak_neo_manager,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
app = FastAPI(title="NIFTY 50 Forecaster Backend")
|
| 44 |
app.add_middleware(
|
| 45 |
CORSMiddleware,
|
| 46 |
allow_origins=["*"],
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
|
| 53 |
+
market_status = "Waiting for next session"
|
| 54 |
+
close_refresh_lock = threading.Lock()
|
| 55 |
+
tplus1_refresh_lock = threading.Lock()
|
| 56 |
+
MARKET_OPEN = time(9, 15)
|
| 57 |
+
FIRST5_READY = time(9, 20)
|
| 58 |
+
MARKET_CLOSE = time(15, 30)
|
| 59 |
+
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
class TotpRequest(BaseModel):
|
| 62 |
+
totp: str
|
| 63 |
|
| 64 |
+
|
| 65 |
+
def refresh_market_close_data_if_due() -> dict:
|
| 66 |
if not close_refresh_due():
|
| 67 |
return {"status": "skipped", "reason": "close refresh is not due"}
|
| 68 |
if not close_refresh_lock.acquire(blocking=False):
|
|
|
|
| 70 |
try:
|
| 71 |
info = refresh_market_close_data()
|
| 72 |
return {"status": "refreshed", **info}
|
| 73 |
+
finally:
|
| 74 |
+
close_refresh_lock.release()
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def latest_tplus1_prediction_date(payload: dict | None = None) -> date | None:
|
| 78 |
+
try:
|
| 79 |
+
latest = payload if payload is not None else latest_tplus1_prediction()
|
| 80 |
+
raw = latest.get("input_date")
|
| 81 |
+
return date.fromisoformat(str(raw)[:10]) if raw else None
|
| 82 |
+
except Exception:
|
| 83 |
+
return None
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def tplus1_refresh_due(now: datetime | None = None, latest_date: date | None = None) -> bool:
|
| 87 |
+
now = now or datetime.now(IST)
|
| 88 |
+
if not is_trading_day(now.date()) or not (TPLUS1_READY <= now.time() < MARKET_CLOSE):
|
| 89 |
+
return False
|
| 90 |
+
latest_date = latest_date if latest_date is not None else latest_tplus1_prediction_date()
|
| 91 |
+
return latest_date != now.date()
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def refresh_tplus1_if_due() -> dict:
|
| 95 |
+
now = datetime.now(IST)
|
| 96 |
+
latest_date = latest_tplus1_prediction_date()
|
| 97 |
+
if not tplus1_refresh_due(now=now, latest_date=latest_date):
|
| 98 |
+
return {"status": "skipped", "reason": "tplus1 refresh is not due"}
|
| 99 |
+
if not tplus1_refresh_lock.acquire(blocking=False):
|
| 100 |
+
return {"status": "skipped", "reason": "tplus1 refresh already running"}
|
| 101 |
+
try:
|
| 102 |
+
prediction = refresh_tplus1_prediction(session_date=now.date())
|
| 103 |
+
return {"status": "refreshed", "prediction": prediction}
|
| 104 |
+
finally:
|
| 105 |
+
tplus1_refresh_lock.release()
|
| 106 |
|
| 107 |
|
| 108 |
def latest_prediction_date(payload: dict | None = None) -> date | None:
|
|
|
|
| 114 |
return None
|
| 115 |
|
| 116 |
|
| 117 |
+
def current_market_state(now: datetime | None = None) -> dict:
|
| 118 |
global market_status
|
| 119 |
now = now or datetime.now(IST)
|
| 120 |
today = now.date()
|
|
|
|
| 124 |
market_is_open_for_t5 = trading_day and FIRST5_READY <= current_time < MARKET_CLOSE
|
| 125 |
market_is_open_for_tplus1 = trading_day and TPLUS1_READY <= current_time < MARKET_CLOSE
|
| 126 |
has_current_first5 = market_is_open_for_t5 and latest_date == today
|
| 127 |
+
tplus1_latest_date = latest_tplus1_prediction_date()
|
| 128 |
+
has_current_tplus1 = market_is_open_for_tplus1 and tplus1_latest_date == today
|
| 129 |
next_session = today if trading_day and current_time < MARKET_CLOSE else next_trading_day(today + timedelta(days=1))
|
| 130 |
|
| 131 |
if not trading_day:
|
|
|
|
| 150 |
else:
|
| 151 |
status = "Prediction Pending"
|
| 152 |
detail = "No current-session prediction has been generated yet."
|
| 153 |
+
else:
|
| 154 |
+
status = "Market Closed"
|
| 155 |
+
detail = "Trading session has ended."
|
| 156 |
+
|
| 157 |
+
if not trading_day:
|
| 158 |
+
tplus1_status = "Market Closed"
|
| 159 |
+
tplus1_detail = f"Next trading session is {next_session.isoformat()}."
|
| 160 |
+
elif current_time < TPLUS1_READY:
|
| 161 |
+
tplus1_status = "Waiting for 2:30 PM"
|
| 162 |
+
tplus1_detail = "The T+1 forecast becomes available at 2:30 PM IST."
|
| 163 |
+
elif current_time < MARKET_CLOSE:
|
| 164 |
+
if has_current_tplus1:
|
| 165 |
+
tplus1_status = "Ready"
|
| 166 |
+
tplus1_detail = "Today's T+1 prediction is available."
|
| 167 |
+
else:
|
| 168 |
+
tplus1_status = "Pending"
|
| 169 |
+
tplus1_detail = "No current-session T+1 prediction has been generated yet."
|
| 170 |
+
else:
|
| 171 |
+
tplus1_status = "Market Closed"
|
| 172 |
+
tplus1_detail = "Trading session has ended."
|
| 173 |
+
|
| 174 |
+
if not trading_day:
|
| 175 |
+
t5_status = "Market Closed"
|
| 176 |
+
t5_detail = f"Next trading session is {next_session.isoformat()}."
|
| 177 |
+
elif current_time < FIRST5_READY:
|
| 178 |
+
t5_status = "Waiting for 9:20 AM"
|
| 179 |
+
t5_detail = "The T+5 forecast becomes available after the first five one-minute bars."
|
| 180 |
+
elif current_time < MARKET_CLOSE:
|
| 181 |
+
if market_status in {"Fetching T+5 Prediction Data...", "Prediction Failed"}:
|
| 182 |
+
t5_status = market_status
|
| 183 |
+
t5_detail = "The first-five-minute prediction job is still resolving."
|
| 184 |
+
elif has_current_first5:
|
| 185 |
+
t5_status = "Ready"
|
| 186 |
+
t5_detail = "Today's first-five-minute prediction is available."
|
| 187 |
+
else:
|
| 188 |
+
t5_status = "Pending"
|
| 189 |
+
t5_detail = "No current-session prediction has been generated yet."
|
| 190 |
+
else:
|
| 191 |
+
t5_status = "Market Closed"
|
| 192 |
+
t5_detail = "Trading session has ended."
|
| 193 |
+
|
| 194 |
+
return {
|
| 195 |
"market_status": status,
|
| 196 |
"market_detail": detail,
|
| 197 |
"server_time_ist": now.isoformat(),
|
|
|
|
| 199 |
"session_date": today.isoformat(),
|
| 200 |
"next_session_date": next_session.isoformat(),
|
| 201 |
"latest_prediction_date": latest_date.isoformat() if latest_date else None,
|
| 202 |
+
"t5_available": has_current_first5,
|
| 203 |
+
"t5_status": t5_status,
|
| 204 |
+
"t5_detail": t5_detail,
|
| 205 |
+
"market_is_open_for_t5": market_is_open_for_t5,
|
| 206 |
+
"tplus1_available": has_current_tplus1,
|
| 207 |
+
"tplus1_status": tplus1_status,
|
| 208 |
+
"tplus1_detail": tplus1_detail,
|
| 209 |
+
"market_is_open_for_tplus1": market_is_open_for_tplus1,
|
| 210 |
+
"latest_tplus1_prediction_date": tplus1_latest_date.isoformat() if tplus1_latest_date else None,
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
def attach_market_state(payload: dict) -> dict:
|
| 215 |
+
state = current_market_state()
|
| 216 |
+
payload.setdefault("data_status", {})
|
| 217 |
+
payload["data_status"].update(state)
|
| 218 |
+
try:
|
| 219 |
+
payload["nifty_quote"] = kotak_neo_manager.fetch_nifty50_quote()
|
| 220 |
+
payload["nifty_quote_error"] = None
|
| 221 |
+
except KotakNeoSessionRequired as exc:
|
| 222 |
+
payload["nifty_quote"] = None
|
| 223 |
+
payload["nifty_quote_error"] = {"status": 401, "message": str(exc)}
|
| 224 |
+
except KotakNeoConfigError as exc:
|
| 225 |
+
payload["nifty_quote"] = None
|
| 226 |
+
payload["nifty_quote_error"] = {"status": 503, "message": str(exc)}
|
| 227 |
+
except KotakNeoError as exc:
|
| 228 |
+
payload["nifty_quote"] = None
|
| 229 |
+
payload["nifty_quote_error"] = {"status": 502, "message": str(exc)}
|
| 230 |
+
|
| 231 |
+
t5_latest = payload.get("latest") or {}
|
| 232 |
tomorrow_latest = payload.get("tomorrow_latest") or {}
|
| 233 |
tplus1_latest = payload.get("tplus1_latest") or {}
|
| 234 |
t5_available = bool(state["t5_available"] and t5_latest.get("prediction"))
|
| 235 |
tplus1_available = bool(state["tplus1_available"] and tplus1_latest.get("prediction"))
|
| 236 |
+
tomorrow_available = bool(tomorrow_latest.get("prediction"))
|
| 237 |
refresh_phase = payload.get("data_status", {}).get("refresh_phase")
|
| 238 |
if refresh_phase in {"waiting_second_payload", "refreshing"}:
|
| 239 |
tomorrow_status = "WAITING FOR SECOND PAYLOAD"
|
|
|
|
| 257 |
"validation_accuracy": tomorrow_latest.get("validation_accuracy"),
|
| 258 |
"test_accuracy": tomorrow_latest.get("test_accuracy"),
|
| 259 |
},
|
| 260 |
+
"t5": {
|
| 261 |
+
"available": t5_available,
|
| 262 |
+
"status": "Ready" if t5_available else state["t5_status"],
|
| 263 |
+
"reason": None if t5_available else state["t5_detail"],
|
| 264 |
+
"input_date": t5_latest.get("input_date"),
|
| 265 |
+
"prediction": t5_latest.get("prediction") if t5_available else None,
|
| 266 |
+
"prob_up": t5_latest.get("prob_up") if t5_available else None,
|
| 267 |
+
"confidence": t5_latest.get("confidence") if t5_available else None,
|
| 268 |
+
"threshold": t5_latest.get("threshold") if t5_available else None,
|
| 269 |
+
"is_overridden": bool(t5_latest.get("is_overridden")) if t5_available else False,
|
| 270 |
+
"model_name": t5_latest.get("model_name"),
|
| 271 |
+
"validation_accuracy": (payload.get("summary") or {}).get("validation_accuracy"),
|
| 272 |
+
"test_accuracy": (payload.get("summary") or {}).get("test_accuracy"),
|
| 273 |
},
|
| 274 |
+
"tplus1": {
|
| 275 |
+
"available": tplus1_available,
|
| 276 |
+
"status": "Ready" if tplus1_available else state["tplus1_status"],
|
| 277 |
+
"reason": None if tplus1_available else state["tplus1_detail"],
|
| 278 |
+
"target_date": tplus1_latest.get("target_date") or state["next_session_date"],
|
| 279 |
+
"input_date": tplus1_latest.get("input_date"),
|
| 280 |
"prediction": tplus1_latest.get("prediction") if tplus1_available else None,
|
| 281 |
+
"prob_up": tplus1_latest.get("prob_up") if tplus1_available else None,
|
| 282 |
+
"confidence": tplus1_latest.get("confidence") if tplus1_available else None,
|
| 283 |
+
"threshold": tplus1_latest.get("threshold") if tplus1_available else None,
|
| 284 |
+
"is_overridden": bool(tplus1_latest.get("overlay_changed")) if tplus1_available else False,
|
| 285 |
+
"model_name": tplus1_latest.get("model_name"),
|
| 286 |
+
"validation_accuracy": (payload.get("tplus1_summary") or {}).get("validation_accuracy"),
|
| 287 |
+
"test_accuracy": (payload.get("tplus1_summary") or {}).get("test_accuracy"),
|
| 288 |
},
|
| 289 |
}
|
| 290 |
return payload
|
| 291 |
|
| 292 |
|
| 293 |
+
async def daily_ist_refresh_loop() -> None:
|
| 294 |
global market_status
|
| 295 |
while True:
|
| 296 |
# Wait until 9:00 AM IST
|
|
|
|
| 318 |
print(f"[scheduler] first5 refresh failed: {exc}", flush=True)
|
| 319 |
market_status = "Prediction Failed"
|
| 320 |
|
| 321 |
+
try:
|
| 322 |
+
await asyncio.to_thread(refresh_daily_data)
|
| 323 |
+
except Exception as exc:
|
| 324 |
+
print(f"[scheduler] daily refresh failed: {exc}", flush=True)
|
| 325 |
+
|
| 326 |
+
await asyncio.sleep(seconds_until_next_ist_run(TPLUS1_READY))
|
| 327 |
+
print("[scheduler] 2:30 PM IST - Refreshing T+1 prediction", flush=True)
|
| 328 |
+
try:
|
| 329 |
+
info = await asyncio.to_thread(refresh_tplus1_if_due)
|
| 330 |
+
print(f"[scheduler] tplus1 refresh result: {info}", flush=True)
|
| 331 |
+
except Exception as exc:
|
| 332 |
+
print(f"[scheduler] tplus1 refresh failed: {exc}", flush=True)
|
| 333 |
+
|
| 334 |
+
await asyncio.sleep(seconds_until_next_ist_run(CLOSE_REFRESH_READY))
|
| 335 |
+
print("[scheduler] 3:45 PM IST - Refreshing close data", flush=True)
|
| 336 |
+
try:
|
| 337 |
info = await asyncio.to_thread(refresh_market_close_data_if_due)
|
| 338 |
print(f"[scheduler] close refresh result: {info}", flush=True)
|
| 339 |
except Exception as exc:
|
|
|
|
| 361 |
print(f"[startup] daily refresh failed: {exc}", flush=True)
|
| 362 |
|
| 363 |
|
| 364 |
+
async def refresh_market_close_once_if_due() -> None:
|
| 365 |
try:
|
| 366 |
info = await asyncio.to_thread(refresh_market_close_data_if_due)
|
| 367 |
if info.get("status") == "refreshed":
|
| 368 |
print(f"[startup] close refresh result: {info}", flush=True)
|
| 369 |
except Exception as exc:
|
| 370 |
+
print(f"[startup] close refresh failed: {exc}", flush=True)
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
async def refresh_tplus1_once_if_due() -> None:
|
| 374 |
+
try:
|
| 375 |
+
info = await asyncio.to_thread(refresh_tplus1_if_due)
|
| 376 |
+
if info.get("status") == "refreshed":
|
| 377 |
+
print(f"[startup] tplus1 refresh result: {info}", flush=True)
|
| 378 |
+
except Exception as exc:
|
| 379 |
+
print(f"[startup] tplus1 refresh failed: {exc}", flush=True)
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
async def warm_dashboard_payload_cache_once() -> None:
|
| 383 |
+
try:
|
| 384 |
+
await asyncio.to_thread(warm_dashboard_payload_cache)
|
| 385 |
+
except Exception as exc:
|
| 386 |
+
print(f"[startup] dashboard payload warmup failed: {exc}", flush=True)
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
async def stale_data_watch_loop() -> None:
|
| 390 |
+
while True:
|
| 391 |
+
try:
|
| 392 |
+
info = await asyncio.to_thread(refresh_stale_data_once)
|
| 393 |
+
if info.get("status") == "refreshed":
|
| 394 |
+
print(f"[stale-watch] refreshed stale data: {info}", flush=True)
|
| 395 |
+
except Exception as exc:
|
| 396 |
+
print(f"[stale-watch] stale refresh failed: {exc}", flush=True)
|
| 397 |
+
await asyncio.sleep(STALE_CHECK_INTERVAL_SECONDS)
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
@app.on_event("startup")
|
| 401 |
+
async def start_scheduler() -> None:
|
| 402 |
global market_status
|
| 403 |
# Initialize correct status on startup based on current time
|
| 404 |
now = datetime.now(IST).time()
|
|
|
|
| 416 |
else:
|
| 417 |
market_status = "Prediction Pending"
|
| 418 |
|
| 419 |
+
asyncio.create_task(refresh_current_session_once())
|
| 420 |
+
asyncio.create_task(refresh_tplus1_once_if_due())
|
| 421 |
+
asyncio.create_task(refresh_market_close_once_if_due())
|
| 422 |
+
asyncio.create_task(warm_dashboard_payload_cache_once())
|
| 423 |
+
asyncio.create_task(stale_data_watch_loop())
|
| 424 |
+
asyncio.create_task(daily_ist_refresh_loop())
|
| 425 |
|
| 426 |
|
| 427 |
@app.get("/health")
|
|
|
|
| 434 |
return {"service": "NIFTY 50 Forecaster Backend", "status": "ok"}
|
| 435 |
|
| 436 |
|
| 437 |
+
@app.get("/dashboard")
|
| 438 |
+
def dashboard(response: Response) -> dict:
|
| 439 |
+
response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate"
|
| 440 |
+
response.headers["Pragma"] = "no-cache"
|
| 441 |
+
try:
|
| 442 |
+
refresh_stale_data_once()
|
| 443 |
+
except Exception as exc:
|
| 444 |
+
print(f"[dashboard] stale refresh failed: {exc}", flush=True)
|
| 445 |
+
return attach_market_state(dashboard_payload())
|
| 446 |
+
|
| 447 |
+
|
| 448 |
+
@app.get("/kotak/status")
|
| 449 |
+
def kotak_status() -> dict:
|
| 450 |
+
return kotak_neo_manager.status()
|
| 451 |
+
|
| 452 |
+
|
| 453 |
+
@app.post("/kotak/auth/totp")
|
| 454 |
+
def kotak_auth_totp(payload: TotpRequest) -> dict:
|
| 455 |
+
try:
|
| 456 |
+
return kotak_neo_manager.authenticate_with_totp(payload.totp)
|
| 457 |
+
except KotakNeoConfigError as exc:
|
| 458 |
+
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
| 459 |
+
except KotakNeoError as exc:
|
| 460 |
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 461 |
+
|
| 462 |
+
|
| 463 |
+
@app.get("/kotak/account")
|
| 464 |
+
def kotak_account() -> dict:
|
| 465 |
+
try:
|
| 466 |
+
return kotak_neo_manager.fetch_account_snapshot()
|
| 467 |
+
except KotakNeoConfigError as exc:
|
| 468 |
+
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
| 469 |
+
except KotakNeoSessionRequired as exc:
|
| 470 |
+
raise HTTPException(status_code=401, detail=str(exc)) from exc
|
| 471 |
+
except KotakNeoError as exc:
|
| 472 |
+
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
| 473 |
+
|
| 474 |
+
|
| 475 |
+
@app.get("/kotak/quote/nifty50")
|
| 476 |
+
def kotak_nifty50_quote() -> dict:
|
| 477 |
+
try:
|
| 478 |
+
return kotak_neo_manager.fetch_nifty50_quote()
|
| 479 |
+
except KotakNeoConfigError as exc:
|
| 480 |
+
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
| 481 |
+
except KotakNeoSessionRequired as exc:
|
| 482 |
+
raise HTTPException(status_code=401, detail=str(exc)) from exc
|
| 483 |
+
except KotakNeoError as exc:
|
| 484 |
+
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
| 485 |
+
|
| 486 |
+
|
| 487 |
+
@app.get("/kotak/activity-log")
|
| 488 |
+
def kotak_activity_log() -> dict:
|
| 489 |
+
try:
|
| 490 |
+
snapshot = kotak_neo_manager.fetch_account_snapshot()
|
| 491 |
+
return {
|
| 492 |
+
"activity_log": snapshot.get("activity_log", {}),
|
| 493 |
+
"trade_history": snapshot.get("trade_history", []),
|
| 494 |
+
"order_book": snapshot.get("order_book", []),
|
| 495 |
+
}
|
| 496 |
+
except KotakNeoConfigError as exc:
|
| 497 |
+
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
| 498 |
+
except KotakNeoSessionRequired as exc:
|
| 499 |
+
raise HTTPException(status_code=401, detail=str(exc)) from exc
|
| 500 |
+
except KotakNeoError as exc:
|
| 501 |
+
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
| 502 |
|
| 503 |
|
| 504 |
@app.get("/cron/keepalive")
|
| 505 |
+
def cron_keepalive(background_tasks: BackgroundTasks) -> dict:
|
| 506 |
+
close_refresh = {"status": "not_checked"}
|
| 507 |
+
tplus1_refresh = {"status": "not_checked"}
|
| 508 |
+
if tplus1_refresh_due():
|
| 509 |
+
background_tasks.add_task(refresh_tplus1_if_due)
|
| 510 |
+
tplus1_refresh = {"status": "scheduled"}
|
| 511 |
+
if close_refresh_due():
|
| 512 |
+
background_tasks.add_task(refresh_market_close_data_if_due)
|
| 513 |
+
close_refresh = {"status": "scheduled"}
|
| 514 |
+
return {
|
| 515 |
+
"status": "awake",
|
| 516 |
+
"market": current_market_state(),
|
| 517 |
+
"tplus1_refresh": tplus1_refresh,
|
| 518 |
+
"close_refresh": close_refresh,
|
| 519 |
+
}
|
| 520 |
|
| 521 |
|
| 522 |
@app.get("/prediction/latest")
|
runtime.py
CHANGED
|
@@ -1263,7 +1263,15 @@ def dashboard_payload() -> dict[str, Any]:
|
|
| 1263 |
with _dashboard_payload_lock:
|
| 1264 |
payload = copy.deepcopy(_dashboard_payload_cached(key))
|
| 1265 |
# Always rebuild track record (fresh Yahoo daily + calendar window), never serve from LRU cache.
|
| 1266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1267 |
return payload
|
| 1268 |
|
| 1269 |
|
|
@@ -1411,6 +1419,9 @@ def build_prediction_track_record(
|
|
| 1411 |
)
|
| 1412 |
source = "Tomorrow (rolling)"
|
| 1413 |
|
|
|
|
|
|
|
|
|
|
| 1414 |
records.append(
|
| 1415 |
{
|
| 1416 |
"date": target_day.isoformat(),
|
|
@@ -1423,7 +1434,7 @@ def build_prediction_track_record(
|
|
| 1423 |
"correct": prediction == actual_direction,
|
| 1424 |
}
|
| 1425 |
)
|
| 1426 |
-
return records
|
| 1427 |
|
| 1428 |
|
| 1429 |
@lru_cache(maxsize=4)
|
|
|
|
| 1263 |
with _dashboard_payload_lock:
|
| 1264 |
payload = copy.deepcopy(_dashboard_payload_cached(key))
|
| 1265 |
# Always rebuild track record (fresh Yahoo daily + calendar window), never serve from LRU cache.
|
| 1266 |
+
track_record = build_prediction_track_record(sessions=10)
|
| 1267 |
+
payload["charts"]["track_record"] = track_record
|
| 1268 |
+
payload["charts"]["track_record_meta"] = {
|
| 1269 |
+
"builder": "rolling_v1",
|
| 1270 |
+
"sessions": 10,
|
| 1271 |
+
"count": len(track_record),
|
| 1272 |
+
"start": track_record[0]["date"] if track_record else None,
|
| 1273 |
+
"end": track_record[-1]["date"] if track_record else None,
|
| 1274 |
+
}
|
| 1275 |
return payload
|
| 1276 |
|
| 1277 |
|
|
|
|
| 1419 |
)
|
| 1420 |
source = "Tomorrow (rolling)"
|
| 1421 |
|
| 1422 |
+
if prediction not in {"UP", "DOWN"}:
|
| 1423 |
+
continue
|
| 1424 |
+
|
| 1425 |
records.append(
|
| 1426 |
{
|
| 1427 |
"date": target_day.isoformat(),
|
|
|
|
| 1434 |
"correct": prediction == actual_direction,
|
| 1435 |
}
|
| 1436 |
)
|
| 1437 |
+
return records[-sessions:]
|
| 1438 |
|
| 1439 |
|
| 1440 |
@lru_cache(maxsize=4)
|