test: cooldown regression — 1h TF не застревает на 24ч + legacy date format

Старые тесты тестировали формулу days*24//tf_hours, повторяя баг.
Новые тесты используют hours-based логику (как prod) и проверяют:
- 4h после стопа на 1h TF = 4 candles >= cooldown 3 (regression)
- legacy date string "2026-03-13" парсится без ошибок

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-15 04:09:58 +10:00
co-authored by Claude Opus 4.6
parent 37a213e6d1
commit 59eadc9f9c
+43 -15
View File
@@ -127,27 +127,55 @@ class TestSpotShortSkip:
class TestCooldownAfterStop:
"""After a stop-loss, bot should wait cooldown_bars before next signal."""
def _calc_cooldown(self, last_stop_iso: str, tf_hours: int):
"""Replicate prod cooldown logic (hours-based)."""
from datetime import datetime as dt_cls, timezone as tz
try:
stop_dt = dt_cls.fromisoformat(last_stop_iso)
if stop_dt.tzinfo is None:
stop_dt = stop_dt.replace(tzinfo=tz.utc)
except (ValueError, TypeError):
stop_dt = dt_cls.combine(date.fromisoformat(last_stop_iso),
dt_cls.min.time(), tzinfo=tz.utc)
hours_since = (dt_cls.now(tz.utc) - stop_dt).total_seconds() / 3600
return int(hours_since / tf_hours)
def test_cooldown_blocks_signal(self):
"""Within cooldown period, signal should be skipped."""
last_stop_date = str(date.today())
current_date = str(date.today()) # same day
tf_hours = 4
days_since = 0
candles_since = days_since * 24 // tf_hours # 0
cooldown_bars = 3
assert candles_since < cooldown_bars # should skip
from datetime import datetime as dt_cls, timezone as tz
# Stop happened 1 hour ago
last_stop = (dt_cls.now(tz.utc) - timedelta(hours=1)).isoformat()
candles_since = self._calc_cooldown(last_stop, tf_hours=1)
assert candles_since < 3 # 1h < 3 bars → should skip
def test_cooldown_expired_allows_signal(self):
"""After cooldown period expires, signal should proceed."""
last_stop_date = str(date.today() - timedelta(days=2))
current_date = str(date.today())
tf_hours = 4
days_since = 2
candles_since = days_since * 24 // tf_hours # 12
cooldown_bars = 3
from datetime import datetime as dt_cls, timezone as tz
# Stop happened 5 hours ago, cooldown=3 bars on 1h TF
last_stop = (dt_cls.now(tz.utc) - timedelta(hours=5)).isoformat()
candles_since = self._calc_cooldown(last_stop, tf_hours=1)
assert candles_since >= 3 # 5h >= 3 bars → should proceed
assert candles_since >= cooldown_bars # should proceed
def test_cooldown_1h_not_stuck_for_full_day(self):
"""REGRESSION: cooldown must NOT block for 24h on 1h TF.
Old bug: cooldown used .days (whole days), so on 1h TF with
cooldown_bars=3, bot waited 24h instead of 3h.
"""
from datetime import datetime as dt_cls, timezone as tz
# Stop 4 hours ago — should be past cooldown_bars=3 on 1h TF
last_stop = (dt_cls.now(tz.utc) - timedelta(hours=4)).isoformat()
candles_since = self._calc_cooldown(last_stop, tf_hours=1)
assert candles_since >= 3, \
f"4h after stop on 1h TF = {candles_since} candles, expected >= 3"
def test_cooldown_legacy_date_format(self):
"""Legacy last_stop_date as date string should still work."""
from datetime import datetime as dt_cls, timezone as tz
# Legacy: stored as date "2026-03-13" (2 days ago)
last_stop = str(date.today() - timedelta(days=2))
candles_since = self._calc_cooldown(last_stop, tf_hours=4)
assert candles_since >= 3 # 2 days = 48h / 4h = 12 candles >= 3
def test_cooldown_minus_one_uses_shift(self):
"""cooldown_bars=-1 means use shift as cooldown."""