Universe Selection using multiple conditions with price and volume (using Algorithm Framework)

Hi All,

I'm trying to build a custom Universe Selection Model simply combining several conditions using price and volume.

I'm using self.SetUniverseSelection(CoarseFundamentalUniverseSelectionModel(self.CoarseSelectionFunction)) in my framework and then basically customizing the def CoarseSelectionFunction within the same script.

So what I'm trying to do is filtering stocks (before opening the market every day) with close price > X, close price > close price of last day, last volume > X, average volume of the past n days > X, close price > max price of the last n days, etc.

As you can see in the attached backtest, I followed examples I found in the community and managed to get some of the above conditions working, However, I could not get the close price in an elegant way so had to use SimpleMovingAverage(1) to do that (terrible, I know). Also, how do I do the max function, or the lag closing prices, etc??

I think this could be a good, simple example of a template in which you add all the functions needed in the class SymbolData(object) such as moving averages and so on, and then perform all the filtering within the def CoarseSelectionFunction(self, coarse) adding many conditions to filter(lambda x: (x.is_ready and x.price.Current.Value > 20), self.averages.values())

Thank you so much in advance for the help!


Update Backtest

To filter stocks with the close price and volume, a portfolio construction model should be developed. The basic idea is creating the class to save the moving average indicator and rolling window, then update them with the AdjustedPrice and Volume properties in  CoarseSelectionFunction. Please see the attached example


Thank you very much as always Jing!

I was looking for a framework based solution so this template is great. One question if I may, I don't fully understand how this two lines avg = self.averages[cf.Symbol] avg.update(cf.EndTime, cf.Price, cf.Volume) affect the SymbolData object?

Also, is it possible to debug/log within the UniverseSelectionModel? I keep getting errors.

Something like this:

for x in sorted_values[:self.coarse_count]:
self.Log('symbol: ' + str(x.symbol.Value) + ' close price: ' + str(x.price.Current.Value))



avg = self.averages[cf.Symbol] 

avg.update(cf.EndTime, cf.Price, cf.Volume)

These two lines are used to update the rolling window and indicators in SymbolData with the new daily price and volume data.

To debug in SelectCoarse(self, algorithm, coarse), you can use algorithm.Debug() or algorithm.Log().


Thanks Jing!


Update Backtest


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.


This discussion is closed