Upload 2 files
Browse filesFiles changed (2)
- runtime.py+50-60
- test_seeding.pyAdded+17
1398 unmodified lines1399140014011402140314041405140614073 unmodified lines1411141214131414141514161417141814191420142114221423142414251426142714281 unmodified line14301431143214331434143514361437143814391440144114429 unmodified lines145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414959 unmodified lines150515061507150815091510151115121513151420 unmodified lines153515361537153815391540154111 unmodified lines155315541555155615571558155927 unmodified lines15871588158915901591159215932 unmodified lines1596159715981599160016011602160316041605160616071398 unmodified linesexcept Exception:passreturn {"tomorrow": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0},"t5": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0},"tplus1": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0},}3 unmodified linesdef seed_live_accuracy_from_backtest() -> dict[str, Any]:"""Seed the live accuracy ledger from backtest test predictions.This creates the baseline entries from the test set so thataccuracy starts at the backtest level and moves smoothly."""ledger = {"tomorrow": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0, "live_count": 0},"t5": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0, "live_count": 0},"tplus1": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0, "live_count": 0},}# Seed T+5t5_test = load_test_predictions()if not t5_test.empty and "correct" in t5_test.columns:for _, row in t5_test.iterrows():1 unmodified linepred = str(row.get("prediction", "")).upper()correct = bool(row.get("correct"))actual = pred if correct else ("DOWN" if pred == "UP" else "UP")ledger["t5"]["entries"].append({"date": day, "prediction": pred,"actual": actual, "correct": correct,"source": "backtest",})# Seed Tomorrowtom_test = load_tomorrow_test_predictions()if not tom_test.empty:if "pred" in tom_test.columns and "prediction" not in tom_test.columns:9 unmodified linespred = str(row.get("prediction", "")).upper()correct = bool(row.get("correct"))actual = pred if correct else ("DOWN" if pred == "UP" else "UP")ledger["tomorrow"]["entries"].append({"date": day, "prediction": pred,"actual": actual, "correct": correct,"source": "backtest",})# Seed T+1t1_test = load_tplus1_test_predictions()if not t1_test.empty:for _, row in t1_test.iterrows():target_date = row.get("target_date")if pd.isna(target_date):continueday = pd.to_datetime(target_date).date().isoformat()pred = str(row.get("prediction", "")).upper()if "correct" in t1_test.columns:correct = bool(row.get("correct"))actual = pred if correct else ("DOWN" if pred == "UP" else "UP")elif {"target", "prediction"}.issubset(t1_test.columns):target_val = pd.to_numeric(row.get("target"), errors="coerce")actual = "UP" if target_val == 1 else "DOWN"correct = pred == actualelse:continueledger["tplus1"]["entries"].append({"date": day, "prediction": pred,"actual": actual, "correct": correct,"source": "backtest",})for model_id in ("t5", "tomorrow", "tplus1"):entries = ledger[model_id]["entries"]total = len(entries)correct = sum(1 for e in entries if e.get("correct"))ledger[model_id]["total"] = totalledger[model_id]["correct_count"] = correctledger[model_id]["accuracy"] = correct / total if total > 0 else Noneledger[model_id]["live_count"] = 0save_live_accuracy(ledger)return ledger9 unmodified lines"""ledger = load_live_accuracy()# If ledger is empty/unseeded, seed from backtest firstif not any(ledger[m].get("entries") for m in ("t5", "tomorrow", "tplus1")):ledger = seed_live_accuracy_from_backtest()daily = pd.read_parquet(NIFTY_1D_PATH)daily["_date"] = pd.to_datetime(daily["date"], errors="coerce").dt.normalize()today_rows = daily[daily["_date"].dt.date == session_date]20 unmodified lines"prediction": pred,"actual": actual_close_gt_open,"correct": pred == actual_close_gt_open,"source": "live",})except Exception:pass11 unmodified lines"prediction": pred,"actual": actual_close_gt_open,"correct": pred == actual_close_gt_open,"source": "live",})except Exception:pass27 unmodified lines"prediction": pred,"actual": t1_actual,"correct": pred == t1_actual,"source": "live",})except Exception:pass2 unmodified linesfor model_id in ("t5", "tomorrow", "tplus1"):entries = ledger[model_id]["entries"]total = len(entries)live_count = sum(1 for e in entries if e.get("source") == "live")correct = sum(1 for e in entries if e.get("correct"))ledger[model_id]["total"] = totalledger[model_id]["live_count"] = live_countledger[model_id]["correct_count"] = correctledger[model_id]["accuracy"] = correct / total if total > 0 else Nonesave_live_accuracy(ledger)clear_dashboard_payload_cache()1398 unmodified lines1399140014011402140314041405140614073 unmodified lines141114121413141414151416141714181419142014211422142314241 unmodified line14261427142814291430143114321433143414351436143714389 unmodified lines14481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714789 unmodified lines148814891490149114921493149414951496149720 unmodified lines151815191520152115221523152411 unmodified lines153615371538153915401541154227 unmodified lines15701571157215731574157515762 unmodified lines15791580158115821583158415851586158715881589159015911592159315941595159615971398 unmodified linesexcept Exception:passreturn {"tomorrow": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0, "backtest_count": 0, "live_count": 0},"t5": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0, "backtest_count": 0, "live_count": 0},"tplus1": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0, "backtest_count": 0, "live_count": 0},}3 unmodified linesdef seed_live_accuracy_from_backtest() -> dict[str, Any]:"""Seed the live accuracy ledger from backtest test predictions."""ledger = {"tomorrow": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0, "backtest_count": 0, "live_count": 0},"t5": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0, "backtest_count": 0, "live_count": 0},"tplus1": {"entries": [], "accuracy": None, "total": 0, "correct_count": 0, "backtest_count": 0, "live_count": 0},}# T+5t5_test = load_test_predictions()if not t5_test.empty and "correct" in t5_test.columns:for _, row in t5_test.iterrows():1 unmodified linepred = str(row.get("prediction", "")).upper()correct = bool(row.get("correct"))actual = pred if correct else ("DOWN" if pred == "UP" else "UP")if pred in ("UP", "DOWN"):ledger["t5"]["entries"].append({"date": day, "prediction": pred, "actual": actual, "correct": correct, "source": "backtest"})ledger["t5"]["backtest_count"] = len(ledger["t5"]["entries"])# Tomorrowtom_test = load_tomorrow_test_predictions()if not tom_test.empty:if "pred" in tom_test.columns and "prediction" not in tom_test.columns:9 unmodified linespred = str(row.get("prediction", "")).upper()correct = bool(row.get("correct"))actual = pred if correct else ("DOWN" if pred == "UP" else "UP")if pred in ("UP", "DOWN"):ledger["tomorrow"]["entries"].append({"date": day, "prediction": pred, "actual": actual, "correct": correct, "source": "backtest"})ledger["tomorrow"]["backtest_count"] = len(ledger["tomorrow"]["entries"])# T+1t1_test = load_tplus1_test_predictions()if not t1_test.empty:if "correct" not in t1_test.columns and {"target", "prediction"}.issubset(t1_test.columns):t1_test["correct"] = t1_test["prediction"].str.upper() == np.where(pd.to_numeric(t1_test["target"], errors="coerce") == 1, "UP", "DOWN")if "correct" in t1_test.columns:for _, row in t1_test.iterrows():target_date = row.get("target_date")if pd.isna(target_date):continueday = pd.to_datetime(target_date).date().isoformat()pred = str(row.get("prediction", "")).upper()correct = bool(row.get("correct"))actual = pred if correct else ("DOWN" if pred == "UP" else "UP")if pred in ("UP", "DOWN"):ledger["tplus1"]["entries"].append({"date": day, "prediction": pred, "actual": actual, "correct": correct, "source": "backtest"})ledger["tplus1"]["backtest_count"] = len(ledger["tplus1"]["entries"])save_live_accuracy(ledger)return ledger9 unmodified lines"""ledger = load_live_accuracy()# If ledger is empty (or unseeded), seed from backtest firstif not any(ledger[m]["entries"] for m in ("t5", "tomorrow", "tplus1")):ledger = seed_live_accuracy_from_backtest()daily = pd.read_parquet(NIFTY_1D_PATH)daily["_date"] = pd.to_datetime(daily["date"], errors="coerce").dt.normalize()today_rows = daily[daily["_date"].dt.date == session_date]20 unmodified lines"prediction": pred,"actual": actual_close_gt_open,"correct": pred == actual_close_gt_open,"source": "live"})except Exception:pass11 unmodified lines"prediction": pred,"actual": actual_close_gt_open,"correct": pred == actual_close_gt_open,"source": "live"})except Exception:pass27 unmodified lines"prediction": pred,"actual": t1_actual,"correct": pred == t1_actual,"source": "live"})except Exception:pass2 unmodified linesfor model_id in ("t5", "tomorrow", "tplus1"):entries = ledger[model_id]["entries"]total = len(entries)correct = sum(1 for e in entries if e.get("correct"))backtest_count = sum(1 for e in entries if e.get("source") == "backtest")live_count = sum(1 for e in entries if e.get("source") == "live")# If we have entries but no source is set, assume they need re-seedingif backtest_count == 0 and live_count == 0 and total > 0:pass # The user can delete the file to force a re-seed. Or we just keep them.ledger[model_id]["total"] = totalledger[model_id]["correct_count"] = correctledger[model_id]["accuracy"] = correct / total if total > 0 else Noneledger[model_id]["backtest_count"] = backtest_countledger[model_id]["live_count"] = live_countsave_live_accuracy(ledger)clear_dashboard_payload_cache()
1234567891011121314151617import sysfrom pathlib import Path
# Add backend to pathbackend_path = Path(__file__).parent / 'backend'sys.path.insert(0, str(backend_path))
from nifty_backend.runtime import seed_live_accuracy_from_backtest
print("Seeding live accuracy...")ledger = seed_live_accuracy_from_backtest()for model in ('t5', 'tomorrow', 'tplus1'): entries = ledger[model]['entries'] total = len(entries) correct = sum(1 for e in entries if e.get('correct')) accuracy = correct / total if total > 0 else 0 print(f"{model}: entries={total}, backtest_count={ledger[model]['backtest_count']}, correct={correct}, accuracy={accuracy:.3f}")runtime.py+50-60
test_seeding.pyAdded+17