From a43c088448f4f95fe57caaa68ded70803f7fa368 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 22 Jan 2022 07:11:59 +0100 Subject: [PATCH] Allow @informative in webserver mode --- freqtrade/rpc/api_server/api_v1.py | 8 +++++--- freqtrade/rpc/api_server/deps.py | 10 ++++++++++ freqtrade/rpc/api_server/webserver.py | 2 ++ freqtrade/rpc/rpc.py | 4 ++-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/freqtrade/rpc/api_server/api_v1.py b/freqtrade/rpc/api_server/api_v1.py index 1c1ff39df..648ec6c57 100644 --- a/freqtrade/rpc/api_server/api_v1.py +++ b/freqtrade/rpc/api_server/api_v1.py @@ -20,7 +20,7 @@ from freqtrade.rpc.api_server.api_schemas import (AvailablePairs, Balances, Blac Stats, StatusMsg, StrategyListResponse, StrategyResponse, SysInfo, Version, WhitelistResponse) -from freqtrade.rpc.api_server.deps import get_config, get_rpc, get_rpc_optional +from freqtrade.rpc.api_server.deps import get_config, get_exchange, get_rpc, get_rpc_optional from freqtrade.rpc.rpc import RPCException @@ -217,12 +217,14 @@ def pair_candles(pair: str, timeframe: str, limit: Optional[int], rpc: RPC = Dep @router.get('/pair_history', response_model=PairHistory, tags=['candle data']) def pair_history(pair: str, timeframe: str, timerange: str, strategy: str, - config=Depends(get_config)): + config=Depends(get_config), exchange=Depends(get_exchange)): + # The initial call to this endpoint can be slow, as it may need to initialize + # the exchange class. config = deepcopy(config) config.update({ 'strategy': strategy, }) - return RPC._rpc_analysed_history_full(config, pair, timeframe, timerange) + return RPC._rpc_analysed_history_full(config, pair, timeframe, timerange, exchange) @router.get('/plot_config', response_model=PlotConfig, tags=['candle data']) diff --git a/freqtrade/rpc/api_server/deps.py b/freqtrade/rpc/api_server/deps.py index 16f9a78c0..b428d9c6d 100644 --- a/freqtrade/rpc/api_server/deps.py +++ b/freqtrade/rpc/api_server/deps.py @@ -1,5 +1,7 @@ from typing import Any, Dict, Iterator, Optional +from fastapi import Depends + from freqtrade.persistence import Trade from freqtrade.rpc.rpc import RPC, RPCException @@ -28,3 +30,11 @@ def get_config() -> Dict[str, Any]: def get_api_config() -> Dict[str, Any]: return ApiServer._config['api_server'] + + +def get_exchange(config=Depends(get_config)): + if not ApiServer._exchange: + from freqtrade.resolvers import ExchangeResolver + ApiServer._exchange = ExchangeResolver.load_exchange( + config['exchange']['name'], config) + return ApiServer._exchange diff --git a/freqtrade/rpc/api_server/webserver.py b/freqtrade/rpc/api_server/webserver.py index 235063191..63812f52f 100644 --- a/freqtrade/rpc/api_server/webserver.py +++ b/freqtrade/rpc/api_server/webserver.py @@ -41,6 +41,8 @@ class ApiServer(RPCHandler): _has_rpc: bool = False _bgtask_running: bool = False _config: Dict[str, Any] = {} + # Exchange - only available in webserver mode. + _exchange = None def __new__(cls, *args, **kwargs): """ diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index e969aa0a7..2232496de 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -995,7 +995,7 @@ class RPC: @staticmethod def _rpc_analysed_history_full(config, pair: str, timeframe: str, - timerange: str) -> Dict[str, Any]: + timerange: str, exchange) -> Dict[str, Any]: timerange_parsed = TimeRange.parse_timerange(timerange) _data = load_data( @@ -1010,7 +1010,7 @@ class RPC: from freqtrade.data.dataprovider import DataProvider from freqtrade.resolvers.strategy_resolver import StrategyResolver strategy = StrategyResolver.load_strategy(config) - strategy.dp = DataProvider(config, exchange=None, pairlists=None) + strategy.dp = DataProvider(config, exchange=exchange, pairlists=None) df_analyzed = strategy.analyze_ticker(_data[pair], {'pair': pair})