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
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2018-03-24 20:56:20 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from freqtrade.strategy.interface import IStrategy
|
2018-03-24 17:11:21 +00:00
|
|
|
from freqtrade.strategy.resolver import StrategyResolver
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_search_strategy():
|
2018-03-24 21:16:42 +00:00
|
|
|
default_location = os.path.join(os.path.dirname(
|
|
|
|
os.path.realpath(__file__)), '..', '..', 'strategy'
|
|
|
|
)
|
|
|
|
assert isinstance(
|
|
|
|
StrategyResolver._search_strategy(default_location, 'DefaultStrategy'), IStrategy
|
|
|
|
)
|
2018-03-24 20:56:20 +00:00
|
|
|
assert StrategyResolver._search_strategy(default_location, 'NotFoundStrategy') is None
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_load_strategy(result):
|
2018-03-24 17:11:21 +00:00
|
|
|
strategy = StrategyResolver()
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2018-03-24 17:11:21 +00:00
|
|
|
assert not hasattr(StrategyResolver, 'custom_strategy')
|
2018-03-24 19:44:04 +00:00
|
|
|
strategy._load_strategy('TestStrategy')
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2018-03-24 17:11:21 +00:00
|
|
|
assert not hasattr(StrategyResolver, 'custom_strategy')
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
assert hasattr(strategy.custom_strategy, 'populate_indicators')
|
|
|
|
assert 'adx' in strategy.populate_indicators(result)
|
|
|
|
|
|
|
|
|
2018-02-02 09:01:09 +00:00
|
|
|
def test_load_not_found_strategy(caplog):
|
2018-03-24 17:11:21 +00:00
|
|
|
strategy = StrategyResolver()
|
2018-02-02 09:01:09 +00:00
|
|
|
|
2018-03-24 17:11:21 +00:00
|
|
|
assert not hasattr(StrategyResolver, 'custom_strategy')
|
2018-02-02 09:01:09 +00:00
|
|
|
strategy._load_strategy('NotFoundStrategy')
|
|
|
|
|
2018-03-24 19:44:04 +00:00
|
|
|
error_msg = "Impossible to load Strategy '{}'. This class does not " \
|
2018-02-02 09:01:09 +00:00
|
|
|
"exist or contains Python code errors".format('NotFoundStrategy')
|
2018-03-24 20:56:20 +00:00
|
|
|
assert ('freqtrade.strategy.resolver', logging.ERROR, error_msg) in caplog.record_tuples
|
2018-02-02 09:01:09 +00:00
|
|
|
|
|
|
|
|
2018-01-15 08:35:11 +00:00
|
|
|
def test_strategy(result):
|
2018-03-24 19:44:04 +00:00
|
|
|
strategy = StrategyResolver({'strategy': 'DefaultStrategy'})
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
assert hasattr(strategy.custom_strategy, 'minimal_roi')
|
2018-02-11 13:02:42 +00:00
|
|
|
assert strategy.minimal_roi[0] == 0.04
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
assert hasattr(strategy.custom_strategy, 'stoploss')
|
|
|
|
assert strategy.stoploss == -0.10
|
|
|
|
|
|
|
|
assert hasattr(strategy.custom_strategy, 'populate_indicators')
|
|
|
|
assert 'adx' in strategy.populate_indicators(result)
|
|
|
|
|
|
|
|
assert hasattr(strategy.custom_strategy, 'populate_buy_trend')
|
|
|
|
dataframe = strategy.populate_buy_trend(strategy.populate_indicators(result))
|
|
|
|
assert 'buy' in dataframe.columns
|
|
|
|
|
|
|
|
assert hasattr(strategy.custom_strategy, 'populate_sell_trend')
|
|
|
|
dataframe = strategy.populate_sell_trend(strategy.populate_indicators(result))
|
|
|
|
assert 'sell' in dataframe.columns
|
|
|
|
|
|
|
|
|
|
|
|
def test_strategy_override_minimal_roi(caplog):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2018-01-15 08:35:11 +00:00
|
|
|
config = {
|
2018-03-24 19:44:04 +00:00
|
|
|
'strategy': 'DefaultStrategy',
|
2018-01-15 08:35:11 +00:00
|
|
|
'minimal_roi': {
|
|
|
|
"0": 0.5
|
|
|
|
}
|
|
|
|
}
|
2018-03-24 17:11:21 +00:00
|
|
|
strategy = StrategyResolver(config)
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
assert hasattr(strategy.custom_strategy, 'minimal_roi')
|
2018-02-11 13:02:42 +00:00
|
|
|
assert strategy.minimal_roi[0] == 0.5
|
2018-03-24 17:11:21 +00:00
|
|
|
assert ('freqtrade.strategy.resolver',
|
2018-01-15 08:35:11 +00:00
|
|
|
logging.INFO,
|
|
|
|
'Override strategy \'minimal_roi\' with value in config file.'
|
|
|
|
) in caplog.record_tuples
|
|
|
|
|
|
|
|
|
|
|
|
def test_strategy_override_stoploss(caplog):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2018-01-15 08:35:11 +00:00
|
|
|
config = {
|
2018-03-24 19:44:04 +00:00
|
|
|
'strategy': 'DefaultStrategy',
|
2018-01-15 08:35:11 +00:00
|
|
|
'stoploss': -0.5
|
|
|
|
}
|
2018-03-24 17:11:21 +00:00
|
|
|
strategy = StrategyResolver(config)
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
assert hasattr(strategy.custom_strategy, 'stoploss')
|
|
|
|
assert strategy.stoploss == -0.5
|
2018-03-24 17:11:21 +00:00
|
|
|
assert ('freqtrade.strategy.resolver',
|
2018-01-15 08:35:11 +00:00
|
|
|
logging.INFO,
|
2018-01-20 22:40:41 +00:00
|
|
|
'Override strategy \'stoploss\' with value in config file: -0.5.'
|
|
|
|
) in caplog.record_tuples
|
|
|
|
|
|
|
|
|
|
|
|
def test_strategy_override_ticker_interval(caplog):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
|
2018-01-20 22:40:41 +00:00
|
|
|
config = {
|
2018-03-24 19:44:04 +00:00
|
|
|
'strategy': 'DefaultStrategy',
|
2018-01-20 22:40:41 +00:00
|
|
|
'ticker_interval': 60
|
|
|
|
}
|
2018-03-24 17:11:21 +00:00
|
|
|
strategy = StrategyResolver(config)
|
2018-01-20 22:40:41 +00:00
|
|
|
|
|
|
|
assert hasattr(strategy.custom_strategy, 'ticker_interval')
|
|
|
|
assert strategy.ticker_interval == 60
|
2018-03-24 17:11:21 +00:00
|
|
|
assert ('freqtrade.strategy.resolver',
|
2018-01-20 22:40:41 +00:00
|
|
|
logging.INFO,
|
|
|
|
'Override strategy \'ticker_interval\' with value in config file: 60.'
|
2018-01-15 08:35:11 +00:00
|
|
|
) in caplog.record_tuples
|
|
|
|
|
|
|
|
|
|
|
|
def test_strategy_fallback_default_strategy():
|
2018-03-24 17:11:21 +00:00
|
|
|
strategy = StrategyResolver()
|
2018-01-15 08:35:11 +00:00
|
|
|
strategy.logger = logging.getLogger(__name__)
|
|
|
|
|
2018-03-24 17:11:21 +00:00
|
|
|
assert not hasattr(StrategyResolver, 'custom_strategy')
|
2018-01-15 08:35:11 +00:00
|
|
|
strategy._load_strategy('../../super_duper')
|
2018-03-24 17:11:21 +00:00
|
|
|
assert not hasattr(StrategyResolver, 'custom_strategy')
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2018-01-18 05:50:12 +00:00
|
|
|
|
2018-01-15 08:35:11 +00:00
|
|
|
def test_strategy_singleton():
|
2018-03-24 19:44:04 +00:00
|
|
|
strategy1 = StrategyResolver({'strategy': 'DefaultStrategy'})
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
assert hasattr(strategy1.custom_strategy, 'minimal_roi')
|
2018-02-11 13:02:42 +00:00
|
|
|
assert strategy1.minimal_roi[0] == 0.04
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2018-03-24 17:11:21 +00:00
|
|
|
strategy2 = StrategyResolver()
|
2018-01-15 08:35:11 +00:00
|
|
|
assert hasattr(strategy2.custom_strategy, 'minimal_roi')
|
2018-02-11 13:02:42 +00:00
|
|
|
assert strategy2.minimal_roi[0] == 0.04
|