adapt tests
This commit is contained in:
parent
b4d2a3f495
commit
a38c2121cc
@ -16,6 +16,9 @@ from freqtrade.logger import Logger
|
|||||||
from freqtrade.strategy.interface import IStrategy
|
from freqtrade.strategy.interface import IStrategy
|
||||||
|
|
||||||
|
|
||||||
|
logger = Logger(name=__name__).get_logger()
|
||||||
|
|
||||||
|
|
||||||
class StrategyResolver(object):
|
class StrategyResolver(object):
|
||||||
"""
|
"""
|
||||||
This class contains all the logic to load custom strategy class
|
This class contains all the logic to load custom strategy class
|
||||||
@ -28,8 +31,6 @@ class StrategyResolver(object):
|
|||||||
"""
|
"""
|
||||||
config = config or {}
|
config = config or {}
|
||||||
|
|
||||||
self.logger = Logger(name=__name__).get_logger()
|
|
||||||
|
|
||||||
# Verify the strategy is in the configuration, otherwise fallback to the default strategy
|
# Verify the strategy is in the configuration, otherwise fallback to the default strategy
|
||||||
if 'strategy' in config:
|
if 'strategy' in config:
|
||||||
strategy = config['strategy']
|
strategy = config['strategy']
|
||||||
@ -43,17 +44,17 @@ class StrategyResolver(object):
|
|||||||
# Check if we need to override configuration
|
# Check if we need to override configuration
|
||||||
if 'minimal_roi' in config:
|
if 'minimal_roi' in config:
|
||||||
self.custom_strategy.minimal_roi = config['minimal_roi']
|
self.custom_strategy.minimal_roi = config['minimal_roi']
|
||||||
self.logger.info("Override strategy \'minimal_roi\' with value in config file.")
|
logger.info("Override strategy \'minimal_roi\' with value in config file.")
|
||||||
|
|
||||||
if 'stoploss' in config:
|
if 'stoploss' in config:
|
||||||
self.custom_strategy.stoploss = config['stoploss']
|
self.custom_strategy.stoploss = config['stoploss']
|
||||||
self.logger.info(
|
logger.info(
|
||||||
"Override strategy \'stoploss\' with value in config file: %s.", config['stoploss']
|
"Override strategy \'stoploss\' with value in config file: %s.", config['stoploss']
|
||||||
)
|
)
|
||||||
|
|
||||||
if 'ticker_interval' in config:
|
if 'ticker_interval' in config:
|
||||||
self.custom_strategy.ticker_interval = config['ticker_interval']
|
self.custom_strategy.ticker_interval = config['ticker_interval']
|
||||||
self.logger.info(
|
logger.info(
|
||||||
"Override strategy \'ticker_interval\' with value in config file: %s.",
|
"Override strategy \'ticker_interval\' with value in config file: %s.",
|
||||||
config['ticker_interval']
|
config['ticker_interval']
|
||||||
)
|
)
|
||||||
@ -83,18 +84,18 @@ class StrategyResolver(object):
|
|||||||
for path in abs_paths:
|
for path in abs_paths:
|
||||||
self.custom_strategy = self._search_strategy(path, strategy_name)
|
self.custom_strategy = self._search_strategy(path, strategy_name)
|
||||||
if self.custom_strategy:
|
if self.custom_strategy:
|
||||||
self.logger.info('Using resolved strategy %s from \'%s\'', strategy_name, path)
|
logger.info('Using resolved strategy %s from \'%s\'', strategy_name, path)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
raise ImportError('not found')
|
raise ImportError('not found')
|
||||||
# Fallback to the default strategy
|
# Fallback to the default strategy
|
||||||
except (ImportError, TypeError) as error:
|
except (ImportError, TypeError) as error:
|
||||||
self.logger.error(
|
logger.error(
|
||||||
"Impossible to load Strategy '%s'. This class does not exist"
|
"Impossible to load Strategy '%s'. This class does not exist"
|
||||||
" or contains Python code errors",
|
" or contains Python code errors",
|
||||||
strategy_name
|
strategy_name
|
||||||
)
|
)
|
||||||
self.logger.error(
|
logger.error(
|
||||||
"The error is:\n%s.",
|
"The error is:\n%s.",
|
||||||
error
|
error
|
||||||
)
|
)
|
||||||
@ -119,17 +120,18 @@ class StrategyResolver(object):
|
|||||||
)
|
)
|
||||||
return next(valid_strategies_gen, None)
|
return next(valid_strategies_gen, None)
|
||||||
|
|
||||||
def _search_strategy(self, directory: str, strategy_name: str) -> Optional[IStrategy]:
|
@staticmethod
|
||||||
|
def _search_strategy(directory: str, strategy_name: str) -> Optional[IStrategy]:
|
||||||
"""
|
"""
|
||||||
Search for the strategy_name in the given directory
|
Search for the strategy_name in the given directory
|
||||||
:param directory: relative or absolute directory path
|
:param directory: relative or absolute directory path
|
||||||
:return: name of the strategy class
|
:return: name of the strategy class
|
||||||
"""
|
"""
|
||||||
self.logger.debug('Searching for strategy %s in \'%s\'', strategy_name, directory)
|
logger.debug('Searching for strategy %s in \'%s\'', strategy_name, directory)
|
||||||
for entry in os.listdir(directory):
|
for entry in os.listdir(directory):
|
||||||
# Only consider python files
|
# Only consider python files
|
||||||
if not entry.endswith('.py'):
|
if not entry.endswith('.py'):
|
||||||
self.logger.debug('Ignoring %s', entry)
|
logger.debug('Ignoring %s', entry)
|
||||||
continue
|
continue
|
||||||
strategy = StrategyResolver._get_valid_strategies(
|
strategy = StrategyResolver._get_valid_strategies(
|
||||||
os.path.abspath(os.path.join(directory, entry)), strategy_name
|
os.path.abspath(os.path.join(directory, entry)), strategy_name
|
||||||
|
@ -2,19 +2,16 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from freqtrade.strategy.interface import IStrategy
|
||||||
from freqtrade.strategy.resolver import StrategyResolver
|
from freqtrade.strategy.resolver import StrategyResolver
|
||||||
|
|
||||||
|
|
||||||
def test_search_strategy():
|
def test_search_strategy():
|
||||||
assert StrategyResolver._search_strategy('DefaultStrategy') == '.'
|
default_location = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..', 'strategy')
|
||||||
assert StrategyResolver._search_strategy('TestStrategy') == 'user_data.strategies.'
|
assert isinstance(StrategyResolver._search_strategy(default_location, 'DefaultStrategy'), IStrategy)
|
||||||
assert StrategyResolver._search_strategy('NotFoundStrategy') is None
|
assert StrategyResolver._search_strategy(default_location, 'NotFoundStrategy') is None
|
||||||
|
|
||||||
|
|
||||||
def test_strategy_structure():
|
|
||||||
assert hasattr(StrategyResolver, 'populate_indicators')
|
|
||||||
assert hasattr(StrategyResolver, 'populate_buy_trend')
|
|
||||||
assert hasattr(StrategyResolver, 'populate_sell_trend')
|
|
||||||
|
|
||||||
|
|
||||||
def test_load_strategy(result):
|
def test_load_strategy(result):
|
||||||
@ -39,7 +36,7 @@ def test_load_not_found_strategy(caplog):
|
|||||||
|
|
||||||
error_msg = "Impossible to load Strategy '{}'. This class does not " \
|
error_msg = "Impossible to load Strategy '{}'. This class does not " \
|
||||||
"exist or contains Python code errors".format('NotFoundStrategy')
|
"exist or contains Python code errors".format('NotFoundStrategy')
|
||||||
assert ('test_strategy', logging.ERROR, error_msg) in caplog.record_tuples
|
assert ('freqtrade.strategy.resolver', logging.ERROR, error_msg) in caplog.record_tuples
|
||||||
|
|
||||||
|
|
||||||
def test_strategy(result):
|
def test_strategy(result):
|
||||||
|
Loading…
Reference in New Issue
Block a user