top of page

Buy-Sell Signals:
Bollinger Bands

The project creates buy and sell signals for the selected ticker, timeframe, and band length using Bollinger Bands.

In order to avoid the look-ahead bias, the previous 1 and 2 days have been taken into account in each data point.

The project additionally applies trailing stop-loss in which the stop-loss percentage could be determined by the user.

The strategy is as follows:

Open Long position if;

-Price crosses above the lower band

Open Short position if;

-Price crosses below the upper band

Importing required libraries:

Screen Shot 2022-09-13 at 19.53.58.png

Getting historical data and calculating Bollinger Bands:

Screen Shot 2022-09-13 at 19.54.25.png

Defining buy and sell conditions:

Screen Shot 2022-09-13 at 19.56.04.png

Adding stop-loss and trailing stop-loss:

Screen Shot 2022-09-13 at 19.56.23.png

Defining a function to plot the chart and signals:

Screen Shot 2022-09-13 at 19.56.39.png

Final output where the price chart, Bollinger Bands, and buy-sell signals are plotted:

Ticker, timeframe, time interval, band_length, standard deviation, and stop-loss percentage could be changed as desired.

Screen Shot 2022-09-13 at 19.56.54.png
bottom of page