2018-06-23 09:13:49 +00:00
|
|
|
import logging
|
2018-08-15 06:37:20 +00:00
|
|
|
import sys
|
2018-06-23 09:13:49 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
from freqtrade.strategy.interface import IStrategy
|
2018-09-29 11:46:38 +00:00
|
|
|
# Import Default-Strategy to have hyperopt correctly resolve
|
|
|
|
from freqtrade.strategy.default_strategy import DefaultStrategy # noqa: F401
|
2018-06-23 09:13:49 +00:00
|
|
|
|
2019-04-09 09:27:35 +00:00
|
|
|
|
2018-06-23 09:13:49 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-07-16 05:11:17 +00:00
|
|
|
def import_strategy(strategy: IStrategy, config: dict) -> IStrategy:
|
2018-06-23 09:13:49 +00:00
|
|
|
"""
|
|
|
|
Imports given Strategy instance to global scope
|
|
|
|
of freqtrade.strategy and returns an instance of it
|
|
|
|
"""
|
2018-08-12 08:16:51 +00:00
|
|
|
|
2018-06-23 09:13:49 +00:00
|
|
|
# Copy all attributes from base class and class
|
2018-08-12 08:16:51 +00:00
|
|
|
comb = {**strategy.__class__.__dict__, **strategy.__dict__}
|
|
|
|
|
|
|
|
# Delete '_abc_impl' from dict as deepcopy fails on 3.7 with
|
|
|
|
# `TypeError: can't pickle _abc_data objects``
|
|
|
|
# This will only apply to python 3.7
|
2018-08-15 06:37:20 +00:00
|
|
|
if sys.version_info.major == 3 and sys.version_info.minor == 7 and '_abc_impl' in comb:
|
2018-08-12 08:16:51 +00:00
|
|
|
del comb['_abc_impl']
|
|
|
|
|
|
|
|
attr = deepcopy(comb)
|
2019-04-09 09:27:35 +00:00
|
|
|
|
2018-06-23 09:13:49 +00:00
|
|
|
# Adjust module name
|
|
|
|
attr['__module__'] = 'freqtrade.strategy'
|
|
|
|
|
|
|
|
name = strategy.__class__.__name__
|
|
|
|
clazz = type(name, (IStrategy,), attr)
|
|
|
|
|
|
|
|
logger.debug(
|
|
|
|
'Imported strategy %s.%s as %s.%s',
|
|
|
|
strategy.__module__, strategy.__class__.__name__,
|
|
|
|
clazz.__module__, strategy.__class__.__name__,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Modify global scope to declare class
|
|
|
|
globals()[name] = clazz
|
|
|
|
|
2018-07-16 05:11:17 +00:00
|
|
|
return clazz(config)
|