"""Tests for DefaultStrategy metrics integration.""" import sys import sqlite3 import tempfile from pathlib import Path sys.path.append(str(Path(__file__).resolve().parents[1])) from strategies import DefaultStrategy from models import Book, BookSnapshot, OrderbookLevel, Metric from repositories.sqlite_repository import SQLiteOrderflowRepository def test_strategy_uses_metric_calculator(): """Test that strategy uses MetricCalculator for OBI calculation.""" strategy = DefaultStrategy("BTC-USDT") # Create test book with snapshots book = Book() snapshot = BookSnapshot( id=1, timestamp=1000, bids={50000.0: OrderbookLevel(price=50000.0, size=10.0, liquidation_count=0, order_count=1)}, asks={50001.0: OrderbookLevel(price=50001.0, size=5.0, liquidation_count=0, order_count=1)}, ) book.add_snapshot(snapshot) # Test OBI calculation obi_values = strategy.compute_OBI(book) assert len(obi_values) == 1 # OBI = (10 - 5) / (10 + 5) = 0.333... assert abs(obi_values[0] - 0.333333) < 0.001 def test_strategy_loads_stored_metrics(): """Test that strategy can load stored metrics from database.""" with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as tmp_file: db_path = Path(tmp_file.name) try: # Create test database with metrics repo = SQLiteOrderflowRepository(db_path) with repo.connect() as conn: repo.create_metrics_table(conn) # Insert test metrics test_metrics = [ Metric(snapshot_id=1, timestamp=1000, obi=0.1, cvd=10.0, best_bid=50000.0, best_ask=50001.0), Metric(snapshot_id=2, timestamp=1001, obi=0.2, cvd=15.0, best_bid=50002.0, best_ask=50003.0), Metric(snapshot_id=3, timestamp=1002, obi=0.3, cvd=20.0, best_bid=50004.0, best_ask=50005.0), ] repo.insert_metrics_batch(conn, test_metrics) conn.commit() # Test strategy loading strategy = DefaultStrategy("BTC-USDT") strategy.set_db_path(db_path) loaded_metrics = strategy.load_stored_metrics(1000, 1002) assert len(loaded_metrics) == 3 assert loaded_metrics[0].obi == 0.1 assert loaded_metrics[0].cvd == 10.0 assert loaded_metrics[-1].obi == 0.3 assert loaded_metrics[-1].cvd == 20.0 finally: db_path.unlink(missing_ok=True) def test_strategy_metrics_summary(): """Test that strategy generates correct metrics summary.""" strategy = DefaultStrategy("BTC-USDT") # Create test metrics metrics = [ Metric(snapshot_id=1, timestamp=1000, obi=0.1, cvd=10.0), Metric(snapshot_id=2, timestamp=1001, obi=-0.2, cvd=5.0), Metric(snapshot_id=3, timestamp=1002, obi=0.3, cvd=15.0), ] summary = strategy.get_metrics_summary(metrics) assert summary["obi_min"] == -0.2 assert summary["obi_max"] == 0.3 assert abs(summary["obi_avg"] - 0.0667) < 0.001 # (0.1 + (-0.2) + 0.3) / 3 assert summary["cvd_start"] == 10.0 assert summary["cvd_end"] == 15.0 assert summary["cvd_change"] == 5.0 # 15.0 - 10.0 assert summary["total_snapshots"] == 3 def test_strategy_empty_metrics(): """Test strategy behavior with empty metrics.""" strategy = DefaultStrategy("BTC-USDT") # Test with empty book book = Book() obi_values = strategy.compute_OBI(book) assert obi_values == [] # Test with empty metrics summary = strategy.get_metrics_summary([]) assert summary == {} # Test loading from non-existent database strategy.set_db_path(Path("nonexistent.db")) metrics = strategy.load_stored_metrics(1000, 2000) assert metrics == []