From 8dbef6bbeab0662a5014082f7ab65e2abb63d1ac Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 24 Oct 2020 20:25:40 +0200 Subject: [PATCH] Add test for cooldown period --- .../plugins/protections/cooldown_period.py | 8 ++-- tests/plugins/test_protections.py | 43 ++++++++++++++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/freqtrade/plugins/protections/cooldown_period.py b/freqtrade/plugins/protections/cooldown_period.py index c6b6685b2..24f55419b 100644 --- a/freqtrade/plugins/protections/cooldown_period.py +++ b/freqtrade/plugins/protections/cooldown_period.py @@ -1,9 +1,8 @@ import logging -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from typing import Any, Dict - from freqtrade.persistence import Trade from freqtrade.plugins.protections import IProtection, ProtectionReturn @@ -28,7 +27,7 @@ class CooldownPeriod(IProtection): """ Short method description - used for startup-messages """ - return (f"{self.name} - Cooldown period.") + return (f"{self.name} - Cooldown period of {self._stopduration} min.") def _cooldown_period(self, pair: str, date_now: datetime, ) -> ProtectionReturn: """ @@ -43,7 +42,8 @@ class CooldownPeriod(IProtection): trade = Trade.get_trades(filters).first() if trade: self.log_on_refresh(logger.info, f"Cooldown for {pair} for {self._stopduration}.") - until = trade.close_date + timedelta(minutes=self._stopduration) + until = trade.close_date.replace( + tzinfo=timezone.utc) + timedelta(minutes=self._stopduration) return True, until, self._reason() return False, None, None diff --git a/tests/plugins/test_protections.py b/tests/plugins/test_protections.py index d2815338e..59ada7c1e 100644 --- a/tests/plugins/test_protections.py +++ b/tests/plugins/test_protections.py @@ -3,7 +3,7 @@ from datetime import datetime, timedelta import pytest -from freqtrade.persistence import Trade +from freqtrade.persistence import PairLocks, Trade from freqtrade.strategy.interface import SellType from tests.conftest import get_patched_freqtradebot, log_has_re @@ -76,6 +76,43 @@ def test_stoploss_guard(mocker, default_conf, fee, caplog): assert freqtrade.protections.global_stop() assert log_has_re(message, caplog) + assert PairLocks.is_global_lock() + + +@pytest.mark.usefixtures("init_persistence") +def test_CooldownPeriod(mocker, default_conf, fee, caplog): + default_conf['protections'] = [{ + "method": "CooldownPeriod", + "stopduration": 60, + }] + freqtrade = get_patched_freqtradebot(mocker, default_conf) + message = r"Trading stopped due to .*" + assert not freqtrade.protections.global_stop() + assert not freqtrade.protections.stop_per_pair('XRP/BTC') + + assert not log_has_re(message, caplog) + caplog.clear() + + Trade.session.add(generate_mock_trade( + 'XRP/BTC', fee.return_value, False, sell_reason=SellType.STOP_LOSS.value, + min_ago_open=200, min_ago_close=30, + )) + + assert not freqtrade.protections.global_stop() + assert freqtrade.protections.stop_per_pair('XRP/BTC') + assert PairLocks.is_pair_locked('XRP/BTC') + assert not PairLocks.is_global_lock() + + Trade.session.add(generate_mock_trade( + 'ETH/BTC', fee.return_value, False, sell_reason=SellType.ROI.value, + min_ago_open=205, min_ago_close=35, + )) + + assert not freqtrade.protections.global_stop() + assert not PairLocks.is_pair_locked('ETH/BTC') + assert freqtrade.protections.stop_per_pair('ETH/BTC') + assert PairLocks.is_pair_locked('ETH/BTC') + assert not PairLocks.is_global_lock() @pytest.mark.parametrize("protectionconf,desc_expected,exception_expected", [ @@ -84,6 +121,10 @@ def test_stoploss_guard(mocker, default_conf, fee, caplog): "2 stoplosses within 60 minutes.'}]", None ), + ({"method": "CooldownPeriod", "stopduration": 60}, + "[{'CooldownPeriod': 'CooldownPeriod - Cooldown period of 60 min.'}]", + None + ), ]) def test_protection_manager_desc(mocker, default_conf, protectionconf, desc_expected, exception_expected):