Back

Indicator; rolling window to get indicator values n days back.

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 :)

Update Backtest







0

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.


just post the algo how far you got,..

0

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];''
0

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

https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/MultipleSymbolConsolidationAlgorithm.py
0


Thanks for the help! The github link was also very helpful.  Appreciate the help

0

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):

0

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

 

0

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.


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')

0

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])

 

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed