Hi Ovidiu,
Apologies for the delay in getting back to you (because snow storm, work travel, vacation, snow storm, snow storm, in that order :) ).
I was finally able to get an example working for you here I think. Check out the attached code - this algo will start with the top 1% of stocks based on average daily dollar volume (using set_universe() ). Every day at 9:59am it ranks the universe of stocks based on their price range (high - low traded price) so far today.
It takes a parameter set in the context() method to determine what fraction of the universe to sample/buy. I've used the top 1/3 in this example. So each day at 9:59am the algo will select the top 1/3 of stocks based on their price range and make a $1,000 investment in each one. The algo is currently set up to rebalance each day at 10am, if a stock is still in the 'top' list from yesterday the position is rebalanced to $1,000 exposure, if the stock has dropped out of the screen we close out the position entirely.
I've used the record() method to plot the average price range for the stocks that make it into my top 1/3 screened 'top' list.
Disclaimer: I did not spend much (any) time on performance optimization here, I ran the attached backtest for this YTD and it was ok, I kicked a backtest off for the past 1y period and its still running... I'm sure there are a couple of ways this could be speeded up.
Hope this is a helpful start for you and anyone else interested in this type of algo. As always if anyone else finds bugs with this code please point them out!
Happy to iterate on this as well - Jess
Disclaimer
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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.