60 lines
2.2 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
from visualizer import Visualizer
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)
visualizer = Visualizer(window_seconds=60, max_bars=500)
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}")
# Set database path for strategy and visualizer to access stored metrics
strategy.set_db_path(db_path)
visualizer.set_db_path(db_path)
# Build snapshots and calculate metrics
storage.build_booktick_from_db(db_path, db_date)
logging.info(f"Processed {len(storage.book.snapshots)} snapshots with metrics")
# Strategy analyzes metrics from the database
strategy.on_booktick(storage.book)
# Update visualization after processing each database
logging.info(f"Updating visualization for {db_path.name}")
visualizer.update_from_book(storage.book)
# Show final visualization
logging.info("Processing complete. Displaying final visualization...")
if db_paths: # Ensure we have processed at least one database
visualizer.show()
if __name__ == "__main__":
typer.run(main)