From c0654f3caf2b07fcff4a9bd51b6ba4462af43771 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 24 Aug 2020 20:38:01 +0200 Subject: [PATCH] Add resiliancy against not having a analyzed dataframe yet --- freqtrade/rpc/rpc.py | 46 +++++++++++++++++++++------------ tests/rpc/test_rpc_apiserver.py | 10 +++++++ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index d3df9f4bd..94a4bbed3 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -657,19 +657,21 @@ class RPC: return self._freqtrade.edge.accepted_pairs() def _convert_dataframe_to_dict(self, strategy: str, pair: str, dataframe: DataFrame, - last_analyzed: datetime): + last_analyzed: datetime) -> Dict[str, Any]: + has_content = len(dataframe) != 0 + if has_content: - dataframe.loc[:, '__date_ts'] = dataframe.loc[:, 'date'].astype(int64) // 1000 // 1000 - # Move open to seperate column when signal for easy plotting - if 'buy' in dataframe.columns: - buy_mask = (dataframe['buy'] == 1) - dataframe.loc[buy_mask, '_buy_signal_open'] = dataframe.loc[buy_mask, 'open'] - if 'sell' in dataframe.columns: - sell_mask = (dataframe['sell'] == 1) - dataframe.loc[sell_mask, '_sell_signal_open'] = dataframe.loc[sell_mask, 'open'] - dataframe = dataframe.replace({NAN: None}) + dataframe.loc[:, '__date_ts'] = dataframe.loc[:, 'date'].astype(int64) // 1000 // 1000 + # Move open to seperate column when signal for easy plotting + if 'buy' in dataframe.columns: + buy_mask = (dataframe['buy'] == 1) + dataframe.loc[buy_mask, '_buy_signal_open'] = dataframe.loc[buy_mask, 'open'] + if 'sell' in dataframe.columns: + sell_mask = (dataframe['sell'] == 1) + dataframe.loc[sell_mask, '_sell_signal_open'] = dataframe.loc[sell_mask, 'open'] + dataframe = dataframe.replace({NAN: None}) - return { + res = { 'pair': pair, 'strategy': strategy, 'columns': list(dataframe.columns), @@ -677,17 +679,27 @@ class RPC: 'length': len(dataframe), 'last_analyzed': last_analyzed, 'last_analyzed_ts': int(last_analyzed.timestamp()), - 'data_start': str(dataframe.iloc[0]['date']), - 'data_start_ts': int(dataframe.iloc[0]['__date_ts']), - 'data_stop': str(dataframe.iloc[-1]['date']), - 'data_stop_ts': int(dataframe.iloc[-1]['__date_ts']), + 'data_start': '', + 'data_start_ts': 0, + 'data_stop': '', + 'data_stop_ts': 0, } + if has_content: + res.update({ + 'data_start': str(dataframe.iloc[0]['date']), + 'data_start_ts': int(dataframe.iloc[0]['__date_ts']), + 'data_stop': str(dataframe.iloc[-1]['date']), + 'data_stop_ts': int(dataframe.iloc[-1]['__date_ts']), + }) + return res def _analysed_dataframe(self, pair: str, timeframe: str, limit: int) -> Dict[str, Any]: - _data, last_analyzed = self._freqtrade.dataprovider.get_analyzed_dataframe(pair, timeframe) + _data, last_analyzed = self._freqtrade.dataprovider.get_analyzed_dataframe( + pair, timeframe) + _data = _data.copy() if limit: - _data = _data.iloc[-limit:].copy() + _data = _data.iloc[-limit:] return self._convert_dataframe_to_dict(self._freqtrade.config['strategy'], pair, _data, last_analyzed) diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index d6c938373..b98483061 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -817,6 +817,16 @@ def test_api_pair_candles(botclient, ohlcv_history): ftbot, client = botclient timeframe = '5m' amount = 2 + + rc = client_get(client, + f"{BASE_URI}/pair_candles?limit={amount}&pair=XRP%2FBTC&timeframe={timeframe}") + assert_response(rc) + assert 'columns' in rc.json + assert 'data_start_ts' in rc.json + assert 'data_start' in rc.json + assert 'data_stop' in rc.json + assert 'data_stop_ts' in rc.json + assert len(rc.json['data']) == 0 ohlcv_history['sma'] = ohlcv_history['close'].rolling(2).mean() ohlcv_history['buy'] = 0 ohlcv_history.iloc[1]['buy'] = 1