15cb6b37aa
Pipeline AST -> compile_strategy -> BacktestEngine -> Sharpe/DSR/DD. Caso zero-trade ritorna report tutto-zero. n_trials_dsr correzione multiple-testing parametrizzata via init. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
import numpy as np
|
|
import pandas as pd
|
|
import pytest
|
|
|
|
from multi_swarm.agents.falsification import FalsificationAgent, FalsificationReport
|
|
from multi_swarm.protocol.parser import parse_strategy
|
|
|
|
|
|
@pytest.fixture
|
|
def trending_ohlcv() -> pd.DataFrame:
|
|
idx = pd.date_range("2024-01-01", periods=500, freq="1h", tz="UTC")
|
|
close = 100 + np.cumsum(np.random.RandomState(0).normal(0.01, 1.0, 500))
|
|
return pd.DataFrame(
|
|
{
|
|
"open": close,
|
|
"high": close + 0.5,
|
|
"low": close - 0.5,
|
|
"close": close,
|
|
"volume": 1.0,
|
|
},
|
|
index=idx,
|
|
)
|
|
|
|
|
|
def test_falsification_returns_report(trending_ohlcv: pd.DataFrame) -> None:
|
|
src = (
|
|
"(strategy "
|
|
"(when (gt (indicator rsi 14) 70.0) (entry-short)) "
|
|
"(when (lt (indicator rsi 14) 30.0) (entry-long)))"
|
|
)
|
|
ast = parse_strategy(src)
|
|
agent = FalsificationAgent(fees_bp=5.0, n_trials_dsr=20)
|
|
report = agent.evaluate(ast, trending_ohlcv)
|
|
assert isinstance(report, FalsificationReport)
|
|
assert isinstance(report.sharpe, float)
|
|
assert isinstance(report.dsr, float)
|
|
assert 0.0 <= report.dsr <= 1.0
|
|
assert isinstance(report.max_drawdown, float)
|
|
assert isinstance(report.n_trades, int)
|
|
|
|
|
|
def test_falsification_zero_trades_returns_zero_metrics(trending_ohlcv: pd.DataFrame) -> None:
|
|
src = "(strategy (when (gt (feature close) 1e9) (entry-long)))"
|
|
ast = parse_strategy(src)
|
|
agent = FalsificationAgent(fees_bp=5.0, n_trials_dsr=20)
|
|
report = agent.evaluate(ast, trending_ohlcv)
|
|
assert report.n_trades == 0
|
|
assert report.sharpe == 0.0
|