From 6e965a2924bb922a9979def705e0abb90ea734ea Mon Sep 17 00:00:00 2001 From: "Bobby (aider)" Date: Thu, 13 Feb 2025 23:26:16 -0800 Subject: [PATCH] fix: Simplify Bollinger Bands calculation and remove debug statements --- src/pages/backtesting/backtesting_page.py | 34 +++++------------------ 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/src/pages/backtesting/backtesting_page.py b/src/pages/backtesting/backtesting_page.py index 8e42014..b7523de 100644 --- a/src/pages/backtesting/backtesting_page.py +++ b/src/pages/backtesting/backtesting_page.py @@ -56,42 +56,22 @@ class DynamicStrategy(Strategy): std = float(ind_config['params']['std']) def bb_calc(x): - # Debug print input data - print(f"BB input data first few values: {x[:5]}") + # Calculate BB using pandas-ta + bb_result = ta.bbands(close=pd.Series(x), length=length, std=std) - # Calculate BB directly using pandas-ta - bb_result = ta.bbands(close=x, length=length, std=std) - - # Debug print the result - print(f"BB calculation direct result type: {type(bb_result)}") - print(f"BB calculation direct result:\n{bb_result.head() if hasattr(bb_result, 'head') else bb_result}") - - if bb_result is None: - print("BB calculation returned None") + if bb_result is None or bb_result.empty: return np.zeros(len(x)), np.zeros(len(x)), np.zeros(len(x)) - # Get column names - bb_cols = bb_result.columns if hasattr(bb_result, 'columns') else [] - print(f"BB result columns: {bb_cols}") - - # Find the correct column names - upper_col = [col for col in bb_cols if 'BBU_' in col][0] - middle_col = [col for col in bb_cols if 'BBM_' in col][0] - lower_col = [col for col in bb_cols if 'BBL_' in col][0] - - print(f"Found BB columns - Upper: {upper_col}, Middle: {middle_col}, Lower: {lower_col}") + # Get the column names (they should be BBL, BBM, BBU) + upper_col = f'BBU_{length}_{std}.0' + middle_col = f'BBM_{length}_{std}.0' + lower_col = f'BBL_{length}_{std}.0' # Extract and process the values upper = bb_result[upper_col].fillna(method='ffill').fillna(0).values middle = bb_result[middle_col].fillna(method='ffill').fillna(0).values lower = bb_result[lower_col].fillna(method='ffill').fillna(0).values - # Debug print the processed values - print(f"Processed BB values first few rows:") - print(f"Upper: {upper[:5]}") - print(f"Middle: {middle[:5]}") - print(f"Lower: {lower[:5]}") - return upper, middle, lower bb_vals = self.I(bb_calc, self.data.Close)