Hello, I am still very new here but I was wondering how/if you can sort by an indicator value in coarse universe selection?
Specifically, I am trying to rank 10 stocks on their momentum (MOM Indicator) and was wondering how to implement indicators in universe selection.
Thank you!
class CasualAsparagusPelican(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2015, 1, 1)
self.setEndDate(2022, 1, 1)
self.SetCash(100000)
self.SetBenchmark("SPY")
self.UniverseSettings.Resolution = Resolution.Daily
self.universe = self.AddUniverse(self.CoarseSelectionFunction)
self.changes = None
# top 500 stocks worth more than $10 and > $10M daily volume
def MyCoarseFilterFunction(self, coarse):
sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
filtered = [ x.Symbol for x in sortedByDollarVolume
if x.Price > 10 and x.DollarVolume > 10000000 ]
return filtered[:200]
#Sorts by top 10 Momentum
sortedByMomentum =
def OnData(self, data):
#if no changes do nothing
if self.changes is None:
return
#Liquidate removed securities
for security in self.changes.RemovedSecurities:
if security.Invested:
self.Liquidate(security.Symbol)
#Allocate 9% of portfolio to added security:
for security in self.changes.AddedSecurities:
self.SetHoldings(security.Symbol, .09)