strategy: remove unneeded population methods in resolver

This commit is contained in:
gcarq 2018-03-24 23:20:21 +01:00
parent 1cec06f808
commit 280886104c
3 changed files with 37 additions and 79 deletions

View File

@ -36,7 +36,7 @@ class Analyze(object):
:param config: Bot configuration (use the one from Configuration()) :param config: Bot configuration (use the one from Configuration())
""" """
self.config = config self.config = config
self.strategy = StrategyResolver(self.config) self.strategy = StrategyResolver(self.config).strategy
@staticmethod @staticmethod
def parse_ticker_dataframe(ticker: list) -> DataFrame: def parse_ticker_dataframe(ticker: list) -> DataFrame:

View File

@ -10,8 +10,6 @@ import os
from collections import OrderedDict from collections import OrderedDict
from typing import Optional, Dict, Type from typing import Optional, Dict, Type
from pandas import DataFrame
from freqtrade.constants import Constants from freqtrade.constants import Constants
from freqtrade.strategy.interface import IStrategy from freqtrade.strategy.interface import IStrategy
@ -38,42 +36,37 @@ class StrategyResolver(object):
strategy = Constants.DEFAULT_STRATEGY strategy = Constants.DEFAULT_STRATEGY
# Try to load the strategy # Try to load the strategy
self._load_strategy(strategy) self.strategy = self._load_strategy(strategy)
# Set attributes # Set attributes
# 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.strategy.minimal_roi = config['minimal_roi']
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.strategy.stoploss = float(config['stoploss'])
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.strategy.ticker_interval = int(config['ticker_interval'])
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']
) )
# Minimal ROI designed for the strategy # Minimal ROI designed for the strategy
self.minimal_roi = OrderedDict(sorted( self.strategy.minimal_roi = OrderedDict(sorted(
{int(key): value for (key, value) in self.custom_strategy.minimal_roi.items()}.items(), {int(key): value for (key, value) in self.strategy.minimal_roi.items()}.items(),
key=lambda t: t[0])) # sort after converting to number key=lambda t: t[0])) # sort after converting to number
# Optimal stoploss designed for the strategy def _load_strategy(self, strategy_name: str) -> Optional[IStrategy]:
self.stoploss = float(self.custom_strategy.stoploss)
self.ticker_interval = int(self.custom_strategy.ticker_interval)
def _load_strategy(self, strategy_name: str) -> None:
""" """
Search and loads the specified strategy. Search and loads the specified strategy.
:param strategy_name: name of the module to import :param strategy_name: name of the module to import
:return: None :return: Strategy instance or None
""" """
try: try:
current_path = os.path.dirname(os.path.realpath(__file__)) current_path = os.path.dirname(os.path.realpath(__file__))
@ -82,10 +75,10 @@ class StrategyResolver(object):
current_path, current_path,
] ]
for path in abs_paths: for path in abs_paths:
self.custom_strategy = self._search_strategy(path, strategy_name) strategy = self._search_strategy(path, strategy_name)
if self.custom_strategy: if strategy:
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 strategy
raise ImportError('not found') raise ImportError('not found')
# Fallback to the default strategy # Fallback to the default strategy
@ -99,6 +92,7 @@ class StrategyResolver(object):
"The error is:\n%s.", "The error is:\n%s.",
error error
) )
return None
@staticmethod @staticmethod
def _get_valid_strategies(module_path: str, strategy_name: str) -> Optional[Type[IStrategy]]: def _get_valid_strategies(module_path: str, strategy_name: str) -> Optional[Type[IStrategy]]:
@ -139,28 +133,3 @@ class StrategyResolver(object):
if strategy: if strategy:
return strategy() return strategy()
return None return None
def populate_indicators(self, dataframe: DataFrame) -> DataFrame:
"""
Populate indicators that will be used in the Buy and Sell strategy
:param dataframe: Raw data from the exchange and parsed by parse_ticker_dataframe()
:return: a Dataframe with all mandatory indicators for the strategies
"""
return self.custom_strategy.populate_indicators(dataframe)
def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame:
"""
Based on TA indicators, populates the buy signal for the given dataframe
:param dataframe: DataFrame
:return: DataFrame with buy column
:return:
"""
return self.custom_strategy.populate_buy_trend(dataframe)
def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame:
"""
Based on TA indicators, populates the sell signal for the given dataframe
:param dataframe: DataFrame
:return: DataFrame with buy column
"""
return self.custom_strategy.populate_sell_trend(dataframe)

View File

