Hi Nikita.
Welcome to QuantConnect! I suggest you complete our BootCamp section and read the documentation section. There are several examples in posts on the discussion forum on how to implement an algorithm that uses Bollinger Bands to emit trading signals, such as this one.
First, construct the Bollinger Band indicator in the method Initialize() and warm up the indicator:
## In Initialize()
# Set Boilinger Bands
self.bband = self.BB("SPY", 20, 2, MovingAverageType.Simple, Resolution.Daily)
# Set WarmUp period
self.SetWarmUp(20)
Retrieve the price and the current indicator values, for the upper band and the middle band in the method OnData() and implement your trading logic:
# Retrieve current price
price = self.Securities["SPY"].Price
# Sell if price is higher than upper band
if not self.Portfolio.Invested and price > self.bband.UpperBand.Current.Value:
self.SetHoldings("SPY",-1)
# Liquidate if price is lower than middle band
if self.Portfolio.Invested and price < self.bband.MiddleBand.Current.Value:
self.Liquidate()
In the attached backtest I demonstrate how to put on a short position if the price exceeds the upper Bollinger band. The algorithm liquidates the position when the price is lower than the middle-band. This example can be extended and used on the long side.