2018-01-28 07:38:41 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, protected-access, C0103
|
2018-01-15 08:35:11 +00:00
|
|
|
import logging
|
2019-04-16 18:25:48 +00:00
|
|
|
import warnings
|
2018-07-05 21:30:24 +00:00
|
|
|
from base64 import urlsafe_b64encode
|
2018-11-24 19:39:16 +00:00
|
|
|
from pathlib import Path
|
2018-03-24 20:56:20 +00:00
|
|
|
|
2018-03-25 14:28:04 +00:00
|
|
|
import pytest
|
2018-07-22 15:39:35 +00:00
|
|
|
from pandas import DataFrame
|
2018-03-25 14:28:04 +00:00
|
|
|
|
2019-12-30 14:02:17 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2019-04-16 18:25:48 +00:00
|
|
|
from freqtrade.resolvers import StrategyResolver
|
2018-03-24 20:56:20 +00:00
|
|
|
from freqtrade.strategy.interface import IStrategy
|
2019-09-08 07:54:15 +00:00
|
|
|
from tests.conftest import log_has, log_has_re
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_search_strategy():
|
2020-02-18 19:26:20 +00:00
|
|
|
default_location = Path(__file__).parent / 'strats'
|
2019-07-12 20:45:49 +00:00
|
|
|
|
|
|
|
s, _ = StrategyResolver._search_object(
|
|
|
|
directory=default_location,
|
2021-08-26 05:25:53 +00:00
|
|
|
object_name='StrategyTestV2',
|
2020-09-17 05:38:56 +00:00
|
|
|
add_source=True,
|
2018-03-24 21:16:42 +00:00
|
|
|
)
|
2019-12-24 13:01:28 +00:00
|
|
|
assert issubclass(s, IStrategy)
|
2019-07-12 20:45:49 +00:00
|
|
|
|
|
|
|
s, _ = StrategyResolver._search_object(
|
2018-11-24 19:00:02 +00:00
|
|
|
directory=default_location,
|
2020-09-17 05:38:56 +00:00
|
|
|
object_name='NotFoundStrategy',
|
|
|
|
add_source=True,
|
2019-07-12 20:45:49 +00:00
|
|
|
)
|
|
|
|
assert s is None
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
|
2020-02-15 01:32:10 +00:00
|
|
|
def test_search_all_strategies_no_failed():
|
2020-02-18 19:12:10 +00:00
|
|
|
directory = Path(__file__).parent / "strats"
|
2020-02-15 01:22:21 +00:00
|
|
|
strategies = StrategyResolver.search_all_objects(directory, enum_failed=False)
|
2019-12-24 14:35:38 +00:00
|
|
|
assert isinstance(strategies, list)
|
2021-09-21 05:11:53 +00:00
|
|
|
assert len(strategies) == 5
|
2019-12-24 14:35:38 +00:00
|
|
|
assert isinstance(strategies[0], dict)
|
|
|
|
|
|
|
|
|
2020-02-15 01:32:10 +00:00
|
|
|
def test_search_all_strategies_with_failed():
|
2020-02-18 19:12:10 +00:00
|
|
|
directory = Path(__file__).parent / "strats"
|
2020-02-15 01:32:10 +00:00
|
|
|
strategies = StrategyResolver.search_all_objects(directory, enum_failed=True)
|
|
|
|
assert isinstance(strategies, list)
|
2021-09-21 05:11:53 +00:00
|
|
|
assert len(strategies) == 6
|
2020-02-20 05:22:36 +00:00
|
|
|
# with enum_failed=True search_all_objects() shall find 2 good strategies
|
2020-02-15 03:54:18 +00:00
|
|
|
# and 1 which fails to load
|
2021-09-21 05:11:53 +00:00
|
|
|
assert len([x for x in strategies if x['class'] is not None]) == 5
|
2020-02-15 03:54:18 +00:00
|
|
|
assert len([x for x in strategies if x['class'] is None]) == 1
|
2020-02-15 01:32:10 +00:00
|
|
|
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_load_strategy(default_conf, result):
|
2019-11-01 12:41:25 +00:00
|
|
|
default_conf.update({'strategy': 'SampleStrategy',
|
|
|
|
'strategy_path': str(Path(__file__).parents[2] / 'freqtrade/templates')
|
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2020-09-26 14:33:48 +00:00
|
|
|
assert isinstance(strategy.__source__, str)
|
|
|
|
assert 'class SampleStrategy' in strategy.__source__
|
2020-09-17 05:38:56 +00:00
|
|
|
assert isinstance(strategy.__file__, str)
|
2019-12-23 09:23:48 +00:00
|
|
|
assert 'rsi' in strategy.advise_indicators(result, {'pair': 'ETH/BTC'})
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_load_strategy_base64(result, caplog, default_conf):
|
2019-11-01 12:41:25 +00:00
|
|
|
with (Path(__file__).parents[2] / 'freqtrade/templates/sample_strategy.py').open("rb") as file:
|
2019-07-14 18:05:28 +00:00
|
|
|
encoded_string = urlsafe_b64encode(file.read()).decode("utf-8")
|
2019-08-27 04:41:07 +00:00
|
|
|
default_conf.update({'strategy': 'SampleStrategy:{}'.format(encoded_string)})
|
2019-07-25 05:17:25 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
assert 'rsi' in strategy.advise_indicators(result, {'pair': 'ETH/BTC'})
|
2019-07-21 13:16:19 +00:00
|
|
|
# Make sure strategy was loaded from base64 (using temp directory)!!
|
2019-08-27 04:41:07 +00:00
|
|
|
assert log_has_re(r"Using resolved strategy SampleStrategy from '"
|
2019-10-18 10:48:12 +00:00
|
|
|
r".*(/|\\).*(/|\\)SampleStrategy\.py'\.\.\.", caplog)
|
2018-07-05 21:30:24 +00:00
|
|
|
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_load_strategy_invalid_directory(result, caplog, default_conf):
|
2021-09-21 17:14:14 +00:00
|
|
|
default_conf['strategy'] = 'StrategyTestV3'
|
2019-06-17 12:34:45 +00:00
|
|
|
extra_dir = Path.cwd() / 'some/path'
|
2020-02-18 19:26:20 +00:00
|
|
|
with pytest.raises(OperationalException):
|
2021-08-26 05:25:53 +00:00
|
|
|
StrategyResolver._load_strategy('StrategyTestV2', config=default_conf,
|
2020-02-18 19:26:20 +00:00
|
|
|
extra_dir=extra_dir)
|
2018-06-23 12:42:22 +00:00
|
|
|
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has_re(r'Path .*' + r'some.*path.*' + r'.* does not exist', caplog)
|
2018-03-25 14:28:04 +00:00
|
|
|
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_load_not_found_strategy(default_conf):
|
2019-09-21 17:54:44 +00:00
|
|
|
default_conf['strategy'] = 'NotFoundStrategy'
|
2019-07-12 20:45:49 +00:00
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match=r"Impossible to load Strategy 'NotFoundStrategy'. "
|
|
|
|
r"This class does not exist or contains Python code errors."):
|
2019-12-23 09:23:48 +00:00
|
|
|
StrategyResolver.load_strategy(default_conf)
|
2018-02-02 09:01:09 +00:00
|
|
|
|
|
|
|
|
2019-09-21 17:54:44 +00:00
|
|
|
def test_load_strategy_noname(default_conf):
|
|
|
|
default_conf['strategy'] = ''
|
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match="No strategy set. Please use `--strategy` to specify "
|
|
|
|
"the strategy class to use."):
|
2019-12-23 09:23:48 +00:00
|
|
|
StrategyResolver.load_strategy(default_conf)
|
2019-09-21 17:54:44 +00:00
|
|
|
|
|
|
|
|
2021-09-21 17:14:14 +00:00
|
|
|
def test_strategy_v2(result, default_conf):
|
2021-08-26 05:25:53 +00:00
|
|
|
default_conf.update({'strategy': 'StrategyTestV2'})
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2018-07-29 18:36:03 +00:00
|
|
|
metadata = {'pair': 'ETH/BTC'}
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.minimal_roi[0] == 0.04
|
2019-07-25 05:17:25 +00:00
|
|
|
assert default_conf["minimal_roi"]['0'] == 0.04
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.stoploss == -0.10
|
2019-07-25 05:17:25 +00:00
|
|
|
assert default_conf['stoploss'] == -0.10
|
2018-07-15 07:01:08 +00:00
|
|
|
|
2020-06-02 07:36:04 +00:00
|
|
|
assert strategy.timeframe == '5m'
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.ticker_interval == '5m'
|
2020-06-01 18:47:27 +00:00
|
|
|
assert default_conf['timeframe'] == '5m'
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
df_indicators = strategy.advise_indicators(result, metadata=metadata)
|
2018-07-18 20:04:34 +00:00
|
|
|
assert 'adx' in df_indicators
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2021-08-18 10:19:17 +00:00
|
|
|
dataframe = strategy.advise_buy(df_indicators, metadata=metadata)
|
2021-08-25 04:43:58 +00:00
|
|
|
assert 'buy' not in dataframe.columns
|
|
|
|
assert 'enter_long' in dataframe.columns
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2021-08-18 10:19:17 +00:00
|
|
|
dataframe = strategy.advise_sell(df_indicators, metadata=metadata)
|
2021-08-25 04:43:58 +00:00
|
|
|
assert 'sell' not in dataframe.columns
|
|
|
|
assert 'exit_long' in dataframe.columns
|
2021-08-08 09:38:34 +00:00
|
|
|
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_strategy_override_minimal_roi(caplog, default_conf):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2018-01-15 08:35:11 +00:00
|
|
|
'minimal_roi': {
|
2021-06-13 09:34:44 +00:00
|
|
|
"20": 0.1,
|
2018-01-15 08:35:11 +00:00
|
|
|
"0": 0.5
|
|
|
|
}
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.minimal_roi[0] == 0.5
|
2021-06-13 09:34:44 +00:00
|
|
|
assert log_has(
|
|
|
|
"Override strategy 'minimal_roi' with value in config file: {'20': 0.1, '0': 0.5}.",
|
|
|
|
caplog)
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_strategy_override_stoploss(caplog, default_conf):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2018-01-15 08:35:11 +00:00
|
|
|
'stoploss': -0.5
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.stoploss == -0.5
|
2019-08-11 18:22:50 +00:00
|
|
|
assert log_has("Override strategy 'stoploss' with value in config file: -0.5.", caplog)
|
2018-01-20 22:40:41 +00:00
|
|
|
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_strategy_override_trailing_stop(caplog, default_conf):
|
2019-01-05 06:10:25 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2019-01-05 06:10:25 +00:00
|
|
|
'trailing_stop': True
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-01-05 06:10:25 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.trailing_stop
|
|
|
|
assert isinstance(strategy.trailing_stop, bool)
|
2019-08-11 18:22:50 +00:00
|
|
|
assert log_has("Override strategy 'trailing_stop' with value in config file: True.", caplog)
|
2019-01-05 06:10:25 +00:00
|
|
|
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_strategy_override_trailing_stop_positive(caplog, default_conf):
|
2019-01-05 06:10:25 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2019-01-05 06:10:25 +00:00
|
|
|
'trailing_stop_positive': -0.1,
|
|
|
|
'trailing_stop_positive_offset': -0.2
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-01-05 06:10:25 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.trailing_stop_positive == -0.1
|
2019-08-11 18:22:50 +00:00
|
|
|
assert log_has("Override strategy 'trailing_stop_positive' with value in config file: -0.1.",
|
|
|
|
caplog)
|
2019-01-05 06:10:25 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.trailing_stop_positive_offset == -0.2
|
2019-08-11 18:22:50 +00:00
|
|
|
assert log_has("Override strategy 'trailing_stop_positive' with value in config file: -0.1.",
|
|
|
|
caplog)
|
2019-01-05 06:10:25 +00:00
|
|
|
|
|
|
|
|
2020-06-01 18:47:27 +00:00
|
|
|
def test_strategy_override_timeframe(caplog, default_conf):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2020-06-01 18:47:27 +00:00
|
|
|
'timeframe': 60,
|
2019-03-23 19:40:07 +00:00
|
|
|
'stake_currency': 'ETH'
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2018-01-20 22:40:41 +00:00
|
|
|
|
2020-06-01 18:49:40 +00:00
|
|
|
assert strategy.timeframe == 60
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.stake_currency == 'ETH'
|
2020-06-01 18:47:27 +00:00
|
|
|
assert log_has("Override strategy 'timeframe' with value in config file: 60.",
|
2019-08-11 18:22:50 +00:00
|
|
|
caplog)
|
2018-07-18 19:45:04 +00:00
|
|
|
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_strategy_override_process_only_new_candles(caplog, default_conf):
|
2018-08-09 18:12:45 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2018-09-01 17:52:40 +00:00
|
|
|
'process_only_new_candles': True
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2018-08-09 18:12:45 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.process_only_new_candles
|
2019-08-11 18:22:50 +00:00
|
|
|
assert log_has("Override strategy 'process_only_new_candles' with value in config file: True.",
|
|
|
|
caplog)
|
2018-08-09 18:12:45 +00:00
|
|
|
|
2018-08-09 18:17:55 +00:00
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_strategy_override_order_types(caplog, default_conf):
|
2018-11-17 09:14:18 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
|
|
|
|
order_types = {
|
|
|
|
'buy': 'market',
|
|
|
|
'sell': 'limit',
|
2018-11-25 18:03:28 +00:00
|
|
|
'stoploss': 'limit',
|
|
|
|
'stoploss_on_exchange': True,
|
2018-11-17 09:14:18 +00:00
|
|
|
}
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2018-11-17 09:14:18 +00:00
|
|
|
'order_types': order_types
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2018-11-17 09:14:18 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.order_types
|
2018-11-25 18:03:28 +00:00
|
|
|
for method in ['buy', 'sell', 'stoploss', 'stoploss_on_exchange']:
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.order_types[method] == order_types[method]
|
2018-11-17 09:14:18 +00:00
|
|
|
|
2019-08-11 18:22:50 +00:00
|
|
|
assert log_has("Override strategy 'order_types' with value in config file:"
|
|
|
|
" {'buy': 'market', 'sell': 'limit', 'stoploss': 'limit',"
|
|
|
|
" 'stoploss_on_exchange': True}.", caplog)
|
2018-11-17 09:14:18 +00:00
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2018-11-17 12:12:11 +00:00
|
|
|
'order_types': {'buy': 'market'}
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2018-11-17 12:12:11 +00:00
|
|
|
# Raise error for invalid configuration
|
|
|
|
with pytest.raises(ImportError,
|
2021-09-21 17:14:14 +00:00
|
|
|
match=r"Impossible to load Strategy 'StrategyTestV3'. "
|
2018-11-17 12:12:11 +00:00
|
|
|
r"Order-types mapping is incomplete."):
|
2019-12-23 09:23:48 +00:00
|
|
|
StrategyResolver.load_strategy(default_conf)
|
2018-11-17 12:12:11 +00:00
|
|
|
|
2018-11-17 09:14:18 +00:00
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_strategy_override_order_tif(caplog, default_conf):
|
2018-12-10 18:17:56 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
|
|
|
|
order_time_in_force = {
|
|
|
|
'buy': 'fok',
|
|
|
|
'sell': 'gtc',
|
|
|
|
}
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2018-12-10 18:17:56 +00:00
|
|
|
'order_time_in_force': order_time_in_force
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2018-12-10 18:17:56 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.order_time_in_force
|
2018-12-10 18:17:56 +00:00
|
|
|
for method in ['buy', 'sell']:
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.order_time_in_force[method] == order_time_in_force[method]
|
2018-12-10 18:17:56 +00:00
|
|
|
|
2019-08-11 18:22:50 +00:00
|
|
|
assert log_has("Override strategy 'order_time_in_force' with value in config file:"
|
|
|
|
" {'buy': 'fok', 'sell': 'gtc'}.", caplog)
|
2018-12-10 18:17:56 +00:00
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2018-12-10 18:17:56 +00:00
|
|
|
'order_time_in_force': {'buy': 'fok'}
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2018-12-10 18:17:56 +00:00
|
|
|
# Raise error for invalid configuration
|
|
|
|
with pytest.raises(ImportError,
|
2021-09-21 17:14:14 +00:00
|
|
|
match=r"Impossible to load Strategy 'StrategyTestV3'. "
|
2018-12-10 18:17:56 +00:00
|
|
|
r"Order-time-in-force mapping is incomplete."):
|
2019-12-23 09:23:48 +00:00
|
|
|
StrategyResolver.load_strategy(default_conf)
|
2018-12-10 18:17:56 +00:00
|
|
|
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_strategy_override_use_sell_signal(caplog, default_conf):
|
2019-01-24 06:03:41 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
assert strategy.use_sell_signal
|
|
|
|
assert isinstance(strategy.use_sell_signal, bool)
|
2019-01-24 06:03:41 +00:00
|
|
|
# must be inserted to configuration
|
2021-06-26 14:39:01 +00:00
|
|
|
assert 'use_sell_signal' in default_conf
|
|
|
|
assert default_conf['use_sell_signal']
|
2019-01-24 06:03:41 +00:00
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2021-06-26 14:39:01 +00:00
|
|
|
'use_sell_signal': False,
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-01-24 06:03:41 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
assert not strategy.use_sell_signal
|
|
|
|
assert isinstance(strategy.use_sell_signal, bool)
|
2019-10-05 10:29:59 +00:00
|
|
|
assert log_has("Override strategy 'use_sell_signal' with value in config file: False.", caplog)
|
2019-01-24 06:03:41 +00:00
|
|
|
|
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_strategy_override_use_sell_profit_only(caplog, default_conf):
|
2019-01-24 06:03:41 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
|
|
|
assert not strategy.sell_profit_only
|
|
|
|
assert isinstance(strategy.sell_profit_only, bool)
|
2019-01-24 06:03:41 +00:00
|
|
|
# must be inserted to configuration
|
2021-06-26 14:39:01 +00:00
|
|
|
assert 'sell_profit_only' in default_conf
|
|
|
|
assert not default_conf['sell_profit_only']
|
2019-01-24 06:03:41 +00:00
|
|
|
|
2019-07-25 05:17:25 +00:00
|
|
|
default_conf.update({
|
2021-09-21 17:14:14 +00:00
|
|
|
'strategy': 'StrategyTestV3',
|
2021-06-26 14:39:01 +00:00
|
|
|
'sell_profit_only': True,
|
2019-07-25 05:17:25 +00:00
|
|
|
})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-01-24 06:03:41 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy.sell_profit_only
|
|
|
|
assert isinstance(strategy.sell_profit_only, bool)
|
2019-08-11 18:22:50 +00:00
|
|
|
assert log_has("Override strategy 'sell_profit_only' with value in config file: True.", caplog)
|
2019-01-24 06:03:41 +00:00
|
|
|
|
|
|
|
|
2019-07-25 23:05:20 +00:00
|
|
|
@pytest.mark.filterwarnings("ignore:deprecated")
|
2019-07-25 05:17:25 +00:00
|
|
|
def test_deprecate_populate_indicators(result, default_conf):
|
2020-02-18 19:12:10 +00:00
|
|
|
default_location = Path(__file__).parent / "strats"
|
2021-08-26 05:04:33 +00:00
|
|
|
default_conf.update({'strategy': 'TestStrategyLegacyV1',
|
2019-07-25 05:17:25 +00:00
|
|
|
'strategy_path': default_location})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2018-07-18 19:45:04 +00:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
# Cause all warnings to always be triggered.
|
|
|
|
warnings.simplefilter("always")
|
2019-12-23 09:23:48 +00:00
|
|
|
indicators = strategy.advise_indicators(result, {'pair': 'ETH/BTC'})
|
2018-07-18 19:45:04 +00:00
|
|
|
assert len(w) == 1
|
|
|
|
assert issubclass(w[-1].category, DeprecationWarning)
|
2018-07-22 15:39:35 +00:00
|
|
|
assert "deprecated - check out the Sample strategy to see the current function headers!" \
|
|
|
|
in str(w[-1].message)
|
2018-07-18 19:45:04 +00:00
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
2019-01-31 05:51:03 +00:00
|
|
|
# Cause all warnings to always be triggered.
|
2018-07-18 19:45:04 +00:00
|
|
|
warnings.simplefilter("always")
|
2021-08-18 10:19:17 +00:00
|
|
|
strategy.advise_buy(indicators, {'pair': 'ETH/BTC'})
|
2018-07-18 19:45:04 +00:00
|
|
|
assert len(w) == 1
|
|
|
|
assert issubclass(w[-1].category, DeprecationWarning)
|
2018-07-22 15:39:35 +00:00
|
|
|
assert "deprecated - check out the Sample strategy to see the current function headers!" \
|
|
|
|
in str(w[-1].message)
|
2018-07-18 19:45:04 +00:00
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
# Cause all warnings to always be triggered.
|
|
|
|
warnings.simplefilter("always")
|
2021-08-18 10:19:17 +00:00
|
|
|
strategy.advise_sell(indicators, {'pair': 'ETH_BTC'})
|
2018-07-18 19:45:04 +00:00
|
|
|
assert len(w) == 1
|
|
|
|
assert issubclass(w[-1].category, DeprecationWarning)
|
2018-07-22 15:39:35 +00:00
|
|
|
assert "deprecated - check out the Sample strategy to see the current function headers!" \
|
|
|
|
in str(w[-1].message)
|
2018-07-18 21:23:30 +00:00
|
|
|
|
|
|
|
|
2019-07-25 23:05:20 +00:00
|
|
|
@pytest.mark.filterwarnings("ignore:deprecated")
|
2020-06-02 11:08:21 +00:00
|
|
|
def test_call_deprecated_function(result, monkeypatch, default_conf, caplog):
|
2020-02-18 19:12:10 +00:00
|
|
|
default_location = Path(__file__).parent / "strats"
|
2020-06-02 11:08:21 +00:00
|
|
|
del default_conf['timeframe']
|
2021-08-26 05:04:33 +00:00
|
|
|
default_conf.update({'strategy': 'TestStrategyLegacyV1',
|
2019-07-25 05:17:25 +00:00
|
|
|
'strategy_path': default_location})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2018-07-29 18:36:03 +00:00
|
|
|
metadata = {'pair': 'ETH/BTC'}
|
2018-07-18 21:23:30 +00:00
|
|
|
|
2018-07-22 15:39:35 +00:00
|
|
|
# Make sure we are using a legacy function
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy._populate_fun_len == 2
|
|
|
|
assert strategy._buy_fun_len == 2
|
|
|
|
assert strategy._sell_fun_len == 2
|
|
|
|
assert strategy.INTERFACE_VERSION == 1
|
2020-06-02 08:11:50 +00:00
|
|
|
assert strategy.timeframe == '5m'
|
|
|
|
assert strategy.ticker_interval == '5m'
|
2019-08-26 17:44:33 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
indicator_df = strategy.advise_indicators(result, metadata=metadata)
|
2019-08-26 17:44:33 +00:00
|
|
|
assert isinstance(indicator_df, DataFrame)
|
|
|
|
assert 'adx' in indicator_df.columns
|
|
|
|
|
2021-08-18 12:03:44 +00:00
|
|
|
enterdf = strategy.advise_buy(result, metadata=metadata)
|
|
|
|
assert isinstance(enterdf, DataFrame)
|
|
|
|
assert 'buy' in enterdf.columns
|
2019-08-26 17:44:33 +00:00
|
|
|
|
2021-08-18 12:03:44 +00:00
|
|
|
exitdf = strategy.advise_sell(result, metadata=metadata)
|
|
|
|
assert isinstance(exitdf, DataFrame)
|
|
|
|
assert 'sell' in exitdf
|
2019-08-26 17:44:33 +00:00
|
|
|
|
2020-06-15 04:35:31 +00:00
|
|
|
assert log_has("DEPRECATED: Please migrate to using 'timeframe' instead of 'ticker_interval'.",
|
2020-06-02 11:08:21 +00:00
|
|
|
caplog)
|
|
|
|
|
2019-08-26 17:44:33 +00:00
|
|
|
|
2021-08-24 04:45:09 +00:00
|
|
|
def test_strategy_interface_versioning(result, default_conf):
|
2021-08-26 05:25:53 +00:00
|
|
|
default_conf.update({'strategy': 'StrategyTestV2'})
|
2019-12-23 09:23:48 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-08-26 17:44:33 +00:00
|
|
|
metadata = {'pair': 'ETH/BTC'}
|
|
|
|
|
|
|
|
# Make sure we are using a legacy function
|
2019-12-23 09:23:48 +00:00
|
|
|
assert strategy._populate_fun_len == 3
|
|
|
|
assert strategy._buy_fun_len == 3
|
|
|
|
assert strategy._sell_fun_len == 3
|
|
|
|
assert strategy.INTERFACE_VERSION == 2
|
2018-07-22 15:39:35 +00:00
|
|
|
|
2019-12-23 09:23:48 +00:00
|
|
|
indicator_df = strategy.advise_indicators(result, metadata=metadata)
|
2018-11-25 18:03:28 +00:00
|
|
|
assert isinstance(indicator_df, DataFrame)
|
2018-07-22 15:39:35 +00:00
|
|
|
assert 'adx' in indicator_df.columns
|
|
|
|
|
2021-08-18 10:19:17 +00:00
|
|
|
enterdf = strategy.advise_buy(result, metadata=metadata)
|
|
|
|
assert isinstance(enterdf, DataFrame)
|
2021-08-24 04:45:09 +00:00
|
|
|
|
|
|
|
assert 'buy' not in enterdf.columns
|
|
|
|
assert 'enter_long' in enterdf.columns
|
2021-08-08 09:38:34 +00:00
|
|
|
|
2021-08-18 10:19:17 +00:00
|
|
|
exitdf = strategy.advise_sell(result, metadata=metadata)
|
|
|
|
assert isinstance(exitdf, DataFrame)
|
2021-08-24 04:45:09 +00:00
|
|
|
assert 'sell' not in exitdf
|
|
|
|
assert 'exit_long' in exitdf
|