Add test for cooldown period

This commit is contained in:
Matthias 2020-10-24 20:25:40 +02:00
parent fe0afb9883
commit 8dbef6bbea
2 changed files with 46 additions and 5 deletions

View File

@ -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

View File

@ -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):