Files
aura-trade/backend/tests/test_risk_sentinel.py

44 lines
1.8 KiB
Python

import pytest
from app.engine.risk_sentinel import RiskSentinel
def test_calculate_safe_lot_1_percent():
sentinel = RiskSentinel(max_risk_pct=1.0)
equity = 1000.0
entry = 2740.0
sl = 2735.0 # 5.0 move = 50 pips. $10 risk / (50 * 10) = 0.02 lot
lot = sentinel.calculate_safe_lot(equity, entry, sl, "XAUUSD")
assert lot == 0.02
def test_reject_missing_stop_loss():
sentinel = RiskSentinel()
order = {"symbol": "XAUUSD", "type": "BUY", "lot": 0.01, "price": 2740.0, "sl": 0.0}
is_safe, reason = sentinel.validate_order(1000.0, 1000.0, order, 1.5)
assert is_safe is False
assert "SAFETY_VIOLATION" in reason
def test_reject_overleverage():
sentinel = RiskSentinel(max_risk_pct=1.0)
# On $500 equity with 50 pips SL, safe lot is 0.01. Requesting 0.10 should be rejected!
order = {"symbol": "XAUUSD", "type": "BUY", "lot": 0.10, "price": 2740.0, "sl": 2735.0}
is_safe, reason = sentinel.validate_order(500.0, 500.0, order, 1.5)
assert is_safe is False
assert "OVERLEVERAGE_ATTEMPT" in reason
def test_circuit_breaker_trigger():
sentinel = RiskSentinel(max_daily_dd_pct=2.5)
daily_start = 1000.0
current_equity = 970.0 # $30 loss = 3.0% loss (> 2.5% max)
order = {"symbol": "XAUUSD", "type": "BUY", "lot": 0.01, "price": 2740.0, "sl": 2735.0}
is_safe, reason = sentinel.validate_order(current_equity, daily_start, order, 1.5)
assert is_safe is False
assert "CIRCUIT_BREAKER_TRIPPED" in reason
assert sentinel.is_circuit_breaker_tripped is True
def test_spread_spike_filter():
sentinel = RiskSentinel(max_spread_pips=25.0)
order = {"symbol": "XAUUSD", "type": "BUY", "lot": 0.01, "price": 2740.0, "sl": 2735.0}
# Spread is 30 pips ($0.30)
is_safe, reason = sentinel.validate_order(1000.0, 1000.0, order, 30.0)
assert is_safe is False
assert "SPREAD_TOO_HIGH" in reason