40 lines
983 B
Python
40 lines
983 B
Python
from __future__ import annotations
|
|
|
|
import pandas as pd
|
|
|
|
from web_core.analytics import backtest_signals
|
|
from web_core.constants import TREND_BEAR, TREND_BULL, TREND_NEUTRAL
|
|
|
|
|
|
def test_backtest_signals_supports_cost_and_hold_controls() -> None:
|
|
idx = pd.date_range("2025-01-01", periods=8, freq="D")
|
|
df = pd.DataFrame(
|
|
{
|
|
"Close": [100.0, 101.0, 103.0, 104.0, 102.0, 99.0, 97.0, 98.0],
|
|
"trend_state": [
|
|
TREND_NEUTRAL,
|
|
TREND_BULL,
|
|
TREND_BULL,
|
|
TREND_BEAR,
|
|
TREND_BEAR,
|
|
TREND_BULL,
|
|
TREND_BULL,
|
|
TREND_BULL,
|
|
],
|
|
},
|
|
index=idx,
|
|
)
|
|
|
|
out = backtest_signals(
|
|
df,
|
|
slippage_bps=5.0,
|
|
fee_bps=2.0,
|
|
stop_loss_pct=2.0,
|
|
take_profit_pct=3.0,
|
|
min_hold_bars=1,
|
|
max_hold_bars=3,
|
|
)
|
|
|
|
assert out["trades"] >= 1
|
|
assert "avg_pnl_pct" in out
|