So I want the value of the Bollinger band and Keltner channel,python, from the current day and the previous day using rolling windows. I tried using the sample code and forum posts but have been unable to apply it to bollinger bands and keltner channel. I am still confused on how to use the rolling windwos.. Can someone point me in the right direction?
Thanks :)
Michael Manus
just post the algo how far you got,..
Paul Park
Oops sorry here it is. I apologize if it looks really bad I was trying to follow the github example.
### <summary> ### Basic template algorithm simply initializes the date range and cash. This is a skeleton ### framework you can use for designing an algorithm. ### </summary> class BasicTemplateAlgorithm(QCAlgorithm): '''Basic template algorithm simply initializes the date range and cash''' def Initialize(self): '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.''' self.SetStartDate(2000,10, 7) #Set Start Date self.SetEndDate(2018,3,11) #Set End Date self.SetCash(100000) #Set Strategy Cash # Find more symbols here: http://quantconnect.com/data self.AddEquity("NVDA", Resolution.Daily) self.Debug("numpy test >>> print numpy.pi: " + str(np.pi)) self.window = RollingWindow[TradeBar](2) # For other security types, use QuoteBar # Creates an indicator and adds to a rolling window when it is updated self.bb("NVDA", 20,1.5).Updated += self.BBUpdated self.BBWindow = RollingWindow[IndicatorDataPoint](2) def SmaUpdated(self, sender, updated): '''Adds updated values to rolling window''' self.BBWindow.Add(updated) def OnData(self, data): '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here. Arguments: data: Slice object keyed by symbol containing the stock data ''' # Add SPY TradeBar in rollling window self.window.Add(data["NVDA"]) # Wait for windows to be ready. if not (self.window.IsReady and self.BBWindow.IsReady): return currBar = self.window[0] # Current bar had index zero. pastBar = self.window[1] '' Different attempt //OnData '' _BBWindow.Add(_bb); '' if(!_BBWindow.IsReady) return; '' //value '' BB_2_days_ago = BBWindow[2];''
Michael Manus
hi, i played a little with python. i am not a pro but when you take a look at some algos on github you get the idea how it is done. I stored the values of the bands in rolling windows because i dont know how to deepcopy the whole keltner channel class to store only one copy in one window.
the indicators update automatically at the daily resolution.
checkout one day the following example to work with more stock data
Paul Park
Thanks for the help! The github link was also very helpful. Appreciate the help
Aaron Martin
Hi, I am quite new to Python and Quant Connect, but shouldn't line 23 be:
def BBUpdated(self, sender, updated):
instead of:Â
def SmaUpdated(self, sender, updated):
Rahul Chowdhury
Hey Aaron,
You are correct. It looks like the original code snippet has a typo on line 23.
There is also an error in the way the BB indicator is defined.
self.bb("NVDA", 20,1.5).Updated += self.BBUpdated
this line of code should be done in two lines.
self.bb = self.BB("NVDA", 20, 1.5) self.bb.Updated += self.BBUpdated
Â
Alvin lim
HiÂ
Can I ask how we can use a variable to add the stock symbol instead of manually adding the ticker?
self.window.Add(data['SPY')
Rahul Chowdhury
Hi Alvin,
If you have many symbols, you can create a dictionary which is keyed by symbol and holds your rolling windows. Using the keys of the dictionary, you can also access the slice data for each symbol.
Here's an example where we add tickers from a list to our data subscriptions and then add their respective symbols to a dictionary of rolling windows. Then we can loop over our dictionary which is keyed by symbols, so that we don't have to manually type in tickers.
class MyAlgo(QCAlgorithm): def Initialize(self): self.SetStartDate(2020, 2, 20) self.SetEndDate(2020, 2, 25) tickers = ["SPY", "TLT", "GOOG", "TSLA", "XOM", "GS", "JNJ", "AMZN"] self.windows = {} for ticker in tickers: symbol = AddEquity(ticker, Resolution.Daily).Symbol self.windows[symbol] = RollingWindow[TradeBar](10) def OnData(self, data): for symbol in self.windows: self.windows[symbol].Add(data[symbol])
Â
Paul Park
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!