Good day,
In order to understand Lean better, I peppered a simple example with Debug messages to see what going on on the hood.
In below algorithm i am trying to print out a 2 day moving average for the equities (line 57/58)
But it returns this value as 0.0 for all assests. Why am I not getting any values?
Regards,
Andre
Andre Meeusen
class LiquidUniverseSelection(QCAlgorithm):
  Â
  filteredByPrice = None
  changes = None
  def __init__(self):
    self.mom = []
   Â
  Â
  def Initialize(self):
    self.SetStartDate(2018, 1, 2) Â
    self.SetEndDate(2019, 1, 9)Â
    self.SetCash(100000) Â
    self.AddUniverse(self.CoarseSelectionFilter)
    self.SetAlpha(MOMAlphaModel())
    Â
  def CoarseSelectionFilter(self, coarse):
    EquityCountCoarseSelectionFilter = 0
    self.Debug(f"EquityStartCountCoarseSelectionFilter = {EquityCountCoarseSelectionFilter}")
    for cf in coarse:
      symbol = cf.Symbol
      EquityCountCoarseSelectionFilter = EquityCountCoarseSelectionFilter + 1
    self.Debug(f"EquityEndCountCoarseSelectionFilter = {EquityCountCoarseSelectionFilter}")
    Â
   Â
    Â
    #1. Sort descending by daily dollar volume
    self.sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True)
    Â
    #2. Select only Symbols with a price of more than $10 per share
    self.symbols_by_price = [c.Symbol for c in self.sortedByDollarVolume]# if c.Price > 10]
    Â
    #3. Return the 8 most liquid Symbols from the filteredByPrice list
    self.Top100 = self.symbols_by_price[:100]
    Â
    Â
    self.Debug(f"NumberOfEquitiesInTop100 = {len(self.Top100)}")
    self.Debug(f"TopOneInTop100 = {self.Top100[0]}")
    self.Debug(f"TopOneInTop100 = {self.Top100[0]}")
    self.Debug(f"BottemOneInTop100 = {self.Top100[-1]}")
    #self.Debug(f"OnUpdate({self.Time}:: ","ordered (sorted_mom=)",ordered)
    return self.Top100
  Â
  Â
class MOMAlphaModel(AlphaModel):Â
  def __init__(self):
    self.mom = {}
    #self.DailyMovingAverage
  def OnSecuritiesChanged(self, algorithm, changes):
    Â
    # 1. Initialize a 14-day momentum indicator for each symbol
    Â
    Â
    for security in changes.AddedSecurities:
      symbol = security.Symbol
      algorithm.Debug(f"added security={symbol} ")
      x=algorithm.MOM(symbol, 2, Resolution.Daily)
      algorithm.Debug(f"indicator={x}")
      symbol_string = "{}".format(symbol)
      algorithm.Debug(f"added security={symbol}")
      #algorithm.Debug(f"dictionary={self.mom}")
    pass
Derek Melchin
Hi Andre,
The algorithm above logs 0 for the indicator values because the indicators are never warmed up.
for security in changes.AddedSecurities: symbol = security.Symbol x=algorithm.MOM(symbol, 2, Resolution.Daily) algorithm.Debug(f"indicator={x}")
In addition, when we have a dynamic universe of securities, we should use the full indicator name (Momentum) instead of the short-cut method (algorithm.MOM). Refer to this related thread for an example.
Best,
Derek Melchin
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.
Andre Meeusen
Hi Derek,
Thank you . I managed to warm up the indicator with a history call.Â
You leave me with a cliffhanger though, as the link to the related thread leads to an error 404...
Thanks again for your time :)
Regards,
Andre
Â
Andre Meeusen
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!