Add strategy code to __code__

This commit is contained in:
Matthias 2020-09-17 06:56:51 +02:00
parent 4b6b7f8343
commit ba10bd7756
2 changed files with 32 additions and 10 deletions

View File

@ -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

View File

@ -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/<string: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):