Hi, I'm new to QuantConnect and I'm having some trouble with Rolling Windows.
In my code I initialize a rolling window like this
After filling the window I wanted to find the max high price of the bars in the window but skipping the most recent element like this:
self.maxHigh = max(self.TradeBarWindow[1:], key = lambda y: y.High).High
but it gives me an error of method. However If I erase the brackets, it works fine, but it considers the high value of the most recent element also.
Another thing that is bugging me is the fact that I created a list and then appended the rolling window to it, but then if I try to access its elements like "High" or "Close", it throws me an error also.
newArrayTradeBar = [ ]
newArrayTradeBar.append(self.TradeBarWindow)
#Can't access the element newArrayTradeBar[0].Close for example, despite the fact that newArrayTradeBar[0] returns me a TradeBar class element
What am I missing?
Shile Wen
Hi Gustavo,
Calling the Python max method on a RollingWindow object won't work as it's not Python list-like, so we either need to cast it to a list or use list comprehension. Furthermore, one idea for why trying to access the TradeBar .Close is because the TradeBar is empty, so we need to first check that data.Bars.ContainsKey('SPY') before adding the bar to our list/RW. I've shown all of this in the attached backtest.
Best,
Shile Wen
Gustav
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!