Hi,
I'm pretty new to Quanconnect platform, thus I'm having a difficult time implementing my trading algorithm on this platform. I have been creating most of my algorithms through Python in Quantopian, but since they removed integration I have decided to use Quantconnect.
Being said, I have an example of Bollinger Bands With Trading Algo, when I try to backtest my algorithm on Quantconnect I get a lot of errors and I'm not sure how to fix it. Can someone show/explain me a very basic structure on how to backtest my algo on this platform?
Please see the code below.
Thanks,
Nikita
Douglas Stridsberg
Hi Nikita,
Welcome!
The code you mention doesn't seem to have been included in the post, do you mind posting it?
Also, have you taken a look at the indicator suite example in the repository? It should contain the basics on how to use BBs in a strategy.
Halldor Andersen
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.
Nikita Douglass
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!