Spaces:
Sleeping
Sleeping
Upload 38 files
Browse files1243 unmodified lines12441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712683 unmodified lines12721273127412751276127712781 unmodified line128012811282128312841285128619 unmodified lines1306130713081309131013111312384 unmodified lines169716981699170017011702448 unmodified lines2151215221532154215521564 unmodified lines216121622163216421652166216721682169217021712 unmodified lines217421752176217721782179218040 unmodified lines2221222222232224222522261243 unmodified linesreturn df.sort_values("date").reset_index(drop=True)def sync_mfe_outputs() -> bool:"""Refresh bundled MFE artifacts from the forecasting project when available."""if not MFE_SOURCE_OUTPUT_DIR.exists():return MFE_SUMMARY_PATH.exists()MFE_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)synced = Falsefor name in ("summary.json", "latest_prediction.csv", "test_predictions.csv"):source = MFE_SOURCE_OUTPUT_DIR / nametarget = MFE_OUTPUT_DIR / nameif not source.exists():continueif not target.exists() or source.stat().st_mtime > target.stat().st_mtime:target.write_bytes(source.read_bytes())synced = Truereturn synced or MFE_SUMMARY_PATH.exists()def load_mfe_summary() -> dict[str, Any]:sync_mfe_outputs()if not MFE_SUMMARY_PATH.exists():return {}try:3 unmodified linesdef load_mfe_latest() -> dict[str, Any]:sync_mfe_outputs()summary = load_mfe_summary()if MFE_LATEST_PATH.exists():try:1 unmodified lineup_pts = float(row.get("predicted_up_points", summary.get("latest_predicted_up_points", 0)))down_pts = float(row.get("predicted_down_points", summary.get("latest_predicted_down_points", 0)))return {"input_date": str(row.get("input_date") or summary.get("latest_input_date", ""))[:10],"first5_start": row.get("first5_start") or summary.get("latest_first5_start"),"first5_end": row.get("first5_end") or summary.get("latest_first5_end"),"first5_close": float(row.get("first5_close") or summary.get("latest_first5_close") or 0),19 unmodified linesdef load_mfe_backtest() -> pd.DataFrame:sync_mfe_outputs()if not MFE_TEST_PREDICTIONS_PATH.exists():return pd.DataFrame()frame = pd.read_csv(MFE_TEST_PREDICTIONS_PATH)384 unmodified linesreturn predictiondef refresh_daily_data() -> dict[str, Any]:daily = fetch_yahoo_daily(period="1mo")combined = append_parquet_rows(NIFTY_1D_PATH, daily, ["date"])448 unmodified lineslatest_daily = latest_parquet_date(NIFTY_1D_PATH)latest_minutes = latest_parquet_date(NIFTY_1M_PATH)latest_t5 = latest_prediction_input_date(LATEST_PATH)latest_tomorrow = latest_tomorrow_input_date()latest_tplus1 = latest_prediction_input_date(TPLUS1_LATEST_PATH)return {4 unmodified lines"latest_daily_date": latest_daily.isoformat() if latest_daily else None,"latest_minute_date": latest_minutes.isoformat() if latest_minutes else None,"latest_t5_date": latest_t5.isoformat() if latest_t5 else None,"latest_tomorrow_date": latest_tomorrow.isoformat() if latest_tomorrow else None,"latest_tplus1_date": latest_tplus1.isoformat() if latest_tplus1 else None,"daily_stale": is_stale(latest_daily, expected_daily),"minutes_stale": is_stale(latest_minutes, expected_minutes),"t5_stale": is_stale(latest_t5, expected_minutes),"tomorrow_stale": is_stale(latest_tomorrow, expected_daily),"tplus1_stale": is_stale(latest_tplus1, expected_tplus1),}2 unmodified linesdef refresh_stale_data_once(now: datetime | None = None) -> dict[str, Any]:now = now or datetime.now(IST)status = stale_data_status(now)if not any(status[key] for key in ("daily_stale", "minutes_stale", "t5_stale", "tomorrow_stale", "tplus1_stale")):return {"status": "fresh", **status, "actions": []}if not _stale_refresh_lock.acquire(blocking=False):return {"status": "skipped", "reason": "stale refresh already running", **status, "actions": []}40 unmodified linesprediction = refresh_first5_prediction(session_date=now.date())actions.append({"name": "t5_prediction", "input_date": prediction.input_date})if status["tplus1_stale"] and is_trading_day(now.date()) and now.time() >= TPLUS1_READY:prediction = refresh_tplus1_prediction(session_date=now.date())actions.append({"name": "tplus1_prediction", "input_date": prediction.get("input_date")})1243 unmodified lines12441245124612471248124912503 unmodified lines1254125512561257125812591 unmodified line126112621263126412651266126719 unmodified lines128712881289129012911292384 unmodified lines1677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734448 unmodified lines21832184218521862187218821894 unmodified lines21942195219621972198219922002201220222032204220522062 unmodified lines220922102211221222132214221540 unmodified lines22562257225822592260226122622263226422652266226722681243 unmodified linesreturn df.sort_values("date").reset_index(drop=True)def load_mfe_summary() -> dict[str, Any]:if not MFE_SUMMARY_PATH.exists():return {}try:3 unmodified linesdef load_mfe_latest() -> dict[str, Any]:summary = load_mfe_summary()if MFE_LATEST_PATH.exists():try:1 unmodified lineup_pts = float(row.get("predicted_up_points", summary.get("latest_predicted_up_points", 0)))down_pts = float(row.get("predicted_down_points", summary.get("latest_predicted_down_points", 0)))return {"input_date": str(row.get("input_date") or row.get("date") or summary.get("latest_input_date", ""))[:10],"first5_start": row.get("first5_start") or summary.get("latest_first5_start"),"first5_end": row.get("first5_end") or summary.get("latest_first5_end"),"first5_close": float(row.get("first5_close") or summary.get("latest_first5_close") or 0),19 unmodified linesdef load_mfe_backtest() -> pd.DataFrame:if not MFE_TEST_PREDICTIONS_PATH.exists():return pd.DataFrame()frame = pd.read_csv(MFE_TEST_PREDICTIONS_PATH)384 unmodified linesreturn predictiondef refresh_mfe_prediction(session_date: date | None = None, minutes: pd.DataFrame | None = None) -> dict[str, Any]:if session_date is None:today = datetime.now(IST).date()if not is_trading_day(today):raise RuntimeError(f"{today.isoformat()} is not an NSE trading session.")else:today = session_dateminutes = fetch_yahoo_minutes(period="7d") if minutes is None else minutesfirst5 = first5_features_from_minutes(minutes, session_date=session_date)row = build_model_row(first5)payload = joblib.load(MFE_OUTPUT_DIR / "nifty_opening_mfe_regressor.joblib")up_model = payload["up_model"]down_model = payload["down_model"]up_features = payload["up_features"]down_features = payload["down_features"]up_pred = float(up_model.predict(row[up_features])[0])down_pred = float(down_model.predict(row[down_features])[0])input_date_str = str(pd.to_datetime(row["date"].iloc[0]).date())out = {"input_date": input_date_str,"first5_start": str(pd.to_datetime(row["first5_start"].iloc[0])),"first5_end": str(pd.to_datetime(row["first5_end"].iloc[0])),"first5_close": float(row["first5_close"].iloc[0]) if "first5_close" in row.columns else float(row["close"].iloc[0]),"predicted_up_points": up_pred,"predicted_down_points": down_pred,}pd.DataFrame([out]).to_csv(MFE_LATEST_PATH, index=False)if MFE_TEST_PREDICTIONS_PATH.exists():history = pd.read_csv(MFE_TEST_PREDICTIONS_PATH)if "date" in history.columns:history["date_obj"] = pd.to_datetime(history["date"], errors="coerce").dt.datehistory = history[history["date_obj"] != pd.to_datetime(input_date_str).date()].copy()history.drop(columns=["date_obj"], inplace=True)new_row = pd.DataFrame([{"date": input_date_str,"first5_close": out["first5_close"],"predicted_up_points": out["predicted_up_points"],"predicted_down_points": out["predicted_down_points"]}])history = pd.concat([history, new_row], ignore_index=True)history.to_csv(MFE_TEST_PREDICTIONS_PATH, index=False)return outdef refresh_daily_data() -> dict[str, Any]:daily = fetch_yahoo_daily(period="1mo")combined = append_parquet_rows(NIFTY_1D_PATH, daily, ["date"])448 unmodified lineslatest_daily = latest_parquet_date(NIFTY_1D_PATH)latest_minutes = latest_parquet_date(NIFTY_1M_PATH)latest_t5 = latest_prediction_input_date(LATEST_PATH)latest_mfe = latest_prediction_input_date(MFE_LATEST_PATH)latest_tomorrow = latest_tomorrow_input_date()latest_tplus1 = latest_prediction_input_date(TPLUS1_LATEST_PATH)return {4 unmodified lines"latest_daily_date": latest_daily.isoformat() if latest_daily else None,"latest_minute_date": latest_minutes.isoformat() if latest_minutes else None,"latest_t5_date": latest_t5.isoformat() if latest_t5 else None,"latest_mfe_date": latest_mfe.isoformat() if latest_mfe else None,"latest_tomorrow_date": latest_tomorrow.isoformat() if latest_tomorrow else None,"latest_tplus1_date": latest_tplus1.isoformat() if latest_tplus1 else None,"daily_stale": is_stale(latest_daily, expected_daily),"minutes_stale": is_stale(latest_minutes, expected_minutes),"t5_stale": is_stale(latest_t5, expected_minutes),"mfe_stale": is_stale(latest_mfe, expected_minutes),"tomorrow_stale": is_stale(latest_tomorrow, expected_daily),"tplus1_stale": is_stale(latest_tplus1, expected_tplus1),}2 unmodified linesdef refresh_stale_data_once(now: datetime | None = None) -> dict[str, Any]:now = now or datetime.now(IST)status = stale_data_status(now)if not any(status[key] for key in ("daily_stale", "minutes_stale", "t5_stale", "mfe_stale", "tomorrow_stale", "tplus1_stale")):return {"status": "fresh", **status, "actions": []}if not _stale_refresh_lock.acquire(blocking=False):return {"status": "skipped", "reason": "stale refresh already running", **status, "actions": []}40 unmodified linesprediction = refresh_first5_prediction(session_date=now.date())actions.append({"name": "t5_prediction", "input_date": prediction.input_date})if status["mfe_stale"] and is_trading_day(now.date()) and now.time() >= FIRST5_READY:try:mfe_pred = refresh_mfe_prediction(session_date=now.date())actions.append({"name": "mfe_prediction", "input_date": mfe_pred["input_date"]})except Exception as e:actions.append({"name": "mfe_prediction", "error": str(e)})if status["tplus1_stale"] and is_trading_day(now.date()) and now.time() >= TPLUS1_READY:prediction = refresh_tplus1_prediction(session_date=now.date())actions.append({"name": "tplus1_prediction", "input_date": prediction.get("input_date")})
runtime.py+64-22