From da0992f85990035bb77d566b067ff8ce6b39bdf8 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 Jan 2023 06:45:31 +0100 Subject: [PATCH 1/5] add Config typehint in rpc --- freqtrade/rpc/rpc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index 32563376c..e169b9c4d 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -1127,12 +1127,12 @@ class RPC: return self._freqtrade.active_pair_whitelist @staticmethod - def _rpc_analysed_history_full(config, pair: str, timeframe: str, + def _rpc_analysed_history_full(config: Config, pair: str, timeframe: str, timerange: str, exchange) -> Dict[str, Any]: timerange_parsed = TimeRange.parse_timerange(timerange) _data = load_data( - datadir=config.get("datadir"), + datadir=config["datadir"], pairs=[pair], timeframe=timeframe, timerange=timerange_parsed, From 3216a05a9edbfa66bafa046075d025ded8659075 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 Jan 2023 18:15:07 +0100 Subject: [PATCH 2/5] Enable plot_config to work in webserver mode (requires strategy argument) --- freqtrade/rpc/api_server/api_v1.py | 14 ++++++++++++-- freqtrade/rpc/rpc.py | 10 ++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/freqtrade/rpc/api_server/api_v1.py b/freqtrade/rpc/api_server/api_v1.py index e26df6eea..42cede0d0 100644 --- a/freqtrade/rpc/api_server/api_v1.py +++ b/freqtrade/rpc/api_server/api_v1.py @@ -248,8 +248,18 @@ def pair_history(pair: str, timeframe: str, timerange: str, strategy: str, @router.get('/plot_config', response_model=PlotConfig, tags=['candle data']) -def plot_config(rpc: RPC = Depends(get_rpc)): - return PlotConfig.parse_obj(rpc._rpc_plot_config()) +def plot_config(strategy: Optional[str] = None, config=Depends(get_config), + rpc: Optional[RPC] = Depends(get_rpc_optional)): + if not strategy: + if not rpc: + raise RPCException("Strategy is mandatory in webserver mode.") + return PlotConfig.parse_obj(rpc._rpc_plot_config()) + else: + config1 = deepcopy(config) + config1.update({ + 'strategy': strategy + }) + return PlotConfig.parse_obj(RPC._rpc_plot_config_with_strategy(config1)) @router.get('/strategies', response_model=StrategyListResponse, tags=['strategy']) diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index e169b9c4d..0201c6e45 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -1157,6 +1157,16 @@ class RPC: self._freqtrade.strategy.plot_config['subplots'] = {} return self._freqtrade.strategy.plot_config + @staticmethod + def _rpc_plot_config_with_strategy(config: Config) -> Dict[str, Any]: + + from freqtrade.resolvers.strategy_resolver import StrategyResolver + strategy = StrategyResolver.load_strategy(config) + + if (strategy.plot_config and 'subplots' not in strategy.plot_config): + strategy.plot_config['subplots'] = {} + return strategy.plot_config + @staticmethod def _rpc_sysinfo() -> Dict[str, Any]: return { From 2298656e45559403dce4139484e7bbb83e1be2b5 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 Jan 2023 18:15:14 +0100 Subject: [PATCH 3/5] Bump api_version to 2.23 --- freqtrade/rpc/api_server/api_v1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/freqtrade/rpc/api_server/api_v1.py b/freqtrade/rpc/api_server/api_v1.py index 42cede0d0..b64f6c0e8 100644 --- a/freqtrade/rpc/api_server/api_v1.py +++ b/freqtrade/rpc/api_server/api_v1.py @@ -40,7 +40,8 @@ logger = logging.getLogger(__name__) # 2.20: Add websocket endpoints # 2.21: Add new_candle messagetype # 2.22: Add FreqAI to backtesting -API_VERSION = 2.22 +# 2.23: Allow plot config request in webserver mode +API_VERSION = 2.23 # Public API, requires no auth. router_public = APIRouter() From 634b80f0e76c12f376731cce4a48049ba81cab0e Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 Jan 2023 18:15:35 +0100 Subject: [PATCH 4/5] Add tests for plotconfig in ws mode --- tests/rpc/test_rpc_apiserver.py | 17 ++++++++++++++++- tests/strategy/strats/hyperoptable_strategy.py | 5 +++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 5b452627b..cd1a988d2 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -1417,7 +1417,7 @@ def test_api_pair_history(botclient, ohlcv_history): "No data for UNITTEST/BTC, 5m in 20200111-20200112 found.") -def test_api_plot_config(botclient): +def test_api_plot_config(botclient, mocker): ftbot, client = botclient rc = client_get(client, f"{BASE_URI}/plot_config") @@ -1441,6 +1441,21 @@ def test_api_plot_config(botclient): assert isinstance(rc.json()['main_plot'], dict) assert isinstance(rc.json()['subplots'], dict) + rc = client_get(client, f"{BASE_URI}/plot_config?strategy=freqai_test_classifier") + assert_response(rc) + res = rc.json() + assert 'target_roi' in res['subplots'] + assert 'do_predict' in res['subplots'] + + rc = client_get(client, f"{BASE_URI}/plot_config?strategy=HyperoptableStrategy") + assert_response(rc) + assert rc.json()['subplots'] == {} + + mocker.patch('freqtrade.rpc.api_server.api_v1.get_rpc_optional', return_value=None) + + rc = client_get(client, f"{BASE_URI}/plot_config") + assert_response(rc) + def test_api_strategies(botclient, tmpdir): ftbot, client = botclient diff --git a/tests/strategy/strats/hyperoptable_strategy.py b/tests/strategy/strats/hyperoptable_strategy.py index 9850a5675..eadbc533f 100644 --- a/tests/strategy/strats/hyperoptable_strategy.py +++ b/tests/strategy/strats/hyperoptable_strategy.py @@ -34,6 +34,11 @@ class HyperoptableStrategy(StrategyTestV3): protection_enabled = BooleanParameter(default=True) protection_cooldown_lookback = IntParameter([0, 50], default=30) + # Invalid plot config ... + plot_config = { + "main_plot": {}, + } + @property def protections(self): prot = [] From 892fb77ec39287750967888991afb29f03906d80 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 18 Jan 2023 19:31:20 +0100 Subject: [PATCH 5/5] Update mypy pre-commit hook --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 306e4bbda..4a70fc3a6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: # stages: [push] - repo: https://github.com/pre-commit/mirrors-mypy - rev: "v0.942" + rev: "v0.991" hooks: - id: mypy exclude: build_helpers