Files
quant-trading/research/lab/trials.py
Gahow Wang 722cade974 fix: unify backtest execution and add forward validation candidates
Share point-in-time signals and a cash-funded order ledger across backtests and trading. Correct eligibility, missing-price fills, quality factors, regime inputs, warmup windows, and cash-injection accounting. Add reproducible registered-strategy comparisons and paired forward candidates with a 20% resize band.

Validation: 125 Python tests, 23 web tests, TypeScript/Vite build, 87 strategy causality checks, and real-data target/order/equity parity for 10 strategies.
2026-10-01 02:24:01 -07:00

46 lines
1.7 KiB
Python

"""Append-only trial records; legacy CSVs are read without discarding wide rows."""
import csv
import json
from datetime import datetime, timezone
from pathlib import Path
import fcntl
def append_trials(path, rows, protocol):
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
created = datetime.now(timezone.utc).isoformat()
with path.open('a') as fh:
fcntl.flock(fh, fcntl.LOCK_EX)
for row in rows:
record = {'schema': 1, 'created_at': created, 'protocol': protocol, 'result': row}
fh.write(json.dumps(record, allow_nan=False, default=float) + '\n')
def count_trials(directory, market):
"""Observed trials are a lower bound; unrecorded searches remain unknown."""
total = 0
paths = [Path(directory) / f'trials_{market}.csv', Path(directory) / f'trials_{market}.jsonl']
if market == 'us':
paths += [Path(directory) / 'trials_us_sweep.csv', Path(directory) / 'trials_us_sweep.jsonl']
for path in paths:
if not path.exists():
continue
with path.open() as fh:
if path.suffix == '.jsonl':
for line in fh:
if line.strip():
record = json.loads(line)
if record.get('schema') != 1:
raise ValueError(f'Unknown trial schema: {path}')
total += 1
else:
rows = csv.reader(fh)
next(rows, None)
for row in rows:
if row:
if len(row) < 2:
raise ValueError(f'Incomplete trial row: {path}')
total += 1
return total