Skip to content

Commit

Permalink
Recursively search subdirectories in user_data/strategies for a strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
samgermain committed Mar 31, 2022
1 parent ddb0254 commit 6df15a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 9 additions & 4 deletions freqtrade/resolvers/iresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class IResolver:

@classmethod
def build_search_paths(cls, config: Dict[str, Any], user_subdir: Optional[str] = None,
extra_dir: Optional[str] = None) -> List[Path]:
extra_dirs: Optional[List[str]] = None) -> List[Path]:

abs_paths: List[Path] = []
if cls.initial_search_path:
Expand All @@ -53,9 +53,10 @@ def build_search_paths(cls, config: Dict[str, Any], user_subdir: Optional[str] =
if user_subdir:
abs_paths.insert(0, config['user_data_dir'].joinpath(user_subdir))

if extra_dir:
if extra_dirs:
# Add extra directory to the top of the search paths
abs_paths.insert(0, Path(extra_dir).resolve())
for dir in extra_dirs:
abs_paths.insert(0, Path(dir).resolve())

return abs_paths

Expand Down Expand Up @@ -164,9 +165,13 @@ def load_object(cls, object_name: str, config: dict, *, kwargs: dict,
:return: Object instance or None
"""

extra_dirs: List[str] = []
if extra_dir:
extra_dirs.append(extra_dir)

abs_paths = cls.build_search_paths(config,
user_subdir=cls.user_subdir,
extra_dir=extra_dir)
extra_dirs=extra_dirs)

found_object = cls._load_object(paths=abs_paths, object_name=object_name,
kwargs=kwargs)
Expand Down
10 changes: 8 additions & 2 deletions freqtrade/resolvers/strategy_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import tempfile
from base64 import urlsafe_b64decode
from inspect import getfullargspec
from os import walk
from pathlib import Path
from typing import Any, Dict, Optional
from typing import Any, Dict, List, Optional

from freqtrade.constants import REQUIRED_ORDERTIF, REQUIRED_ORDERTYPES, USERPATH_STRATEGIES
from freqtrade.exceptions import OperationalException
Expand Down Expand Up @@ -166,10 +167,15 @@ def _load_strategy(strategy_name: str,
:param extra_dir: additional directory to search for the given strategy
:return: Strategy instance or None
"""
extra_dirs: List[str] = [
path[0] for path in walk(f"{config['user_data_dir']}/{USERPATH_STRATEGIES}")
] # sub-directories
if extra_dir:
extra_dirs.append(extra_dir)

abs_paths = StrategyResolver.build_search_paths(config,
user_subdir=USERPATH_STRATEGIES,
extra_dir=extra_dir)
extra_dirs=extra_dirs)

if ":" in strategy_name:
logger.info("loading base64 encoded strategy")
Expand Down

0 comments on commit 6df15a7

Please sign in to comment.