Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
All time New High

What is the best way to select a group of stocks that are reaching new all times highs.

I try something like this but its consume too much CPU with more than 400 sids.

h_close = history(9999, '1d', 'open_price', ffill=True)  
high_pct = h_close[sid][-1] / max(h_close[sid]) - 1  

Maybe the best way is using pipeline but i'm a little lost with the implementation.

Thanks,

4 responses

I think you could make a function call that stores the current all time high in context.

Is there any other way to obtain these all new highs?

Stop with the 9999 day lookback. It's killing you. Get the all time high data from somewhere (farm it out and hard code it or fetch a csv the first day you're live). Then have it persist in context somewhere, so you're only doing a single if GT comparison. Use something like this, full disclosure, I'm the author, to get the whole history of the market. Sort each frame by date descending, slice on the first day you'll test the algo, then sort by price descending. Get record 0. Now, you have the all time high for everything.

Boom. Done.

This assumes context persists across days and never ever gets reset.

Thanks Andrew, I will take your advice.

John