Engine: add stop loss integration (fixed & trailing) with tests

This commit is contained in:
DaM
2026-01-30 17:05:47 +01:00
parent af7b862f60
commit c569170fcc
24 changed files with 1121 additions and 137 deletions

View File

@@ -0,0 +1,73 @@
# src/risk/stops/test_atr_stop.py
import sys
from pathlib import Path
import pandas as pd
import numpy as np
import pytest
# Añadir raíz del proyecto al path
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
from src.risk.stops.base import StopLoss
from src.backtest.trade import TradeType
from src.risk.stops.atr_stop import ATRStop
from src.backtest.trade import TradeType
def atr_data():
return pd.DataFrame(
{
"high": [10, 11, 12, 13, 14],
"low": [9, 10, 11, 12, 13],
"close": [9.5, 10.5, 11.5, 12.5, 13.5],
},
index=pd.date_range("2024-01-01", periods=5, freq="D"),
)
def test_atr_stop_long():
stop = ATRStop(atr_period=3, multiplier=2.0)
price = stop.get_stop_price(
data=atr_data(),
idx=4,
entry_price=14,
trade_type=TradeType.LONG,
)
assert price < 14 # stop por debajo
def test_atr_stop_short():
stop = ATRStop(atr_period=3, multiplier=2.0)
price = stop.get_stop_price(
data=atr_data(),
idx=4,
entry_price=14,
trade_type=TradeType.SHORT,
)
assert price > 14 # stop por encima
def test_atr_stop_requires_columns():
stop = ATRStop()
bad_data = pd.DataFrame({"close": [1, 2, 3]})
with pytest.raises(ValueError):
stop.get_stop_price(
data=bad_data,
idx=2,
entry_price=3,
trade_type=TradeType.LONG,
)
def test_atr_stop_invalid_params():
with pytest.raises(ValueError):
ATRStop(atr_period=0)
with pytest.raises(ValueError):
ATRStop(multiplier=0)

View File

@@ -0,0 +1,51 @@
# tests/risk/stops/test_fixed_stop.py
import sys
from pathlib import Path
import pandas as pd
import pytest
# Añadir raíz del proyecto al path
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
from src.risk.stops.fixed_stop import FixedStop
from src.backtest.trade import TradeType
def dummy_data():
return pd.DataFrame(
{"close": [100, 101, 102]},
index=pd.date_range("2024-01-01", periods=3, freq="D")
)
def test_fixed_stop_long():
stop = FixedStop(0.02)
price = stop.get_stop_price(
data=dummy_data(),
idx=1,
entry_price=100,
trade_type=TradeType.LONG
)
assert price == 98
def test_fixed_stop_short():
stop = FixedStop(0.02)
price = stop.get_stop_price(
data=dummy_data(),
idx=1,
entry_price=100,
trade_type=TradeType.SHORT
)
assert price == 102
def test_fixed_stop_invalid_fraction():
with pytest.raises(ValueError):
FixedStop(0)
with pytest.raises(ValueError):
FixedStop(1)
with pytest.raises(ValueError):
FixedStop(-0.1)