New to the platform and been a few years so coding is rusty; thanks in advance for your patience. I'm looking to run at minute resolution and compare the highs and lows of the last 3 bars. My searching has told me rolling windows is the best option for this, however it seems like I'm not getting the correct data or using them incorrectly. When I run my code, I get a variabled referenced before assignment error. I copied my code through the variable assignment below, I would assume the if statement is not being satisfied but am not sure why. Any advice would be appreciated.
def Initialize(self):
self.SetStartDate(2021, 1, 1) #Starts Jan 1 2020
self.SetEndDate(2021,2,3) #Ends Today
self.SetCash(1000) #Thousand dollar balance
self.spy = self.AddEquity("SPY", Resolution.Minute) #Add SPY Minute Data
self.spy.SetDataNormalizationMode(DataNormalizationMode.Raw) #Set Data Normalization Mode
self.SetWarmup(3)
self.window = RollingWindow[TradeBar](3)
self.lowWindow = RollingWindow[float](3)
self.highWindow = RollingWindow[float](3)
def OnData(self, data):
self.lowWindow.Add(data["SPY"].Low)
self.highWindow.Add(data["SPY"].High)
if self.highWindow.IsReady and self.lowWindow.IsReady:
High = self.highWindow[0]
Low = self.lowWindow[0]
LastHigh = self.highWindow[1]
LastLow = self.lowWindow[1]
OldHigh = self.highWindow[2]
OldLow = self.lowWindow[2]
Crypto Cam
Have been troubleshooting and have worked myself to this point, but still not getting any data. Open to any advice or suggestions, at this point it's still early so I'm considering just trying a different platform.
self.spy = self.AddEquity("SPY", Resolution.Minute) #Add SPY Minute Data
self.spy.SetDataNormalizationMode(DataNormalizationMode.Raw) #Set Data Normalization Mode
self.UniverseSettings.Resolution = Resolution.Minute
self.window = RollingWindow[TradeBar](2)
self.lowWindow = RollingWindow[float](2)
self.highWindow = RollingWindow[float](2)
self.SetWarmUp(5)
def OnData(self, data):
self.lowWindow.Add(data["SPY"].Low)
self.highWindow.Add(data["SPY"].High)
if self.highWindow.IsReady and self.lowWindow.IsReady:
High = self.highWindow[0]
Low = self.lowWindow[0]
LastHigh = self.highWindow[1]
LastLow = self.lowWindow[1]
OldHigh = self.highWindow[2]
OldLow = self.lowWindow[2]
else:
return
Crypto Cam
I was very close with the last code I pasted. Needed to make the rolling window a size of 3 rather than 2. This is now working for me, hopefully this can help someone else too!
Crypto Cam
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!