-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresults_tracker.py
More file actions
207 lines (174 loc) · 7.65 KB
/
Copy pathresults_tracker.py
File metadata and controls
207 lines (174 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
Live results tracker — maintains a single CSV audit trail of all picks + outcomes.
Run after check_results.py resolves picks; also callable standalone.
Output: data/live_results.csv
"""
import os, json, glob, logging
import pandas as pd
from datetime import datetime
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s")
logger = logging.getLogger(__name__)
PICKS_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(PICKS_DIR, "data")
TRACKER_PATH = os.path.join(DATA_DIR, "live_results.csv")
COLS = [
"date", "player_name", "stat", "league",
"pp_line", "direction", "model_projection",
"confidence", "edge", "actual", "hit",
"kalshi_agree", "vegas_agree", "note",
]
def load_existing() -> pd.DataFrame:
if os.path.exists(TRACKER_PATH):
return pd.read_csv(TRACKER_PATH)
return pd.DataFrame(columns=COLS)
def log_pending_picks(picks_data: list, date_str: str):
"""Log emailed picks immediately at 5pm with hit=None (pending). Called from email_picks.py."""
existing = load_existing()
existing_keys = set(
zip(existing["date"], existing["player_name"], existing["stat"])
) if len(existing) else set()
new_rows = []
for p in picks_data:
key = (date_str, p["player_name"], p["stat"])
if key in existing_keys:
continue
new_rows.append({
"date": date_str,
"player_name": p["player_name"],
"stat": p["stat"],
"league": p.get("league", "NBA"),
"pp_line": p["pp_line"],
"direction": p["direction"],
"model_projection": p["model_projection"],
"confidence": p["confidence"],
"edge": p["edge"],
"actual": None,
"hit": None,
"kalshi_agree": p.get("kalshi_agree"),
"vegas_agree": p.get("vegas_agree"),
"note": "pending",
})
if not new_rows:
logger.info("All picks already in CSV.")
return existing
combined = pd.concat([existing, pd.DataFrame(new_rows)], ignore_index=True)
combined.to_csv(TRACKER_PATH, index=False)
logger.info(f"Logged {len(new_rows)} pending picks → {TRACKER_PATH} ({len(combined)} total rows)")
return combined
def update_outcomes(results: list, date_str: str):
"""Update hit/actual in CSV after ESPN results are available. Called from check_results.py."""
df = load_existing()
updated = 0
for r in results:
mask = (
(df["date"] == date_str) &
(df["player_name"] == r["player_name"]) &
(df["stat"] == r["stat"])
)
if mask.any():
df.loc[mask, "actual"] = r.get("actual")
df.loc[mask, "hit"] = r.get("hit")
df.loc[mask, "note"] = r.get("note", "")
else:
# Pick wasn't pre-logged at 5pm — add it now as a safety net
new_row = {c: None for c in COLS}
new_row.update({
"date": date_str,
"player_name": r["player_name"],
"stat": r["stat"],
"league": r.get("league", "NBA"),
"pp_line": r.get("pp_line"),
"direction": r.get("direction"),
"model_projection": r.get("model_projection"),
"confidence": r.get("confidence"),
"edge": r.get("edge"),
"actual": r.get("actual"),
"hit": r.get("hit"),
"note": r.get("note", "recovered"),
})
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
updated += 1
df.to_csv(TRACKER_PATH, index=False)
logger.info(f"Updated {updated} outcomes for {date_str} → {TRACKER_PATH}")
return df
def sync_all_results():
"""Scan all results_*.json files and merge into live_results.csv."""
existing = load_existing()
# Key: (date, player_name, stat) — prevents duplicates on re-run
existing_keys = set(
zip(existing["date"], existing["player_name"], existing["stat"])
) if len(existing) else set()
result_files = sorted(glob.glob(os.path.join(PICKS_DIR, "results_*.json")))
new_rows = []
for rf in result_files:
date_str = os.path.basename(rf).replace("results_", "").replace(".json", "")
# Load corresponding picks file for extra metadata
picks_path = os.path.join(PICKS_DIR, f"picks_{date_str}.json")
picks_meta = {}
if os.path.exists(picks_path):
with open(picks_path) as f:
for p in json.load(f):
picks_meta[(p["player_name"], p["stat"])] = p
with open(rf) as f:
results = json.load(f)
for r in results:
key = (date_str, r["player_name"], r["stat"])
if key in existing_keys:
continue
meta = picks_meta.get((r["player_name"], r["stat"]), {})
new_rows.append({
"date": date_str,
"player_name": r["player_name"],
"stat": r["stat"],
"league": r.get("league", "NBA"),
"pp_line": r.get("pp_line"),
"direction": r.get("direction"),
"model_projection": r.get("model_projection"),
"confidence": r.get("confidence"),
"edge": r.get("edge"),
"actual": r.get("actual"),
"hit": r.get("hit"),
"kalshi_agree": meta.get("kalshi_agree"),
"vegas_agree": meta.get("vegas_agree"),
"note": r.get("note", ""),
})
if not new_rows:
logger.info("No new results to add.")
return existing
combined = pd.concat([existing, pd.DataFrame(new_rows)], ignore_index=True)
combined.to_csv(TRACKER_PATH, index=False)
logger.info(f"Added {len(new_rows)} new rows → {TRACKER_PATH} ({len(combined)} total)")
return combined
def print_summary(df: pd.DataFrame = None):
"""Print rolling accuracy summary by league, stat, and direction."""
if df is None:
df = load_existing()
if df.empty:
logger.info("No results yet.")
return
decided = df[df["hit"].notna() & (df["hit"] != "")].copy()
decided["hit"] = decided["hit"].astype(bool)
n = len(decided)
acc = decided["hit"].mean() if n > 0 else float("nan")
print(f"\n{'='*55}")
print(f"OVERALL: {n} picks decided — accuracy {acc:.1%}")
print(f"{'='*55}")
if "league" in decided.columns:
for league, grp in decided.groupby("league"):
print(f" {league}: {len(grp)} picks {grp['hit'].mean():.1%}")
print("\nBy stat:")
for stat, grp in decided.groupby("stat"):
print(f" {stat:20s}: {len(grp):3d} picks {grp['hit'].mean():.1%}")
print("\nBy direction:")
for d, grp in decided.groupby("direction"):
print(f" {d:5s}: {len(grp):3d} picks {grp['hit'].mean():.1%}")
print("\nBy confidence tier:")
decided["conf_tier"] = pd.cut(decided["confidence"],
bins=[0, 65, 70, 75, 80, 100],
labels=["60-65", "65-70", "70-75", "75-80", "80+"])
for tier, grp in decided.groupby("conf_tier", observed=True):
print(f" {tier}: {len(grp):3d} picks {grp['hit'].mean():.1%}")
print()
if __name__ == "__main__":
df = sync_all_results()
print_summary(df)