Buckets:
| #!/usr/bin/env python3 | |
| """Run the backtest with TRAINED models (loads saved model weights). | |
| Usage: | |
| python scripts/run_backtest_trained.py --data data/parquet/BTCUSDT_2021-05-19.parquet | |
| python scripts/run_backtest_trained.py --data data/parquet/LUNAUSDT_2022-05-10.parquet | |
| """ | |
| import argparse | |
| import logging | |
| import sys | |
| from pathlib import Path | |
| ML_DIR = Path(__file__).resolve().parent.parent / "ml" | |
| sys.path.insert(0, str(ML_DIR)) | |
| PROJECT_ROOT = Path(__file__).resolve().parent.parent | |
| sys.path.insert(0, str(PROJECT_ROOT)) | |
| from flash_crash_watchdog.cascade import DetectionCascade | |
| from flash_crash_watchdog.data.historical_loader import load_parquet | |
| from flash_crash_watchdog.eval.backtest import run_backtest | |
| from flash_crash_watchdog.models.stage2_isolation_forest import Stage2IsolationForest | |
| from flash_crash_watchdog.models.stage3_tcn import Stage3TCN, TCNConfig | |
| def main() -> int: | |
| parser = argparse.ArgumentParser(description="Run backtest with trained models") | |
| parser.add_argument("--data", required=True, help="Parquet file of crash data") | |
| parser.add_argument("--config", default="configs/pipeline.yml") | |
| parser.add_argument("--models", default="models/", help="Directory with trained models") | |
| parser.add_argument("--output", default="results/backtest_trained.json") | |
| parser.add_argument("--max-ticks", type=int, default=0, | |
| help="Max ticks to process (0 = all, for speed use 500000)") | |
| args = parser.parse_args() | |
| logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") | |
| # Load cascade | |
| cascade = DetectionCascade.from_config(args.config) | |
| # Load trained models if they exist | |
| models_dir = Path(args.models) | |
| stage2_path = models_dir / "stage2_isolation_forest.joblib" | |
| stage3_path = models_dir / "stage3_tcn.pt" | |
| if stage2_path.exists(): | |
| logger.info("Loading trained Stage 2 from %s", stage2_path) | |
| cascade.s2.load(stage2_path) | |
| else: | |
| logger.warning("No trained Stage 2 found at %s — using untrained fallback", stage2_path) | |
| if stage3_path.exists(): | |
| logger.info("Loading trained Stage 3 from %s", stage3_path) | |
| cascade.s3.load(stage3_path) | |
| else: | |
| logger.warning("No trained Stage 3 found at %s — using untrained fallback", stage3_path) | |
| # Load data | |
| df = load_parquet(args.data) | |
| if args.max_ticks > 0 and len(df) > args.max_ticks: | |
| logger.info("Sampling down to %d ticks (from %d) for speed", args.max_ticks, len(df)) | |
| indices = range(0, len(df), len(df) // args.max_ticks) | |
| df = df.iloc[indices[:args.max_ticks]].copy() | |
| # Run backtest | |
| results = run_backtest(cascade, df) | |
| results.print_summary() | |
| output_path = Path(args.output) | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| results.save(output_path) | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |
Xet Storage Details
- Size:
- 2.93 kB
- Xet hash:
- 303fa95674e79bbf29b64fe240b0ba12fe0fd64ff1b99ce211c2b58055839571
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.