2018-07-17 07:21:53 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, C0103
|
|
|
|
import logging
|
2020-08-24 09:09:09 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2021-06-30 04:43:49 +00:00
|
|
|
from pathlib import Path
|
2018-07-17 07:21:53 +00:00
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
2020-02-22 10:52:39 +00:00
|
|
|
import pytest
|
2018-07-17 07:21:53 +00:00
|
|
|
from pandas import DataFrame
|
|
|
|
|
2019-07-11 18:23:23 +00:00
|
|
|
from freqtrade.configuration import TimeRange
|
2023-04-12 04:30:03 +00:00
|
|
|
from freqtrade.constants import CUSTOM_TAG_MAX_LENGTH
|
2020-08-24 09:09:09 +00:00
|
|
|
from freqtrade.data.dataprovider import DataProvider
|
2019-12-27 09:44:08 +00:00
|
|
|
from freqtrade.data.history import load_data
|
2022-09-09 18:31:30 +00:00
|
|
|
from freqtrade.enums import ExitCheckTuple, ExitType, HyperoptState, SignalDirection
|
2021-03-27 10:26:26 +00:00
|
|
|
from freqtrade.exceptions import OperationalException, StrategyError
|
2022-08-19 13:49:31 +00:00
|
|
|
from freqtrade.optimize.hyperopt_tools import HyperoptStateContainer
|
2021-07-03 07:08:52 +00:00
|
|
|
from freqtrade.optimize.space import SKDecimal
|
2020-10-25 09:54:30 +00:00
|
|
|
from freqtrade.persistence import PairLocks, Trade
|
2020-02-11 22:39:15 +00:00
|
|
|
from freqtrade.resolvers import StrategyResolver
|
2022-05-30 05:22:16 +00:00
|
|
|
from freqtrade.strategy.hyper import detect_parameters
|
2022-05-23 18:18:09 +00:00
|
|
|
from freqtrade.strategy.parameters import (BaseParameter, BooleanParameter, CategoricalParameter,
|
|
|
|
DecimalParameter, IntParameter, RealParameter)
|
2020-02-22 10:52:39 +00:00
|
|
|
from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper
|
2023-05-14 08:32:18 +00:00
|
|
|
from freqtrade.util import dt_now
|
2022-06-09 17:36:15 +00:00
|
|
|
from tests.conftest import (CURRENT_TEST_STRATEGY, TRADE_SIDES, create_mock_trades, log_has,
|
|
|
|
log_has_re)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
2021-09-21 17:14:14 +00:00
|
|
|
from .strats.strategy_test_v3 import StrategyTestV3
|
2018-07-17 07:21:53 +00:00
|
|
|
|
2020-09-28 17:43:15 +00:00
|
|
|
|
2018-07-17 07:21:53 +00:00
|
|
|
# Avoid to reinit the same object again and again
|
2021-09-21 17:14:14 +00:00
|
|
|
_STRATEGY = StrategyTestV3(config={})
|
2020-06-13 17:40:58 +00:00
|
|
|
_STRATEGY.dp = DataProvider({}, None, None)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
2021-09-04 18:23:51 +00:00
|
|
|
def test_returns_latest_signal(ohlcv_history):
|
2023-05-14 08:32:18 +00:00
|
|
|
ohlcv_history.loc[1, 'date'] = dt_now()
|
2020-03-29 09:27:40 +00:00
|
|
|
# Take a copy to correctly modify the call
|
|
|
|
mocked_history = ohlcv_history.copy()
|
2021-09-04 18:23:51 +00:00
|
|
|
mocked_history['enter_long'] = 0
|
|
|
|
mocked_history['exit_long'] = 0
|
|
|
|
mocked_history['enter_short'] = 0
|
|
|
|
mocked_history['exit_short'] = 0
|
2022-01-16 07:04:39 +00:00
|
|
|
# Set tags in lines that don't matter to test nan in the sell line
|
2022-01-22 16:25:21 +00:00
|
|
|
mocked_history.loc[0, 'enter_tag'] = 'wrong_line'
|
2022-01-16 07:04:39 +00:00
|
|
|
mocked_history.loc[0, 'exit_tag'] = 'wrong_line'
|
2021-09-04 18:23:51 +00:00
|
|
|
mocked_history.loc[1, 'exit_long'] = 1
|
|
|
|
|
|
|
|
assert _STRATEGY.get_entry_signal('ETH/BTC', '5m', mocked_history) == (None, None)
|
2021-11-06 14:24:52 +00:00
|
|
|
assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (False, True, None)
|
|
|
|
assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history, True) == (False, False, None)
|
2021-09-04 18:23:51 +00:00
|
|
|
mocked_history.loc[1, 'exit_long'] = 0
|
|
|
|
mocked_history.loc[1, 'enter_long'] = 1
|
|
|
|
|
2021-09-08 07:27:08 +00:00
|
|
|
assert _STRATEGY.get_entry_signal(
|
|
|
|
'ETH/BTC', '5m', mocked_history) == (SignalDirection.LONG, None)
|
2021-11-06 14:24:52 +00:00
|
|
|
assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (True, False, None)
|
|
|
|
assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history, True) == (False, False, None)
|
2021-09-04 18:23:51 +00:00
|
|
|
mocked_history.loc[1, 'exit_long'] = 0
|
|
|
|
mocked_history.loc[1, 'enter_long'] = 0
|
|
|
|
|
|
|
|
assert _STRATEGY.get_entry_signal('ETH/BTC', '5m', mocked_history) == (None, None)
|
2021-11-06 14:24:52 +00:00
|
|
|
assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (False, False, None)
|
|
|
|
assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history, True) == (False, False, None)
|
2021-09-04 18:23:51 +00:00
|
|
|
mocked_history.loc[1, 'exit_long'] = 0
|
|
|
|
mocked_history.loc[1, 'enter_long'] = 1
|
2021-09-26 13:20:59 +00:00
|
|
|
mocked_history.loc[1, 'enter_tag'] = 'buy_signal_01'
|
2018-07-17 07:21:53 +00:00
|
|
|
|
2021-09-04 18:23:51 +00:00
|
|
|
assert _STRATEGY.get_entry_signal(
|
|
|
|
'ETH/BTC', '5m', mocked_history) == (SignalDirection.LONG, 'buy_signal_01')
|
2021-11-06 14:24:52 +00:00
|
|
|
assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (True, False, None)
|
|
|
|
assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history, True) == (False, False, None)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
2021-09-04 18:23:51 +00:00
|
|
|
mocked_history.loc[1, 'exit_long'] = 0
|
|
|
|
mocked_history.loc[1, 'enter_long'] = 0
|
|
|
|
mocked_history.loc[1, 'enter_short'] = 1
|
|
|
|
mocked_history.loc[1, 'exit_short'] = 0
|
2021-09-26 13:20:59 +00:00
|
|
|
mocked_history.loc[1, 'enter_tag'] = 'sell_signal_01'
|
|
|
|
|
2022-02-21 18:19:12 +00:00
|
|
|
# Don't provide short signal while in spot mode
|
|
|
|
assert _STRATEGY.get_entry_signal('ETH/BTC', '5m', mocked_history) == (None, None)
|
|
|
|
|
|
|
|
_STRATEGY.config['trading_mode'] = 'futures'
|
2022-03-11 18:43:00 +00:00
|
|
|
# Short signal get's ignored as can_short is not set.
|
2022-03-12 07:58:54 +00:00
|
|
|
assert _STRATEGY.get_entry_signal('ETH/BTC', '5m', mocked_history) == (None, None)
|
2022-03-11 18:43:00 +00:00
|
|
|
|
|
|
|
_STRATEGY.can_short = True
|
|
|
|
|
2021-09-04 18:23:51 +00:00
|
|
|
assert _STRATEGY.get_entry_signal(
|
2021-09-26 13:20:59 +00:00
|
|
|
'ETH/BTC', '5m', mocked_history) == (SignalDirection.SHORT, 'sell_signal_01')
|
2021-11-06 14:24:52 +00:00
|
|
|
assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (False, False, None)
|
|
|
|
assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history, True) == (True, False, None)
|
2021-07-20 09:14:48 +00:00
|
|
|
|
2021-09-04 18:23:51 +00:00
|
|
|
mocked_history.loc[1, 'enter_short'] = 0
|
|
|
|
mocked_history.loc[1, 'exit_short'] = 1
|
2021-11-06 14:24:52 +00:00
|
|
|
mocked_history.loc[1, 'exit_tag'] = 'sell_signal_02'
|
2021-09-04 18:23:51 +00:00
|
|
|
assert _STRATEGY.get_entry_signal(
|
|
|
|
'ETH/BTC', '5m', mocked_history) == (None, None)
|
2021-11-06 14:24:52 +00:00
|
|
|
assert _STRATEGY.get_exit_signal(
|
|
|
|
'ETH/BTC', '5m', mocked_history) == (False, False, 'sell_signal_02')
|
|
|
|
assert _STRATEGY.get_exit_signal(
|
|
|
|
'ETH/BTC', '5m', mocked_history, True) == (False, True, 'sell_signal_02')
|
2020-06-13 18:04:15 +00:00
|
|
|
|
2022-03-11 18:43:00 +00:00
|
|
|
_STRATEGY.can_short = False
|
2022-02-21 18:19:12 +00:00
|
|
|
_STRATEGY.config['trading_mode'] = 'spot'
|
|
|
|
|
2020-06-13 18:04:15 +00:00
|
|
|
|
2024-01-14 12:51:26 +00:00
|
|
|
def test_analyze_pair_empty(mocker, caplog, ohlcv_history):
|
2020-06-13 18:04:15 +00:00
|
|
|
mocker.patch.object(_STRATEGY.dp, 'ohlcv', return_value=ohlcv_history)
|
2018-07-17 07:21:53 +00:00
|
|
|
mocker.patch.object(
|
2019-08-04 10:55:03 +00:00
|
|
|
_STRATEGY, '_analyze_ticker_internal',
|
2020-06-13 18:04:15 +00:00
|
|
|
return_value=DataFrame([])
|
2018-07-17 07:21:53 +00:00
|
|
|
)
|
2020-06-13 18:04:15 +00:00
|
|
|
mocker.patch.object(_STRATEGY, 'assert_df')
|
|
|
|
|
|
|
|
_STRATEGY.analyze_pair('ETH/BTC')
|
|
|
|
|
|
|
|
assert log_has('Empty dataframe for pair ETH/BTC', caplog)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
2021-08-25 04:43:58 +00:00
|
|
|
def test_get_signal_empty(default_conf, caplog):
|
|
|
|
assert (None, None) == _STRATEGY.get_latest_candle(
|
2021-07-21 19:00:51 +00:00
|
|
|
'foo', default_conf['timeframe'], DataFrame()
|
|
|
|
)
|
2020-03-08 10:35:31 +00:00
|
|
|
assert log_has('Empty candle (OHLCV) data for pair foo', caplog)
|
2018-12-12 18:35:51 +00:00
|
|
|
caplog.clear()
|
|
|
|
|
2021-08-25 04:43:58 +00:00
|
|
|
assert (None, None) == _STRATEGY.get_latest_candle('bar', default_conf['timeframe'], None)
|
2020-03-08 10:35:31 +00:00
|
|
|
assert log_has('Empty candle (OHLCV) data for pair bar', caplog)
|
2020-06-18 05:03:30 +00:00
|
|
|
caplog.clear()
|
|
|
|
|
2021-08-25 04:43:58 +00:00
|
|
|
assert (None, None) == _STRATEGY.get_latest_candle(
|
2021-07-20 16:56:03 +00:00
|
|
|
'baz',
|
|
|
|
default_conf['timeframe'],
|
|
|
|
DataFrame([])
|
|
|
|
)
|
2020-06-18 05:03:30 +00:00
|
|
|
assert log_has('Empty candle (OHLCV) data for pair baz', caplog)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
2021-08-25 04:43:58 +00:00
|
|
|
def test_get_signal_exception_valueerror(mocker, caplog, ohlcv_history):
|
2018-07-17 07:21:53 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2020-06-13 17:40:58 +00:00
|
|
|
mocker.patch.object(_STRATEGY.dp, 'ohlcv', return_value=ohlcv_history)
|
2018-07-17 07:21:53 +00:00
|
|
|
mocker.patch.object(
|
2019-08-04 10:55:03 +00:00
|
|
|
_STRATEGY, '_analyze_ticker_internal',
|
2018-07-17 07:21:53 +00:00
|
|
|
side_effect=ValueError('xyz')
|
|
|
|
)
|
2020-06-13 17:40:58 +00:00
|
|
|
_STRATEGY.analyze_pair('foo')
|
2020-02-06 19:26:04 +00:00
|
|
|
assert log_has_re(r'Strategy caused the following exception: xyz.*', caplog)
|
2020-06-13 17:40:58 +00:00
|
|
|
caplog.clear()
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
mocker.patch.object(
|
2020-06-13 17:40:58 +00:00
|
|
|
_STRATEGY, 'analyze_ticker',
|
|
|
|
side_effect=Exception('invalid ticker history ')
|
2018-07-17 07:21:53 +00:00
|
|
|
)
|
2020-06-13 17:40:58 +00:00
|
|
|
_STRATEGY.analyze_pair('foo')
|
2020-02-06 19:26:04 +00:00
|
|
|
assert log_has_re(r'Strategy caused the following exception: xyz.*', caplog)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_get_signal_old_dataframe(default_conf, mocker, caplog, ohlcv_history):
|
2018-07-17 07:21:53 +00:00
|
|
|
# default_conf defines a 5m interval. we check interval * 2 + 5m
|
|
|
|
# this is necessary as the last candle is removed (partial candles) by default
|
2023-05-14 08:32:18 +00:00
|
|
|
ohlcv_history.loc[1, 'date'] = dt_now() - timedelta(minutes=16)
|
2020-03-29 09:29:31 +00:00
|
|
|
# Take a copy to correctly modify the call
|
|
|
|
mocked_history = ohlcv_history.copy()
|
2021-08-25 04:43:58 +00:00
|
|
|
mocked_history['exit_long'] = 0
|
|
|
|
mocked_history['enter_long'] = 0
|
|
|
|
mocked_history.loc[1, 'enter_long'] = 1
|
2020-03-29 09:29:31 +00:00
|
|
|
|
|
|
|
caplog.set_level(logging.INFO)
|
2020-03-29 09:27:40 +00:00
|
|
|
mocker.patch.object(_STRATEGY, 'assert_df')
|
2020-06-13 17:40:58 +00:00
|
|
|
|
2021-08-25 04:43:58 +00:00
|
|
|
assert (None, None) == _STRATEGY.get_latest_candle(
|
2021-07-20 16:56:03 +00:00
|
|
|
'xyz',
|
|
|
|
default_conf['timeframe'],
|
|
|
|
mocked_history
|
|
|
|
)
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('Outdated history for pair xyz. Last tick is 16 minutes old', caplog)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
2021-08-02 18:17:58 +00:00
|
|
|
def test_get_signal_no_sell_column(default_conf, mocker, caplog, ohlcv_history):
|
|
|
|
# default_conf defines a 5m interval. we check interval * 2 + 5m
|
|
|
|
# this is necessary as the last candle is removed (partial candles) by default
|
2023-05-14 08:32:18 +00:00
|
|
|
ohlcv_history.loc[1, 'date'] = dt_now()
|
2021-08-02 18:17:58 +00:00
|
|
|
# Take a copy to correctly modify the call
|
|
|
|
mocked_history = ohlcv_history.copy()
|
|
|
|
# Intentionally don't set sell column
|
|
|
|
# mocked_history['sell'] = 0
|
2021-08-25 04:43:58 +00:00
|
|
|
mocked_history['enter_long'] = 0
|
|
|
|
mocked_history.loc[1, 'enter_long'] = 1
|
2021-08-02 18:17:58 +00:00
|
|
|
|
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
mocker.patch.object(_STRATEGY, 'assert_df')
|
|
|
|
|
2021-08-25 04:43:58 +00:00
|
|
|
assert (SignalDirection.LONG, None) == _STRATEGY.get_entry_signal(
|
2021-08-02 18:17:58 +00:00
|
|
|
'xyz',
|
|
|
|
default_conf['timeframe'],
|
|
|
|
mocked_history
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-01-07 06:51:49 +00:00
|
|
|
def test_ignore_expired_candle(default_conf):
|
2021-01-04 19:49:24 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
strategy.ignore_buying_expired_candle_after = 60
|
|
|
|
|
2021-01-07 06:51:49 +00:00
|
|
|
latest_date = datetime(2020, 12, 30, 7, 0, 0, tzinfo=timezone.utc)
|
|
|
|
# Add 1 candle length as the "latest date" defines candle open.
|
|
|
|
current_time = latest_date + timedelta(seconds=80 + 300)
|
2021-01-04 19:49:24 +00:00
|
|
|
|
2021-08-18 12:03:44 +00:00
|
|
|
assert strategy.ignore_expired_candle(
|
|
|
|
latest_date=latest_date,
|
|
|
|
current_time=current_time,
|
|
|
|
timeframe_seconds=300,
|
|
|
|
enter=True
|
|
|
|
) is True
|
2021-01-04 19:49:24 +00:00
|
|
|
|
2021-01-07 06:51:49 +00:00
|
|
|
current_time = latest_date + timedelta(seconds=30 + 300)
|
|
|
|
|
2023-02-22 19:58:24 +00:00
|
|
|
assert strategy.ignore_expired_candle(
|
2021-08-18 12:03:44 +00:00
|
|
|
latest_date=latest_date,
|
|
|
|
current_time=current_time,
|
|
|
|
timeframe_seconds=300,
|
|
|
|
enter=True
|
2023-02-22 19:58:24 +00:00
|
|
|
) is not True
|
2021-01-07 06:51:49 +00:00
|
|
|
|
2021-01-04 19:49:24 +00:00
|
|
|
|
2020-12-19 16:59:49 +00:00
|
|
|
def test_assert_df_raise(mocker, caplog, ohlcv_history):
|
2023-05-14 08:32:18 +00:00
|
|
|
ohlcv_history.loc[1, 'date'] = dt_now() - timedelta(minutes=16)
|
2020-03-29 09:40:13 +00:00
|
|
|
# Take a copy to correctly modify the call
|
|
|
|
mocked_history = ohlcv_history.copy()
|
|
|
|
mocked_history['sell'] = 0
|
|
|
|
mocked_history['buy'] = 0
|
|
|
|
mocked_history.loc[1, 'buy'] = 1
|
|
|
|
|
|
|
|
caplog.set_level(logging.INFO)
|
2020-06-13 17:40:58 +00:00
|
|
|
mocker.patch.object(_STRATEGY.dp, 'ohlcv', return_value=ohlcv_history)
|
|
|
|
mocker.patch.object(_STRATEGY.dp, 'get_analyzed_dataframe', return_value=(mocked_history, 0))
|
2020-03-29 09:40:13 +00:00
|
|
|
mocker.patch.object(
|
|
|
|
_STRATEGY, 'assert_df',
|
2020-04-19 04:58:44 +00:00
|
|
|
side_effect=StrategyError('Dataframe returned...')
|
2020-03-29 09:40:13 +00:00
|
|
|
)
|
2020-06-13 17:40:58 +00:00
|
|
|
_STRATEGY.analyze_pair('xyz')
|
2020-03-29 09:44:36 +00:00
|
|
|
assert log_has('Unable to analyze candle (OHLCV) data for pair xyz: Dataframe returned...',
|
|
|
|
caplog)
|
|
|
|
|
|
|
|
|
2020-12-19 16:59:49 +00:00
|
|
|
def test_assert_df(ohlcv_history, caplog):
|
2020-12-15 19:49:46 +00:00
|
|
|
df_len = len(ohlcv_history) - 1
|
2021-09-21 17:14:14 +00:00
|
|
|
ohlcv_history.loc[:, 'enter_long'] = 0
|
|
|
|
ohlcv_history.loc[:, 'exit_long'] = 0
|
2020-03-29 09:44:36 +00:00
|
|
|
# Ensure it's running when passed correctly
|
|
|
|
_STRATEGY.assert_df(ohlcv_history, len(ohlcv_history),
|
2020-12-15 19:49:46 +00:00
|
|
|
ohlcv_history.loc[df_len, 'close'], ohlcv_history.loc[df_len, 'date'])
|
2020-03-29 09:44:36 +00:00
|
|
|
|
2020-04-19 04:58:44 +00:00
|
|
|
with pytest.raises(StrategyError, match=r"Dataframe returned from strategy.*length\."):
|
2020-03-29 09:44:36 +00:00
|
|
|
_STRATEGY.assert_df(ohlcv_history, len(ohlcv_history) + 1,
|
2020-12-15 19:49:46 +00:00
|
|
|
ohlcv_history.loc[df_len, 'close'], ohlcv_history.loc[df_len, 'date'])
|
2020-03-29 09:44:36 +00:00
|
|
|
|
2020-04-19 04:58:44 +00:00
|
|
|
with pytest.raises(StrategyError,
|
2020-03-29 09:44:36 +00:00
|
|
|
match=r"Dataframe returned from strategy.*last close price\."):
|
|
|
|
_STRATEGY.assert_df(ohlcv_history, len(ohlcv_history),
|
2020-12-15 19:59:58 +00:00
|
|
|
ohlcv_history.loc[df_len, 'close'] + 0.01,
|
|
|
|
ohlcv_history.loc[df_len, 'date'])
|
2020-04-19 04:58:44 +00:00
|
|
|
with pytest.raises(StrategyError,
|
2020-03-29 09:44:36 +00:00
|
|
|
match=r"Dataframe returned from strategy.*last date\."):
|
|
|
|
_STRATEGY.assert_df(ohlcv_history, len(ohlcv_history),
|
2020-12-15 19:49:46 +00:00
|
|
|
ohlcv_history.loc[df_len, 'close'], ohlcv_history.loc[0, 'date'])
|
2021-06-19 17:32:29 +00:00
|
|
|
with pytest.raises(StrategyError,
|
|
|
|
match=r"No dataframe returned \(return statement missing\?\)."):
|
|
|
|
_STRATEGY.assert_df(None, len(ohlcv_history),
|
|
|
|
ohlcv_history.loc[df_len, 'close'], ohlcv_history.loc[0, 'date'])
|
|
|
|
with pytest.raises(StrategyError,
|
2021-09-21 17:14:14 +00:00
|
|
|
match="enter_long/buy column not set."):
|
|
|
|
_STRATEGY.assert_df(ohlcv_history.drop('enter_long', axis=1), len(ohlcv_history),
|
2021-06-19 17:32:29 +00:00
|
|
|
ohlcv_history.loc[df_len, 'close'], ohlcv_history.loc[0, 'date'])
|
2020-03-29 09:40:13 +00:00
|
|
|
|
2020-05-29 17:37:18 +00:00
|
|
|
_STRATEGY.disable_dataframe_checks = True
|
|
|
|
caplog.clear()
|
|
|
|
_STRATEGY.assert_df(ohlcv_history, len(ohlcv_history),
|
2020-12-15 19:49:46 +00:00
|
|
|
ohlcv_history.loc[2, 'close'], ohlcv_history.loc[0, 'date'])
|
2020-05-29 17:37:18 +00:00
|
|
|
assert log_has_re(r"Dataframe returned from strategy.*last date\.", caplog)
|
2020-05-30 07:43:50 +00:00
|
|
|
# reset to avoid problems in other tests due to test leakage
|
2020-05-29 17:37:18 +00:00
|
|
|
_STRATEGY.disable_dataframe_checks = False
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
2021-08-09 12:53:18 +00:00
|
|
|
def test_advise_all_indicators(default_conf, testdatadir) -> None:
|
2020-02-11 22:39:15 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
2019-10-19 13:21:47 +00:00
|
|
|
timerange = TimeRange.parse_timerange('1510694220-1510700340')
|
2020-03-08 10:35:31 +00:00
|
|
|
data = load_data(testdatadir, '1m', ['UNITTEST/BTC'], timerange=timerange,
|
|
|
|
fill_up_missing=True)
|
2021-08-09 12:53:18 +00:00
|
|
|
processed = strategy.advise_all_indicators(data)
|
2022-09-30 04:44:19 +00:00
|
|
|
assert len(processed['UNITTEST/BTC']) == 103
|
2018-08-17 04:50:36 +00:00
|
|
|
|
|
|
|
|
2022-08-13 07:53:18 +00:00
|
|
|
def test_freqai_not_initialized(default_conf) -> None:
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
strategy.ft_bot_start()
|
|
|
|
with pytest.raises(OperationalException, match=r'freqAI is not enabled\.'):
|
|
|
|
strategy.freqai.start()
|
|
|
|
|
|
|
|
|
2021-08-09 12:53:18 +00:00
|
|
|
def test_advise_all_indicators_copy(mocker, default_conf, testdatadir) -> None:
|
2020-04-02 18:23:20 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
aimock = mocker.patch('freqtrade.strategy.interface.IStrategy.advise_indicators')
|
|
|
|
timerange = TimeRange.parse_timerange('1510694220-1510700340')
|
|
|
|
data = load_data(testdatadir, '1m', ['UNITTEST/BTC'], timerange=timerange,
|
|
|
|
fill_up_missing=True)
|
2021-08-09 12:53:18 +00:00
|
|
|
strategy.advise_all_indicators(data)
|
2020-04-02 18:23:20 +00:00
|
|
|
assert aimock.call_count == 1
|
|
|
|
# Ensure that a copy of the dataframe is passed to advice_indicators
|
|
|
|
assert aimock.call_args_list[0][0][0] is not data
|
2018-08-17 04:50:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_min_roi_reached(default_conf, fee) -> None:
|
|
|
|
|
2019-01-12 12:38:49 +00:00
|
|
|
# Use list to confirm sequence does not matter
|
2019-01-01 15:32:45 +00:00
|
|
|
min_roi_list = [{20: 0.05, 55: 0.01, 0: 0.1},
|
|
|
|
{0: 0.1, 20: 0.05, 55: 0.01}]
|
|
|
|
for roi in min_roi_list:
|
2020-02-11 22:39:15 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-01-01 15:32:45 +00:00
|
|
|
strategy.minimal_roi = roi
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
2023-05-14 08:32:18 +00:00
|
|
|
open_date=dt_now() - timedelta(hours=1),
|
2019-01-01 15:32:45 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2019-01-01 15:32:45 +00:00
|
|
|
open_rate=1,
|
|
|
|
)
|
|
|
|
|
2023-05-14 15:55:24 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, 0.02, dt_now() - timedelta(minutes=56))
|
|
|
|
assert strategy.min_roi_reached(trade, 0.12, dt_now() - timedelta(minutes=56))
|
2019-01-01 15:32:45 +00:00
|
|
|
|
2023-05-14 15:55:24 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, 0.04, dt_now() - timedelta(minutes=39))
|
|
|
|
assert strategy.min_roi_reached(trade, 0.06, dt_now() - timedelta(minutes=39))
|
2019-01-01 15:32:45 +00:00
|
|
|
|
2023-05-14 15:55:24 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, -0.01, dt_now() - timedelta(minutes=1))
|
|
|
|
assert strategy.min_roi_reached(trade, 0.02, dt_now() - timedelta(minutes=1))
|
2018-09-01 18:01:18 +00:00
|
|
|
|
|
|
|
|
2019-01-12 12:38:49 +00:00
|
|
|
def test_min_roi_reached2(default_conf, fee) -> None:
|
|
|
|
|
|
|
|
# test with ROI raising after last interval
|
|
|
|
min_roi_list = [{20: 0.07,
|
|
|
|
30: 0.05,
|
|
|
|
55: 0.30,
|
|
|
|
0: 0.1
|
|
|
|
},
|
|
|
|
{0: 0.1,
|
|
|
|
20: 0.07,
|
|
|
|
30: 0.05,
|
|
|
|
55: 0.30
|
|
|
|
},
|
|
|
|
]
|
|
|
|
for roi in min_roi_list:
|
2020-02-11 22:39:15 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-01-12 12:38:49 +00:00
|
|
|
strategy.minimal_roi = roi
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
2023-05-14 15:55:24 +00:00
|
|
|
open_date=dt_now() - timedelta(hours=1),
|
2019-01-12 12:38:49 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2019-01-12 12:38:49 +00:00
|
|
|
open_rate=1,
|
|
|
|
)
|
|
|
|
|
2023-05-14 15:55:24 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, 0.02, dt_now() - timedelta(minutes=56))
|
|
|
|
assert strategy.min_roi_reached(trade, 0.12, dt_now() - timedelta(minutes=56))
|
2019-01-12 12:38:49 +00:00
|
|
|
|
2023-05-14 15:55:24 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, 0.04, dt_now() - timedelta(minutes=39))
|
|
|
|
assert strategy.min_roi_reached(trade, 0.071, dt_now() - timedelta(minutes=39))
|
2019-01-12 12:38:49 +00:00
|
|
|
|
2023-05-14 15:55:24 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, 0.04, dt_now() - timedelta(minutes=26))
|
|
|
|
assert strategy.min_roi_reached(trade, 0.06, dt_now() - timedelta(minutes=26))
|
2019-01-12 12:38:49 +00:00
|
|
|
|
|
|
|
# Should not trigger with 20% profit since after 55 minutes only 30% is active.
|
2023-05-14 15:55:24 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, 0.20, dt_now() - timedelta(minutes=2))
|
|
|
|
assert strategy.min_roi_reached(trade, 0.31, dt_now() - timedelta(minutes=2))
|
2019-06-20 00:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_min_roi_reached3(default_conf, fee) -> None:
|
|
|
|
|
|
|
|
# test for issue #1948
|
|
|
|
min_roi = {20: 0.07,
|
|
|
|
30: 0.05,
|
|
|
|
55: 0.30,
|
|
|
|
}
|
2020-02-11 22:39:15 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-06-20 00:26:02 +00:00
|
|
|
strategy.minimal_roi = min_roi
|
|
|
|
trade = Trade(
|
2020-03-11 16:44:03 +00:00
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
amount=5,
|
2023-05-14 15:55:24 +00:00
|
|
|
open_date=dt_now() - timedelta(hours=1),
|
2020-03-11 16:44:03 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2020-03-11 16:44:03 +00:00
|
|
|
open_rate=1,
|
2019-06-20 00:26:02 +00:00
|
|
|
)
|
|
|
|
|
2023-05-14 15:55:24 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, 0.02, dt_now() - timedelta(minutes=56))
|
|
|
|
assert not strategy.min_roi_reached(trade, 0.12, dt_now() - timedelta(minutes=56))
|
2019-06-20 00:26:02 +00:00
|
|
|
|
2023-05-14 15:55:24 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, 0.04, dt_now() - timedelta(minutes=39))
|
|
|
|
assert strategy.min_roi_reached(trade, 0.071, dt_now() - timedelta(minutes=39))
|
2019-06-20 00:26:02 +00:00
|
|
|
|
2023-05-14 15:55:24 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, 0.04, dt_now() - timedelta(minutes=26))
|
|
|
|
assert strategy.min_roi_reached(trade, 0.06, dt_now() - timedelta(minutes=26))
|
2019-06-20 00:26:02 +00:00
|
|
|
|
|
|
|
# Should not trigger with 20% profit since after 55 minutes only 30% is active.
|
2023-05-14 15:55:24 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, 0.20, dt_now() - timedelta(minutes=2))
|
|
|
|
assert strategy.min_roi_reached(trade, 0.31, dt_now() - timedelta(minutes=2))
|
2019-01-12 12:38:49 +00:00
|
|
|
|
|
|
|
|
2020-12-19 19:22:32 +00:00
|
|
|
@pytest.mark.parametrize(
|
2022-07-30 15:28:16 +00:00
|
|
|
'profit,adjusted,expected,liq,trailing,custom,profit2,adjusted2,expected2,custom_stop', [
|
2020-12-19 19:22:32 +00:00
|
|
|
# Profit, adjusted stoploss(absolute), profit for 2nd call, enable trailing,
|
|
|
|
# enable custom stoploss, expected after 1st call, expected after 2nd call
|
2022-07-30 15:28:16 +00:00
|
|
|
(0.2, 0.9, ExitType.NONE, None, False, False, 0.3, 0.9, ExitType.NONE, None),
|
|
|
|
(0.2, 0.9, ExitType.NONE, None, False, False, -0.2, 0.9, ExitType.STOP_LOSS, None),
|
2023-10-22 07:24:28 +00:00
|
|
|
(0.2, 0.9, ExitType.NONE, 0.92, False, False, -0.09, 0.9, ExitType.LIQUIDATION, None),
|
2022-07-30 15:28:16 +00:00
|
|
|
(0.2, 1.14, ExitType.NONE, None, True, False, 0.05, 1.14, ExitType.TRAILING_STOP_LOSS,
|
|
|
|
None),
|
|
|
|
(0.01, 0.96, ExitType.NONE, None, True, False, 0.05, 1, ExitType.NONE, None),
|
|
|
|
(0.05, 1, ExitType.NONE, None, True, False, -0.01, 1, ExitType.TRAILING_STOP_LOSS, None),
|
2020-12-19 19:22:32 +00:00
|
|
|
# Default custom case - trails with 10%
|
2022-07-30 15:28:16 +00:00
|
|
|
(0.05, 0.95, ExitType.NONE, None, False, True, -0.02, 0.95, ExitType.NONE, None),
|
|
|
|
(0.05, 0.95, ExitType.NONE, None, False, True, -0.06, 0.95, ExitType.TRAILING_STOP_LOSS,
|
|
|
|
None),
|
|
|
|
(0.05, 1, ExitType.NONE, None, False, True, -0.06, 1, ExitType.TRAILING_STOP_LOSS,
|
2020-12-19 19:22:32 +00:00
|
|
|
lambda **kwargs: -0.05),
|
2022-07-30 15:28:16 +00:00
|
|
|
(0.05, 1, ExitType.NONE, None, False, True, 0.09, 1.04, ExitType.NONE,
|
2020-12-19 19:22:32 +00:00
|
|
|
lambda **kwargs: -0.05),
|
2022-07-30 15:28:16 +00:00
|
|
|
(0.05, 0.95, ExitType.NONE, None, False, True, 0.09, 0.98, ExitType.NONE,
|
2020-12-19 19:22:32 +00:00
|
|
|
lambda current_profit, **kwargs: -0.1 if current_profit < 0.6 else -(current_profit * 2)),
|
|
|
|
# Error case - static stoploss in place
|
2022-07-30 15:28:16 +00:00
|
|
|
(0.05, 0.9, ExitType.NONE, None, False, True, 0.09, 0.9, ExitType.NONE,
|
2020-12-19 19:22:32 +00:00
|
|
|
lambda **kwargs: None),
|
|
|
|
])
|
2023-02-13 19:08:54 +00:00
|
|
|
def test_ft_stoploss_reached(default_conf, fee, profit, adjusted, expected, liq, trailing, custom,
|
|
|
|
profit2, adjusted2, expected2, custom_stop) -> None:
|
2020-12-19 19:08:03 +00:00
|
|
|
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.01,
|
|
|
|
amount=1,
|
2023-05-14 15:55:24 +00:00
|
|
|
open_date=dt_now() - timedelta(hours=1),
|
2020-12-19 19:08:03 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2020-12-19 19:08:03 +00:00
|
|
|
open_rate=1,
|
2022-07-30 15:28:16 +00:00
|
|
|
liquidation_price=liq,
|
2020-12-19 19:08:03 +00:00
|
|
|
)
|
2021-08-09 17:38:56 +00:00
|
|
|
trade.adjust_min_max_rates(trade.open_rate, trade.open_rate)
|
2020-12-19 19:08:03 +00:00
|
|
|
strategy.trailing_stop = trailing
|
|
|
|
strategy.trailing_stop_positive = -0.05
|
2020-12-20 10:12:22 +00:00
|
|
|
strategy.use_custom_stoploss = custom
|
2020-12-20 10:17:50 +00:00
|
|
|
original_stopvalue = strategy.custom_stoploss
|
2020-12-19 19:22:32 +00:00
|
|
|
if custom_stop:
|
2020-12-20 10:17:50 +00:00
|
|
|
strategy.custom_stoploss = custom_stop
|
2020-12-19 19:08:03 +00:00
|
|
|
|
2023-05-14 09:10:21 +00:00
|
|
|
now = dt_now()
|
2022-02-12 14:21:36 +00:00
|
|
|
current_rate = trade.open_rate * (1 + profit)
|
2023-02-13 19:08:54 +00:00
|
|
|
sl_flag = strategy.ft_stoploss_reached(current_rate=current_rate, trade=trade,
|
|
|
|
current_time=now, current_profit=profit,
|
|
|
|
force_stoploss=0, high=None)
|
2022-03-25 05:46:29 +00:00
|
|
|
assert isinstance(sl_flag, ExitCheckTuple)
|
|
|
|
assert sl_flag.exit_type == expected
|
2022-03-25 05:55:37 +00:00
|
|
|
if expected == ExitType.NONE:
|
2022-03-25 05:46:29 +00:00
|
|
|
assert sl_flag.exit_flag is False
|
2020-12-19 19:08:03 +00:00
|
|
|
else:
|
2022-03-25 05:46:29 +00:00
|
|
|
assert sl_flag.exit_flag is True
|
2020-12-19 19:08:03 +00:00
|
|
|
assert round(trade.stop_loss, 2) == adjusted
|
2022-02-12 14:21:36 +00:00
|
|
|
current_rate2 = trade.open_rate * (1 + profit2)
|
2020-12-19 19:08:03 +00:00
|
|
|
|
2023-02-13 19:08:54 +00:00
|
|
|
sl_flag = strategy.ft_stoploss_reached(current_rate=current_rate2, trade=trade,
|
|
|
|
current_time=now, current_profit=profit2,
|
|
|
|
force_stoploss=0, high=None)
|
2022-03-25 05:46:29 +00:00
|
|
|
assert sl_flag.exit_type == expected2
|
2022-03-25 05:55:37 +00:00
|
|
|
if expected2 == ExitType.NONE:
|
2022-03-25 05:46:29 +00:00
|
|
|
assert sl_flag.exit_flag is False
|
2020-12-19 19:08:03 +00:00
|
|
|
else:
|
2022-03-25 05:46:29 +00:00
|
|
|
assert sl_flag.exit_flag is True
|
2020-12-19 19:08:03 +00:00
|
|
|
assert round(trade.stop_loss, 2) == adjusted2
|
|
|
|
|
2020-12-20 10:17:50 +00:00
|
|
|
strategy.custom_stoploss = original_stopvalue
|
2020-12-19 19:22:32 +00:00
|
|
|
|
2020-12-19 19:08:03 +00:00
|
|
|
|
2022-03-12 10:15:27 +00:00
|
|
|
def test_custom_exit(default_conf, fee, caplog) -> None:
|
2021-04-26 18:26:14 +00:00
|
|
|
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.01,
|
|
|
|
amount=1,
|
2023-05-14 15:55:24 +00:00
|
|
|
open_date=dt_now() - timedelta(hours=1),
|
2021-04-26 18:26:14 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=1,
|
2023-08-29 16:31:51 +00:00
|
|
|
leverage=1.0,
|
2021-04-26 18:26:14 +00:00
|
|
|
)
|
|
|
|
|
2023-05-14 09:10:21 +00:00
|
|
|
now = dt_now()
|
2021-08-24 17:55:00 +00:00
|
|
|
res = strategy.should_exit(trade, 1, now,
|
2021-08-25 04:43:58 +00:00
|
|
|
enter=False, exit_=False,
|
2021-08-24 17:55:00 +00:00
|
|
|
low=None, high=None)
|
2021-04-26 18:26:14 +00:00
|
|
|
|
2022-05-22 08:20:01 +00:00
|
|
|
assert res == []
|
2021-04-26 18:26:14 +00:00
|
|
|
|
2022-03-12 10:15:27 +00:00
|
|
|
strategy.custom_exit = MagicMock(return_value=True)
|
2021-08-24 17:55:00 +00:00
|
|
|
res = strategy.should_exit(trade, 1, now,
|
2021-08-25 04:43:58 +00:00
|
|
|
enter=False, exit_=False,
|
2021-08-24 17:55:00 +00:00
|
|
|
low=None, high=None)
|
2022-05-22 08:20:01 +00:00
|
|
|
assert res[0].exit_flag is True
|
|
|
|
assert res[0].exit_type == ExitType.CUSTOM_EXIT
|
|
|
|
assert res[0].exit_reason == 'custom_exit'
|
2021-04-26 18:26:14 +00:00
|
|
|
|
2022-03-12 10:15:27 +00:00
|
|
|
strategy.custom_exit = MagicMock(return_value='hello world')
|
2021-04-26 18:26:14 +00:00
|
|
|
|
2021-08-24 17:55:00 +00:00
|
|
|
res = strategy.should_exit(trade, 1, now,
|
2021-08-25 04:43:58 +00:00
|
|
|
enter=False, exit_=False,
|
2021-08-24 17:55:00 +00:00
|
|
|
low=None, high=None)
|
2022-05-22 08:20:01 +00:00
|
|
|
assert res[0].exit_type == ExitType.CUSTOM_EXIT
|
|
|
|
assert res[0].exit_flag is True
|
|
|
|
assert res[0].exit_reason == 'hello world'
|
2021-04-26 18:26:14 +00:00
|
|
|
|
|
|
|
caplog.clear()
|
2023-04-12 04:30:03 +00:00
|
|
|
strategy.custom_exit = MagicMock(return_value='h' * CUSTOM_TAG_MAX_LENGTH * 2)
|
2021-08-24 17:55:00 +00:00
|
|
|
res = strategy.should_exit(trade, 1, now,
|
2021-08-25 04:43:58 +00:00
|
|
|
enter=False, exit_=False,
|
2021-08-24 17:55:00 +00:00
|
|
|
low=None, high=None)
|
2022-05-22 08:20:01 +00:00
|
|
|
assert res[0].exit_type == ExitType.CUSTOM_EXIT
|
|
|
|
assert res[0].exit_flag is True
|
2023-04-12 04:30:03 +00:00
|
|
|
assert res[0].exit_reason == 'h' * (CUSTOM_TAG_MAX_LENGTH)
|
2022-04-10 06:37:35 +00:00
|
|
|
assert log_has_re('Custom exit reason returned from custom_exit is too long.*', caplog)
|
2021-04-26 18:26:14 +00:00
|
|
|
|
|
|
|
|
2022-05-22 09:01:18 +00:00
|
|
|
def test_should_sell(default_conf, fee) -> None:
|
2022-05-22 08:31:29 +00:00
|
|
|
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.01,
|
|
|
|
amount=1,
|
2023-05-14 15:55:24 +00:00
|
|
|
open_date=dt_now() - timedelta(hours=1),
|
2022-05-22 08:31:29 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=1,
|
2023-08-29 16:31:51 +00:00
|
|
|
leverage=1.0,
|
2022-05-22 08:31:29 +00:00
|
|
|
)
|
2023-05-14 09:10:21 +00:00
|
|
|
now = dt_now()
|
2022-05-22 08:31:29 +00:00
|
|
|
res = strategy.should_exit(trade, 1, now,
|
|
|
|
enter=False, exit_=False,
|
|
|
|
low=None, high=None)
|
|
|
|
|
|
|
|
assert res == []
|
|
|
|
strategy.min_roi_reached = MagicMock(return_value=True)
|
|
|
|
|
|
|
|
res = strategy.should_exit(trade, 1, now,
|
|
|
|
enter=False, exit_=False,
|
|
|
|
low=None, high=None)
|
|
|
|
assert len(res) == 1
|
|
|
|
assert res == [ExitCheckTuple(exit_type=ExitType.ROI)]
|
|
|
|
|
|
|
|
strategy.min_roi_reached = MagicMock(return_value=True)
|
2023-02-13 19:08:54 +00:00
|
|
|
strategy.ft_stoploss_reached = MagicMock(
|
2022-05-22 08:31:29 +00:00
|
|
|
return_value=ExitCheckTuple(exit_type=ExitType.STOP_LOSS))
|
|
|
|
|
|
|
|
res = strategy.should_exit(trade, 1, now,
|
|
|
|
enter=False, exit_=False,
|
|
|
|
low=None, high=None)
|
|
|
|
assert len(res) == 2
|
|
|
|
assert res == [
|
|
|
|
ExitCheckTuple(exit_type=ExitType.STOP_LOSS),
|
2022-05-22 09:01:18 +00:00
|
|
|
ExitCheckTuple(exit_type=ExitType.ROI),
|
2022-05-22 08:31:29 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
strategy.custom_exit = MagicMock(return_value='hello world')
|
2022-05-22 09:01:18 +00:00
|
|
|
# custom-exit and exit-signal is first
|
2022-05-22 08:31:29 +00:00
|
|
|
res = strategy.should_exit(trade, 1, now,
|
|
|
|
enter=False, exit_=False,
|
|
|
|
low=None, high=None)
|
|
|
|
assert len(res) == 3
|
|
|
|
assert res == [
|
|
|
|
ExitCheckTuple(exit_type=ExitType.CUSTOM_EXIT, exit_reason='hello world'),
|
|
|
|
ExitCheckTuple(exit_type=ExitType.STOP_LOSS),
|
2022-05-22 09:01:18 +00:00
|
|
|
ExitCheckTuple(exit_type=ExitType.ROI),
|
2022-05-22 08:31:29 +00:00
|
|
|
]
|
|
|
|
|
2023-02-13 19:08:54 +00:00
|
|
|
strategy.ft_stoploss_reached = MagicMock(
|
2022-05-22 09:01:18 +00:00
|
|
|
return_value=ExitCheckTuple(exit_type=ExitType.TRAILING_STOP_LOSS))
|
2022-05-22 08:31:29 +00:00
|
|
|
# Regular exit signal
|
|
|
|
res = strategy.should_exit(trade, 1, now,
|
|
|
|
enter=False, exit_=True,
|
|
|
|
low=None, high=None)
|
|
|
|
assert len(res) == 3
|
|
|
|
assert res == [
|
|
|
|
ExitCheckTuple(exit_type=ExitType.EXIT_SIGNAL),
|
|
|
|
ExitCheckTuple(exit_type=ExitType.ROI),
|
2022-05-22 09:01:18 +00:00
|
|
|
ExitCheckTuple(exit_type=ExitType.TRAILING_STOP_LOSS),
|
2022-05-22 08:31:29 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Regular exit signal, no ROI
|
|
|
|
strategy.min_roi_reached = MagicMock(return_value=False)
|
|
|
|
res = strategy.should_exit(trade, 1, now,
|
|
|
|
enter=False, exit_=True,
|
|
|
|
low=None, high=None)
|
|
|
|
assert len(res) == 2
|
|
|
|
assert res == [
|
|
|
|
ExitCheckTuple(exit_type=ExitType.EXIT_SIGNAL),
|
2022-05-22 09:01:18 +00:00
|
|
|
ExitCheckTuple(exit_type=ExitType.TRAILING_STOP_LOSS),
|
2022-05-22 08:31:29 +00:00
|
|
|
]
|
|
|
|
|
2022-05-22 09:01:18 +00:00
|
|
|
|
2021-09-22 18:14:52 +00:00
|
|
|
@pytest.mark.parametrize('side', TRADE_SIDES)
|
|
|
|
def test_leverage_callback(default_conf, side) -> None:
|
|
|
|
default_conf['strategy'] = 'StrategyTestV2'
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
|
|
|
|
assert strategy.leverage(
|
|
|
|
pair='XRP/USDT',
|
|
|
|
current_time=datetime.now(timezone.utc),
|
|
|
|
current_rate=2.2,
|
|
|
|
proposed_leverage=1.0,
|
|
|
|
max_leverage=5.0,
|
|
|
|
side=side,
|
2022-06-05 08:21:06 +00:00
|
|
|
entry_tag=None,
|
2021-09-22 18:14:52 +00:00
|
|
|
) == 1
|
|
|
|
|
|
|
|
default_conf['strategy'] = CURRENT_TEST_STRATEGY
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
assert strategy.leverage(
|
|
|
|
pair='XRP/USDT',
|
|
|
|
current_time=datetime.now(timezone.utc),
|
|
|
|
current_rate=2.2,
|
|
|
|
proposed_leverage=1.0,
|
|
|
|
max_leverage=5.0,
|
|
|
|
side=side,
|
2022-06-05 08:21:06 +00:00
|
|
|
entry_tag='entry_tag_test',
|
2021-09-22 18:14:52 +00:00
|
|
|
) == 3
|
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_analyze_ticker_default(ohlcv_history, mocker, caplog) -> None:
|
2018-08-09 18:07:01 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-08-09 18:02:24 +00:00
|
|
|
ind_mock = MagicMock(side_effect=lambda x, meta: x)
|
2021-09-22 18:42:31 +00:00
|
|
|
entry_mock = MagicMock(side_effect=lambda x, meta: x)
|
|
|
|
exit_mock = MagicMock(side_effect=lambda x, meta: x)
|
2018-08-09 18:02:24 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.strategy.interface.IStrategy',
|
|
|
|
advise_indicators=ind_mock,
|
2021-09-22 18:42:31 +00:00
|
|
|
advise_entry=entry_mock,
|
|
|
|
advise_exit=exit_mock,
|
2018-08-09 18:02:24 +00:00
|
|
|
|
|
|
|
)
|
2021-09-21 17:14:14 +00:00
|
|
|
strategy = StrategyTestV3({})
|
2020-03-08 10:35:31 +00:00
|
|
|
strategy.analyze_ticker(ohlcv_history, {'pair': 'ETH/BTC'})
|
2018-08-09 18:02:24 +00:00
|
|
|
assert ind_mock.call_count == 1
|
2021-09-22 18:42:31 +00:00
|
|
|
assert entry_mock.call_count == 1
|
|
|
|
assert entry_mock.call_count == 1
|
2018-08-09 18:02:24 +00:00
|
|
|
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('TA Analysis Launched', caplog)
|
|
|
|
assert not log_has('Skipping TA Analysis for already analyzed candle', caplog)
|
2018-08-09 18:07:01 +00:00
|
|
|
caplog.clear()
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
strategy.analyze_ticker(ohlcv_history, {'pair': 'ETH/BTC'})
|
2018-09-01 17:52:40 +00:00
|
|
|
# No analysis happens as process_only_new_candles is true
|
2018-08-09 18:02:24 +00:00
|
|
|
assert ind_mock.call_count == 2
|
2021-09-22 18:42:31 +00:00
|
|
|
assert entry_mock.call_count == 2
|
|
|
|
assert entry_mock.call_count == 2
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('TA Analysis Launched', caplog)
|
|
|
|
assert not log_has('Skipping TA Analysis for already analyzed candle', caplog)
|
2018-08-09 18:02:24 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test__analyze_ticker_internal_skip_analyze(ohlcv_history, mocker, caplog) -> None:
|
2018-08-09 18:07:01 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-08-09 18:02:24 +00:00
|
|
|
ind_mock = MagicMock(side_effect=lambda x, meta: x)
|
2021-09-22 18:42:31 +00:00
|
|
|
entry_mock = MagicMock(side_effect=lambda x, meta: x)
|
|
|
|
exit_mock = MagicMock(side_effect=lambda x, meta: x)
|
2018-08-09 18:02:24 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.strategy.interface.IStrategy',
|
|
|
|
advise_indicators=ind_mock,
|
2021-09-22 18:42:31 +00:00
|
|
|
advise_entry=entry_mock,
|
|
|
|
advise_exit=exit_mock,
|
2018-08-09 18:02:24 +00:00
|
|
|
|
|
|
|
)
|
2021-09-21 17:14:14 +00:00
|
|
|
strategy = StrategyTestV3({})
|
2020-06-13 17:40:58 +00:00
|
|
|
strategy.dp = DataProvider({}, None, None)
|
2018-09-01 17:52:40 +00:00
|
|
|
strategy.process_only_new_candles = True
|
2018-08-09 18:02:24 +00:00
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
ret = strategy._analyze_ticker_internal(ohlcv_history, {'pair': 'ETH/BTC'})
|
2018-12-02 15:03:34 +00:00
|
|
|
assert 'high' in ret.columns
|
|
|
|
assert 'low' in ret.columns
|
|
|
|
assert 'close' in ret.columns
|
|
|
|
assert isinstance(ret, DataFrame)
|
2018-08-09 18:02:24 +00:00
|
|
|
assert ind_mock.call_count == 1
|
2021-09-22 18:42:31 +00:00
|
|
|
assert entry_mock.call_count == 1
|
|
|
|
assert entry_mock.call_count == 1
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('TA Analysis Launched', caplog)
|
|
|
|
assert not log_has('Skipping TA Analysis for already analyzed candle', caplog)
|
2018-08-09 18:07:01 +00:00
|
|
|
caplog.clear()
|
2018-08-09 18:02:24 +00:00
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
ret = strategy._analyze_ticker_internal(ohlcv_history, {'pair': 'ETH/BTC'})
|
2018-09-01 17:52:40 +00:00
|
|
|
# No analysis happens as process_only_new_candles is true
|
2018-08-09 18:02:24 +00:00
|
|
|
assert ind_mock.call_count == 1
|
2021-09-22 18:42:31 +00:00
|
|
|
assert entry_mock.call_count == 1
|
|
|
|
assert entry_mock.call_count == 1
|
2018-08-09 18:02:24 +00:00
|
|
|
# only skipped analyze adds buy and sell columns, otherwise it's all mocked
|
2021-09-21 05:11:53 +00:00
|
|
|
assert 'enter_long' in ret.columns
|
|
|
|
assert 'exit_long' in ret.columns
|
|
|
|
assert ret['enter_long'].sum() == 0
|
|
|
|
assert ret['exit_long'].sum() == 0
|
2019-08-11 18:16:52 +00:00
|
|
|
assert not log_has('TA Analysis Launched', caplog)
|
|
|
|
assert log_has('Skipping TA Analysis for already analyzed candle', caplog)
|
2019-08-12 17:50:22 +00:00
|
|
|
|
|
|
|
|
2020-10-17 09:28:34 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2019-08-12 17:50:22 +00:00
|
|
|
def test_is_pair_locked(default_conf):
|
2020-10-26 06:36:25 +00:00
|
|
|
PairLocks.timeframe = default_conf['timeframe']
|
2021-08-08 08:57:20 +00:00
|
|
|
PairLocks.use_db = True
|
2020-02-11 22:39:15 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2020-10-17 09:28:34 +00:00
|
|
|
# No lock should be present
|
2020-10-25 09:54:30 +00:00
|
|
|
assert len(PairLocks.get_pair_locks(None)) == 0
|
2019-08-12 17:50:22 +00:00
|
|
|
|
|
|
|
pair = 'ETH/BTC'
|
|
|
|
assert not strategy.is_pair_locked(pair)
|
2023-05-14 16:14:35 +00:00
|
|
|
strategy.lock_pair(pair, dt_now() + timedelta(minutes=4))
|
2019-08-12 17:50:22 +00:00
|
|
|
# ETH/BTC locked for 4 minutes
|
|
|
|
assert strategy.is_pair_locked(pair)
|
|
|
|
|
|
|
|
# XRP/BTC should not be locked now
|
|
|
|
pair = 'XRP/BTC'
|
|
|
|
assert not strategy.is_pair_locked(pair)
|
2019-12-22 08:46:00 +00:00
|
|
|
|
|
|
|
# Unlocking a pair that's not locked should not raise an error
|
|
|
|
strategy.unlock_pair(pair)
|
|
|
|
|
|
|
|
# Unlock original pair
|
|
|
|
pair = 'ETH/BTC'
|
|
|
|
strategy.unlock_pair(pair)
|
|
|
|
assert not strategy.is_pair_locked(pair)
|
2020-02-22 10:52:39 +00:00
|
|
|
|
2021-10-30 07:39:40 +00:00
|
|
|
# Lock with reason
|
|
|
|
reason = "TestLockR"
|
2023-05-14 16:14:35 +00:00
|
|
|
strategy.lock_pair(pair, dt_now() + timedelta(minutes=4), reason)
|
2021-10-30 07:39:40 +00:00
|
|
|
assert strategy.is_pair_locked(pair)
|
|
|
|
strategy.unlock_reason(reason)
|
|
|
|
assert not strategy.is_pair_locked(pair)
|
|
|
|
|
2020-08-24 09:09:09 +00:00
|
|
|
pair = 'BTC/USDT'
|
|
|
|
# Lock until 14:30
|
|
|
|
lock_time = datetime(2020, 5, 1, 14, 30, 0, tzinfo=timezone.utc)
|
2020-10-26 06:37:07 +00:00
|
|
|
# Subtract 2 seconds, as locking rounds up to the next candle.
|
|
|
|
strategy.lock_pair(pair, lock_time - timedelta(seconds=2))
|
2020-10-17 09:28:34 +00:00
|
|
|
|
2020-08-24 09:09:09 +00:00
|
|
|
assert not strategy.is_pair_locked(pair)
|
|
|
|
# latest candle is from 14:20, lock goes to 14:30
|
2022-04-24 12:10:25 +00:00
|
|
|
assert strategy.is_pair_locked(pair, candle_date=lock_time + timedelta(minutes=-10))
|
|
|
|
assert strategy.is_pair_locked(pair, candle_date=lock_time + timedelta(minutes=-50))
|
2020-08-24 09:09:09 +00:00
|
|
|
|
|
|
|
# latest candle is from 14:25 (lock should be lifted)
|
|
|
|
# Since this is the "new candle" available at 14:30
|
2022-04-24 12:10:25 +00:00
|
|
|
assert not strategy.is_pair_locked(pair, candle_date=lock_time + timedelta(minutes=-4))
|
2020-08-24 09:09:09 +00:00
|
|
|
|
|
|
|
# Should not be locked after time expired
|
2022-04-24 12:10:25 +00:00
|
|
|
assert not strategy.is_pair_locked(pair, candle_date=lock_time + timedelta(minutes=10))
|
2020-08-24 09:09:09 +00:00
|
|
|
|
|
|
|
# Change timeframe to 15m
|
|
|
|
strategy.timeframe = '15m'
|
|
|
|
# Candle from 14:14 - lock goes until 14:30
|
2022-04-24 12:10:25 +00:00
|
|
|
assert strategy.is_pair_locked(pair, candle_date=lock_time + timedelta(minutes=-16))
|
|
|
|
assert strategy.is_pair_locked(pair, candle_date=lock_time + timedelta(minutes=-15, seconds=-2))
|
2020-08-24 09:09:09 +00:00
|
|
|
# Candle from 14:15 - lock goes until 14:30
|
2022-04-24 12:10:25 +00:00
|
|
|
assert not strategy.is_pair_locked(pair, candle_date=lock_time + timedelta(minutes=-15))
|
2020-08-24 09:09:09 +00:00
|
|
|
|
2020-02-22 10:52:39 +00:00
|
|
|
|
2020-06-14 05:00:55 +00:00
|
|
|
def test_is_informative_pairs_callback(default_conf):
|
2022-04-25 05:02:09 +00:00
|
|
|
default_conf.update({'strategy': 'StrategyTestV2'})
|
2020-06-14 05:00:55 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
# Should return empty
|
|
|
|
# Uses fallback to base implementation
|
2021-09-19 23:44:12 +00:00
|
|
|
assert [] == strategy.gather_informative_pairs()
|
2020-06-14 05:00:55 +00:00
|
|
|
|
|
|
|
|
2020-02-22 10:52:39 +00:00
|
|
|
@pytest.mark.parametrize('error', [
|
|
|
|
ValueError, KeyError, Exception,
|
|
|
|
])
|
|
|
|
def test_strategy_safe_wrapper_error(caplog, error):
|
|
|
|
def failing_method():
|
|
|
|
raise error('This is an error.')
|
|
|
|
|
|
|
|
with pytest.raises(StrategyError, match=r'This is an error.'):
|
|
|
|
strategy_safe_wrapper(failing_method, message='DeadBeef')()
|
|
|
|
|
|
|
|
assert log_has_re(r'DeadBeef.*', caplog)
|
|
|
|
ret = strategy_safe_wrapper(failing_method, message='DeadBeef', default_retval=True)()
|
|
|
|
|
|
|
|
assert isinstance(ret, bool)
|
|
|
|
assert ret
|
|
|
|
|
2020-06-14 05:15:24 +00:00
|
|
|
caplog.clear()
|
2021-08-16 12:16:24 +00:00
|
|
|
# Test suppressing error
|
2020-06-14 05:15:24 +00:00
|
|
|
ret = strategy_safe_wrapper(failing_method, message='DeadBeef', supress_error=True)()
|
|
|
|
assert log_has_re(r'DeadBeef.*', caplog)
|
|
|
|
|
2020-02-22 10:52:39 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('value', [
|
|
|
|
1, 22, 55, True, False, {'a': 1, 'b': '112'},
|
|
|
|
[1, 2, 3, 4], (4, 2, 3, 6)
|
|
|
|
])
|
|
|
|
def test_strategy_safe_wrapper(value):
|
|
|
|
|
|
|
|
def working_method(argumentpassedin):
|
|
|
|
return argumentpassedin
|
|
|
|
|
|
|
|
ret = strategy_safe_wrapper(working_method, message='DeadBeef')(value)
|
|
|
|
|
2021-10-21 14:25:38 +00:00
|
|
|
assert isinstance(ret, type(value))
|
2020-02-22 10:52:39 +00:00
|
|
|
assert ret == value
|
2021-03-27 10:26:26 +00:00
|
|
|
|
|
|
|
|
2022-06-09 17:36:15 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_strategy_safe_wrapper_trade_copy(fee):
|
|
|
|
create_mock_trades(fee)
|
|
|
|
|
|
|
|
def working_method(trade):
|
|
|
|
assert len(trade.orders) > 0
|
|
|
|
assert trade.orders
|
|
|
|
trade.orders = []
|
|
|
|
assert len(trade.orders) == 0
|
|
|
|
return trade
|
|
|
|
|
|
|
|
trade = Trade.get_open_trades()[0]
|
|
|
|
# Don't assert anything before strategy_wrapper.
|
|
|
|
# This ensures that relationship loading works correctly.
|
|
|
|
ret = strategy_safe_wrapper(working_method, message='DeadBeef')(trade=trade)
|
|
|
|
assert isinstance(ret, Trade)
|
|
|
|
assert id(trade) != id(ret)
|
|
|
|
# Did not modify the original order
|
|
|
|
assert len(trade.orders) > 0
|
|
|
|
assert len(ret.orders) == 0
|
|
|
|
|
|
|
|
|
2021-03-27 10:26:26 +00:00
|
|
|
def test_hyperopt_parameters():
|
2022-08-19 13:49:31 +00:00
|
|
|
HyperoptStateContainer.set_state(HyperoptState.INDICATORS)
|
2021-03-28 17:49:20 +00:00
|
|
|
from skopt.space import Categorical, Integer, Real
|
2022-08-19 13:49:31 +00:00
|
|
|
|
2021-03-27 10:26:26 +00:00
|
|
|
with pytest.raises(OperationalException, match=r"Name is determined.*"):
|
|
|
|
IntParameter(low=0, high=5, default=1, name='hello')
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r"IntParameter space must be.*"):
|
|
|
|
IntParameter(low=0, default=5, space='buy')
|
|
|
|
|
2021-04-01 07:17:39 +00:00
|
|
|
with pytest.raises(OperationalException, match=r"RealParameter space must be.*"):
|
|
|
|
RealParameter(low=0, default=5, space='buy')
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r"DecimalParameter space must be.*"):
|
|
|
|
DecimalParameter(low=0, default=5, space='buy')
|
2021-03-27 10:26:26 +00:00
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r"IntParameter space invalid\."):
|
|
|
|
IntParameter([0, 10], high=7, default=5, space='buy')
|
|
|
|
|
2021-04-01 07:17:39 +00:00
|
|
|
with pytest.raises(OperationalException, match=r"RealParameter space invalid\."):
|
|
|
|
RealParameter([0, 10], high=7, default=5, space='buy')
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r"DecimalParameter space invalid\."):
|
|
|
|
DecimalParameter([0, 10], high=7, default=5, space='buy')
|
2021-03-27 10:26:26 +00:00
|
|
|
|
2021-03-28 17:49:20 +00:00
|
|
|
with pytest.raises(OperationalException, match=r"CategoricalParameter space must.*"):
|
|
|
|
CategoricalParameter(['aa'], default='aa', space='buy')
|
|
|
|
|
2021-03-28 17:31:54 +00:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
BaseParameter(opt_range=[0, 1], default=1, space='buy')
|
2021-03-27 10:26:26 +00:00
|
|
|
|
2021-03-28 17:49:20 +00:00
|
|
|
intpar = IntParameter(low=0, high=5, default=1, space='buy')
|
|
|
|
assert intpar.value == 1
|
|
|
|
assert isinstance(intpar.get_space(''), Integer)
|
2021-04-24 05:18:35 +00:00
|
|
|
assert isinstance(intpar.range, range)
|
|
|
|
assert len(list(intpar.range)) == 1
|
|
|
|
# Range contains ONLY the default / value.
|
|
|
|
assert list(intpar.range) == [intpar.value]
|
2021-05-01 14:36:53 +00:00
|
|
|
intpar.in_space = True
|
2021-04-24 05:18:35 +00:00
|
|
|
|
|
|
|
assert len(list(intpar.range)) == 6
|
|
|
|
assert list(intpar.range) == [0, 1, 2, 3, 4, 5]
|
2021-03-28 17:49:20 +00:00
|
|
|
|
2021-04-01 07:17:39 +00:00
|
|
|
fltpar = RealParameter(low=0.0, high=5.5, default=1.0, space='buy')
|
2021-03-27 10:26:26 +00:00
|
|
|
assert fltpar.value == 1
|
2021-07-03 07:08:52 +00:00
|
|
|
assert isinstance(fltpar.get_space(''), Real)
|
2021-03-27 10:26:26 +00:00
|
|
|
|
2021-07-03 07:08:52 +00:00
|
|
|
fltpar = DecimalParameter(low=0.0, high=0.5, default=0.14, decimals=1, space='buy')
|
|
|
|
assert fltpar.value == 0.1
|
|
|
|
assert isinstance(fltpar.get_space(''), SKDecimal)
|
|
|
|
assert isinstance(fltpar.range, list)
|
|
|
|
assert len(list(fltpar.range)) == 1
|
|
|
|
# Range contains ONLY the default / value.
|
|
|
|
assert list(fltpar.range) == [fltpar.value]
|
|
|
|
fltpar.in_space = True
|
|
|
|
assert len(list(fltpar.range)) == 6
|
|
|
|
assert list(fltpar.range) == [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]
|
2021-04-01 07:17:39 +00:00
|
|
|
|
2021-03-29 17:27:19 +00:00
|
|
|
catpar = CategoricalParameter(['buy_rsi', 'buy_macd', 'buy_none'],
|
|
|
|
default='buy_macd', space='buy')
|
2021-03-28 17:49:20 +00:00
|
|
|
assert catpar.value == 'buy_macd'
|
2021-07-03 07:08:52 +00:00
|
|
|
assert isinstance(catpar.get_space(''), Categorical)
|
|
|
|
assert isinstance(catpar.range, list)
|
|
|
|
assert len(list(catpar.range)) == 1
|
|
|
|
# Range contains ONLY the default / value.
|
|
|
|
assert list(catpar.range) == [catpar.value]
|
|
|
|
catpar.in_space = True
|
|
|
|
assert len(list(catpar.range)) == 3
|
|
|
|
assert list(catpar.range) == ['buy_rsi', 'buy_macd', 'buy_none']
|
2021-03-28 17:49:20 +00:00
|
|
|
|
2021-08-04 18:52:56 +00:00
|
|
|
boolpar = BooleanParameter(default=True, space='buy')
|
|
|
|
assert boolpar.value is True
|
|
|
|
assert isinstance(boolpar.get_space(''), Categorical)
|
|
|
|
assert isinstance(boolpar.range, list)
|
|
|
|
assert len(list(boolpar.range)) == 1
|
|
|
|
|
|
|
|
boolpar.in_space = True
|
|
|
|
assert len(list(boolpar.range)) == 2
|
|
|
|
|
|
|
|
assert list(boolpar.range) == [True, False]
|
|
|
|
|
2022-08-19 13:49:31 +00:00
|
|
|
HyperoptStateContainer.set_state(HyperoptState.OPTIMIZE)
|
|
|
|
assert len(list(intpar.range)) == 1
|
|
|
|
assert len(list(fltpar.range)) == 1
|
|
|
|
assert len(list(catpar.range)) == 1
|
|
|
|
assert len(list(boolpar.range)) == 1
|
|
|
|
|
2021-03-27 10:26:26 +00:00
|
|
|
|
|
|
|
def test_auto_hyperopt_interface(default_conf):
|
2022-07-16 09:15:14 +00:00
|
|
|
default_conf.update({'strategy': 'HyperoptableStrategyV2'})
|
2021-03-27 10:26:26 +00:00
|
|
|
PairLocks.timeframe = default_conf['timeframe']
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2022-05-30 05:08:37 +00:00
|
|
|
strategy.ft_bot_start()
|
2021-09-05 13:34:57 +00:00
|
|
|
with pytest.raises(OperationalException):
|
|
|
|
next(strategy.enumerate_parameters('deadBeef'))
|
|
|
|
|
2021-03-27 10:26:26 +00:00
|
|
|
assert strategy.buy_rsi.value == strategy.buy_params['buy_rsi']
|
|
|
|
# PlusDI is NOT in the buy-params, so default should be used
|
|
|
|
assert strategy.buy_plusdi.value == 0.5
|
|
|
|
assert strategy.sell_rsi.value == strategy.sell_params['sell_rsi']
|
2021-03-28 17:49:20 +00:00
|
|
|
|
2021-09-05 13:34:57 +00:00
|
|
|
assert repr(strategy.sell_rsi) == 'IntParameter(74)'
|
|
|
|
|
2021-03-28 17:49:20 +00:00
|
|
|
# Parameter is disabled - so value from sell_param dict will NOT be used.
|
|
|
|
assert strategy.sell_minusdi.value == 0.5
|
2021-05-29 11:02:18 +00:00
|
|
|
all_params = strategy.detect_all_parameters()
|
|
|
|
assert isinstance(all_params, dict)
|
2022-05-30 05:22:16 +00:00
|
|
|
# Only one buy param at class level
|
|
|
|
assert len(all_params['buy']) == 1
|
|
|
|
# Running detect params at instance level reveals both parameters.
|
|
|
|
assert len(list(detect_parameters(strategy, 'buy'))) == 2
|
2021-05-29 11:02:18 +00:00
|
|
|
assert len(all_params['sell']) == 2
|
2021-08-04 18:52:56 +00:00
|
|
|
# Number of Hyperoptable parameters
|
2022-05-30 05:22:16 +00:00
|
|
|
assert all_params['count'] == 5
|
2021-04-05 08:53:00 +00:00
|
|
|
|
2021-05-29 11:02:18 +00:00
|
|
|
strategy.__class__.sell_rsi = IntParameter([0, 10], default=5, space='buy')
|
2021-04-05 08:53:00 +00:00
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r"Inconclusive parameter.*"):
|
2022-05-30 05:22:16 +00:00
|
|
|
[x for x in detect_parameters(strategy, 'sell')]
|
2021-06-30 04:43:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_auto_hyperopt_interface_loadparams(default_conf, mocker, caplog):
|
|
|
|
default_conf.update({'strategy': 'HyperoptableStrategy'})
|
|
|
|
del default_conf['stoploss']
|
|
|
|
del default_conf['minimal_roi']
|
|
|
|
mocker.patch.object(Path, 'is_file', MagicMock(return_value=True))
|
|
|
|
mocker.patch.object(Path, 'open')
|
|
|
|
expected_result = {
|
|
|
|
"strategy_name": "HyperoptableStrategy",
|
|
|
|
"params": {
|
|
|
|
"stoploss": {
|
|
|
|
"stoploss": -0.05,
|
|
|
|
},
|
|
|
|
"roi": {
|
|
|
|
"0": 0.2,
|
|
|
|
"1200": 0.01
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 18:04:28 +00:00
|
|
|
mocker.patch('freqtrade.strategy.hyper.HyperoptTools.load_params',
|
|
|
|
return_value=expected_result)
|
2021-06-30 04:43:49 +00:00
|
|
|
PairLocks.timeframe = default_conf['timeframe']
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
assert strategy.stoploss == -0.05
|
|
|
|
assert strategy.minimal_roi == {0: 0.2, 1200: 0.01}
|
|
|
|
|
|
|
|
expected_result = {
|
|
|
|
"strategy_name": "HyperoptableStrategy_No",
|
|
|
|
"params": {
|
|
|
|
"stoploss": {
|
|
|
|
"stoploss": -0.05,
|
|
|
|
},
|
|
|
|
"roi": {
|
|
|
|
"0": 0.2,
|
|
|
|
"1200": 0.01
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 18:04:28 +00:00
|
|
|
mocker.patch('freqtrade.strategy.hyper.HyperoptTools.load_params',
|
|
|
|
return_value=expected_result)
|
2021-06-30 04:43:49 +00:00
|
|
|
with pytest.raises(OperationalException, match="Invalid parameter file provided."):
|
|
|
|
StrategyResolver.load_strategy(default_conf)
|
|
|
|
|
2023-04-04 18:04:28 +00:00
|
|
|
mocker.patch('freqtrade.strategy.hyper.HyperoptTools.load_params',
|
|
|
|
MagicMock(side_effect=ValueError()))
|
2021-06-30 04:43:49 +00:00
|
|
|
|
|
|
|
StrategyResolver.load_strategy(default_conf)
|
|
|
|
assert log_has("Invalid parameter file format.", caplog)
|
2024-01-14 12:56:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('function,raises', [
|
2024-02-27 04:55:26 +00:00
|
|
|
('populate_entry_trend', False),
|
2024-01-14 13:33:47 +00:00
|
|
|
('advise_entry', False),
|
2024-02-27 04:55:26 +00:00
|
|
|
('populate_exit_trend', False),
|
2024-01-14 13:33:47 +00:00
|
|
|
('advise_exit', False),
|
2024-01-14 12:56:02 +00:00
|
|
|
])
|
2024-02-27 04:55:26 +00:00
|
|
|
def test_pandas_warning_direct(ohlcv_history, function, raises, recwarn):
|
2024-01-14 12:56:02 +00:00
|
|
|
|
|
|
|
df = _STRATEGY.populate_indicators(ohlcv_history, {'pair': 'ETH/BTC'})
|
|
|
|
if raises:
|
2024-02-27 04:55:26 +00:00
|
|
|
assert len(recwarn) == 1
|
|
|
|
# https://github.com/pandas-dev/pandas/issues/56503
|
|
|
|
# Fixed in 2.2.x
|
|
|
|
getattr(_STRATEGY, function)(df, {'pair': 'ETH/BTC'})
|
2024-01-14 12:56:02 +00:00
|
|
|
else:
|
2024-02-27 04:55:26 +00:00
|
|
|
assert len(recwarn) == 0
|
|
|
|
|
2024-01-14 12:56:02 +00:00
|
|
|
getattr(_STRATEGY, function)(df, {'pair': 'ETH/BTC'})
|
|
|
|
|
|
|
|
|
2024-01-14 13:33:47 +00:00
|
|
|
def test_pandas_warning_through_analyze_pair(ohlcv_history, mocker, recwarn):
|
2024-01-14 12:56:02 +00:00
|
|
|
|
|
|
|
mocker.patch.object(_STRATEGY.dp, 'ohlcv', return_value=ohlcv_history)
|
2024-01-14 13:33:47 +00:00
|
|
|
_STRATEGY.analyze_pair('ETH/BTC')
|
|
|
|
assert len(recwarn) == 0
|