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,50 @@
import sys
from pathlib import Path
import pytest
# Añadir raíz del proyecto al path
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
from src.risk.sizing.percent_risk import PercentRiskSizer
def test_percent_risk_basic():
sizer = PercentRiskSizer(risk_fraction=0.01) # 1%
capital = 10_000
entry_price = 100
stop_price = 95
units = sizer.calculate_size(
capital=capital,
entry_price=entry_price,
stop_price=stop_price
)
# riesgo = 100€
# riesgo por unidad = 5
# unidades = 100 / 5 = 20
assert units == 20
def test_percent_risk_zero_distance():
sizer = PercentRiskSizer(0.01)
units = sizer.calculate_size(
capital=10_000,
entry_price=100,
stop_price=100
)
assert units == 0.0
def test_percent_risk_requires_stop():
sizer = PercentRiskSizer(0.01)
with pytest.raises(ValueError):
sizer.calculate_size(
capital=10_000,
entry_price=100
)