@ -19,15 +19,15 @@ def test_search_strategy():
def test_load_strategy(result): def test_load_strategy(result):
strategy = StrategyResolver() resolver = StrategyResolver()
assert not hasattr(StrategyResolver, 'custom_strategy') assert not hasattr(StrategyResolver, 'custom_strategy')
strategy._load_strategy('TestStrategy') resolver._load_strategy('TestStrategy')
assert not hasattr(StrategyResolver, 'custom_strategy') assert not hasattr(StrategyResolver, 'custom_strategy')
assert hasattr(strategy.custom_strategy, 'populate_indicators') assert hasattr(resolver.strategy, 'populate_indicators')
assert 'adx' in strategy.populate_indicators(result) assert 'adx' in resolver.strategy.populate_indicators(result)
def test_load_not_found_strategy(caplog): def test_load_not_found_strategy(caplog):
@ -42,23 +42,23 @@ def test_load_not_found_strategy(caplog):
def test_strategy(result): def test_strategy(result):
strategy = StrategyResolver({'strategy': 'DefaultStrategy'}) resolver = StrategyResolver({'strategy': 'DefaultStrategy'})
assert hasattr(strategy.custom_strategy, 'minimal_roi') assert hasattr(resolver.strategy, 'minimal_roi')
assert strategy.minimal_roi[0] == 0.04 assert resolver.strategy.minimal_roi[0] == 0.04
assert hasattr(strategy.custom_strategy, 'stoploss') assert hasattr(resolver.strategy, 'stoploss')
assert strategy.stoploss == -0.10 assert resolver.strategy.stoploss == -0.10
assert hasattr(strategy.custom_strategy, 'populate_indicators') assert hasattr(resolver.strategy, 'populate_indicators')
assert 'adx' in strategy.populate_indicators(result) assert 'adx' in resolver.strategy.populate_indicators(result)
assert hasattr(strategy.custom_strategy, 'populate_buy_trend') assert hasattr(resolver.strategy, 'populate_buy_trend')
dataframe = strategy.populate_buy_trend(strategy.populate_indicators(result)) dataframe = resolver.strategy.populate_buy_trend(resolver.strategy.populate_indicators(result))
assert 'buy' in dataframe.columns assert 'buy' in dataframe.columns
assert hasattr(strategy.custom_strategy, 'populate_sell_trend') assert hasattr(resolver.strategy, 'populate_sell_trend')
dataframe = strategy.populate_sell_trend(strategy.populate_indicators(result)) dataframe = resolver.strategy.populate_sell_trend(resolver.strategy.populate_indicators(result))
assert 'sell' in dataframe.columns assert 'sell' in dataframe.columns
@ -70,10 +70,10 @@ def test_strategy_override_minimal_roi(caplog):
"0": 0.5 "0": 0.5
} }
} }
strategy = StrategyResolver(config) resolver = StrategyResolver(config)
assert hasattr(strategy.custom_strategy, 'minimal_roi') assert hasattr(resolver.strategy, 'minimal_roi')
assert strategy.minimal_roi[0] == 0.5 assert resolver.strategy.minimal_roi[0] == 0.5
assert ('freqtrade.strategy.resolver', assert ('freqtrade.strategy.resolver',
logging.INFO, logging.INFO,
'Override strategy \'minimal_roi\' with value in config file.' 'Override strategy \'minimal_roi\' with value in config file.'
@ -86,10 +86,10 @@ def test_strategy_override_stoploss(caplog):
'strategy': 'DefaultStrategy', 'strategy': 'DefaultStrategy',
'stoploss': -0.5 'stoploss': -0.5
} }
strategy = StrategyResolver(config) resolver = StrategyResolver(config)
assert hasattr(strategy.custom_strategy, 'stoploss') assert hasattr(resolver.strategy, 'stoploss')
assert strategy.stoploss == -0.5 assert resolver.strategy.stoploss == -0.5
assert ('freqtrade.strategy.resolver', assert ('freqtrade.strategy.resolver',
logging.INFO, logging.INFO,
'Override strategy \'stoploss\' with value in config file: -0.5.' 'Override strategy \'stoploss\' with value in config file: -0.5.'
@ -103,10 +103,10 @@ def test_strategy_override_ticker_interval(caplog):
'strategy': 'DefaultStrategy', 'strategy': 'DefaultStrategy',
'ticker_interval': 60 'ticker_interval': 60
} }
strategy = StrategyResolver(config) resolver = StrategyResolver(config)
assert hasattr(strategy.custom_strategy, 'ticker_interval') assert hasattr(resolver.strategy, 'ticker_interval')
assert strategy.ticker_interval == 60 assert resolver.strategy.ticker_interval == 60
assert ('freqtrade.strategy.resolver', assert ('freqtrade.strategy.resolver',
logging.INFO, logging.INFO,
'Override strategy \'ticker_interval\' with value in config file: 60.' 'Override strategy \'ticker_interval\' with value in config file: 60.'
@ -120,14 +120,3 @@ def test_strategy_fallback_default_strategy():
assert not hasattr(StrategyResolver, 'custom_strategy') assert not hasattr(StrategyResolver, 'custom_strategy')
strategy._load_strategy('../../super_duper') strategy._load_strategy('../../super_duper')
assert not hasattr(StrategyResolver, 'custom_strategy') assert not hasattr(StrategyResolver, 'custom_strategy')
def test_strategy_singleton():
strategy1 = StrategyResolver({'strategy': 'DefaultStrategy'})
assert hasattr(strategy1.custom_strategy, 'minimal_roi')
assert strategy1.minimal_roi[0] == 0.04
strategy2 = StrategyResolver()
assert hasattr(strategy2.custom_strategy, 'minimal_roi')
assert strategy2.minimal_roi[0] == 0.04