Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Intraday Average Range by Time of Day

Hi
I am trying to develop an indicator that calculates the average range of a 3 minute bar (or any time period) for that same time of day over the last 20 days. So if it is 10:30 am I would like to be able to find the average range of all bars at 10:30 am on the previous 20 days.
I have been able to do this using a csv. file in Python with for loops etc. but am not sure how to do it in Quantopian. Any help or guidance would be greatly appreciated.

Thanks

Tim

7 responses

Hmm, that actually sounds sorta tricky, I think I have something that accomplishes what you're trying to do. This will group a price dataframe by the time of day and put that into a Pandas panel. In this example I then take the mean which gives you the average prices at each time of day. Is this along the lines of what you're trying to accomplish??

You probably don't need to get into Panels for this, which I find to be much more trouble than they are worth. I once posted a research notebook which did time-of-day analysis, but I can't find it now. I think I made a minute column, did a groupby on that, and then calculated the mean of the groupby. In this case, you could resample to '3m', calculate the high-low range to a column and the time/minute-of-day column, groupby and mean.

Thanks David! That helps a lot.
Simon- Would I need to merge the open high low and close values on the last 20 days into one data frame to perform re-sampling? I used resampling used groupby when I had a 1 min ohlc csv file but it was all in one data frame already. How would that look within the function?

https://www.quantopian.com/posts/rolling-mean-range-of-intraday-cross-section

Seemed easier to share as a notebook. Hopefully you can figure out how to apply it to your algo. Really wish we had mplot3d so we could do some time-series of cross-section graphs. Those would be pretty.

FWIW I find it really, really important to avoid doing list/map comprehensions in algos when what you are trying to accomplish can be done purely within pandas. I find that that is what winds up slowing backtests to a crawl...

Thank you! That helps a lot.

Hi Tim,

Have a look at the notebook I posted:

https://www.quantopian.com/posts/research-notebook-clone-test

It contains a pivot table, with columns corresponding to trading minute, and rows to trading day. Once you have such a pivot table, then I think that computing your statistics is straightforward--just pick the columns and rows of interest and do the math.

Cheers,

Grant