From e9c4e6a69d9c173b30c6b4d2263ad658b1950b42 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 20 Mar 2022 13:14:52 +0100 Subject: [PATCH] Update derived strategy documentation --- docs/strategy-advanced.md | 11 +++++++---- freqtrade/resolvers/iresolver.py | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/docs/strategy-advanced.md b/docs/strategy-advanced.md index fa1c09560..533402528 100644 --- a/docs/strategy-advanced.md +++ b/docs/strategy-advanced.md @@ -146,7 +146,7 @@ def version(self) -> str: The strategies can be derived from other strategies. This avoids duplication of your custom strategy code. You can use this technique to override small parts of your main strategy, leaving the rest untouched: -``` python +``` python title="user_data/strategies/myawesomestrategy.py" class MyAwesomeStrategy(IStrategy): ... stoploss = 0.13 @@ -155,6 +155,10 @@ class MyAwesomeStrategy(IStrategy): # should be in any custom strategy... ... +``` + +``` python title="user_data/strategies/MyAwesomeStrategy2.py" +from myawesomestrategy import MyAwesomeStrategy class MyAwesomeStrategy2(MyAwesomeStrategy): # Override something stoploss = 0.08 @@ -163,16 +167,15 @@ class MyAwesomeStrategy2(MyAwesomeStrategy): Both attributes and methods may be overridden, altering behavior of the original strategy in a way you need. +While keeping the subclass in the same file is technically possible, it can lead to some problems with hyperopt parameter files. + !!! Note "Parent-strategy in different files" If you have the parent-strategy in a different file, you can still import the strategy. Assuming `myawesomestrategy.py` is the filename, and `MyAwesomeStrategy` the strategy you need to import: ``` python - from myawesomestrategy import MyAwesomeStrategy ``` - This is the recommended way to derive strategies to avoid problems with hyperopt parameter files. - ## Embedding Strategies Freqtrade provides you with an easy way to embed the strategy into your configuration file. diff --git a/freqtrade/resolvers/iresolver.py b/freqtrade/resolvers/iresolver.py index 8d132da70..3ab461041 100644 --- a/freqtrade/resolvers/iresolver.py +++ b/freqtrade/resolvers/iresolver.py @@ -75,8 +75,8 @@ class IResolver: # Generate spec based on absolute path # Pass object_name as first argument to have logging print a reasonable name. with PathModifier(module_path.parent): - - spec = importlib.util.spec_from_file_location(module_path.stem or "", str(module_path)) + module_name = module_path.stem or "" + spec = importlib.util.spec_from_file_location(module_name, str(module_path)) if not spec: return iter([None]) @@ -95,7 +95,7 @@ class IResolver: module, inspect.isclass) if ((object_name is None or object_name == name) and issubclass(obj, cls.object_type) and obj is not cls.object_type - and obj.__module__ == module_path.stem or "" + and obj.__module__ == module_name ) ) # The __module__ check ensures we only use strategies that are defined in this folder.