-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_cache.py
More file actions
224 lines (179 loc) · 7.72 KB
/
Copy pathbuild_cache.py
File metadata and controls
224 lines (179 loc) · 7.72 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""
Costruisce una cache locale di window storiche mese per mese.
Ideale per backtest su periodi lunghi (3-6 mesi) senza sovraccaricare le API.
Uso:
# Ultimi 6 mesi (default)
python build_cache.py
# Range personalizzato
python build_cache.py --start 2025-12-01 --end 2026-06-01
# Salva in un file specifico
python build_cache.py --cache data/cache_6m.pkl
# Vedi progresso senza salvare (dry)
python build_cache.py --dry-run
Dopo la build, usa la cache con:
python backtest_runner.py --load-cache data/cache_6m.pkl --max-entry-sum 0.999 --min-profit-pct 0.0
"""
from __future__ import annotations
import argparse
import asyncio
import logging
import pickle
import sys
from datetime import datetime, timezone, timedelta
from pathlib import Path
import yaml
from backtest.historical_fetcher import HistoricalFetcher, HistoricalWindow
from data.data_api import DataAPI
from data.gamma_api import GammaAPI
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
)
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
_DEFAULT_CACHE = Path("data/cache_6m.pkl")
_CONFIG_PATH = Path("config/settings.yaml")
def _load_config() -> dict:
with _CONFIG_PATH.open(encoding="utf-8") as fh:
return yaml.safe_load(fh)
def _parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(description="Build historical window cache month by month")
p.add_argument("--start", default=None, help="Start date YYYY-MM-DD (default: 6 months ago)")
p.add_argument("--end", default=None, help="End date YYYY-MM-DD (default: yesterday)")
p.add_argument("--cache", default=str(_DEFAULT_CACHE), help=f"Cache file path (default: {_DEFAULT_CACHE})")
p.add_argument("--max-per-month", type=int, default=5000, help="Max windows per month (default: 5000)")
p.add_argument("--dry-run", action="store_true", help="Fetch but do not save")
p.add_argument("--pause", type=float, default=5.0, help="Pause in seconds between months (default: 5)")
return p.parse_args()
def _load_existing_cache(path: Path) -> list[HistoricalWindow]:
if path.exists():
with path.open("rb") as fh:
data = pickle.load(fh)
logger.info("Cache esistente: %d windows in %s", len(data), path)
return data
return []
def _save_cache(windows: list[HistoricalWindow], path: Path) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("wb") as fh:
pickle.dump(windows, fh)
size_kb = path.stat().st_size // 1024
logger.info("Cache salvata: %d windows, %d KB -> %s", len(windows), size_kb, path)
def _month_slots(start_ts: float, end_ts: float) -> list[tuple[float, float, str]]:
"""Restituisce liste di (month_start, month_end, label) per ogni mese nel range."""
slots = []
cur = datetime.fromtimestamp(start_ts, tz=timezone.utc).replace(day=1, hour=0, minute=0, second=0)
end_dt = datetime.fromtimestamp(end_ts, tz=timezone.utc)
while cur < end_dt:
# Fine mese
if cur.month == 12:
next_month = cur.replace(year=cur.year + 1, month=1)
else:
next_month = cur.replace(month=cur.month + 1)
slot_end = min(next_month, end_dt)
slots.append((cur.timestamp(), slot_end.timestamp(), cur.strftime("%Y-%m")))
cur = next_month
return slots
async def _fetch_month(
fetcher: HistoricalFetcher,
month_start: float,
month_end: float,
label: str,
limit: int,
) -> list[HistoricalWindow]:
logger.info("Fetching %s ...", label)
try:
windows = await fetcher.fetch_windows(
start_ts=month_start,
end_ts=month_end,
limit=limit,
)
windows = await fetcher.enrich_with_outcomes(windows)
resolved = sum(1 for w in windows if w.winning_side is not None)
logger.info(
"%s: %d windows, %d resolved", label, len(windows), resolved
)
return windows
except Exception as exc:
logger.error("Errore su %s: %s", label, exc)
return []
async def _run(args: argparse.Namespace) -> None:
config = _load_config()
slug_patterns: list[str] = config.get("markets", {}).get(
"slug_patterns", ["btc-updown-5m", "btc-updown-15m"]
)
now = datetime.now(tz=timezone.utc)
if args.start:
start_ts = datetime.strptime(args.start, "%Y-%m-%d").replace(tzinfo=timezone.utc).timestamp()
else:
start_ts = (now - timedelta(days=180)).timestamp()
if args.end:
end_ts = datetime.strptime(args.end, "%Y-%m-%d").replace(tzinfo=timezone.utc).timestamp()
else:
end_ts = (now - timedelta(days=1)).timestamp()
start_str = datetime.fromtimestamp(start_ts, tz=timezone.utc).strftime("%Y-%m-%d")
end_str = datetime.fromtimestamp(end_ts, tz=timezone.utc).strftime("%Y-%m-%d")
cache_path = Path(args.cache)
months = _month_slots(start_ts, end_ts)
print(f"\n{'='*60}")
print(f" BUILD CACHE {start_str} to {end_str}")
print(f" Mesi da scaricare: {len(months)}")
print(f" Cache: {cache_path}")
print(f" Dry-run: {args.dry_run}")
print(f"{'='*60}\n")
# Carica cache esistente per non rifare mesi gia' scaricati
existing = _load_existing_cache(cache_path)
existing_months = set()
for w in existing:
dt = datetime.fromtimestamp(w.end_ts, tz=timezone.utc)
existing_months.add(dt.strftime("%Y-%m"))
all_windows: list[HistoricalWindow] = list(existing)
seen_ids = {w.condition_id for w in all_windows}
async with DataAPI() as data_api, GammaAPI() as gamma_api:
fetcher = HistoricalFetcher(data_api, gamma_api, slug_patterns)
for i, (m_start, m_end, label) in enumerate(months):
if label in existing_months:
print(f" [{i+1}/{len(months)}] {label} — gia' in cache, skip")
continue
print(f" [{i+1}/{len(months)}] {label} — downloading...")
windows = await _fetch_month(
fetcher, m_start, m_end, label, args.max_per_month
)
new_count = 0
for w in windows:
if w.condition_id not in seen_ids:
all_windows.append(w)
seen_ids.add(w.condition_id)
new_count += 1
print(f" +{new_count} nuovi window (totale: {len(all_windows)})")
if not args.dry_run:
all_windows_sorted = sorted(all_windows, key=lambda w: w.end_ts)
_save_cache(all_windows_sorted, cache_path)
# Pausa tra mesi per non sovraccaricare la Gamma API
if i < len(months) - 1:
logger.info("Pausa %.0fs prima del prossimo mese...", args.pause)
await asyncio.sleep(args.pause)
all_windows.sort(key=lambda w: w.end_ts)
resolved = sum(1 for w in all_windows if w.winning_side is not None)
print(f"\n{'='*60}")
print(f" CACHE COMPLETATA")
print(f" Totale windows: {len(all_windows)}")
print(f" Risolti: {resolved}")
if not args.dry_run:
size_kb = cache_path.stat().st_size // 1024
print(f" File: {cache_path} ({size_kb} KB)")
print(f"\nUsa la cache con:")
print(f" python backtest_runner.py --load-cache {cache_path} \\")
print(f" --entry-offset 240 --max-entry-sum 0.999 --min-profit-pct 0.0 --csv risultati.csv")
print(f"{'='*60}")
def main() -> None:
args = _parse_args()
try:
asyncio.run(_run(args))
except KeyboardInterrupt:
print("\nInterrotto. La cache parziale e' stata salvata.")
except Exception as exc:
logger.error("Errore: %s", exc, exc_info=True)
sys.exit(1)
if __name__ == "__main__":
main()