From 082fb11bbedde8a9521f72c1018a1f2ba73357fc Mon Sep 17 00:00:00 2001 From: Kamontat Chantrachirathumrong Date: Thu, 20 May 2021 01:54:48 +0700 Subject: [PATCH 1/5] Avoid having error `cannot set a frame with no defined index and a scalar` --- freqtrade/optimize/backtesting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index c6b05c8c6..e6172282b 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -191,8 +191,8 @@ class Backtesting: data: Dict = {} # Create dict with data for pair, pair_data in processed.items(): - pair_data.loc[:, 'buy'] = 0 # cleanup from previous run - pair_data.loc[:, 'sell'] = 0 # cleanup from previous run + pair_data['buy'] = 0 # cleanup from previous run + pair_data['sell'] = 0 # cleanup from previous run df_analyzed = self.strategy.advise_sell( self.strategy.advise_buy(pair_data, {'pair': pair}), {'pair': pair})[headers].copy() From 48210170e7a562205f459dd3568f810417c021fd Mon Sep 17 00:00:00 2001 From: Kamontat Chantrachirathumrong Date: Thu, 20 May 2021 11:49:25 +0700 Subject: [PATCH 2/5] wrap with is not empty --- freqtrade/optimize/backtesting.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index e6172282b..955b9e614 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -191,8 +191,9 @@ class Backtesting: data: Dict = {} # Create dict with data for pair, pair_data in processed.items(): - pair_data['buy'] = 0 # cleanup from previous run - pair_data['sell'] = 0 # cleanup from previous run + if not pair_data.empty: + pair_data.loc[:, 'buy'] = 0 # cleanup if buy_signal is exist + pair_data.loc[:, 'sell'] = 0 # cleanup if sell_signal is exist df_analyzed = self.strategy.advise_sell( self.strategy.advise_buy(pair_data, {'pair': pair}), {'pair': pair})[headers].copy() From 1b3bfb2e7fcb21914857cc103d4873377e3dd7d2 Mon Sep 17 00:00:00 2001 From: Kamontat Chantrachirathumrong Date: Thu, 20 May 2021 11:50:15 +0700 Subject: [PATCH 3/5] found root cause. --- freqtrade/optimize/hyperopt.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index 326ae144c..0250935b9 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -349,19 +349,25 @@ class Hyperopt: def prepare_hyperopt_data(self) -> None: data, timerange = self.backtesting.load_bt_data() logger.info("Dataload complete. Calculating indicators") - preprocessed = self.backtesting.strategy.ohlcvdata_to_dataframe(data) + processed: Dict[str, DataFrame] = {} + preprocessed = self.backtesting.strategy.ohlcvdata_to_dataframe(data) # Trim startup period from analyzed dataframe for pair, df in preprocessed.items(): - preprocessed[pair] = trim_dataframe(df, timerange, - startup_candles=self.backtesting.required_startup) - self.min_date, self.max_date = get_timerange(preprocessed) + trimed_df = trim_dataframe(df, timerange, + startup_candles=self.backtesting.required_startup) + if not trimed_df.empty: + processed[pair] = trimed_df + else: + logger.warn(f'Pair {pair} got removed because triming dataframe left nothing') + + self.min_date, self.max_date = get_timerange(processed) logger.info(f'Hyperopting with data from {self.min_date.strftime(DATETIME_PRINT_FORMAT)} ' f'up to {self.max_date.strftime(DATETIME_PRINT_FORMAT)} ' f'({(self.max_date - self.min_date).days} days)..') - dump(preprocessed, self.data_pickle_file) + dump(processed, self.data_pickle_file) def start(self) -> None: self.random_state = self._set_random_state(self.config.get('hyperopt_random_state', None)) From c2b9da68e14d96c1b012170a7de69be83e28352f Mon Sep 17 00:00:00 2001 From: Kamontat Chantrachirathumrong Date: Thu, 20 May 2021 11:56:11 +0700 Subject: [PATCH 4/5] fix indent --- freqtrade/optimize/backtesting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 955b9e614..0144d934b 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -192,8 +192,8 @@ class Backtesting: # Create dict with data for pair, pair_data in processed.items(): if not pair_data.empty: - pair_data.loc[:, 'buy'] = 0 # cleanup if buy_signal is exist - pair_data.loc[:, 'sell'] = 0 # cleanup if sell_signal is exist + pair_data.loc[:, 'buy'] = 0 # cleanup if buy_signal is exist + pair_data.loc[:, 'sell'] = 0 # cleanup if sell_signal is exist df_analyzed = self.strategy.advise_sell( self.strategy.advise_buy(pair_data, {'pair': pair}), {'pair': pair})[headers].copy() From 6172e67fcd0693f292bf032acbb2383ce7b65c72 Mon Sep 17 00:00:00 2001 From: Kamontat Chantrachirathumrong Date: Thu, 20 May 2021 11:56:31 +0700 Subject: [PATCH 5/5] Update hyperopt.py --- freqtrade/optimize/hyperopt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index 0250935b9..3d4588145 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -357,9 +357,9 @@ class Hyperopt: trimed_df = trim_dataframe(df, timerange, startup_candles=self.backtesting.required_startup) if not trimed_df.empty: - processed[pair] = trimed_df + processed[pair] = trimed_df else: - logger.warn(f'Pair {pair} got removed because triming dataframe left nothing') + logger.warn(f'Pair {pair} got removed because triming dataframe left nothing') self.min_date, self.max_date = get_timerange(processed)