integrated BASE64 encoded strategy loading

This commit is contained in:
Gert Wohlgemuth 2018-07-05 14:30:24 -07:00
parent 2bb63ba33d
commit 8bbee4038b
2 changed files with 29 additions and 2 deletions

View File

@ -6,14 +6,16 @@ This module load custom strategies
import importlib.util import importlib.util
import inspect import inspect
import logging import logging
import os from base64 import urlsafe_b64decode
from collections import OrderedDict from collections import OrderedDict
from typing import Optional, Dict, Type from typing import Optional, Dict, Type
from freqtrade import constants from freqtrade import constants
from freqtrade.strategy import import_strategy from freqtrade.strategy import import_strategy
from freqtrade.strategy.interface import IStrategy from freqtrade.strategy.interface import IStrategy
import tempfile
import os
from pathlib import Path
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -80,6 +82,22 @@ class StrategyResolver(object):
# Add extra strategy directory on top of search paths # Add extra strategy directory on top of search paths
abs_paths.insert(0, extra_dir) abs_paths.insert(0, extra_dir)
if ":" in strategy_name and "http" not in strategy_name:
print("loading none http based strategy: {}".format(strategy_name))
strat = strategy_name.split(":")
if len(strat) == 2:
temp = Path(tempfile.mkdtemp("freq", "strategy"))
name = strat[0] + ".py"
temp.joinpath(name).write_text(urlsafe_b64decode(strat[1]).decode('utf-8'))
temp.joinpath("__init__.py").touch()
strategy_name = os.path.splitext(name)[0]
# register temp path with the bot
abs_paths.insert(0, temp.absolute())
for path in abs_paths: for path in abs_paths:
try: try:
strategy = self._search_strategy(path, strategy_name) strategy = self._search_strategy(path, strategy_name)

View File

@ -1,6 +1,7 @@
# pragma pylint: disable=missing-docstring, protected-access, C0103 # pragma pylint: disable=missing-docstring, protected-access, C0103
import logging import logging
import os import os
from base64 import urlsafe_b64encode
import pytest import pytest
@ -50,6 +51,14 @@ def test_load_strategy(result):
assert 'adx' in resolver.strategy.populate_indicators(result) assert 'adx' in resolver.strategy.populate_indicators(result)
def test_load_strategy_byte64(result):
with open("freqtrade/tests/strategy/test_strategy.py", "r") as file:
encoded_string = urlsafe_b64encode(file.read().encode("utf-8")).decode("utf-8")
resolver = StrategyResolver({'strategy': 'TestStrategy:{}'.format(encoded_string)})
assert hasattr(resolver.strategy, 'populate_indicators')
assert 'adx' in resolver.strategy.populate_indicators(result)
def test_load_strategy_invalid_directory(result, caplog): def test_load_strategy_invalid_directory(result, caplog):
resolver = StrategyResolver() resolver = StrategyResolver()
extra_dir = os.path.join('some', 'path') extra_dir = os.path.join('some', 'path')