Make risk guard symbol aware
This commit is contained in:
@@ -6,7 +6,7 @@ from crypto_spot_bot.models import Candle, Ticker, Trade, utc_now
|
||||
from crypto_spot_bot.storage import Storage
|
||||
|
||||
|
||||
def test_risk_guard_blocks_after_consecutive_losses(make_settings, tmp_path) -> None:
|
||||
def test_risk_guard_reduces_size_after_consecutive_losses(make_settings, tmp_path) -> None:
|
||||
settings = make_settings(tmp_path, risk_max_consecutive_losses=2)
|
||||
storage = Storage(settings.database_path)
|
||||
now = utc_now()
|
||||
@@ -28,9 +28,78 @@ def test_risk_guard_blocks_after_consecutive_losses(make_settings, tmp_path) ->
|
||||
|
||||
guard = risk_guard_snapshot(settings, storage.closed_trades(), storage.latest_equity())
|
||||
|
||||
assert guard["block_new_entries"] is True
|
||||
assert guard["block_new_entries"] is False
|
||||
assert "consecutive_losses" in guard["reasons"]
|
||||
assert guard["position_size_multiplier"] == 0.0
|
||||
assert guard["position_size_multiplier"] == settings.risk_reduce_multiplier
|
||||
|
||||
|
||||
def test_risk_guard_blocks_only_bad_symbol(make_settings, tmp_path) -> None:
|
||||
settings = make_settings(tmp_path, risk_max_consecutive_losses=3, symbols=["BTCUSDT", "ETHUSDT"])
|
||||
storage = Storage(settings.database_path)
|
||||
now = utc_now()
|
||||
for _ in range(3):
|
||||
storage.insert_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
symbol="BTCUSDT",
|
||||
side="SELL",
|
||||
qty=1.0,
|
||||
entry_price=100.0,
|
||||
exit_price=99.0,
|
||||
net_pnl=-1.0,
|
||||
opened_at=now,
|
||||
closed_at=now,
|
||||
entry_diagnostics={"forecast": {"probability_up": 0.64, "model": "torch_gru"}},
|
||||
)
|
||||
)
|
||||
storage.insert_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
symbol="ETHUSDT",
|
||||
side="SELL",
|
||||
qty=1.0,
|
||||
entry_price=100.0,
|
||||
exit_price=102.0,
|
||||
net_pnl=2.0,
|
||||
opened_at=now,
|
||||
closed_at=now,
|
||||
entry_diagnostics={"forecast": {"probability_up": 0.64, "model": "torch_gru"}},
|
||||
)
|
||||
)
|
||||
|
||||
guard = risk_guard_snapshot(settings, storage.closed_trades(), storage.latest_equity())
|
||||
|
||||
assert guard["block_new_entries"] is False
|
||||
assert guard["blocked_symbols"] == ["BTCUSDT"]
|
||||
symbol_rows = {row["symbol"]: row for row in guard["symbols"]}
|
||||
assert symbol_rows["BTCUSDT"]["block_new_entries"] is True
|
||||
assert symbol_rows["ETHUSDT"]["block_new_entries"] is False
|
||||
|
||||
|
||||
def test_risk_guard_ignores_trades_outside_active_universe(make_settings, tmp_path) -> None:
|
||||
settings = make_settings(tmp_path, risk_max_consecutive_losses=2, symbols=["BTCUSDT", "ETHUSDT"])
|
||||
storage = Storage(settings.database_path)
|
||||
now = utc_now()
|
||||
for _ in range(4):
|
||||
storage.insert_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
symbol="HYPEUSDT",
|
||||
side="SELL",
|
||||
qty=1.0,
|
||||
entry_price=100.0,
|
||||
exit_price=99.0,
|
||||
net_pnl=-1.0,
|
||||
opened_at=now,
|
||||
closed_at=now,
|
||||
)
|
||||
)
|
||||
|
||||
guard = risk_guard_snapshot(settings, storage.closed_trades(), storage.latest_equity())
|
||||
|
||||
assert guard["block_new_entries"] is False
|
||||
assert guard["reasons"] == []
|
||||
assert guard["blocked_symbols"] == []
|
||||
|
||||
|
||||
def test_data_quality_flags_missing_candle_gap() -> None:
|
||||
|
||||
Reference in New Issue
Block a user