I am trying to get the High and Low of a 20 period rolling window. Does anyone know if this is possible and how to do it in python?
QUANTCONNECT COMMUNITY
I am trying to get the High and Low of a 20 period rolling window. Does anyone know if this is possible and how to do it in python?
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.
Alethea Lin
Hi Nicholas,
You can set up MIN and MAX indicators to get the high low in a rolling window. Please see the attached backtest on how to do that. In addition, it might be helpful to check out the Reference Table to see what indicators are available in QC. We have plenty of them!
Hope this helps!
Nicholas Stern
The only thing is that I want the period of the window or max/min of the window to be a daily item not a minute item.
Alethea Lin
Hi Nicholas,
You can read this in the documentation. Change the Resolution to Daily. Both of the code below will work.
self.UniverseSettings.Resolution = Resolution.Daily
self.AAPL = self.AddEquity("AAPL", Resolution.Daily)
Please also check out the new Bootcamp "Momentum-Based Tactical Allocation".
Gmamuze Cht
Hello Nicholas, Alethea,
Is there a way to get the min or max value of a rollingwindow range ?Â
For example :Â
self.SetWarmUp(40)
self.max = self.MAX("AAPL", [20, 40])
Thank you !
Rahul Chowdhury
Hey Gmamuze,
We can find the max value in a rolling window range by first converting the RollingWindow to a list, then slicing it and finding its max.
This can be accomplished with a one liner.
max_value = max(list(self.priceWindow)[20:40])
Best
Rahul
Gmamuze Cht
Hello Rahul,
Very clear explanation, thank you !
For my need I try to convert it in C#, and if i follow your process, i have to :
1 /Create a new list.
My example :Â Bar_Close = new List<decimal>(100);
2/ Add rollingwindow data, close or open and so... in the list.
Bar_Close.Add(data["SPY"].Close);
3/ Slice the list by [] to use it
Max_Bar_close = (decimal)Math.Max(Bar_Close[0:40]);
Â
I have probably a missconception to convert Python to C#, because this does not work.
Rahul Chowdhury
Hey Gmamuze,
We can also accomplish this in C# with a one liner. We can add the elements of our RollingWindow to a list and slice it using GetRange
# returns slice window[20:40]
var Bar_Close = new List<Decimal>(40);
Bar_Close.AddRange(window);
Bar_Close.GetRange(20, 20)
to slice our RollingWindow.And then we can use the .Max() method to return the maximum value of the slice.
var max = Bar_Close.GetRange(20, 20).Max()
Best
Rahul
Newoptionz
Thanks Rahul ChowdhuryÂ
I still don't get why the 40 changes to a 20:40, as in
self.priceWindow = RollingWindow[float](40)
…
max_value = max(list(self.priceWindow)[20:40])
would someone please explain?
Â
Fred Painchaud
Hi newoptionz,
RollingWindow is an iterable. Think of it as a sequence. Above, it can store 40 elements, indexed from 0 to 39.
Since it is an iterable, you can create a standard Python list from it with list(self.priceWindow). Think of it as a cast (but it is not!). So then, you have a list of 40 elements (the same ones - the list constructor just iterated over your window). Then you slice that list from 20 included to 40 excluded, so 39 included. That's 20 elements. The last 20.
Then you use Python built-in max() function to compute the max of that half window (which is now a list).
The move to a list is necessary because slicing is not implemented over RollingWindow.
I hope that's what you wanted explained - and not why the max of the last 20 elements of the rolling window is being computed.
hehe
Fred
Newoptionz
Fred Painchaud great explanation. Â It is much clearer to me now. Â Yes I was confused and now understand that the max of the last as 20 elements is being calculated. Â
The Original question asked for the max of a 20 period rolling window, so these must be the last 20 periods added to a 40 period window.
thanks
Nicholas Stern
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!