Increase pylint score on strategy

This commit is contained in:
Gerald Lonlas 2018-01-27 21:26:57 -08:00
parent 67c6c380e1
commit 776dd4a0d5
3 changed files with 45 additions and 31 deletions

View File

@ -1,8 +1,22 @@
"""
IStrategy interface
This module defines the interface to apply for strategies
"""
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from pandas import DataFrame from pandas import DataFrame
class IStrategy(ABC): class IStrategy(ABC):
"""
Interface for freqtrade strategies
Defines the mandatory structure must follow any custom strategies
Attributes you can use:
minimal_roi -> Dict: Minimal ROI designed for the strategy
stoploss -> float: optimal stoploss designed for the strategy
ticker_interval -> int: value of the ticker interval to use for the strategy
"""
@property @property
def name(self) -> str: def name(self) -> str:
""" """
@ -11,13 +25,6 @@ class IStrategy(ABC):
""" """
return self.__class__.__name__ return self.__class__.__name__
"""
Attributes you can use:
minimal_roi -> Dict: Minimal ROI designed for the strategy
stoploss -> float: optimal stoploss designed for the strategy
ticker_interval -> int: value of the ticker interval to use for the strategy
"""
@abstractmethod @abstractmethod
def populate_indicators(self, dataframe: DataFrame) -> DataFrame: def populate_indicators(self, dataframe: DataFrame) -> DataFrame:
""" """

View File

@ -1,10 +1,12 @@
"""
This module load custom strategies
"""
import os import os
import sys import sys
import logging import logging
import importlib import importlib
from pandas import DataFrame from pandas import DataFrame
from typing import Dict
from freqtrade.strategy.interface import IStrategy from freqtrade.strategy.interface import IStrategy
@ -12,16 +14,36 @@ sys.path.insert(0, r'../../user_data/strategies')
class Strategy(object): class Strategy(object):
"""
This class contains all the logic to load custom strategy class
"""
__instance = None __instance = None
DEFAULT_STRATEGY = 'default_strategy' DEFAULT_STRATEGY = 'default_strategy'
def __new__(cls): def __new__(cls):
"""
Used to create the Singleton
:return: Strategy object
"""
if Strategy.__instance is None: if Strategy.__instance is None:
Strategy.__instance = object.__new__(cls) Strategy.__instance = object.__new__(cls)
return Strategy.__instance return Strategy.__instance
def __init__(self):
if Strategy.__instance is None:
self.logger = None
self.minimal_roi = None
self.stoploss = None
self.ticker_interval = None
self.custom_strategy = None
def init(self, config): def init(self, config):
"""
Load the custom class from config parameter
:param config:
:return:
"""
self.logger = logging.getLogger(__name__) self.logger = logging.getLogger(__name__)
# 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
@ -42,21 +64,22 @@ class Strategy(object):
if 'stoploss' in config: if 'stoploss' in config:
self.custom_strategy.stoploss = config['stoploss'] self.custom_strategy.stoploss = config['stoploss']
self.logger.info( self.logger.info(
"Override strategy \'stoploss\' with value in config file: {}.".format( "Override strategy \'stoploss\' with value in config file: %s.", config['stoploss']
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( self.logger.info(
"Override strategy \'ticker_interval\' with value in config file: {}.".format( "Override strategy \'ticker_interval\' with value in config file: %s.",
config['ticker_interval'] config['ticker_interval']
)
) )
# Minimal ROI designed for the strategy
self.minimal_roi = self.custom_strategy.minimal_roi self.minimal_roi = self.custom_strategy.minimal_roi
# Optimal stoploss designed for the strategy
self.stoploss = self.custom_strategy.stoploss self.stoploss = self.custom_strategy.stoploss
self.ticker_interval = self.custom_strategy.ticker_interval self.ticker_interval = self.custom_strategy.ticker_interval
def _load_strategy(self, strategy_name: str) -> None: def _load_strategy(self, strategy_name: str) -> None:
@ -90,7 +113,7 @@ class Strategy(object):
module = importlib.import_module(filename, __package__) module = importlib.import_module(filename, __package__)
custom_strategy = getattr(module, module.class_name) custom_strategy = getattr(module, module.class_name)
self.logger.info("Load strategy class: {} ({}.py)".format(module.class_name, filename)) self.logger.info("Load strategy class: %s (%s.py)", module.class_name, filename)
return custom_strategy() return custom_strategy()
@staticmethod @staticmethod
@ -126,20 +149,6 @@ class Strategy(object):
return path return path
def minimal_roi(self) -> Dict:
"""
Minimal ROI designed for the strategy
:return: Dict: Value for the Minimal ROI
"""
return
def stoploss(self) -> float:
"""
Optimal stoploss designed for the strategy
:return: float | return None to disable it
"""
return self.custom_strategy.stoploss
def populate_indicators(self, dataframe: DataFrame) -> DataFrame: def populate_indicators(self, dataframe: DataFrame) -> DataFrame:
""" """
Populate indicators that will be used in the Buy and Sell strategy Populate indicators that will be used in the Buy and Sell strategy

View File

@ -28,8 +28,6 @@ def test_search_strategy():
def test_strategy_structure(): def test_strategy_structure():
assert hasattr(Strategy, 'init') assert hasattr(Strategy, 'init')
assert hasattr(Strategy, 'minimal_roi')
assert hasattr(Strategy, 'stoploss')
assert hasattr(Strategy, 'populate_indicators') assert hasattr(Strategy, 'populate_indicators')
assert hasattr(Strategy, 'populate_buy_trend') assert hasattr(Strategy, 'populate_buy_trend')
assert hasattr(Strategy, 'populate_sell_trend') assert hasattr(Strategy, 'populate_sell_trend')