47 lines
1.6 KiB
Python
Raw Normal View History

import logging
import typer
from pathlib import Path
from typing import List
from datetime import datetime, timezone
from storage import Storage
from strategies import DefaultStrategy
databases_path = Path("../data/OKX")
storage = None
def main(instrument: str = typer.Argument(..., help="Instrument to backtest, e.g. BTC-USDT"),
start_date: str = typer.Argument(..., help="Start date, e.g. 2025-07-01"),
end_date: str = typer.Argument(..., help="End date, e.g. 2025-08-01")):
start_date = datetime.strptime(start_date, "%Y-%m-%d").replace(tzinfo=timezone.utc)
end_date = datetime.strptime(end_date, "%Y-%m-%d").replace(tzinfo=timezone.utc)
db_paths = list(databases_path.glob(f"{instrument}*.db"))
db_paths.sort()
storage = Storage(instrument)
strategy = DefaultStrategy(instrument)
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
for db_path in db_paths:
db_name = db_path.name.split(".")[0].split("-")[2:5]
db_date = datetime.strptime("".join(db_name), "%y%m%d").replace(tzinfo=timezone.utc)
if db_date < start_date or db_date >= end_date:
continue
logging.info(f"Processing database: {db_path.name}")
strategy.set_db_path(db_path)
storage.build_booktick_from_db(db_path)
logging.info(f"Processed {len(storage.book.snapshots)} snapshots with metrics")
strategy.on_booktick(storage.book)
logging.info("Processing complete.")
if __name__ == "__main__":
typer.run(main)