Phase 4: orchestrator + cycles auto-execute
Componente runtime/ che cabla core+clients+state+safety in un engine autonomo notify-only: nessuna conferma manuale, ordini combo piazzati direttamente quando le regole passano. 311 test pass, copertura totale 94%, runtime/ 90%, mypy strict pulito, ruff clean. Moduli: - runtime/alert_manager.py: escalation tree LOW/MEDIUM/HIGH/CRITICAL → audit + Telegram + kill switch. - runtime/dependencies.py: build_runtime() costruisce RuntimeContext con tutti i client MCP, repository, audit log, kill switch, alert manager. - runtime/entry_cycle.py: flusso settimanale (snapshot parallelo spot/dvol/funding/macro/holdings/equity → validate_entry → compute_bias → options_chain → select_strikes → liquidity_gate → sizing_engine → combo_builder.build → place_combo_order → notify_position_opened). - runtime/monitor_cycle.py: loop 12h con dvol_history per il return_4h, exit_decision.evaluate, close auto-execute. - runtime/health_check.py: probe parallelo MCP + SQLite + environment match; 3 strikes consecutivi → kill switch HIGH. - runtime/recovery.py: riconciliazione SQLite vs broker all'avvio; mismatch → kill switch CRITICAL. - runtime/scheduler.py: AsyncIOScheduler builder con cron entry (lun 14:00), monitor (02/14), health (5min). - runtime/orchestrator.py: façade boot() + run_entry/monitor/health + install_scheduler + run_forever, con env check vs strategy. CLI: - start: avvia engine bloccante (asyncio.run + scheduler). - dry-run --cycle entry|monitor|health: esegue un singolo ciclo per debug/test in produzione. - stop: documenta lo shutdown via SIGTERM al container. Documentazione: - docs/06-operational-flow.md riscritto per il modello notify-only auto-execute (no conferma manuale, no memory, no brain-bridge). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,296 @@
|
||||
"""Integration tests for the monitor cycle (open positions → exit decisions)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from pytest_httpx import HTTPXMock
|
||||
|
||||
from cerbero_bite.config import golden_config
|
||||
from cerbero_bite.config.mcp_endpoints import load_endpoints
|
||||
from cerbero_bite.runtime import build_runtime
|
||||
from cerbero_bite.runtime.monitor_cycle import run_monitor_cycle
|
||||
from cerbero_bite.state import (
|
||||
DvolSnapshot,
|
||||
PositionRecord,
|
||||
transaction,
|
||||
)
|
||||
from cerbero_bite.state import connect as connect_state
|
||||
|
||||
pytestmark = pytest.mark.httpx_mock(assert_all_responses_were_requested=False)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def now() -> datetime:
|
||||
return datetime(2026, 4, 27, 14, 0, tzinfo=UTC)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def runtime_paths(tmp_path: Path) -> tuple[Path, Path]:
|
||||
return tmp_path / "state.sqlite", tmp_path / "audit.log"
|
||||
|
||||
|
||||
def _seed_position(
|
||||
ctx,
|
||||
*,
|
||||
proposal_id,
|
||||
short_mid_at_entry: Decimal,
|
||||
long_mid_at_entry: Decimal,
|
||||
n_contracts: int = 2,
|
||||
now: datetime,
|
||||
):
|
||||
short_strike = Decimal("2475")
|
||||
long_strike = Decimal("2350")
|
||||
width = short_strike - long_strike
|
||||
credit_eth_per = short_mid_at_entry - long_mid_at_entry
|
||||
spot = Decimal("3000")
|
||||
record = PositionRecord(
|
||||
proposal_id=proposal_id,
|
||||
spread_type="bull_put",
|
||||
expiry=now + timedelta(days=18),
|
||||
short_strike=short_strike,
|
||||
long_strike=long_strike,
|
||||
short_instrument="ETH-15MAY26-2475-P",
|
||||
long_instrument="ETH-15MAY26-2350-P",
|
||||
n_contracts=n_contracts,
|
||||
spread_width_usd=width,
|
||||
spread_width_pct=width / spot,
|
||||
credit_eth=credit_eth_per * Decimal(n_contracts),
|
||||
credit_usd=credit_eth_per * Decimal(n_contracts) * spot,
|
||||
max_loss_usd=(width - credit_eth_per * spot) * Decimal(n_contracts),
|
||||
spot_at_entry=spot,
|
||||
dvol_at_entry=Decimal("50"),
|
||||
delta_at_entry=Decimal("-0.12"),
|
||||
eth_price_at_entry=spot,
|
||||
proposed_at=now - timedelta(days=4),
|
||||
opened_at=now - timedelta(days=4),
|
||||
status="open",
|
||||
created_at=now - timedelta(days=4),
|
||||
updated_at=now - timedelta(days=4),
|
||||
)
|
||||
db_path = ctx.db_path
|
||||
conn = connect_state(db_path)
|
||||
try:
|
||||
with transaction(conn):
|
||||
ctx.repository.create_position(conn, record)
|
||||
finally:
|
||||
conn.close()
|
||||
return record
|
||||
|
||||
|
||||
def _seed_dvol_history(ctx, *, when: datetime, spot: Decimal, dvol: Decimal):
|
||||
conn = connect_state(ctx.db_path)
|
||||
try:
|
||||
with transaction(conn):
|
||||
ctx.repository.record_dvol_snapshot(
|
||||
conn, DvolSnapshot(timestamp=when, dvol=dvol, eth_spot=spot)
|
||||
)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def _wire_market_data(
|
||||
httpx_mock: HTTPXMock,
|
||||
*,
|
||||
spot: float = 3000.0,
|
||||
dvol: float = 50.0,
|
||||
) -> None:
|
||||
httpx_mock.add_response(
|
||||
url="http://mcp-deribit:9011/tools/get_ticker",
|
||||
json={"instrument_name": "ETH-PERPETUAL", "mark_price": spot},
|
||||
is_reusable=True,
|
||||
)
|
||||
httpx_mock.add_response(
|
||||
url="http://mcp-deribit:9011/tools/get_dvol",
|
||||
json={"currency": "ETH", "latest": dvol, "candles": []},
|
||||
is_reusable=True,
|
||||
)
|
||||
|
||||
|
||||
def _wire_position_quotes(
|
||||
httpx_mock: HTTPXMock,
|
||||
*,
|
||||
short_mid: float,
|
||||
long_mid: float,
|
||||
short_delta: float = -0.12,
|
||||
long_delta: float = -0.08,
|
||||
) -> None:
|
||||
httpx_mock.add_response(
|
||||
url="http://mcp-deribit:9011/tools/get_ticker_batch",
|
||||
json={
|
||||
"tickers": [
|
||||
{
|
||||
"instrument_name": "ETH-15MAY26-2475-P",
|
||||
"bid": short_mid * 0.995,
|
||||
"ask": short_mid * 1.005,
|
||||
"mark_price": short_mid,
|
||||
"greeks": {
|
||||
"delta": short_delta,
|
||||
"gamma": 0.001,
|
||||
"theta": -0.0005,
|
||||
"vega": 0.10,
|
||||
},
|
||||
},
|
||||
{
|
||||
"instrument_name": "ETH-15MAY26-2350-P",
|
||||
"bid": long_mid * 0.995,
|
||||
"ask": long_mid * 1.005,
|
||||
"mark_price": long_mid,
|
||||
"greeks": {
|
||||
"delta": long_delta,
|
||||
"gamma": 0.001,
|
||||
"theta": -0.0003,
|
||||
"vega": 0.07,
|
||||
},
|
||||
},
|
||||
],
|
||||
"errors": [],
|
||||
},
|
||||
is_reusable=True,
|
||||
)
|
||||
|
||||
|
||||
def _ctx(runtime_paths, now: datetime):
|
||||
db, audit = runtime_paths
|
||||
return build_runtime(
|
||||
cfg=golden_config(),
|
||||
endpoints=load_endpoints(env={}),
|
||||
token="t",
|
||||
db_path=db,
|
||||
audit_path=audit,
|
||||
retry_max=1,
|
||||
clock=lambda: now,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_monitor_emits_hold_when_no_trigger_fires(
|
||||
runtime_paths: tuple[Path, Path], now: datetime, httpx_mock: HTTPXMock
|
||||
) -> None:
|
||||
ctx = _ctx(runtime_paths, now)
|
||||
proposal_id = uuid4()
|
||||
_seed_position(
|
||||
ctx,
|
||||
proposal_id=proposal_id,
|
||||
short_mid_at_entry=Decimal("0.020"),
|
||||
long_mid_at_entry=Decimal("0.005"),
|
||||
now=now,
|
||||
)
|
||||
|
||||
_wire_market_data(httpx_mock)
|
||||
# Mark close to entry (mid still 60% of credit) → HOLD
|
||||
_wire_position_quotes(httpx_mock, short_mid=0.0143, long_mid=0.0050)
|
||||
|
||||
res = await run_monitor_cycle(ctx, now=now)
|
||||
assert len(res.outcomes) == 1
|
||||
assert res.outcomes[0].action == "HOLD"
|
||||
assert res.outcomes[0].closed is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_monitor_closes_position_on_profit_take(
|
||||
runtime_paths: tuple[Path, Path], now: datetime, httpx_mock: HTTPXMock
|
||||
) -> None:
|
||||
ctx = _ctx(runtime_paths, now)
|
||||
proposal_id = uuid4()
|
||||
_seed_position(
|
||||
ctx,
|
||||
proposal_id=proposal_id,
|
||||
short_mid_at_entry=Decimal("0.020"),
|
||||
long_mid_at_entry=Decimal("0.005"),
|
||||
now=now,
|
||||
)
|
||||
|
||||
_wire_market_data(httpx_mock)
|
||||
# mark = 30% of credit → profit take fires
|
||||
# credit per contract 0.015, so mark per contract 0.0045 → short-long
|
||||
_wire_position_quotes(httpx_mock, short_mid=0.0080, long_mid=0.0035)
|
||||
httpx_mock.add_response(
|
||||
url="http://mcp-deribit:9011/tools/place_combo_order",
|
||||
json={
|
||||
"combo_instrument": "ETH-15MAY26-2475P_2350P",
|
||||
"order_id": "close-1",
|
||||
"state": "filled",
|
||||
"average_price": 0.0045,
|
||||
"filled_amount": 2,
|
||||
},
|
||||
is_reusable=True,
|
||||
)
|
||||
httpx_mock.add_response(
|
||||
url="http://mcp-telegram:9017/tools/notify_position_closed",
|
||||
json={"ok": True},
|
||||
is_reusable=True,
|
||||
)
|
||||
|
||||
res = await run_monitor_cycle(ctx, now=now)
|
||||
assert len(res.outcomes) == 1
|
||||
outcome = res.outcomes[0]
|
||||
assert outcome.action == "CLOSE_PROFIT"
|
||||
assert outcome.closed is True
|
||||
|
||||
conn = connect_state(ctx.db_path)
|
||||
try:
|
||||
positions = ctx.repository.list_positions(conn)
|
||||
finally:
|
||||
conn.close()
|
||||
assert positions[0].status == "closed"
|
||||
assert positions[0].close_reason == "CLOSE_PROFIT"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_monitor_skips_when_kill_switch_armed(
|
||||
runtime_paths: tuple[Path, Path], now: datetime, httpx_mock: HTTPXMock
|
||||
) -> None:
|
||||
ctx = _ctx(runtime_paths, now)
|
||||
ctx.kill_switch.arm(reason="test", source="manual")
|
||||
res = await run_monitor_cycle(ctx, now=now)
|
||||
assert res.outcomes == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_monitor_uses_dvol_history_for_return_4h(
|
||||
runtime_paths: tuple[Path, Path], now: datetime, httpx_mock: HTTPXMock
|
||||
) -> None:
|
||||
ctx = _ctx(runtime_paths, now)
|
||||
proposal_id = uuid4()
|
||||
_seed_position(
|
||||
ctx,
|
||||
proposal_id=proposal_id,
|
||||
short_mid_at_entry=Decimal("0.020"),
|
||||
long_mid_at_entry=Decimal("0.005"),
|
||||
now=now,
|
||||
)
|
||||
# Snapshot 4h ago: spot was 3300 → return = 3000/3300 - 1 ≈ -9% (adverse)
|
||||
_seed_dvol_history(
|
||||
ctx,
|
||||
when=now - timedelta(hours=4),
|
||||
spot=Decimal("3300"),
|
||||
dvol=Decimal("50"),
|
||||
)
|
||||
|
||||
_wire_market_data(httpx_mock, spot=3000.0)
|
||||
_wire_position_quotes(httpx_mock, short_mid=0.0140, long_mid=0.0050)
|
||||
httpx_mock.add_response(
|
||||
url="http://mcp-deribit:9011/tools/place_combo_order",
|
||||
json={
|
||||
"combo_instrument": "ETH-15MAY26-2475P_2350P",
|
||||
"order_id": "close-1",
|
||||
"state": "filled",
|
||||
"average_price": 0.009,
|
||||
"filled_amount": 2,
|
||||
},
|
||||
is_reusable=True,
|
||||
)
|
||||
httpx_mock.add_response(
|
||||
url="http://mcp-telegram:9017/tools/notify_position_closed",
|
||||
json={"ok": True},
|
||||
is_reusable=True,
|
||||
)
|
||||
|
||||
res = await run_monitor_cycle(ctx, now=now)
|
||||
assert res.outcomes[0].action == "CLOSE_AVERSE"
|
||||
assert res.outcomes[0].closed is True
|
||||
Reference in New Issue
Block a user