Open
Conversation
Owner
|
@devashish24460 Thanks for the contribution. A few things to preserve structure of how backtesting works in the dashboard :
No need to download the data yourself either , the dashboard handles that for you as well. Rest, Strategy LGTM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.