fix: harden post-stop reentry lockdown
This commit is contained in:
@@ -590,19 +590,47 @@ class TestSimulatePortfolio:
|
||||
None,
|
||||
"hold",
|
||||
30,
|
||||
reentry_cooldown_days=5,
|
||||
reentry_cooldown_sessions=5,
|
||||
)
|
||||
|
||||
assert baseline is not None and baseline["trades"] == 2
|
||||
assert cooldown is not None and cooldown["trades"] == 1
|
||||
assert cooldown["skipped_cooldown"] == 1
|
||||
assert cooldown["reentry_cooldown_days"] == 5
|
||||
assert cooldown["reentry_cooldown_sessions"] == 5
|
||||
|
||||
def test_initial_stop_cooldown_unlocks_exactly_after_session_five(self):
|
||||
closes = [100.0, 94.0, 96.0, 96.0, 96.0, 96.0, 97.0, 98.0]
|
||||
prices = {"AAA": _sim_prices(self.ORD, closes)}
|
||||
candidates = [
|
||||
_sim_cand("AAA", self.ORD, entry=100.0, stop=95.0, target=120.0),
|
||||
# Four completed sessions since the stop: still locked.
|
||||
_sim_cand("AAA", self.ORD + 5, entry=96.0, stop=90.0, target=115.0),
|
||||
# Five completed sessions since the stop: first permitted re-entry.
|
||||
_sim_cand("AAA", self.ORD + 6, entry=97.0, stop=90.0, target=118.0),
|
||||
]
|
||||
|
||||
sim = bt._simulate_portfolio(
|
||||
candidates,
|
||||
prices,
|
||||
None,
|
||||
"hold",
|
||||
30,
|
||||
reentry_cooldown_sessions=5,
|
||||
include_trades=True,
|
||||
)
|
||||
|
||||
assert sim is not None
|
||||
assert sim["trades"] == 2
|
||||
assert sim["skipped_cooldown"] == 1
|
||||
assert sim["trade_details"][1]["entry_date"] == date.fromordinal(
|
||||
self.ORD + 6
|
||||
).isoformat()
|
||||
|
||||
def test_production_monitor_applies_live_reentry_lockdown(self, monkeypatch):
|
||||
def fake_simulator(*_args, **kwargs):
|
||||
return {
|
||||
"trades": 0,
|
||||
"applied_reentry_lockdown": kwargs.get("reentry_cooldown_days", 0),
|
||||
"applied_reentry_lockdown": kwargs.get("reentry_cooldown_sessions", 0),
|
||||
}
|
||||
|
||||
monkeypatch.setattr(bt, "_simulate_portfolio", fake_simulator)
|
||||
|
||||
Reference in New Issue
Block a user