PolyHedra / tests /test_quant_v6 /test_options_flow.py
yf176
feat: wire options context into v6 watcher
3a1a713
Raw History Blame Contribute Delete
3.62 kB
from __future__ import annotations
from pathlib import Path
import pytest
from polyhedra.agents.base import Mention
from polyhedra.agents.quant.market_watchers import OptionsFlowWatcher
from polyhedra.services.file_writer import FileWriter
from polyhedra.services.options_data import OptionContract, OptionsDataSnapshot
class _FakeOptionsProvider:
def __init__(self, snapshot: OptionsDataSnapshot) -> None:
self.snapshot = snapshot
def fetch(self, symbol: str) -> OptionsDataSnapshot:
assert symbol == self.snapshot.symbol
return self.snapshot
def _snapshot(status: str = "ok") -> OptionsDataSnapshot:
contracts = []
if status == "ok":
contracts = [
OptionContract(
provider="unit-test",
retrieved_at="2026-08-24T00:00:00+00:00",
symbol="SPY",
expiration="2026-08-28",
strike=650.0,
option_type="call",
bid=1.2,
ask=1.3,
last=1.25,
volume=9000,
open_interest=2000,
implied_volatility=0.31,
source_delay_or_entitlement_note="test provider",
),
OptionContract(
provider="unit-test",
retrieved_at="2026-08-24T00:00:00+00:00",
symbol="SPY",
expiration="2026-08-28",
strike=640.0,
option_type="put",
bid=1.0,
ask=1.1,
last=1.05,
volume=200,
open_interest=4000,
implied_volatility=0.28,
source_delay_or_entitlement_note="test provider",
),
]
return OptionsDataSnapshot(
symbol="SPY",
provider="unit-test",
retrieved_at="2026-08-24T00:00:00+00:00",
status=status,
note="available" if status == "ok" else "provider unavailable",
expirations_checked=["2026-08-28"] if status == "ok" else [],
contracts=contracts,
)
@pytest.mark.asyncio
async def test_options_flow_watcher_writes_chain_context(
tmp_path: Path,
) -> None:
watcher = OptionsFlowWatcher(options_provider=_FakeOptionsProvider(_snapshot()))
watcher.files = FileWriter(tmp_path)
response = await watcher.handle(
Mention("optionsflowwatcher", "marketeventwatcher", "continue SPY"),
[],
{"quant_state": {"watchlist": ["SPY"]}},
)
artifact = (tmp_path / response.artifact_path).read_text(encoding="utf-8")
assert response.mentions == ["macrocontextwatcher"]
assert "OPTIONS_MARKET_CONTEXT" in artifact
assert "FLOW_CANDIDATE" in artifact
assert "UNUSUAL_OPTION_FLOW" in artifact
assert "not directional by itself" in artifact
assert "CALL" in artifact
assert "bullish" not in artifact.lower()
assert "bearish" not in artifact.lower()
@pytest.mark.asyncio
async def test_options_flow_watcher_records_data_gap_without_na_signal(tmp_path: Path) -> None:
watcher = OptionsFlowWatcher(
options_provider=_FakeOptionsProvider(_snapshot(status="unavailable"))
)
watcher.files = FileWriter(tmp_path)
response = await watcher.handle(
Mention("optionsflowwatcher", "marketeventwatcher", "continue SPY"),
[],
{"quant_state": {"watchlist": ["SPY"]}},
)
artifact = (tmp_path / response.artifact_path).read_text(encoding="utf-8")
assert "DATA_GAP" in artifact
assert "excluded from signal weighting" in artifact
assert "N/A" not in artifact