Engine: add stop loss integration (fixed & trailing) with tests
This commit is contained in:
51
tests/risk/stops/test_fixed_stop.py
Normal file
51
tests/risk/stops/test_fixed_stop.py
Normal 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)
|
||||
Reference in New Issue
Block a user