wrap strategies with HyperoptStrategy for module lookups with pickle

This commit is contained in:
gcarq 2018-06-23 10:25:03 +02:00 committed by Janne Sinivirta
parent 2738d3aed8
commit 5aae215c94
1 changed files with 19 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import pickle
import signal
import sys
from argparse import Namespace
from copy import deepcopy
from functools import reduce
from math import exp
from operator import itemgetter
@ -26,10 +27,25 @@ from freqtrade.arguments import Arguments
from freqtrade.configuration import Configuration
from freqtrade.optimize import load_data
from freqtrade.optimize.backtesting import Backtesting
from freqtrade.strategy.interface import IStrategy
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):
"""
Hyperopt class, this class contains all the logic to run a hyperopt simulation
@ -39,7 +55,6 @@ class Hyperopt(Backtesting):
hyperopt.start()
"""
def __init__(self, config: Dict[str, Any]) -> None:
super().__init__(config)
# set TARGET_TRADES to suit your number concurrent trades so its realistic
# to the number of days
@ -57,6 +72,9 @@ class Hyperopt(Backtesting):
# check that the reported Σ% values do not exceed this!
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
self.processed: Optional[Dict[str, Any]] = None