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!
Prescott
import clr clr.AddReference("System") clr.AddReference("QuantConnect.Algorithm") clr.AddReference("QuantConnect.Indicators") clr.AddReference("QuantConnect.Common") 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.SetBrokerageModel(BrokerageName.GDAX) self.AddCrypto("BTCUSD", Resolution.Daily) # create a bollinger band self.Bolband = self.BB("BTCUSD", 20, 2, MovingAverageType.Simple, Resolution.Daily); # set warmup period self.SetWarmUp(20) 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: self.Liquidate("BTCUSD")
Jing Wu
Hi prescott, I changed the Current.Value to the close price. It is working now.
Prescott
Thank you so much Jing Wu!
Liquidgenius
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:
Sincerely,
John Ziebro
Liquidgenius
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.
Prescott
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
Sebastian Rojas
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?
Aalap Sharma
Has anyone tried to use this to trade all three cryptos at the same time?
Prescott
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!