From 59eadc9f9c4c84aec17959bcdd1a36bb5b792f8b Mon Sep 17 00:00:00 2001 From: anor Date: Sun, 15 Mar 2026 04:09:58 +1000 Subject: [PATCH] =?UTF-8?q?test:=20cooldown=20regression=20=E2=80=94=201h?= =?UTF-8?q?=20TF=20=D0=BD=D0=B5=20=D0=B7=D0=B0=D1=81=D1=82=D1=80=D0=B5?= =?UTF-8?q?=D0=B2=D0=B0=D0=B5=D1=82=20=D0=BD=D0=B0=2024=D1=87=20+=20legacy?= =?UTF-8?q?=20date=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Старые тесты тестировали формулу 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 --- tests/test_executor.py | 58 +++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/tests/test_executor.py b/tests/test_executor.py index 61ae4d1..3e4e6b2 100644 --- a/tests/test_executor.py +++ b/tests/test_executor.py @@ -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."""