Crypto Bollinger Band Strategy

Hi new to QuantConnect, I was just trying to play around with the new GDAX API. I wanted to create a simple breakout strategy using Python that is buying BTC when daily price closes above the Upper band of the Bollinger Band and sell it when price closes below the Middle Band. I can't seem to get Bollinger Bands to work with BTC or any other product and the documentation seems incomplete. Any help would be greatly appreciated! Thanks!

Update Backtest

import clr

from System import *
import numpy as np
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d

### <summary>
### In this example we are looking for price to breakout above the bollinger bands
### and look to buy when we see that. We hold our position until price touches the
### middle band of the bollinger bands.

class BollingerBreakoutAlgorithm(QCAlgorithm):

def Initialize(self):

self.SetStartDate(2016, 6, 1) #Set Start Date
self.SetEndDate(2017, 7, 1) #Set End Date
self.SetCash(10000) #Set Strategy Cash
self.AddCrypto("BTCUSD", Resolution.Daily)

# create a bollinger band
self.Bolband = self.BB("BTCUSD", 20, 2, MovingAverageType.Simple, Resolution.Daily);

# set warmup period

def OnData(self, data):

holdings = self.Portfolio["BTCUSD"].Quantity
price = self.Securities[self.target_crypto].Price

# buy if price closes above upper bollinger band
if holdings <= 0:
if Current.Value > self.Bolband.LowerBand.Current.Value:
self.SetHoldings("BTCUSD", 1.0)

# sell if price closes below middle bollinger band
if holdings > 0 and Current.Value < self.Bolband.MiddleBand.Current.Value:

Hi prescott, I changed the Current.Value to the close price. It is working now.


Thank you so much Jing Wu! 


Hi Prescott and Jing Wu,

I generally review public crypto algos to see what I can learn. Thanks for the insight on Bollinger usage. I added a few features that might be helpful:

  • Bollinger inidcators charted under "Indicators" in Backtesting
  • Momentum indicator charted under "Indicators" in Backtesting
  • Created a single variable to hold which crypto being traded on, switch between BTCUSD, ETHUSD or LTCUSD  with only a small edit.
  • Backtested on ETHUSD during the same time period
  • Backtested attached.

John Ziebro


A few additional thoughts:

There are quite a few trades in and then out without much movement, these incur transaction fees. If an indicator can be used to prevent these minimal net trades then savings might be able to be found on transaction fees for additional net gain.

Charting the trades (in and out) along with the daily closing price on the Indicators chart would be helpful to see, however, I have been unsuccesful in my attempts.


Hi Liquidgenius, thanks for your contributions! I think you are right, another indicator here would help eliminate losing trades! I'll have to test some different ideas. Thanks


Awesome. I am fairly new to the world of programming. How would I go about to make it so... it doesn't buy/sell below higher/lower from previous entry?


Update Backtest


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.


This discussion is closed