feat(state+runtime): option_chain_snapshots — catena opzioni storica per backtest reale
Aggiunge la persistence della option chain Deribit con cron settimanale ``55 13 * * MON`` (5 minuti prima del trigger entry alle 14:00 UTC), sbloccando il backtest non-stilizzato e la calibrazione empirica dello skew premium. **Schema (migrazione 0004)** Nuova tabella ``option_chain_snapshots`` con primary key composta ``(timestamp, instrument_name)`` — tutti i quote prelevati nello stesso tick condividono il timestamp, così le query "lo snapshot del 2026-05-04 alle 13:55" diventano una singola WHERE timestamp = X. Indici su (asset, timestamp DESC) e (asset, expiry) per supportare sia listing recenti sia query per scadenza specifica. Campi: instrument_name, strike, expiry, option_type (C/P), bid, ask, mid, iv, delta, gamma, theta, vega, open_interest, volume_24h, book_depth_top3. Tutti i numerici sono nullable: il collector è best-effort, un ticker mancante produce comunque una riga (utile per sapere che lo strumento esisteva ma non era quotato). **Modello + repository** - ``OptionChainQuoteRecord`` (Pydantic, in ``state/models.py``). - ``Repository.record_option_chain_snapshot`` (bulk insert idempotente). - ``Repository.list_option_chain_snapshots`` (filtri su asset, timestamp window, expiry window, limit default 50000). - ``Repository.latest_option_chain_timestamp`` (freshness check per dashboard GUI). **Collector** Nuovo ``runtime/option_chain_snapshot_cycle.py`` che: 1. Calcola la finestra scadenze ``[now+dte_min, now+dte_max]`` da ``cfg.structure``: niente richieste su scadenze che il rule engine non userebbe mai. 2. Chiama ``deribit.options_chain()`` con ``min_open_interest=cfg.liquidity.open_interest_min``. 3. Batch ``deribit.get_tickers()`` (max 20 per call, limite Deribit) con error-isolation per batch — un batch fallito non blocca gli altri. 4. NON chiama l'order book per ogni strike (rate-limit guard); ``book_depth_top3`` resta NULL e il liquidity gate live lo chiede on-the-fly per gli strike candidati al picker. Best-effort end-to-end: chain assente, get_tickers giù, persist fallito → ritorna 0 senza alzare eccezioni, logga sempre. **Schedulazione** Wired in ``Orchestrator.install_scheduler`` come job parallelo a ``market_snapshot``, attivo solo quando ``ENABLE_DATA_ANALYSIS=true``. Cron parametrizzabile via il nuovo kwarg ``option_chain_cron`` (default ``55 13 * * MON``). **Test** - 4 unit test del collector (happy path, ticker mancante, chain vuota, fetch fail best-effort) con mock di RuntimeContext. - Aggiornato ``test_install_scheduler_registers_canonical_jobs`` per includere il nuovo job nel set canonico. **Cosa sblocca** - Backtest non-stilizzato: il PR ``feat/backtest-engine`` può dropparsi il modello BS+skew_premium e leggere prezzi reali ``mid`` dalla chain registrata. - Calibrazione empirica dello skew premium (hardcoded a 1.5 nel backtest stilizzato): plot del rapporto fra quote reali Deribit e BS per delta/expiry, regressione → valore data-driven. - Validazione ex-post: "il delta-0.12 era davvero a 25% OTM in quella settimana?" diventa una query SELECT. - Dimensione attesa: ~50 strike × 3 scadenze × 1 snapshot/settimana × 17 colonne ≈ 12 KB/settimana, ~600 KB/anno. Trascurabile. Suite: 409 passed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -129,6 +129,7 @@ def test_install_scheduler_registers_canonical_jobs(tmp_path: Path) -> None:
|
||||
"backup",
|
||||
"manual_actions",
|
||||
"market_snapshot",
|
||||
"option_chain_snapshot",
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
"""TDD per :mod:`cerbero_bite.runtime.option_chain_snapshot_cycle`."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from decimal import Decimal
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from cerbero_bite.clients.deribit import InstrumentMeta
|
||||
from cerbero_bite.runtime.option_chain_snapshot_cycle import (
|
||||
collect_option_chain_snapshot,
|
||||
)
|
||||
from cerbero_bite.state.models import OptionChainQuoteRecord
|
||||
|
||||
|
||||
_NOW = datetime(2026, 5, 4, 13, 55, tzinfo=UTC)
|
||||
|
||||
|
||||
def _meta(name: str, strike: int, expiry_dte: int = 18) -> InstrumentMeta:
|
||||
expiry = _NOW.replace(hour=8, minute=0, second=0)
|
||||
expiry = expiry.replace(day=expiry.day) + (
|
||||
# add days
|
||||
__import__("datetime").timedelta(days=expiry_dte)
|
||||
)
|
||||
return InstrumentMeta(
|
||||
name=name,
|
||||
strike=Decimal(str(strike)),
|
||||
expiry=expiry,
|
||||
option_type="P",
|
||||
open_interest=Decimal("100"),
|
||||
tick_size=Decimal("0.0005"),
|
||||
min_trade_amount=Decimal("1"),
|
||||
)
|
||||
|
||||
|
||||
def _ticker(name: str, *, mark: float = 0.020, bid: float = 0.018,
|
||||
ask: float = 0.022, delta: float = -0.12) -> dict:
|
||||
return {
|
||||
"instrument_name": name,
|
||||
"bid": bid,
|
||||
"ask": ask,
|
||||
"mark_price": mark,
|
||||
"mark_iv": 60.0,
|
||||
"volume_24h": 50,
|
||||
"greeks": {
|
||||
"delta": delta,
|
||||
"gamma": 0.001,
|
||||
"theta": -0.0005,
|
||||
"vega": 0.10,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cfg() -> object:
|
||||
from cerbero_bite.config import golden_config
|
||||
return golden_config()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_ctx(cfg: object) -> MagicMock:
|
||||
"""Mock minimal RuntimeContext."""
|
||||
ctx = MagicMock()
|
||||
ctx.cfg = cfg
|
||||
ctx.db_path = ":memory:"
|
||||
return ctx
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_collector_persists_one_quote_per_instrument(
|
||||
fake_ctx: MagicMock,
|
||||
) -> None:
|
||||
metas = [_meta("ETH-21MAY26-2475-P", 2475), _meta("ETH-21MAY26-2400-P", 2400)]
|
||||
fake_ctx.deribit.options_chain = AsyncMock(return_value=metas)
|
||||
fake_ctx.deribit.get_tickers = AsyncMock(
|
||||
return_value=[_ticker(m.name) for m in metas]
|
||||
)
|
||||
persisted: list[list[OptionChainQuoteRecord]] = []
|
||||
|
||||
def _record(_conn: object, qs: list[OptionChainQuoteRecord]) -> int:
|
||||
persisted.append(qs)
|
||||
return len(qs)
|
||||
|
||||
fake_ctx.repository.record_option_chain_snapshot = _record
|
||||
|
||||
n = await collect_option_chain_snapshot(fake_ctx, asset="ETH", now=_NOW)
|
||||
assert n == 2
|
||||
assert len(persisted) == 1
|
||||
assert {q.instrument_name for q in persisted[0]} == {
|
||||
"ETH-21MAY26-2475-P", "ETH-21MAY26-2400-P",
|
||||
}
|
||||
# Tutti i quote condividono il timestamp del cron tick.
|
||||
assert all(q.timestamp == _NOW for q in persisted[0])
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_collector_handles_missing_tickers_with_null_fields(
|
||||
fake_ctx: MagicMock,
|
||||
) -> None:
|
||||
metas = [_meta("ETH-21MAY26-2475-P", 2475)]
|
||||
fake_ctx.deribit.options_chain = AsyncMock(return_value=metas)
|
||||
fake_ctx.deribit.get_tickers = AsyncMock(return_value=[]) # vuoto
|
||||
persisted: list[list[OptionChainQuoteRecord]] = []
|
||||
|
||||
def _record(_conn: object, qs: list[OptionChainQuoteRecord]) -> int:
|
||||
persisted.append(qs)
|
||||
return len(qs)
|
||||
|
||||
fake_ctx.repository.record_option_chain_snapshot = _record
|
||||
|
||||
n = await collect_option_chain_snapshot(fake_ctx, now=_NOW)
|
||||
assert n == 1
|
||||
assert persisted[0][0].mid is None # ticker mancante ⇒ campi NULL
|
||||
assert persisted[0][0].instrument_name == "ETH-21MAY26-2475-P"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_collector_returns_zero_when_chain_empty(
|
||||
fake_ctx: MagicMock,
|
||||
) -> None:
|
||||
fake_ctx.deribit.options_chain = AsyncMock(return_value=[])
|
||||
n = await collect_option_chain_snapshot(fake_ctx, now=_NOW)
|
||||
assert n == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_collector_swallows_chain_fetch_failure(
|
||||
fake_ctx: MagicMock,
|
||||
) -> None:
|
||||
fake_ctx.deribit.options_chain = AsyncMock(side_effect=RuntimeError("boom"))
|
||||
n = await collect_option_chain_snapshot(fake_ctx, now=_NOW)
|
||||
assert n == 0 # best-effort: non rilancia
|
||||
Reference in New Issue
Block a user