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)
Fred Painchaud
Hi Thomas,
It is certainly possible. In a nutshell, the best practice is to group your indicators into a class which you then instanciate for each symbol in your universe. Check this example, for instance.
Fred
Thomas Nolan
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!