Fix exception in plotting when no trades where generated

as seen in #4981
This commit is contained in:
Matthias 2021-05-22 17:03:16 +02:00
parent feb836eaf6
commit a7bd8b0aa5
2 changed files with 33 additions and 27 deletions

View File

@ -156,33 +156,35 @@ def load_backtest_data(filename: Union[Path, str], strategy: Optional[str] = Non
data = data['strategy'][strategy]['trades'] data = data['strategy'][strategy]['trades']
df = pd.DataFrame(data) df = pd.DataFrame(data)
df['open_date'] = pd.to_datetime(df['open_date'], if not df.empty:
utc=True, df['open_date'] = pd.to_datetime(df['open_date'],
infer_datetime_format=True utc=True,
) infer_datetime_format=True
df['close_date'] = pd.to_datetime(df['close_date'], )
utc=True, df['close_date'] = pd.to_datetime(df['close_date'],
infer_datetime_format=True utc=True,
) infer_datetime_format=True
)
else: else:
# old format - only with lists. # old format - only with lists.
df = pd.DataFrame(data, columns=BT_DATA_COLUMNS_OLD) df = pd.DataFrame(data, columns=BT_DATA_COLUMNS_OLD)
if not df.empty:
df['open_date'] = pd.to_datetime(df['open_date'], df['open_date'] = pd.to_datetime(df['open_date'],
unit='s', unit='s',
utc=True, utc=True,
infer_datetime_format=True infer_datetime_format=True
) )
df['close_date'] = pd.to_datetime(df['close_date'], df['close_date'] = pd.to_datetime(df['close_date'],
unit='s', unit='s',
utc=True, utc=True,
infer_datetime_format=True infer_datetime_format=True
) )
# Create compatibility with new format # Create compatibility with new format
df['profit_abs'] = df['close_rate'] - df['open_rate'] df['profit_abs'] = df['close_rate'] - df['open_rate']
if 'profit_ratio' not in df.columns: if not df.empty:
df['profit_ratio'] = df['profit_percent'] if 'profit_ratio' not in df.columns:
df = df.sort_values("open_date").reset_index(drop=True) df['profit_ratio'] = df['profit_percent']
df = df.sort_values("open_date").reset_index(drop=True)
return df return df

View File

@ -77,7 +77,8 @@ def init_plotscript(config, markets: List, startup_candles: int = 0):
) )
except ValueError as e: except ValueError as e:
raise OperationalException(e) from e raise OperationalException(e) from e
trades = trim_dataframe(trades, timerange, 'open_date') if not trades.empty:
trades = trim_dataframe(trades, timerange, 'open_date')
return {"ohlcv": data, return {"ohlcv": data,
"trades": trades, "trades": trades,
@ -540,8 +541,11 @@ def load_and_plot_trades(config: Dict[str, Any]):
df_analyzed = strategy.analyze_ticker(data, {'pair': pair}) df_analyzed = strategy.analyze_ticker(data, {'pair': pair})
df_analyzed = trim_dataframe(df_analyzed, timerange) df_analyzed = trim_dataframe(df_analyzed, timerange)
trades_pair = trades.loc[trades['pair'] == pair] if not trades.empty:
trades_pair = extract_trades_of_period(df_analyzed, trades_pair) trades_pair = trades.loc[trades['pair'] == pair]
trades_pair = extract_trades_of_period(df_analyzed, trades_pair)
else:
trades_pair = trades
fig = generate_candlestick_graph( fig = generate_candlestick_graph(
pair=pair, pair=pair,