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.
Shile Wen
Hi Maddox,
I suggest this BootCamp on how to do this, all we need to change is to change the input from Price to DollarVolume and change the ExponentialMovingAverage class to SimpleMovingAverage class.
Best,
Shile Wen
Maddox Southard
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!