wrap strategies with HyperoptStrategy for module lookups with pickle

This commit is contained in:
gcarq 2018-06-23 10:25:03 +02:00
parent 168ed91fe1
commit 3360bf4001

View File

@ -11,6 +11,7 @@ import pickle
import signal import signal
import sys import sys
from argparse import Namespace from argparse import Namespace
from copy import deepcopy
from functools import reduce from functools import reduce
from math import exp from math import exp
from operator import itemgetter from operator import itemgetter
@ -26,10 +27,25 @@ from freqtrade.arguments import Arguments
from freqtrade.configuration import Configuration from freqtrade.configuration import Configuration
from freqtrade.optimize import load_data from freqtrade.optimize import load_data
from freqtrade.optimize.backtesting import Backtesting from freqtrade.optimize.backtesting import Backtesting
from freqtrade.strategy.interface import IStrategy
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
HyperoptStrategy = None
def wrap_strategy(strategy: IStrategy) -> Optional[HyperoptStrategy]:
"""Wraps a given Strategy instance to HyperoptStrategy"""
global HyperoptStrategy
attr = deepcopy(dict(strategy.__class__.__dict__))
# Patch module name to make it compatible with pickle
attr['__module__'] = 'freqtrade.optimize.hyperopt'
HyperoptStrategy = type('HyperoptStrategy', (IStrategy,), attr)
return HyperoptStrategy()
class Hyperopt(Backtesting): class Hyperopt(Backtesting):
""" """
Hyperopt class, this class contains all the logic to run a hyperopt simulation Hyperopt class, this class contains all the logic to run a hyperopt simulation
@ -39,7 +55,6 @@ class Hyperopt(Backtesting):
hyperopt.start() hyperopt.start()
""" """
def __init__(self, config: Dict[str, Any]) -> None: def __init__(self, config: Dict[str, Any]) -> None:
super().__init__(config) super().__init__(config)
# set TARGET_TRADES to suit your number concurrent trades so its realistic # set TARGET_TRADES to suit your number concurrent trades so its realistic
# to the number of days # to the number of days
@ -57,6 +72,9 @@ class Hyperopt(Backtesting):
# check that the reported Σ% values do not exceed this! # check that the reported Σ% values do not exceed this!
self.expected_max_profit = 3.0 self.expected_max_profit = 3.0
# Wrap strategy to make it compatible with pickle
self.analyze.strategy = wrap_strategy(self.analyze.strategy)
# Configuration and data used by hyperopt # Configuration and data used by hyperopt
self.processed: Optional[Dict[str, Any]] = None self.processed: Optional[Dict[str, Any]] = None