Upload runtime.py
Browse files- runtime.py +57 -22
runtime.py
CHANGED
|
@@ -1441,10 +1441,10 @@ def load_live_accuracy() -> dict[str, Any]:
|
|
| 1441 |
total = len(entries)
|
| 1442 |
correct = sum(1 for e in entries if e.get("correct"))
|
| 1443 |
current["entries"] = entries
|
| 1444 |
-
current["backtest_count"] = int(
|
| 1445 |
-
current["live_count"] = int(
|
| 1446 |
-
current["total"] = int(
|
| 1447 |
-
current["correct_count"] = int(
|
| 1448 |
current["accuracy"] = (current["correct_count"] / current["total"]) if current["total"] > 0 else None
|
| 1449 |
default[model_id].update(current)
|
| 1450 |
return default
|
|
@@ -1499,10 +1499,22 @@ def update_live_accuracy(session_date: date) -> dict[str, Any]:
|
|
| 1499 |
|
| 1500 |
# --- T+5: today's 9:20 AM prediction vs close > open ---
|
| 1501 |
logged_t5 = {e["date"] for e in ledger["t5"]["entries"]}
|
| 1502 |
-
if session_iso not in logged_t5
|
| 1503 |
-
|
| 1504 |
-
|
| 1505 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1506 |
pred = str(t5_row.get("prediction", "")).upper()
|
| 1507 |
if pred in ("UP", "DOWN"):
|
| 1508 |
ledger["t5"]["entries"].append({
|
|
@@ -1510,16 +1522,29 @@ def update_live_accuracy(session_date: date) -> dict[str, Any]:
|
|
| 1510 |
"prediction": pred,
|
| 1511 |
"actual": actual_close_gt_open,
|
| 1512 |
"correct": pred == actual_close_gt_open,
|
|
|
|
| 1513 |
})
|
| 1514 |
-
|
| 1515 |
-
|
| 1516 |
|
| 1517 |
# --- Tomorrow: yesterday's prediction targeting today vs close > open ---
|
| 1518 |
logged_tom = {e["date"] for e in ledger["tomorrow"]["entries"]}
|
| 1519 |
-
if session_iso not in logged_tom
|
| 1520 |
-
|
| 1521 |
-
|
| 1522 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1523 |
pred = str(tom_row.get("prediction", "")).upper()
|
| 1524 |
if pred in ("UP", "DOWN"):
|
| 1525 |
ledger["tomorrow"]["entries"].append({
|
|
@@ -1527,17 +1552,23 @@ def update_live_accuracy(session_date: date) -> dict[str, Any]:
|
|
| 1527 |
"prediction": pred,
|
| 1528 |
"actual": actual_close_gt_open,
|
| 1529 |
"correct": pred == actual_close_gt_open,
|
|
|
|
| 1530 |
})
|
| 1531 |
-
|
| 1532 |
-
|
| 1533 |
|
| 1534 |
# --- T+1: yesterday's 14:20 prediction targeting today ---
|
| 1535 |
# T+1 target: today's close > yesterday's 14:20 close
|
| 1536 |
logged_t1 = {e["date"] for e in ledger["tplus1"]["entries"]}
|
| 1537 |
-
if session_iso not in logged_t1
|
| 1538 |
-
|
| 1539 |
-
|
| 1540 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1541 |
pred = str(t1_row.get("prediction", "")).upper()
|
| 1542 |
input_date_str = str(t1_row.get("input_date", ""))[:10]
|
| 1543 |
input_day = date.fromisoformat(input_date_str)
|
|
@@ -1560,17 +1591,21 @@ def update_live_accuracy(session_date: date) -> dict[str, Any]:
|
|
| 1560 |
"prediction": pred,
|
| 1561 |
"actual": t1_actual,
|
| 1562 |
"correct": pred == t1_actual,
|
|
|
|
| 1563 |
})
|
| 1564 |
-
|
| 1565 |
-
|
| 1566 |
|
| 1567 |
# Recompute summary stats
|
| 1568 |
for model_id in ("t5", "tomorrow", "tplus1"):
|
| 1569 |
entries = ledger[model_id]["entries"]
|
| 1570 |
total = len(entries)
|
| 1571 |
correct = sum(1 for e in entries if e.get("correct"))
|
|
|
|
| 1572 |
ledger[model_id]["total"] = total
|
| 1573 |
ledger[model_id]["correct_count"] = correct
|
|
|
|
|
|
|
| 1574 |
ledger[model_id]["accuracy"] = correct / total if total > 0 else None
|
| 1575 |
|
| 1576 |
save_live_accuracy(ledger)
|
|
|
|
| 1441 |
total = len(entries)
|
| 1442 |
correct = sum(1 for e in entries if e.get("correct"))
|
| 1443 |
current["entries"] = entries
|
| 1444 |
+
current["backtest_count"] = int(len(backtest_entries))
|
| 1445 |
+
current["live_count"] = int(len(live_entries))
|
| 1446 |
+
current["total"] = int(total)
|
| 1447 |
+
current["correct_count"] = int(correct)
|
| 1448 |
current["accuracy"] = (current["correct_count"] / current["total"]) if current["total"] > 0 else None
|
| 1449 |
default[model_id].update(current)
|
| 1450 |
return default
|
|
|
|
| 1499 |
|
| 1500 |
# --- T+5: today's 9:20 AM prediction vs close > open ---
|
| 1501 |
logged_t5 = {e["date"] for e in ledger["t5"]["entries"]}
|
| 1502 |
+
if session_iso not in logged_t5:
|
| 1503 |
+
t5_history = _load_prediction_history(T5_PREDICTION_HISTORY_PATH)
|
| 1504 |
+
if not t5_history.empty and "target_date" in t5_history.columns:
|
| 1505 |
+
t5_rows = t5_history[t5_history["target_date"].dt.date == session_date]
|
| 1506 |
+
else:
|
| 1507 |
+
t5_rows = pd.DataFrame()
|
| 1508 |
+
if t5_rows.empty and LATEST_PATH.exists():
|
| 1509 |
+
try:
|
| 1510 |
+
t5_row = pd.read_csv(LATEST_PATH).iloc[-1].to_dict()
|
| 1511 |
+
if str(t5_row.get("input_date", ""))[:10] == session_iso:
|
| 1512 |
+
t5_rows = pd.DataFrame([t5_row])
|
| 1513 |
+
except Exception:
|
| 1514 |
+
t5_rows = pd.DataFrame()
|
| 1515 |
+
if not t5_rows.empty:
|
| 1516 |
+
try:
|
| 1517 |
+
t5_row = t5_rows.iloc[-1].to_dict()
|
| 1518 |
pred = str(t5_row.get("prediction", "")).upper()
|
| 1519 |
if pred in ("UP", "DOWN"):
|
| 1520 |
ledger["t5"]["entries"].append({
|
|
|
|
| 1522 |
"prediction": pred,
|
| 1523 |
"actual": actual_close_gt_open,
|
| 1524 |
"correct": pred == actual_close_gt_open,
|
| 1525 |
+
"source": "live",
|
| 1526 |
})
|
| 1527 |
+
except Exception:
|
| 1528 |
+
pass
|
| 1529 |
|
| 1530 |
# --- Tomorrow: yesterday's prediction targeting today vs close > open ---
|
| 1531 |
logged_tom = {e["date"] for e in ledger["tomorrow"]["entries"]}
|
| 1532 |
+
if session_iso not in logged_tom:
|
| 1533 |
+
tom_history = _load_prediction_history(TOMORROW_PREDICTION_HISTORY_PATH)
|
| 1534 |
+
if not tom_history.empty and "target_date" in tom_history.columns:
|
| 1535 |
+
tom_rows = tom_history[tom_history["target_date"].dt.date == session_date]
|
| 1536 |
+
else:
|
| 1537 |
+
tom_rows = pd.DataFrame()
|
| 1538 |
+
if tom_rows.empty and TOMORROW_LATEST_PATH.exists():
|
| 1539 |
+
try:
|
| 1540 |
+
tom_row = pd.read_csv(TOMORROW_LATEST_PATH).iloc[-1].to_dict()
|
| 1541 |
+
if str(tom_row.get("target_date", ""))[:10] == session_iso:
|
| 1542 |
+
tom_rows = pd.DataFrame([tom_row])
|
| 1543 |
+
except Exception:
|
| 1544 |
+
tom_rows = pd.DataFrame()
|
| 1545 |
+
if not tom_rows.empty:
|
| 1546 |
+
try:
|
| 1547 |
+
tom_row = tom_rows.iloc[-1].to_dict()
|
| 1548 |
pred = str(tom_row.get("prediction", "")).upper()
|
| 1549 |
if pred in ("UP", "DOWN"):
|
| 1550 |
ledger["tomorrow"]["entries"].append({
|
|
|
|
| 1552 |
"prediction": pred,
|
| 1553 |
"actual": actual_close_gt_open,
|
| 1554 |
"correct": pred == actual_close_gt_open,
|
| 1555 |
+
"source": "live",
|
| 1556 |
})
|
| 1557 |
+
except Exception:
|
| 1558 |
+
pass
|
| 1559 |
|
| 1560 |
# --- T+1: yesterday's 14:20 prediction targeting today ---
|
| 1561 |
# T+1 target: today's close > yesterday's 14:20 close
|
| 1562 |
logged_t1 = {e["date"] for e in ledger["tplus1"]["entries"]}
|
| 1563 |
+
if session_iso not in logged_t1:
|
| 1564 |
+
t1_history = _load_prediction_history(TPLUS1_PREDICTION_HISTORY_PATH)
|
| 1565 |
+
if not t1_history.empty and "target_date" in t1_history.columns:
|
| 1566 |
+
t1_rows = t1_history[t1_history["target_date"].dt.date == session_date]
|
| 1567 |
+
else:
|
| 1568 |
+
t1_rows = pd.DataFrame()
|
| 1569 |
+
if not t1_rows.empty:
|
| 1570 |
+
try:
|
| 1571 |
+
t1_row = t1_rows.iloc[-1].to_dict()
|
| 1572 |
pred = str(t1_row.get("prediction", "")).upper()
|
| 1573 |
input_date_str = str(t1_row.get("input_date", ""))[:10]
|
| 1574 |
input_day = date.fromisoformat(input_date_str)
|
|
|
|
| 1591 |
"prediction": pred,
|
| 1592 |
"actual": t1_actual,
|
| 1593 |
"correct": pred == t1_actual,
|
| 1594 |
+
"source": "live",
|
| 1595 |
})
|
| 1596 |
+
except Exception:
|
| 1597 |
+
pass
|
| 1598 |
|
| 1599 |
# Recompute summary stats
|
| 1600 |
for model_id in ("t5", "tomorrow", "tplus1"):
|
| 1601 |
entries = ledger[model_id]["entries"]
|
| 1602 |
total = len(entries)
|
| 1603 |
correct = sum(1 for e in entries if e.get("correct"))
|
| 1604 |
+
backtest_count = sum(1 for e in entries if str(e.get("source", "backtest")).lower() == "backtest")
|
| 1605 |
ledger[model_id]["total"] = total
|
| 1606 |
ledger[model_id]["correct_count"] = correct
|
| 1607 |
+
ledger[model_id]["backtest_count"] = backtest_count
|
| 1608 |
+
ledger[model_id]["live_count"] = total - backtest_count
|
| 1609 |
ledger[model_id]["accuracy"] = correct / total if total > 0 else None
|
| 1610 |
|
| 1611 |
save_live_accuracy(ledger)
|