2018-01-28 21:21:25 +00:00
|
|
|
# pragma pylint: disable=attribute-defined-outside-init
|
|
|
|
|
2018-01-28 05:26:57 +00:00
|
|
|
"""
|
|
|
|
This module load custom strategies
|
|
|
|
"""
|
2018-02-07 04:22:17 +00:00
|
|
|
import importlib
|
2018-01-15 08:35:11 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2018-02-06 14:31:50 +00:00
|
|
|
from collections import OrderedDict
|
2018-01-15 08:35:11 +00:00
|
|
|
from pandas import DataFrame
|
2018-02-07 04:22:17 +00:00
|
|
|
from freqtrade.logger import Logger
|
|
|
|
from freqtrade.constants import Constants
|
2018-01-15 08:35:11 +00:00
|
|
|
from freqtrade.strategy.interface import IStrategy
|
|
|
|
|
|
|
|
|
|
|
|
sys.path.insert(0, r'../../user_data/strategies')
|
|
|
|
|
|
|
|
|
|
|
|
class Strategy(object):
|
2018-01-28 05:26:57 +00:00
|
|
|
"""
|
|
|
|
This class contains all the logic to load custom strategy class
|
|
|
|
"""
|
2018-03-02 15:22:00 +00:00
|
|
|
def __init__(self, config: dict = {}) -> None:
|
2018-01-28 05:26:57 +00:00
|
|
|
"""
|
|
|
|
Load the custom class from config parameter
|
|
|
|
:param config:
|
|
|
|
:return:
|
|
|
|
"""
|
2018-02-07 04:22:17 +00:00
|
|
|
self.logger = Logger(name=__name__).get_logger()
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
# Verify the strategy is in the configuration, otherwise fallback to the default strategy
|
|
|
|
if 'strategy' in config:
|
|
|
|
strategy = config['strategy']
|
|
|
|
else:
|
2018-02-07 04:22:17 +00:00
|
|
|
strategy = Constants.DEFAULT_STRATEGY
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
# Load the strategy
|
|
|
|
self._load_strategy(strategy)
|
|
|
|
|
|
|
|
# Set attributes
|
|
|
|
# Check if we need to override configuration
|
|
|
|
if 'minimal_roi' in config:
|
|
|
|
self.custom_strategy.minimal_roi = config['minimal_roi']
|
|
|
|
self.logger.info("Override strategy \'minimal_roi\' with value in config file.")
|
|
|
|
|
|
|
|
if 'stoploss' in config:
|
|
|
|
self.custom_strategy.stoploss = config['stoploss']
|
2018-01-20 22:40:41 +00:00
|
|
|
self.logger.info(
|
2018-01-28 05:26:57 +00:00
|
|
|
"Override strategy \'stoploss\' with value in config file: %s.", config['stoploss']
|
2018-01-20 22:40:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if 'ticker_interval' in config:
|
|
|
|
self.custom_strategy.ticker_interval = config['ticker_interval']
|
|
|
|
self.logger.info(
|
2018-01-28 05:26:57 +00:00
|
|
|
"Override strategy \'ticker_interval\' with value in config file: %s.",
|
|
|
|
config['ticker_interval']
|
2018-01-20 22:40:41 +00:00
|
|
|
)
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2018-01-28 05:26:57 +00:00
|
|
|
# Minimal ROI designed for the strategy
|
2018-02-06 14:31:50 +00:00
|
|
|
self.minimal_roi = OrderedDict(sorted(
|
2018-02-11 13:02:42 +00:00
|
|
|
{int(key): value for (key, value) in self.custom_strategy.minimal_roi.items()}.items(),
|
|
|
|
key=lambda tuple: tuple[0])) # sort after converting to number
|
2018-01-28 05:26:57 +00:00
|
|
|
|
|
|
|
# Optimal stoploss designed for the strategy
|
2018-02-11 13:02:42 +00:00
|
|
|
self.stoploss = float(self.custom_strategy.stoploss)
|
2018-01-28 05:26:57 +00:00
|
|
|
|
2018-01-20 22:40:41 +00:00
|
|
|
self.ticker_interval = self.custom_strategy.ticker_interval
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
def _load_strategy(self, strategy_name: str) -> None:
|
|
|
|
"""
|
|
|
|
Search and load the custom strategy. If no strategy found, fallback on the default strategy
|
|
|
|
Set the object into self.custom_strategy
|
|
|
|
:param strategy_name: name of the module to import
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Start by sanitizing the file name (remove any extensions)
|
|
|
|
strategy_name = self._sanitize_module_name(filename=strategy_name)
|
|
|
|
|
|
|
|
# Search where can be the strategy file
|
|
|
|
path = self._search_strategy(filename=strategy_name)
|
|
|
|
|
|
|
|
# Load the strategy
|
|
|
|
self.custom_strategy = self._load_class(path + strategy_name)
|
|
|
|
|
|
|
|
# Fallback to the default strategy
|
2018-02-02 09:01:09 +00:00
|
|
|
except (ImportError, TypeError) as error:
|
|
|
|
self.logger.error(
|
|
|
|
"Impossible to load Strategy 'user_data/strategies/%s.py'. This file does not exist"
|
|
|
|
" or contains Python code errors",
|
|
|
|
strategy_name
|
|
|
|
)
|
|
|
|
self.logger.error(
|
|
|
|
"The error is:\n%s.",
|
|
|
|
error
|
|
|
|
)
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
def _load_class(self, filename: str) -> IStrategy:
|
|
|
|
"""
|
|
|
|
Import a strategy as a module
|
|
|
|
:param filename: path to the strategy (path from freqtrade/strategy/)
|
|
|
|
:return: return the strategy class
|
|
|
|
"""
|
|
|
|
module = importlib.import_module(filename, __package__)
|
|
|
|
custom_strategy = getattr(module, module.class_name)
|
|
|
|
|
2018-01-28 05:26:57 +00:00
|
|
|
self.logger.info("Load strategy class: %s (%s.py)", module.class_name, filename)
|
2018-01-15 08:35:11 +00:00
|
|
|
return custom_strategy()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _sanitize_module_name(filename: str) -> str:
|
|
|
|
"""
|
|
|
|
Remove any extension from filename
|
|
|
|
:param filename: filename to sanatize
|
|
|
|
:return: return the filename without extensions
|
|
|
|
"""
|
|
|
|
filename = os.path.basename(filename)
|
|
|
|
filename = os.path.splitext(filename)[0]
|
|
|
|
return filename
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _search_strategy(filename: str) -> str:
|
|
|
|
"""
|
|
|
|
Search for the Strategy file in different folder
|
|
|
|
1. search into the user_data/strategies folder
|
|
|
|
2. search into the freqtrade/strategy folder
|
|
|
|
3. if nothing found, return None
|
|
|
|
:param strategy_name: module name to search
|
|
|
|
:return: module path where is the strategy
|
|
|
|
"""
|
|
|
|
pwd = os.path.dirname(os.path.realpath(__file__)) + '/'
|
|
|
|
user_data = os.path.join(pwd, '..', '..', 'user_data', 'strategies', filename + '.py')
|
|
|
|
strategy_folder = os.path.join(pwd, filename + '.py')
|
|
|
|
|
|
|
|
path = None
|
|
|
|
if os.path.isfile(user_data):
|
|
|
|
path = 'user_data.strategies.'
|
|
|
|
elif os.path.isfile(strategy_folder):
|
|
|
|
path = '.'
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
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)
|