Hello All,
I have a small inquiry. I know this is the way to add an indicator to a rolling window (Code 1 below):
Now, I am trying to add the indicator ADX to a 2 day rolling window (this day and the day before) for a list of stocks, say:
list = ["SPY", "TLT"] (can be longer ...)
This is what I wrote (code 2 below), gives no errors, but I still feel something might be wrong, it's not very clear how to do that in the documentation. I appreciate the help:
##############Code 1###############
# In Initialize, create the rolling windows
def Initialize(self):
# Creates an indicator and adds to a rolling window when it is updated
self.SMA("SPY", 5).Updated += self.SmaUpdated
self.smaWindow = RollingWindow[IndicatorDataPoint](5)
# Adds updated values to rolling window
def SmaUpdated(self, sender, updated):
self.smaWindow.Add(updated)
##############Code 2###############
def Initialize(self):
stocks_list = ["SPY","TLT"] for stock in stocks_list:
self.ADX(stock, 2).Updated += self.ADXUpdated
exec("self.ADXWindow_" + stock + "= RollingWindow[IndicatorDataPoint](2)")
def ADXUpdated(self, sender, updated):
stocks_list = ["SPY","TLT"]
for stock in stocks_list:
exec("self.ADXWindow_" + stock + ".Add(updated)")
Rahul Chowdhury
Hi Amine,
We can keep track of each symbol's indicator data with a SymbolData object. We hold our rolling windows in that object and also define our indicator updated event handler inside SymbolData.
Amine Kouta
Hi Rahul,
Thanks a lot for putting the effort into this. I was wondering how we could also retrieve this day's and yesterday's data from each SymbolData object in OnData. Let's say if I wanted to do the following:
if ADX["SPY"](today) > ADX["SPY"](yesterday):
Invest in SPY let's say
Same thing for "TLT" let's say.
Thanks again,
I appreciate it,
Rahul Chowdhury
You can implement that strategy by iterating over the symbols in the dictionary and checking whether that condition holds. Also, don't forget to verify whether your rolling windows are ready before you check.
Amine Kouta
Thanks Rahul, this helped a lot.
Cheers,
Amine Kouta
Hi Raul,
One more simple question, if say I was adding equity data ("SPY","TLT") with a "second" resolution instead of a daily resolution, meaning:
symbol = self.AddEquity(ticker, Resolution.Second).SymbolWould the ADX rolling window still work properly and give daily data in OnData? or will the code need to be modified (e.g. consolidated etc..)?
Thanks again, I appreciate it.
Alexandre Catarino
Hey Amine Kouta,
As long as you keep your ADX indicators subscribed to daily data, it will be updated with daily data. Since the rolling windows are only updated when the indicators are updated, your rolling windows will work properly. This feature is guaranteed by this statement:
self.adx.Updated += self.OnAdxUpdated
Amine Kouta
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!