200-50 EMA Momentum Universe

In this lesson we will select a universe of assets where the 50-EMA is greater than the 200 EMA, create a custom class to hold symbol specific data, and use the history API to prepare our indicators.

What You'll Learn
  • Laying down our universe foundation
  • Grouping data with classes
  • Creating properties and methods to tidy code
  • Using a history call to prepare indicators
Lesson Requirements

We recommend you complete all previous beginner lessons before starting this lesson.

Laying Our Universe Foundation
  1. 200-50 EMA Momentum Universe
  2. Laying Our Universe Foundation

In this lesson we will using two indicators to perform selection on a universe of securities.

Creating Our Universe

Recall that we use coarse universe selection to create a set of stocks based on dollar-volume, and price. To create our universe of assets we need to pass a coarse selection filter function to the self.AddUniverse() method in our initializer.

# Pass the filter function into AddUniverse in your Initializer def Initialize(self): self.AddUniverse(self.CoarseSelectionFilter) # Create a filter function def CoarseSelectionFilter(self, coarse): pass Using Our Filter

Our CoarseSelectionFilter must return a list of symbols, which LEAN automatically subscribes to and adds to our algorithm.

To filter and sort our stocks we can use lambda functions and list comprehension.

# Use the sorted method and lambda to get keys in ascending order (greatest to least in DollarVolume) sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True) # Use list comprehension to get symbols from your sorted list # with a price more than $5 per share and get the top 10 symbols [c.Symbol for c in sortedByDollarVolume if c.Price > 5][:10]Adding and Removing Securities 

The OnSecuritiesChanged event fires whenever we have changes to our universe. It receives a SecurityChanges object containing references to the added and removed securities. The AddedSecurities and RemovedSecurities properties are lists of security objects.

# Liquidate the securities that are no longer in our universe for security in changes.RemovedSecurities: self.Liquidate(security.Symbol) # Set Holdings for the securities added to our universe for security in changes.AddedSecurities: self.SetHoldings(security.Symbol, 0.10)

Task Objectives CompletedContinue

  1. Reverse sort coarse by dollar volume to get the highest volume stocks.
  2. Filter out the stocks less than $10, save the result to self.selected.
  3. Liquidate securities leaving the universe in the OnSecuritiesChanged event.
  4. Allocate 10% holdings to each asset added to the universe.
 Hint: Are you indexing the changes.AddedSecurities and changes.RemovedSecurities, collections with the security.Symbol? If you need to review these topics you can go back to the lesson Liquid Universe Selection. Code: class EMAMomentumUniverse(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2019, 1, 7)
        self.SetEndDate(2019, 4, 1)
        self.SetCash(100000)
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction)
        self.selected = None
    
    def CoarseSelectionFunction(self, coarse):
        #1. Sort coarse by dollar volume
        sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True)
        #2. Filter out the stocks less than $10 and return selected
        self.selected = [c.Symbol for c in sortedByDollarVolume if c.Price > 10][:10]
        return self.selected
        
    def OnSecuritiesChanged(self, changes): 
        #3. Liquidate securities leaving the universe
        for security in changes.RemovedSecurities:
            self.Liquidate(security.Symbol)
        #4. Allocate 10% holdings to each asset added to the universe
        for security in changes.AddedSecurities:
            self.SetHoldings(security.Symbol, 0.10) ------------------------continued below....