From 1a984ac6774a9e7e20105580cc157f5ec482be45 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 21 May 2020 07:12:23 +0200 Subject: [PATCH 1/2] Explicitly raise ValueError if trades are empty --- freqtrade/data/btanalysis.py | 3 +++ tests/data/test_btanalysis.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/freqtrade/data/btanalysis.py b/freqtrade/data/btanalysis.py index b0c642c1d..f98135c27 100644 --- a/freqtrade/data/btanalysis.py +++ b/freqtrade/data/btanalysis.py @@ -194,7 +194,10 @@ def create_cum_profit(df: pd.DataFrame, trades: pd.DataFrame, col_name: str, :param col_name: Column name that will be assigned the results :param timeframe: Timeframe used during the operations :return: Returns df with one additional column, col_name, containing the cumulative profit. + :raise: ValueError if trade-dataframe was found empty. """ + if len(trades) == 0: + raise ValueError("Trade dataframe empty.") from freqtrade.exchange import timeframe_to_minutes timeframe_minutes = timeframe_to_minutes(timeframe) # Resample to timeframe to make sure trades match candles diff --git a/tests/data/test_btanalysis.py b/tests/data/test_btanalysis.py index 4da2acc5e..50cf9db3d 100644 --- a/tests/data/test_btanalysis.py +++ b/tests/data/test_btanalysis.py @@ -178,6 +178,10 @@ def test_create_cum_profit1(testdatadir): assert cum_profits.iloc[0]['cum_profits'] == 0 assert cum_profits.iloc[-1]['cum_profits'] == 0.0798005 + with pytest.raises(ValueError, match='Trade dataframe empty.'): + create_cum_profit(df.set_index('date'), bt_data[bt_data["pair"] == 'NOTAPAIR'], + "cum_profits", timeframe="5m") + def test_calculate_max_drawdown(testdatadir): filename = testdatadir / "backtest-result_test.json" From 1f386c570d2f2ed109f7a3151fdad8f2462a7fec Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 21 May 2020 07:13:08 +0200 Subject: [PATCH 2/2] Don't start plotting profit without trades plotting profit only makes sense when trades are available --- freqtrade/plot/plotting.py | 6 +++++- tests/test_plotting.py | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/freqtrade/plot/plotting.py b/freqtrade/plot/plotting.py index 60f838db2..095ca4133 100644 --- a/freqtrade/plot/plotting.py +++ b/freqtrade/plot/plotting.py @@ -10,8 +10,9 @@ from freqtrade.data.btanalysis import (calculate_max_drawdown, create_cum_profit, extract_trades_of_period, load_trades) from freqtrade.data.converter import trim_dataframe -from freqtrade.exchange import timeframe_to_prev_date from freqtrade.data.history import load_data +from freqtrade.exceptions import OperationalException +from freqtrade.exchange import timeframe_to_prev_date from freqtrade.misc import pair_to_filename from freqtrade.resolvers import StrategyResolver @@ -504,6 +505,9 @@ def plot_profit(config: Dict[str, Any]) -> None: trades = trades[(trades['pair'].isin(plot_elements["pairs"])) & (~trades['close_time'].isnull()) ] + if len(trades) == 0: + raise OperationalException("No trades found, cannot generate Profit-plot without " + "trades from either Backtest result or database.") # Create an average close price of all the pairs that were involved. # this could be useful to gauge the overall market trend diff --git a/tests/test_plotting.py b/tests/test_plotting.py index 0258b94d1..5bb113784 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -374,7 +374,7 @@ def test_start_plot_profit_error(mocker): def test_plot_profit(default_conf, mocker, testdatadir, caplog): default_conf['trade_source'] = 'file' default_conf["datadir"] = testdatadir - default_conf['exportfilename'] = testdatadir / "backtest-result_test.json" + default_conf['exportfilename'] = testdatadir / "backtest-result_test_nofile.json" default_conf['pairs'] = ["ETH/BTC", "LTC/BTC"] profit_mock = MagicMock() @@ -384,6 +384,12 @@ def test_plot_profit(default_conf, mocker, testdatadir, caplog): generate_profit_graph=profit_mock, store_plot_file=store_mock ) + with pytest.raises(OperationalException, + match=r"No trades found, cannot generate Profit-plot.*"): + plot_profit(default_conf) + + default_conf['exportfilename'] = testdatadir / "backtest-result_test.json" + plot_profit(default_conf) # Plot-profit generates one combined plot