test_search_recent_regime_mean_reversion.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import importlib.util
  2. import sys
  3. from pathlib import Path
  4. from types import SimpleNamespace
  5. import pytest
  6. def load_module():
  7. path = Path(__file__).resolve().parents[1] / "scripts" / "search_recent_regime_mean_reversion.py"
  8. spec = importlib.util.spec_from_file_location("search_recent_regime_mean_reversion", path)
  9. assert spec is not None
  10. module = importlib.util.module_from_spec(spec)
  11. assert spec.loader is not None
  12. sys.modules[spec.name] = module
  13. spec.loader.exec_module(module)
  14. return module
  15. def test_trade_return_applies_roundtrip_fee_to_long_and_short():
  16. module = load_module()
  17. assert module.trade_return("long", 100.0, 101.0) == pytest.approx(0.0092)
  18. assert module.trade_return("short", 100.0, 99.0) == pytest.approx((100.0 / 99.0 - 1.0) - 0.0008)
  19. def test_exit_price_uses_open_when_short_stop_gaps():
  20. module = load_module()
  21. position = {"side": "short", "stop": 101.0, "take": 99.0, "midpoint": 99.5}
  22. row = SimpleNamespace(open=102.0, high=103.0, low=100.0)
  23. assert module.exit_price(position, row) == pytest.approx(102.0)
  24. def test_horizon_rows_include_required_recent_windows():
  25. module = load_module()
  26. pandas = pytest.importorskip("pandas")
  27. index = pandas.date_range("2023-01-01", periods=1200, freq="D", tz="UTC")
  28. series = pandas.Series(10_000.0, index=index)
  29. spec = module.Spec("ETH-USDT-SWAP", "3m", "bidir", 24, 144, 0.2, 0.0016, 0.006, 0.006, 20)
  30. rows = module.horizon_rows(spec, series, [])
  31. assert [row["horizon"] for row in rows] == ["7d", "14d", "30d", "90d", "6m", "1y", "3y"]
  32. assert {"total_return", "annualized", "max_drawdown", "calmar", "trades", "win_rate", "profit_factor", "payoff_ratio"} <= set(rows[0])