6ff021fbf4
Crypto opera 24/7: la cadenza settimanale lunedì-only era un retaggio TradFi senza giustificazione. La nuova cadenza è giornaliera (cron 0 14 * * *), con i gate quantitativi a decidere se entrare o saltare. Cambiamenti principali: * runtime/orchestrator.py — _CRON_ENTRY 0 14 * * * (era MON) * runtime/auto_pause.py — pause_until(days=) (era weeks=); minimo clamp 1 giorno (era 1 settimana) * core/backtest.py — MondayPick→DailyPick, monday_picks→daily_picks (1 pick per calendar-day all'ora target); Sharpe annualization su ~120 trade/anno (era 52) * config/schema.py — default cron daily; max_concurrent_positions 1→5; AutoPauseConfig.pause_weeks→pause_days, default 14 * runtime/option_chain_snapshot_cycle.py + orchestrator — cron */15 per accumulo continuo dataset di backtest empirico Strategy yamls (config_version 1.3.0 → 1.4.0, hash rigenerati): * strategy.yaml — max_concurrent 1→5, cap_aggregate coerente * strategy.aggressiva.yaml — max_concurrent 2→8, cap_aggregate 3200→6400, max_contracts_per_trade invariato a 16 * strategy.conservativa.yaml — max_concurrent 1→3 * tutti — pause_weeks→pause_days: 14 GUI (pages/7_📚_Strategia.py): * slider Trade/anno: range 20-200 (era 8-30), default 110, help riallineato sulla math 365 candidature × pass-rate 30-40% * card profili: versione letta dinamicamente da config_version invece che hard-coded "v1.2.0" * warning "entrambi perdono soldi" ora valuta i P/L effettivi (cons['annual_pl'], aggr['annual_pl']) invece del win_rate grezzo; aggiunto stato intermedio quando solo conservativo è in perdita Tests (450/450 passati): * test_auto_pause: pause_days, clamp ≥1 giorno * test_backtest: rinomina + ridisegno daily picks (assert su calendar-day dedupe e hour filter) * test_sizing_engine: other_open_positions=5 per cap default * test_config_loader: version 1.4.0 Docs (README + 9 file in docs/) — tutti i riferimenti weekly/lunedì allineati a daily/24-7, volume option_chain ricalcolato per cron */15 (~1.1 MB/giorno, ~400 MB/anno). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
150 lines
5.0 KiB
Python
150 lines
5.0 KiB
Python
"""Tests for the YAML loader and hash verification."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from decimal import Decimal
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import yaml
|
|
|
|
from cerbero_bite.config.loader import (
|
|
ConfigHashError,
|
|
compute_config_hash,
|
|
load_strategy,
|
|
)
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _golden_yaml_skeleton(**overrides: object) -> dict[str, object]:
|
|
base = {
|
|
"config_version": "1.0.0-test",
|
|
"config_hash": "0" * 64,
|
|
"last_review": "2026-04-26",
|
|
"last_reviewer": "test",
|
|
}
|
|
base.update(overrides)
|
|
return base
|
|
|
|
|
|
def _write_with_correct_hash(path: Path, doc: dict[str, object]) -> None:
|
|
"""Write a YAML doc and patch ``config_hash`` to match the file body."""
|
|
text = yaml.safe_dump(doc, sort_keys=False)
|
|
path.write_text(text, encoding="utf-8")
|
|
new_hash = compute_config_hash(path.read_text(encoding="utf-8"))
|
|
doc = {**doc, "config_hash": new_hash}
|
|
text = yaml.safe_dump(doc, sort_keys=False)
|
|
path.write_text(text, encoding="utf-8")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# compute_config_hash
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_compute_hash_is_independent_of_recorded_hash_value(tmp_path: Path) -> None:
|
|
a = tmp_path / "a.yaml"
|
|
b = tmp_path / "b.yaml"
|
|
a.write_text(
|
|
'config_version: "1.0.0"\nconfig_hash: "aaa"\nfoo: 1\n', encoding="utf-8"
|
|
)
|
|
b.write_text(
|
|
'config_version: "1.0.0"\nconfig_hash: "bbb"\nfoo: 1\n', encoding="utf-8"
|
|
)
|
|
assert compute_config_hash(a.read_text()) == compute_config_hash(b.read_text())
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# load_strategy — happy path
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_load_repo_strategy_yaml(tmp_path: Path) -> None:
|
|
"""The committed strategy.yaml validates with the recorded hash."""
|
|
result = load_strategy(REPO_ROOT / "strategy.yaml")
|
|
assert result.config.config_version == "1.4.0"
|
|
assert result.config.sizing.kelly_fraction == Decimal("0.13")
|
|
assert result.computed_hash == result.config.config_hash
|
|
|
|
|
|
def test_load_with_local_override_merges(tmp_path: Path) -> None:
|
|
main = tmp_path / "strategy.yaml"
|
|
_write_with_correct_hash(main, _golden_yaml_skeleton())
|
|
override = tmp_path / "strategy.local.yaml"
|
|
override.write_text(
|
|
yaml.safe_dump(
|
|
{"sizing": {"max_concurrent_positions": 0}},
|
|
sort_keys=False,
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
loaded = load_strategy(main)
|
|
assert loaded.config.sizing.max_concurrent_positions == 0
|
|
assert override in loaded.sources
|
|
|
|
|
|
def test_local_override_does_not_invalidate_main_hash(tmp_path: Path) -> None:
|
|
main = tmp_path / "strategy.yaml"
|
|
_write_with_correct_hash(main, _golden_yaml_skeleton())
|
|
(tmp_path / "strategy.local.yaml").write_text(
|
|
"sizing:\n kelly_fraction: '0.10'\n", encoding="utf-8"
|
|
)
|
|
loaded = load_strategy(main)
|
|
assert loaded.config.sizing.kelly_fraction == Decimal("0.10")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# load_strategy — error paths
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_load_with_mismatched_hash_raises(tmp_path: Path) -> None:
|
|
main = tmp_path / "strategy.yaml"
|
|
main.write_text(
|
|
yaml.safe_dump(_golden_yaml_skeleton(), sort_keys=False), encoding="utf-8"
|
|
)
|
|
with pytest.raises(ConfigHashError, match="config_hash mismatch"):
|
|
load_strategy(main)
|
|
|
|
|
|
def test_load_with_enforce_hash_false_skips_check(tmp_path: Path) -> None:
|
|
main = tmp_path / "strategy.yaml"
|
|
main.write_text(
|
|
yaml.safe_dump(_golden_yaml_skeleton(), sort_keys=False), encoding="utf-8"
|
|
)
|
|
loaded = load_strategy(main, enforce_hash=False)
|
|
assert loaded.config.config_hash == "0" * 64
|
|
|
|
|
|
def test_load_rejects_top_level_non_mapping(tmp_path: Path) -> None:
|
|
main = tmp_path / "strategy.yaml"
|
|
main.write_text("- a\n- b\n", encoding="utf-8")
|
|
with pytest.raises(ValueError, match="top-level mapping"):
|
|
load_strategy(main, enforce_hash=False)
|
|
|
|
|
|
def test_local_override_path_pointing_to_missing_file_is_ignored(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
main = tmp_path / "strategy.yaml"
|
|
_write_with_correct_hash(main, _golden_yaml_skeleton())
|
|
loaded = load_strategy(
|
|
main, local_override_path=tmp_path / "nonexistent.yaml"
|
|
)
|
|
assert main in loaded.sources
|
|
assert len(loaded.sources) == 1
|
|
|
|
|
|
def test_empty_local_override_file_is_no_op(tmp_path: Path) -> None:
|
|
main = tmp_path / "strategy.yaml"
|
|
_write_with_correct_hash(main, _golden_yaml_skeleton())
|
|
(tmp_path / "strategy.local.yaml").write_text("", encoding="utf-8")
|
|
loaded = load_strategy(main)
|
|
assert loaded.config.sizing.kelly_fraction == Decimal("0.13")
|