stable/freqtrade/tests/strategy/test_strategy.py

214 lines
7.2 KiB
Python
Raw Normal View History

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
from os import path
2018-07-18 19:45:04 +00:00
import warnings
2018-03-24 20:56:20 +00:00
2018-03-25 14:28:04 +00:00
import pytest
from pandas import DataFrame
2018-03-25 14:28:04 +00:00
2018-06-23 09:14:31 +00:00
from freqtrade.strategy import import_strategy
from freqtrade.strategy.default_strategy import DefaultStrategy
2018-03-24 20:56:20 +00:00
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
2018-06-23 09:14:31 +00:00
def test_import_strategy(caplog):
caplog.set_level(logging.DEBUG)
default_config = {}
2018-06-23 09:14:31 +00:00
strategy = DefaultStrategy(default_config)
2018-06-23 09:14:31 +00:00
strategy.some_method = lambda *args, **kwargs: 42
assert strategy.__module__ == 'freqtrade.strategy.default_strategy'
assert strategy.some_method() == 42
imported_strategy = import_strategy(strategy, default_config)
2018-06-23 09:14:31 +00:00
2018-06-23 09:57:26 +00:00
assert dir(strategy) == dir(imported_strategy)
2018-06-23 09:14:31 +00:00
assert imported_strategy.__module__ == 'freqtrade.strategy'
assert imported_strategy.some_method() == 42
assert (
'freqtrade.strategy',
logging.DEBUG,
'Imported strategy freqtrade.strategy.default_strategy.DefaultStrategy '
'as freqtrade.strategy.DefaultStrategy',
) in caplog.record_tuples
2018-01-15 08:35:11 +00:00
def test_search_strategy():
2018-07-20 19:48:59 +00:00
default_config = {}
default_location = path.join(path.dirname(
path.realpath(__file__)), '..', '..', 'strategy'
2018-03-24 21:16:42 +00:00
)
assert isinstance(
StrategyResolver._search_strategy(
default_location,
config=default_config,
strategy_name='DefaultStrategy'
),
IStrategy
2018-03-24 21:16:42 +00:00
)
assert StrategyResolver._search_strategy(
default_location,
config=default_config,
strategy_name='NotFoundStrategy'
) is None
2018-01-15 08:35:11 +00:00
def test_load_strategy(result):
2018-06-23 09:14:31 +00:00
resolver = StrategyResolver({'strategy': 'TestStrategy'})
metadata = {'pair': 'ETH/BTC'}
assert 'adx' in resolver.strategy.advise_indicators(result, metadata=metadata)
2018-01-15 08:35:11 +00:00
def test_load_strategy_invalid_directory(result, caplog):
2018-03-25 14:28:04 +00:00
resolver = StrategyResolver()
extra_dir = path.join('some', 'path')
2018-07-20 19:48:59 +00:00
resolver._load_strategy('TestStrategy', config={}, extra_dir=extra_dir)
assert (
'freqtrade.strategy.resolver',
logging.WARNING,
'Path "{}" does not exist'.format(extra_dir),
) in caplog.record_tuples
2018-07-18 20:04:34 +00:00
assert 'adx' in resolver.strategy.advise_indicators(result, {'pair': 'ETH/BTC'})
2018-03-25 14:28:04 +00:00
def test_load_not_found_strategy():
strategy = StrategyResolver()
with pytest.raises(ImportError,
match=r'Impossible to load Strategy \'NotFoundStrategy\'.'
r' This class does not exist or contains Python code errors'):
strategy._load_strategy(strategy_name='NotFoundStrategy', config={})
2018-01-15 08:35:11 +00:00
def test_strategy(result):
config = {'strategy': 'DefaultStrategy'}
resolver = StrategyResolver(config)
metadata = {'pair': 'ETH/BTC'}
assert resolver.strategy.minimal_roi[0] == 0.04
assert config["minimal_roi"]['0'] == 0.04
2018-01-15 08:35:11 +00:00
assert resolver.strategy.stoploss == -0.10
assert config['stoploss'] == -0.10
assert resolver.strategy.ticker_interval == '5m'
assert config['ticker_interval'] == '5m'
2018-01-15 08:35:11 +00:00
df_indicators = resolver.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
dataframe = resolver.strategy.advise_buy(df_indicators, metadata=metadata)
2018-01-15 08:35:11 +00:00
assert 'buy' in dataframe.columns
dataframe = resolver.strategy.advise_sell(df_indicators, metadata=metadata)
2018-01-15 08:35:11 +00:00
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 = {
'strategy': 'DefaultStrategy',
2018-01-15 08:35:11 +00:00
'minimal_roi': {
"0": 0.5
}
}
resolver = StrategyResolver(config)
2018-01-15 08:35:11 +00:00
assert resolver.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 = {
'strategy': 'DefaultStrategy',
2018-01-15 08:35:11 +00:00
'stoploss': -0.5
}
resolver = StrategyResolver(config)
2018-01-15 08:35:11 +00:00
assert resolver.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,
'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)
config = {
'strategy': 'DefaultStrategy',
'ticker_interval': 60
}
resolver = StrategyResolver(config)
assert resolver.strategy.ticker_interval == 60
2018-03-24 17:11:21 +00:00
assert ('freqtrade.strategy.resolver',
logging.INFO,
'Override strategy \'ticker_interval\' with value in config file: 60.'
2018-01-15 08:35:11 +00:00
) in caplog.record_tuples
2018-07-18 19:45:04 +00:00
def test_deprecate_populate_indicators(result):
default_location = path.join(path.dirname(path.realpath(__file__)))
resolver = StrategyResolver({'strategy': 'TestStrategyLegacy',
'strategy_path': default_location})
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")
indicators = resolver.strategy.advise_indicators(result, 'ETH/BTC')
2018-07-18 19:45:04 +00:00
assert len(w) == 1
assert issubclass(w[-1].category, DeprecationWarning)
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.
2018-07-18 19:45:04 +00:00
warnings.simplefilter("always")
resolver.strategy.advise_buy(indicators, 'ETH/BTC')
2018-07-18 19:45:04 +00:00
assert len(w) == 1
assert issubclass(w[-1].category, DeprecationWarning)
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")
resolver.strategy.advise_sell(indicators, 'ETH_BTC')
2018-07-18 19:45:04 +00:00
assert len(w) == 1
assert issubclass(w[-1].category, DeprecationWarning)
assert "deprecated - check out the Sample strategy to see the current function headers!" \
in str(w[-1].message)
def test_call_deprecated_function(result, monkeypatch):
default_location = path.join(path.dirname(path.realpath(__file__)))
resolver = StrategyResolver({'strategy': 'TestStrategyLegacy',
'strategy_path': default_location})
metadata = {'pair': 'ETH/BTC'}
# Make sure we are using a legacy function
assert resolver.strategy._populate_fun_len == 2
assert resolver.strategy._buy_fun_len == 2
assert resolver.strategy._sell_fun_len == 2
indicator_df = resolver.strategy.advise_indicators(result, metadata=metadata)
assert type(indicator_df) is DataFrame
assert 'adx' in indicator_df.columns
buydf = resolver.strategy.advise_buy(result, metadata=metadata)
assert type(buydf) is DataFrame
assert 'buy' in buydf.columns
selldf = resolver.strategy.advise_sell(result, metadata=metadata)
assert type(selldf) is DataFrame
assert 'sell' in selldf