2020-10-15 05:59:32 +00:00
|
|
|
import random
|
2020-12-02 06:42:39 +00:00
|
|
|
from datetime import datetime, timedelta
|
2020-10-15 05:59:32 +00:00
|
|
|
|
2020-10-15 06:07:09 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-12-07 09:45:35 +00:00
|
|
|
from freqtrade import constants
|
2022-03-25 05:55:37 +00:00
|
|
|
from freqtrade.enums import ExitType
|
2020-10-24 18:25:40 +00:00
|
|
|
from freqtrade.persistence import PairLocks, Trade
|
2020-12-07 09:45:35 +00:00
|
|
|
from freqtrade.plugins.protectionmanager import ProtectionManager
|
2020-10-15 05:59:32 +00:00
|
|
|
from tests.conftest import get_patched_freqtradebot, log_has_re
|
|
|
|
|
|
|
|
|
|
|
|
def generate_mock_trade(pair: str, fee: float, is_open: bool,
|
2022-03-25 05:55:37 +00:00
|
|
|
sell_reason: str = ExitType.SELL_SIGNAL,
|
2020-10-15 05:59:32 +00:00
|
|
|
min_ago_open: int = None, min_ago_close: int = None,
|
2020-11-25 09:48:54 +00:00
|
|
|
profit_rate: float = 0.9
|
2020-10-15 05:59:32 +00:00
|
|
|
):
|
|
|
|
open_rate = random.random()
|
|
|
|
|
|
|
|
trade = Trade(
|
|
|
|
pair=pair,
|
|
|
|
stake_amount=0.01,
|
|
|
|
fee_open=fee,
|
|
|
|
fee_close=fee,
|
|
|
|
open_date=datetime.utcnow() - timedelta(minutes=min_ago_open or 200),
|
|
|
|
close_date=datetime.utcnow() - timedelta(minutes=min_ago_close or 30),
|
|
|
|
open_rate=open_rate,
|
|
|
|
is_open=is_open,
|
|
|
|
amount=0.01 / open_rate,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2020-10-15 05:59:32 +00:00
|
|
|
)
|
2020-12-13 09:33:45 +00:00
|
|
|
trade.recalc_open_trade_value()
|
2020-10-15 05:59:32 +00:00
|
|
|
if not is_open:
|
2020-11-25 09:48:54 +00:00
|
|
|
trade.close(open_rate * profit_rate)
|
2020-10-15 05:59:32 +00:00
|
|
|
trade.sell_reason = sell_reason
|
2020-11-25 09:48:54 +00:00
|
|
|
|
2020-10-15 05:59:32 +00:00
|
|
|
return trade
|
|
|
|
|
|
|
|
|
2020-11-25 09:58:50 +00:00
|
|
|
def test_protectionmanager(mocker, default_conf):
|
|
|
|
default_conf['protections'] = [{'method': protection}
|
|
|
|
for protection in constants.AVAILABLE_PROTECTIONS]
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
for handler in freqtrade.protections._protection_handlers:
|
|
|
|
assert handler.name in constants.AVAILABLE_PROTECTIONS
|
|
|
|
if not handler.has_global_stop:
|
|
|
|
assert handler.global_stop(datetime.utcnow()) == (False, None, None)
|
|
|
|
if not handler.has_local_stop:
|
2020-11-30 18:07:39 +00:00
|
|
|
assert handler.stop_per_pair('XRP/BTC', datetime.utcnow()) == (False, None, None)
|
2020-11-25 09:58:50 +00:00
|
|
|
|
|
|
|
|
2020-12-07 09:45:35 +00:00
|
|
|
@pytest.mark.parametrize('timeframe,expected,protconf', [
|
|
|
|
('1m', [20, 10],
|
|
|
|
[{"method": "StoplossGuard", "lookback_period_candles": 20, "stop_duration": 10}]),
|
|
|
|
('5m', [100, 15],
|
|
|
|
[{"method": "StoplossGuard", "lookback_period_candles": 20, "stop_duration": 15}]),
|
|
|
|
('1h', [1200, 40],
|
|
|
|
[{"method": "StoplossGuard", "lookback_period_candles": 20, "stop_duration": 40}]),
|
|
|
|
('1d', [1440, 5],
|
|
|
|
[{"method": "StoplossGuard", "lookback_period_candles": 1, "stop_duration": 5}]),
|
|
|
|
('1m', [20, 5],
|
|
|
|
[{"method": "StoplossGuard", "lookback_period": 20, "stop_duration_candles": 5}]),
|
|
|
|
('5m', [15, 25],
|
|
|
|
[{"method": "StoplossGuard", "lookback_period": 15, "stop_duration_candles": 5}]),
|
|
|
|
('1h', [50, 600],
|
|
|
|
[{"method": "StoplossGuard", "lookback_period": 50, "stop_duration_candles": 10}]),
|
|
|
|
('1h', [60, 540],
|
|
|
|
[{"method": "StoplossGuard", "lookback_period_candles": 1, "stop_duration_candles": 9}]),
|
|
|
|
])
|
|
|
|
def test_protections_init(mocker, default_conf, timeframe, expected, protconf):
|
|
|
|
default_conf['timeframe'] = timeframe
|
2021-06-17 19:01:22 +00:00
|
|
|
man = ProtectionManager(default_conf, protconf)
|
2020-12-07 09:45:35 +00:00
|
|
|
assert len(man._protection_handlers) == len(protconf)
|
|
|
|
assert man._protection_handlers[0]._lookback_period == expected[0]
|
|
|
|
assert man._protection_handlers[0]._stop_duration == expected[1]
|
|
|
|
|
|
|
|
|
2020-10-15 05:59:32 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_stoploss_guard(mocker, default_conf, fee, caplog):
|
|
|
|
default_conf['protections'] = [{
|
|
|
|
"method": "StoplossGuard",
|
|
|
|
"lookback_period": 60,
|
2020-11-29 10:36:16 +00:00
|
|
|
"stop_duration": 40,
|
2021-02-20 18:37:38 +00:00
|
|
|
"trade_limit": 3
|
2020-10-15 05:59:32 +00:00
|
|
|
}]
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
message = r"Trading stopped due to .*"
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not log_has_re(message, caplog)
|
|
|
|
caplog.clear()
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'XRP/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-10-15 05:59:32 +00:00
|
|
|
min_ago_open=200, min_ago_close=30,
|
2021-08-06 22:19:36 +00:00
|
|
|
))
|
2020-10-15 05:59:32 +00:00
|
|
|
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not log_has_re(message, caplog)
|
|
|
|
caplog.clear()
|
|
|
|
# This trade does not count, as it's closed too long ago
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'BCH/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-10-15 05:59:32 +00:00
|
|
|
min_ago_open=250, min_ago_close=100,
|
|
|
|
))
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'ETH/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-10-15 05:59:32 +00:00
|
|
|
min_ago_open=240, min_ago_close=30,
|
|
|
|
))
|
|
|
|
# 3 Trades closed - but the 2nd has been closed too long ago.
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not log_has_re(message, caplog)
|
|
|
|
caplog.clear()
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'LTC/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-10-15 05:59:32 +00:00
|
|
|
min_ago_open=180, min_ago_close=30,
|
|
|
|
))
|
|
|
|
|
|
|
|
assert freqtrade.protections.global_stop()
|
|
|
|
assert log_has_re(message, caplog)
|
2020-10-24 18:25:40 +00:00
|
|
|
assert PairLocks.is_global_lock()
|
|
|
|
|
2020-11-29 10:37:10 +00:00
|
|
|
# Test 5m after lock-period - this should try and relock the pair, but end-time
|
|
|
|
# should be the previous end-time
|
|
|
|
end_time = PairLocks.get_pair_longest_lock('*').lock_end_time + timedelta(minutes=5)
|
2021-09-19 17:41:19 +00:00
|
|
|
freqtrade.protections.global_stop(end_time)
|
2020-11-29 10:37:10 +00:00
|
|
|
assert not PairLocks.is_global_lock(end_time)
|
|
|
|
|
2020-10-24 18:25:40 +00:00
|
|
|
|
2020-11-25 10:11:55 +00:00
|
|
|
@pytest.mark.parametrize('only_per_pair', [False, True])
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_stoploss_guard_perpair(mocker, default_conf, fee, caplog, only_per_pair):
|
|
|
|
default_conf['protections'] = [{
|
|
|
|
"method": "StoplossGuard",
|
|
|
|
"lookback_period": 60,
|
2021-02-20 18:37:38 +00:00
|
|
|
"trade_limit": 2,
|
2020-12-07 09:21:03 +00:00
|
|
|
"stop_duration": 60,
|
2020-11-25 10:11:55 +00:00
|
|
|
"only_per_pair": only_per_pair
|
|
|
|
}]
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
message = r"Trading stopped due to .*"
|
|
|
|
pair = 'XRP/BTC'
|
|
|
|
assert not freqtrade.protections.stop_per_pair(pair)
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not log_has_re(message, caplog)
|
|
|
|
caplog.clear()
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
pair, fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-11-25 10:11:55 +00:00
|
|
|
min_ago_open=200, min_ago_close=30, profit_rate=0.9,
|
2021-08-06 22:19:36 +00:00
|
|
|
))
|
2020-11-25 10:11:55 +00:00
|
|
|
|
|
|
|
assert not freqtrade.protections.stop_per_pair(pair)
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not log_has_re(message, caplog)
|
|
|
|
caplog.clear()
|
|
|
|
# This trade does not count, as it's closed too long ago
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
pair, fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-11-25 10:11:55 +00:00
|
|
|
min_ago_open=250, min_ago_close=100, profit_rate=0.9,
|
|
|
|
))
|
|
|
|
# Trade does not count for per pair stop as it's the wrong pair.
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'ETH/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-11-25 10:11:55 +00:00
|
|
|
min_ago_open=240, min_ago_close=30, profit_rate=0.9,
|
|
|
|
))
|
|
|
|
# 3 Trades closed - but the 2nd has been closed too long ago.
|
|
|
|
assert not freqtrade.protections.stop_per_pair(pair)
|
|
|
|
assert freqtrade.protections.global_stop() != only_per_pair
|
|
|
|
if not only_per_pair:
|
|
|
|
assert log_has_re(message, caplog)
|
|
|
|
else:
|
|
|
|
assert not log_has_re(message, caplog)
|
|
|
|
|
|
|
|
caplog.clear()
|
|
|
|
|
|
|
|
# 2nd Trade that counts with correct pair
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
pair, fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-11-25 10:11:55 +00:00
|
|
|
min_ago_open=180, min_ago_close=30, profit_rate=0.9,
|
|
|
|
))
|
|
|
|
|
2021-09-19 17:41:19 +00:00
|
|
|
freqtrade.protections.stop_per_pair(pair)
|
2020-11-25 10:11:55 +00:00
|
|
|
assert freqtrade.protections.global_stop() != only_per_pair
|
|
|
|
assert PairLocks.is_pair_locked(pair)
|
|
|
|
assert PairLocks.is_global_lock() != only_per_pair
|
|
|
|
|
|
|
|
|
2020-10-24 18:25:40 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_CooldownPeriod(mocker, default_conf, fee, caplog):
|
|
|
|
default_conf['protections'] = [{
|
|
|
|
"method": "CooldownPeriod",
|
2020-12-07 09:21:03 +00:00
|
|
|
"stop_duration": 60,
|
2020-10-24 18:25:40 +00:00
|
|
|
}]
|
|
|
|
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()
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'XRP/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-10-24 18:25:40 +00:00
|
|
|
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()
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'ETH/BTC', fee.return_value, False, sell_reason=ExitType.ROI.value,
|
2020-10-24 18:25:40 +00:00
|
|
|
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()
|
2020-10-15 06:07:09 +00:00
|
|
|
|
|
|
|
|
2020-11-11 20:11:22 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_LowProfitPairs(mocker, default_conf, fee, caplog):
|
|
|
|
default_conf['protections'] = [{
|
|
|
|
"method": "LowProfitPairs",
|
|
|
|
"lookback_period": 400,
|
2020-12-07 09:21:03 +00:00
|
|
|
"stop_duration": 60,
|
2020-11-11 20:11:22 +00:00
|
|
|
"trade_limit": 2,
|
|
|
|
"required_profit": 0.0,
|
|
|
|
}]
|
|
|
|
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()
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'XRP/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-11-25 09:48:54 +00:00
|
|
|
min_ago_open=800, min_ago_close=450, profit_rate=0.9,
|
2020-11-11 20:11:22 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
# Not locked with 1 trade
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not freqtrade.protections.stop_per_pair('XRP/BTC')
|
|
|
|
assert not PairLocks.is_pair_locked('XRP/BTC')
|
|
|
|
assert not PairLocks.is_global_lock()
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'XRP/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-11-25 09:48:54 +00:00
|
|
|
min_ago_open=200, min_ago_close=120, profit_rate=0.9,
|
2020-11-11 20:11:22 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
# Not locked with 1 trade (first trade is outside of lookback_period)
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not freqtrade.protections.stop_per_pair('XRP/BTC')
|
|
|
|
assert not PairLocks.is_pair_locked('XRP/BTC')
|
|
|
|
assert not PairLocks.is_global_lock()
|
|
|
|
|
2020-11-25 09:48:54 +00:00
|
|
|
# Add positive trade
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'XRP/BTC', fee.return_value, False, sell_reason=ExitType.ROI.value,
|
2020-11-25 09:48:54 +00:00
|
|
|
min_ago_open=20, min_ago_close=10, profit_rate=1.15,
|
|
|
|
))
|
|
|
|
assert not freqtrade.protections.stop_per_pair('XRP/BTC')
|
|
|
|
assert not PairLocks.is_pair_locked('XRP/BTC')
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'XRP/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-11-25 09:48:54 +00:00
|
|
|
min_ago_open=110, min_ago_close=20, profit_rate=0.8,
|
2020-11-11 20:11:22 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
# Locks due to 2nd trade
|
|
|
|
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()
|
|
|
|
|
2020-11-22 10:49:41 +00:00
|
|
|
|
2020-11-30 18:07:39 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_MaxDrawdown(mocker, default_conf, fee, caplog):
|
|
|
|
default_conf['protections'] = [{
|
|
|
|
"method": "MaxDrawdown",
|
|
|
|
"lookback_period": 1000,
|
2020-12-07 09:21:03 +00:00
|
|
|
"stop_duration": 60,
|
2020-11-30 18:07:39 +00:00
|
|
|
"trade_limit": 3,
|
|
|
|
"max_allowed_drawdown": 0.15
|
|
|
|
}]
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
message = r"Trading stopped due to Max.*"
|
|
|
|
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not freqtrade.protections.stop_per_pair('XRP/BTC')
|
|
|
|
caplog.clear()
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'XRP/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-11-30 18:07:39 +00:00
|
|
|
min_ago_open=1000, min_ago_close=900, profit_rate=1.1,
|
|
|
|
))
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'ETH/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-12-07 14:45:02 +00:00
|
|
|
min_ago_open=1000, min_ago_close=900, profit_rate=1.1,
|
|
|
|
))
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'NEO/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-12-07 14:45:02 +00:00
|
|
|
min_ago_open=1000, min_ago_close=900, profit_rate=1.1,
|
|
|
|
))
|
|
|
|
# No losing trade yet ... so max_drawdown will raise exception
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not freqtrade.protections.stop_per_pair('XRP/BTC')
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'XRP/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-11-30 18:07:39 +00:00
|
|
|
min_ago_open=500, min_ago_close=400, profit_rate=0.9,
|
|
|
|
))
|
|
|
|
# Not locked with one trade
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not freqtrade.protections.stop_per_pair('XRP/BTC')
|
|
|
|
assert not PairLocks.is_pair_locked('XRP/BTC')
|
|
|
|
assert not PairLocks.is_global_lock()
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'XRP/BTC', fee.return_value, False, sell_reason=ExitType.STOP_LOSS.value,
|
2020-11-30 18:07:39 +00:00
|
|
|
min_ago_open=1200, min_ago_close=1100, profit_rate=0.5,
|
|
|
|
))
|
|
|
|
|
|
|
|
# Not locked with 1 trade (2nd trade is outside of lookback_period)
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not freqtrade.protections.stop_per_pair('XRP/BTC')
|
|
|
|
assert not PairLocks.is_pair_locked('XRP/BTC')
|
|
|
|
assert not PairLocks.is_global_lock()
|
|
|
|
assert not log_has_re(message, caplog)
|
|
|
|
|
|
|
|
# Winning trade ... (should not lock, does not change drawdown!)
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'XRP/BTC', fee.return_value, False, sell_reason=ExitType.ROI.value,
|
2020-11-30 18:07:39 +00:00
|
|
|
min_ago_open=320, min_ago_close=410, profit_rate=1.5,
|
|
|
|
))
|
|
|
|
assert not freqtrade.protections.global_stop()
|
|
|
|
assert not PairLocks.is_global_lock()
|
|
|
|
|
|
|
|
caplog.clear()
|
|
|
|
|
|
|
|
# Add additional negative trade, causing a loss of > 15%
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(generate_mock_trade(
|
2022-03-25 05:55:37 +00:00
|
|
|
'XRP/BTC', fee.return_value, False, sell_reason=ExitType.ROI.value,
|
2020-11-30 18:07:39 +00:00
|
|
|
min_ago_open=20, min_ago_close=10, profit_rate=0.8,
|
|
|
|
))
|
|
|
|
assert not freqtrade.protections.stop_per_pair('XRP/BTC')
|
|
|
|
# local lock not supported
|
|
|
|
assert not PairLocks.is_pair_locked('XRP/BTC')
|
|
|
|
assert freqtrade.protections.global_stop()
|
|
|
|
assert PairLocks.is_global_lock()
|
|
|
|
assert log_has_re(message, caplog)
|
|
|
|
|
|
|
|
|
2020-10-15 06:07:09 +00:00
|
|
|
@pytest.mark.parametrize("protectionconf,desc_expected,exception_expected", [
|
2020-12-07 09:21:03 +00:00
|
|
|
({"method": "StoplossGuard", "lookback_period": 60, "trade_limit": 2, "stop_duration": 60},
|
2020-10-15 06:07:09 +00:00
|
|
|
"[{'StoplossGuard': 'StoplossGuard - Frequent Stoploss Guard, "
|
|
|
|
"2 stoplosses within 60 minutes.'}]",
|
|
|
|
None
|
|
|
|
),
|
2020-12-07 09:21:03 +00:00
|
|
|
({"method": "CooldownPeriod", "stop_duration": 60},
|
2020-12-07 10:08:54 +00:00
|
|
|
"[{'CooldownPeriod': 'CooldownPeriod - Cooldown period of 60 minutes.'}]",
|
2020-10-24 18:25:40 +00:00
|
|
|
None
|
|
|
|
),
|
2020-12-07 09:21:03 +00:00
|
|
|
({"method": "LowProfitPairs", "lookback_period": 60, "stop_duration": 60},
|
2020-11-11 20:11:22 +00:00
|
|
|
"[{'LowProfitPairs': 'LowProfitPairs - Low Profit Protection, locks pairs with "
|
|
|
|
"profit < 0.0 within 60 minutes.'}]",
|
|
|
|
None
|
|
|
|
),
|
2020-12-07 09:21:03 +00:00
|
|
|
({"method": "MaxDrawdown", "lookback_period": 60, "stop_duration": 60},
|
2020-11-30 18:07:39 +00:00
|
|
|
"[{'MaxDrawdown': 'MaxDrawdown - Max drawdown protection, stop trading if drawdown is > 0.0 "
|
|
|
|
"within 60 minutes.'}]",
|
|
|
|
None
|
|
|
|
),
|
2020-12-07 10:08:54 +00:00
|
|
|
({"method": "StoplossGuard", "lookback_period_candles": 12, "trade_limit": 2,
|
|
|
|
"stop_duration": 60},
|
|
|
|
"[{'StoplossGuard': 'StoplossGuard - Frequent Stoploss Guard, "
|
|
|
|
"2 stoplosses within 12 candles.'}]",
|
|
|
|
None
|
|
|
|
),
|
|
|
|
({"method": "CooldownPeriod", "stop_duration_candles": 5},
|
|
|
|
"[{'CooldownPeriod': 'CooldownPeriod - Cooldown period of 5 candles.'}]",
|
|
|
|
None
|
|
|
|
),
|
|
|
|
({"method": "LowProfitPairs", "lookback_period_candles": 11, "stop_duration": 60},
|
|
|
|
"[{'LowProfitPairs': 'LowProfitPairs - Low Profit Protection, locks pairs with "
|
|
|
|
"profit < 0.0 within 11 candles.'}]",
|
|
|
|
None
|
|
|
|
),
|
|
|
|
({"method": "MaxDrawdown", "lookback_period_candles": 20, "stop_duration": 60},
|
|
|
|
"[{'MaxDrawdown': 'MaxDrawdown - Max drawdown protection, stop trading if drawdown is > 0.0 "
|
|
|
|
"within 20 candles.'}]",
|
|
|
|
None
|
|
|
|
),
|
2020-10-15 06:07:09 +00:00
|
|
|
])
|
|
|
|
def test_protection_manager_desc(mocker, default_conf, protectionconf,
|
|
|
|
desc_expected, exception_expected):
|
|
|
|
|
|
|
|
default_conf['protections'] = [protectionconf]
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
short_desc = str(freqtrade.protections.short_desc())
|
|
|
|
assert short_desc == desc_expected
|