I am trying to create a universe that filters equities by the average dollar volume. I know the CoarseSelectionFunction has the built in DollarVolume object, however, I believe that only provides dollar volume for a single period and I want to calculate it with a trailing window such as 63, 126, or 252 days. Here is what I have come up with so far:

#I only want to look at equities that are fairly liquid and have fundamentals. def CoarseSelectionFunction(self, coarse): sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True) filtered = [ x.Symbol for x in sortedByDollarVolume if x.HasFundamentalData ] return filtered[:500] #After the initial filter I want to try to sort equities by their 63 day average dollar volume (adv63). def FineSelectionFunction(self, fine): symbols = [x.Symbol for x in fine] history = self.History(symbols, 63, Resolution.Daily) adv63 = [] for s in symbols: adv = history.loc[s]['volume'].mean() * history.loc[s]['close'].mean() adv63.append([s,adv]) adv63 = pd.DataFrame(adv63,columns=['symbol','adv63']).set_index('symbol') self.symbols = list(adv63.sort_values('adv63',ascending=False).iloc[:5].index.values) return self.symbols

I believe this achieves the outcome that I want in terms of filtering, however, doing a history call for 500 equities everyday takes entirely too long to in backtesting. Any ideas on how I could optimize this process?

Thanks.

Author