test_build_eth_nextgen_micro_signal_intent.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import importlib.util
  2. import sys
  3. from pathlib import Path
  4. import pandas as pd
  5. import pytest
  6. def load_module():
  7. path = Path(__file__).resolve().parents[1] / "scripts" / "build_eth_nextgen_micro_signal_intent.py"
  8. spec = importlib.util.spec_from_file_location("build_eth_nextgen_micro_signal_intent", 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_latest_active_engine_uses_shifted_prior_day_regime(monkeypatch):
  16. module = load_module()
  17. index = pd.date_range("2026-01-01", periods=32, freq="D", tz="UTC")
  18. existing = pd.DataFrame({"cost_model": ["maker_taker"], "name": ["equal-2-c0003"], "date": [index[0].strftime("%Y-%m-%d")]})
  19. nextgen = pd.Series([100.0] * 31 + [80.0], index=index)
  20. micro = pd.Series([100.0] * 31 + [120.0], index=index)
  21. monkeypatch.setattr(module.pd, "read_csv", lambda _: existing)
  22. monkeypatch.setattr(module.portfolio, "load_nextgen", lambda _, __: (nextgen, []))
  23. monkeypatch.setattr(module.portfolio, "load_micro_candidates", lambda _, __: {module.MICRO_NAME: (micro, [])})
  24. state = module.latest_active_engine()
  25. assert state["active_engine"] == "nextgen"
  26. def test_latest_active_engine_switches_on_prior_completed_day(monkeypatch):
  27. module = load_module()
  28. index = pd.date_range("2026-01-01", periods=33, freq="D", tz="UTC")
  29. existing = pd.DataFrame({"cost_model": ["maker_taker"], "name": ["equal-2-c0003"], "date": [index[0].strftime("%Y-%m-%d")]})
  30. nextgen = pd.Series([100.0] * 31 + [80.0, 80.0], index=index)
  31. micro = pd.Series([100.0] * 31 + [120.0, 120.0], index=index)
  32. monkeypatch.setattr(module.pd, "read_csv", lambda _: existing)
  33. monkeypatch.setattr(module.portfolio, "load_nextgen", lambda _, __: (nextgen, []))
  34. monkeypatch.setattr(module.portfolio, "load_micro_candidates", lambda _, __: {module.MICRO_NAME: (micro, [])})
  35. state = module.latest_active_engine()
  36. assert state["active_engine"] == "micro"
  37. def test_payload_is_readonly(monkeypatch):
  38. module = load_module()
  39. monkeypatch.setattr(module, "latest_active_engine", lambda: {"active_engine": "nextgen", "decision_date": "2026-01-01"})
  40. monkeypatch.setattr(
  41. module.nextgen_intent,
  42. "build_payload",
  43. lambda: {"decision": {"signal": "no_signal"}, "legs": []},
  44. )
  45. monkeypatch.setattr(module, "micro_signal", lambda: {"signal": "short"})
  46. payload = module.build_payload()
  47. assert payload["submitted_orders"] == 0
  48. assert payload["private_key_required"] is False
  49. assert payload["risk_limits"]["no_order_submission"] is True
  50. assert payload["risk_limits"]["blocked_for_live_trading"] is True
  51. assert payload["decision"]["selected_signal"] == "no_signal"