From ba10bd77565e8f86e0a0525ffbbcdd3bfadc98b3 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 17 Sep 2020 06:56:51 +0200 Subject: [PATCH] Add strategy code to __code__ --- freqtrade/resolvers/iresolver.py | 22 ++++++++++++---------- freqtrade/rpc/api_server.py | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/freqtrade/resolvers/iresolver.py b/freqtrade/resolvers/iresolver.py index 846c85a5c..83c614650 100644 --- a/freqtrade/resolvers/iresolver.py +++ b/freqtrade/resolvers/iresolver.py @@ -67,9 +67,10 @@ class IResolver: return iter([None]) valid_objects_gen = ( - obj for name, obj in inspect.getmembers(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) + (obj, inspect.getsource(module)) for name, obj in inspect.getmembers( + 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) ) return valid_objects_gen @@ -93,7 +94,8 @@ class IResolver: obj = next(cls._get_valid_object(module_path, object_name), None) if obj: - return (obj, module_path) + obj[0].__code__ = obj[1] + return (obj[0], module_path) return (None, None) @classmethod @@ -133,10 +135,10 @@ class IResolver: user_subdir=cls.user_subdir, extra_dir=extra_dir) - pairlist = cls._load_object(paths=abs_paths, object_name=object_name, - kwargs=kwargs) - if pairlist: - return pairlist + found_object = cls._load_object(paths=abs_paths, object_name=object_name, + kwargs=kwargs) + if found_object: + return found_object raise OperationalException( f"Impossible to load {cls.object_type_str} '{object_name}'. This class does not exist " "or contains Python code errors." @@ -164,8 +166,8 @@ class IResolver: for obj in cls._get_valid_object(module_path, object_name=None, enum_failed=enum_failed): objects.append( - {'name': obj.__name__ if obj is not None else '', - 'class': obj, + {'name': obj[0].__name__ if obj is not None else '', + 'class': obj[0] if obj is not None else None, 'location': entry, }) return objects diff --git a/freqtrade/rpc/api_server.py b/freqtrade/rpc/api_server.py index 0b80283d9..21c0e5629 100644 --- a/freqtrade/rpc/api_server.py +++ b/freqtrade/rpc/api_server.py @@ -223,6 +223,8 @@ class ApiServer(RPC): view_func=self._plot_config, methods=['GET']) self.app.add_url_rule(f'{BASE_URI}/strategies', 'strategies', view_func=self._list_strategies, methods=['GET']) + self.app.add_url_rule(f'{BASE_URI}/strategy/', 'strategy', + view_func=self._get_strategy, methods=['GET']) self.app.add_url_rule(f'{BASE_URI}/available_pairs', 'pairs', view_func=self._list_available_pairs, methods=['GET']) @@ -586,6 +588,24 @@ class ApiServer(RPC): return self.rest_dump({'strategies': [x['name'] for x in strategy_objs]}) + @require_login + @rpc_catch_errors + def _get_strategy(self, strategy: str): + """ + Get a single strategy + get: + parameters: + - strategy: Only get this strategy + """ + config = deepcopy(self._config) + from freqtrade.resolvers.strategy_resolver import StrategyResolver + strategy_obj = StrategyResolver._load_strategy(strategy, config, None) + + return self.rest_dump({ + 'strategy': strategy_obj.get_strategy_name(), + 'code': strategy_obj.__code__, + }) + @require_login @rpc_catch_errors def _list_available_pairs(self):