Skip to content

Add bollinger strategy#23

Open
devashish24460 wants to merge 2 commits intohimanshu2406:mainfrom
devashish24460:add-bollinger-strategy
Open

Add bollinger strategy#23
devashish24460 wants to merge 2 commits intohimanshu2406:mainfrom
devashish24460:add-bollinger-strategy

Conversation

@devashish24460
Copy link

This pull request introduces the Bollinger Bands Strategy for backtesting stock trading strategies using historical data. The following changes have been made:

Custom Rolling Functions:

We defined two custom functions:

rolling_mean(x, window) — calculates the rolling mean (simple moving average) over a specified window size.

rolling_std(x, window) — calculates the rolling standard deviation over the same window.

Bollinger Bands Strategy:

A custom Bollinger Bands strategy was implemented using the Backtest and Strategy classes from the backtesting library. This strategy includes:

Moving Average (MA): A 20-day rolling simple moving average calculated using the custom rolling_mean function.

Upper and Lower Bands: The upper and lower Bollinger Bands are calculated as the moving average plus and minus two times the rolling standard deviation, respectively.

Buy and Sell Logic:

Buy: If the closing price is below the lower Bollinger Band, a buy signal is generated.

Sell: If the closing price is above the upper Bollinger Band, a sell signal is generated.

If the position is open and the price is between the bands, the strategy will close any existing position.

Backtesting:

We fetched historical stock data using the yfinance library (for AAPL by default) from January 1, 2024, to May 6, 2025.

The fetched data includes the following columns: Open, High, Low, Close, and Volume.

We then applied the Bollinger Bands strategy to this data using the Backtest class.

After running the backtest, the key statistics (such as return, Sharpe ratio, max drawdown, etc.) were printed.

Results Export:

The backtest results, including trade data and performance statistics, were exported to CSV files:

AAPL_trades.csv: Contains detailed trade information.

AAPL_backtest_summary.csv: Contains summary statistics of the backtest.

Additionally, the results were plotted in an interactive chart (BollingerBandsStrategy.html) showing the performance of the strategy over time.

@himanshu2406
Copy link
Owner

@devashish24460 Thanks for the contribution.

A few things to preserve structure of how backtesting works in the dashboard :

  • You seem to be using the backtesting.py library here which uses an event driven architecture and would be pretty slow for multi-asset backtests that Algo Py supports natively.
  • Add the strategy to strategy/public/ directory and use the strategy base class instead , refer : https://github.com/himanshu2406/Algo.Py/blob/main/strategy/public/EmaStrat.py
  • when defining the run function instead of using for loops , try to generate entry and exit signals using vectorized operations instead.
  • After implementing the function here , you can pull up the dashboard and your strategy will automagically be populated in the backtesting tab - you can test it from there.

No need to download the data yourself either , the dashboard handles that for you as well.

Rest, Strategy LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants