Spaces:
Running
Running
Upload runtime.py
Browse files- runtime.py +48 -17
runtime.py
CHANGED
|
@@ -1261,7 +1261,10 @@ def dashboard_payload() -> dict[str, Any]:
|
|
| 1261 |
_file_cache_key(TOMORROW_PREDICTION_HISTORY_PATH),
|
| 1262 |
)
|
| 1263 |
with _dashboard_payload_lock:
|
| 1264 |
-
|
|
|
|
|
|
|
|
|
|
| 1265 |
|
| 1266 |
|
| 1267 |
def warm_dashboard_payload_cache() -> None:
|
|
@@ -1307,6 +1310,38 @@ def _load_forecaster_predictions_by_target() -> dict[date, dict[str, Any]]:
|
|
| 1307 |
return indexed
|
| 1308 |
|
| 1309 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1310 |
def _rolling_tomorrow_prediction(
|
| 1311 |
input_day: date,
|
| 1312 |
daily_rows: pd.DataFrame,
|
|
@@ -1330,20 +1365,7 @@ def build_prediction_track_record(
|
|
| 1330 |
fallback_prob = float(summary.get("latest_forecast_prob_up", 0.49900560447008563))
|
| 1331 |
forecaster_by_target = _load_forecaster_predictions_by_target()
|
| 1332 |
|
| 1333 |
-
|
| 1334 |
-
latest_daily = latest_parquet_date(NIFTY_1D_PATH)
|
| 1335 |
-
if latest_daily is None or latest_daily < end_session:
|
| 1336 |
-
try:
|
| 1337 |
-
refresh_daily_data()
|
| 1338 |
-
except Exception:
|
| 1339 |
-
pass
|
| 1340 |
-
|
| 1341 |
-
daily_rows = pd.read_parquet(NIFTY_1D_PATH)
|
| 1342 |
-
daily_rows["date"] = pd.to_datetime(daily_rows["date"], errors="coerce").dt.normalize()
|
| 1343 |
-
daily_rows = daily_rows.dropna(subset=["date"]).sort_values("date")
|
| 1344 |
-
daily_rows = daily_rows[
|
| 1345 |
-
daily_rows["close"].map(lambda value: np.isfinite(float(value)) if pd.notna(value) else False)
|
| 1346 |
-
].copy()
|
| 1347 |
if daily_rows.empty:
|
| 1348 |
return []
|
| 1349 |
|
|
@@ -1353,6 +1375,16 @@ def build_prediction_track_record(
|
|
| 1353 |
if pd.notna(row["close"]) and np.isfinite(float(row["close"]))
|
| 1354 |
}
|
| 1355 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1356 |
session_dates = last_n_trading_sessions(end_session, sessions)
|
| 1357 |
|
| 1358 |
records: list[dict[str, Any]] = []
|
|
@@ -1485,7 +1517,6 @@ def _dashboard_payload_cached(key: tuple[tuple[str, int | None, int | None], ...
|
|
| 1485 |
"total_test_days": int(tomorrow_summary.get("n_test") or len(tomorrow_test) or 0),
|
| 1486 |
"models": model_metrics,
|
| 1487 |
}
|
| 1488 |
-
track_record = build_prediction_track_record(sessions=10)
|
| 1489 |
return {
|
| 1490 |
"latest": t5_latest,
|
| 1491 |
"tomorrow_latest": tomorrow_latest,
|
|
@@ -1506,7 +1537,7 @@ def _dashboard_payload_cached(key: tuple[tuple[str, int | None, int | None], ...
|
|
| 1506 |
"tomorrow_recent_predictions": _json_ready_frame(tomorrow_recent),
|
| 1507 |
"tomorrow_history_predictions": _json_ready_frame(tomorrow_history.tail(80)),
|
| 1508 |
"tplus1_recent_predictions": _json_ready_frame(tplus1_test.tail(40)),
|
| 1509 |
-
"track_record":
|
| 1510 |
},
|
| 1511 |
"data_status": {
|
| 1512 |
"nifty_1m_rows": int(len(pd.read_parquet(NIFTY_1M_PATH, columns=["date"]))),
|
|
|
|
| 1261 |
_file_cache_key(TOMORROW_PREDICTION_HISTORY_PATH),
|
| 1262 |
)
|
| 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 |
+
payload["charts"]["track_record"] = build_prediction_track_record(sessions=10)
|
| 1267 |
+
return payload
|
| 1268 |
|
| 1269 |
|
| 1270 |
def warm_dashboard_payload_cache() -> None:
|
|
|
|
| 1310 |
return indexed
|
| 1311 |
|
| 1312 |
|
| 1313 |
+
def _load_track_record_daily_rows() -> pd.DataFrame:
|
| 1314 |
+
"""Daily OHLC for track record: live Yahoo pull merged over parquet history."""
|
| 1315 |
+
frames: list[pd.DataFrame] = []
|
| 1316 |
+
if NIFTY_1D_PATH.exists():
|
| 1317 |
+
try:
|
| 1318 |
+
frames.append(pd.read_parquet(NIFTY_1D_PATH))
|
| 1319 |
+
except Exception:
|
| 1320 |
+
pass
|
| 1321 |
+
try:
|
| 1322 |
+
yahoo_daily = fetch_yahoo_daily(period="3mo")
|
| 1323 |
+
if not yahoo_daily.empty:
|
| 1324 |
+
frames.append(yahoo_daily)
|
| 1325 |
+
try:
|
| 1326 |
+
append_parquet_rows(NIFTY_1D_PATH, yahoo_daily, ["date"])
|
| 1327 |
+
except Exception:
|
| 1328 |
+
pass
|
| 1329 |
+
except Exception:
|
| 1330 |
+
pass
|
| 1331 |
+
|
| 1332 |
+
if not frames:
|
| 1333 |
+
return pd.DataFrame()
|
| 1334 |
+
|
| 1335 |
+
combined = pd.concat(frames, ignore_index=True)
|
| 1336 |
+
combined["date"] = pd.to_datetime(combined["date"], errors="coerce").dt.normalize()
|
| 1337 |
+
combined = combined.dropna(subset=["date"]).sort_values("date")
|
| 1338 |
+
combined = combined.drop_duplicates(subset=["date"], keep="last")
|
| 1339 |
+
combined = combined[
|
| 1340 |
+
combined["close"].map(lambda value: np.isfinite(float(value)) if pd.notna(value) else False)
|
| 1341 |
+
].copy()
|
| 1342 |
+
return combined.reset_index(drop=True)
|
| 1343 |
+
|
| 1344 |
+
|
| 1345 |
def _rolling_tomorrow_prediction(
|
| 1346 |
input_day: date,
|
| 1347 |
daily_rows: pd.DataFrame,
|
|
|
|
| 1365 |
fallback_prob = float(summary.get("latest_forecast_prob_up", 0.49900560447008563))
|
| 1366 |
forecaster_by_target = _load_forecaster_predictions_by_target()
|
| 1367 |
|
| 1368 |
+
daily_rows = _load_track_record_daily_rows()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1369 |
if daily_rows.empty:
|
| 1370 |
return []
|
| 1371 |
|
|
|
|
| 1375 |
if pd.notna(row["close"]) and np.isfinite(float(row["close"]))
|
| 1376 |
}
|
| 1377 |
|
| 1378 |
+
end_session = _track_record_end_session()
|
| 1379 |
+
available_through = max(
|
| 1380 |
+
(day for day in closes_by_date if day <= end_session),
|
| 1381 |
+
default=None,
|
| 1382 |
+
)
|
| 1383 |
+
if available_through is None:
|
| 1384 |
+
return []
|
| 1385 |
+
if available_through < end_session:
|
| 1386 |
+
end_session = available_through
|
| 1387 |
+
|
| 1388 |
session_dates = last_n_trading_sessions(end_session, sessions)
|
| 1389 |
|
| 1390 |
records: list[dict[str, Any]] = []
|
|
|
|
| 1517 |
"total_test_days": int(tomorrow_summary.get("n_test") or len(tomorrow_test) or 0),
|
| 1518 |
"models": model_metrics,
|
| 1519 |
}
|
|
|
|
| 1520 |
return {
|
| 1521 |
"latest": t5_latest,
|
| 1522 |
"tomorrow_latest": tomorrow_latest,
|
|
|
|
| 1537 |
"tomorrow_recent_predictions": _json_ready_frame(tomorrow_recent),
|
| 1538 |
"tomorrow_history_predictions": _json_ready_frame(tomorrow_history.tail(80)),
|
| 1539 |
"tplus1_recent_predictions": _json_ready_frame(tplus1_test.tail(40)),
|
| 1540 |
+
"track_record": [],
|
| 1541 |
},
|
| 1542 |
"data_status": {
|
| 1543 |
"nifty_1m_rows": int(len(pd.read_parquet(NIFTY_1M_PATH, columns=["date"]))),
|