Algorithm Framework

Universe Selection

Introduction

The Universe Section Model creates Universe objects which select the assets for your strategy. We have identified three types of universes that cover most people's requirements, and built helper classes to make their implementation easier.Creating Universe Selection Models from scratch isn't simple so we recommend you use one of the helper universes we've provided.

To set a Universe Selection Model you should use theUniverseSelectionself.UniverseSelection method. This should be done from your algorithm Initialize()def Initialize() method:

public override void Initialize()
{
      UniverseSettings.Resolution = Resolution.Minute;
      var symbols = new [] { QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA) };
      SetUniverseSelection( new ManualUniverseSelectionModel(symbols) );
}
def Initialize(self):    # Set requested data resolution
    self.UniverseSettings.Resolution = Resolution.Minute
    symbols = [ Symbol.Create("SPY", SecurityType.Equity, Market.USA) ]
    self.SetUniverseSelection( ManualUniverseSelectionModel(symbols) )

As with all LEAN algorithms; the resolution of the assets added to the universe is configured by the UniverseSettings.Resolutionself.UniverseSettings.Resolution property.

There are 3 types of universes:

  • Manual Universes - Universes which use a fixed, static set of assets.
  • Fundamental Universes - Universes based on coarse price or fundamental data.
  • Scheduled Universes - Universes triggering on regular intervals.

Manual Universe Selection

Manual universe selection chooses a static, fixed set of assets to trade. This is most useful when selecting a set of currencies, or a basket of ETF stocks. You can also add assets with the traditional AddSecurity API.

The ManualUniverseSelectionModel class is initialized with an array of Symbol objects at the UniverseSettings.Resolution data resolution. Symbol objects can be created with the Symbol.Create method.

public override void Initialize()
{
     // Set requested data resolution
     UniverseSettings.Resolution = Resolution.Minute;
     SetUniverseSelection(
        new ManualUniverseSelectionModel(
             QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA)
     ));
}
def Initialize(self):
     self.UniverseSettings.Resolution = Resolution.Minute
     symbols = [ Symbol.Create("SPY", SecurityType.Equity, Market.USA) ]
     self.SetUniverseSelection(ManualUniverseSelectionModel(symbols))

Fundamental Universe Selection

Framework Universe Selection models can use the same function based selection mechanics as other algorithms. QuantConnect provides two helper methods for these universes which handle the framework requirements: CoarseFundamentalUniverseSelectionModel and the FineFundamentalUniverseSelectionModel.

To define a fundamental Universe Selection model you need to create an instance of the class and set with the SetUniverseSelection method:

 // Setting Universe Model in QCAlgorithmFramework algorithm
public override void Initialize()
{
     SetUniverseSelection(new FineFundamentalUniverseSelectionModel(SelectCoarse, SelectFine));
}

IEnumerable<Symbol> SelectCoarse(IEnumerable<CoarseFundamental> coarse)
{
    var tickers = new[] { "AAPL", "AIG", "IBM" };
    return tickers.Select(x =>
        QuantConnect.Symbol.Create(x, SecurityType.Equity, Market.USA)
    );
}

IEnumerable<Symbol> SelectFine(IEnumerable<FineFundamental> fine) {
     return fine.Select(f => f.Symbol);
}
 # Setting Universe Model in QCAlgorithmFramework algorithm
def Initialize(self):
    self.SetUniverseSelection(
           FineFundamentalUniverseSelectionModel(self.SelectCoarse, self.SelectFine)
    )

def SelectCoarse(self, coarse):
    tickers = ["AAPL", "AIG", "IBM"]
    return [Symbol.Create(x, SecurityType.Equity, Market.USA) for x in tickers]

def SelectFine(self, fine):
    return [f.Symbol for f in fine] 

This takes the same universe filtering functions explained in the Universe section. The Coarse selection function is passed a list of CoarseFundamental objects; and returns a list of Symbol objects. The Fine selection function is passed a list of FineFundamental objects generated from coarse selection results; and should return a list of Symbol objects. See the example algorithms at the top of the page to get started.

Scheduled Universe Selection

Scheduled universes allow you to perform universe selection at fixed, regular intervals. In live trading this might be applied to fetching tickers from DropBox, or performing analysis on historical data and choosing resulting symbols. The class for creating scheduled universes is called ScheduledUniverseSelectionModel.

public ScheduledUniverseSelectionModel(
     DateRule dateRule,
     TimeRule timeRule,
     Func<DateTime, IEnumerable<Symbol>> selector,
     UniverseSettings settings = null,
     ISecurityInitializer initializer = null
)
ScheduledUniverseSelectionModel(dateRule, timeRule, selector,  universeSettings=null, securityInitializer=null)

The universe selection helper works in the same way as the Scheduled Event API, requiring a DateRule, a TimeRule and a selector function to execute which returns a list of Symbol objects.

// Selection will run on mon/tues/thurs at 00:00/06:00/12:00/18:00
SetUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.Every(DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Thursday),
    TimeRules.Every(TimeSpan.FromHours(12)),
    SelectSymbols // selection function in algorithm.
));

// Create selection function which returns symbol objects.
IEnumerable<Symbol> SelectSymbols(DateTime dateTime)
{
     return new[]
     {
          Symbol.Create("SPY", SecurityType.Equity, Market.USA),
          Symbol.Create("AAPL", SecurityType.Equity, Market.USA),
          Symbol.Create("IBM", SecurityType.Equity, Market.USA)
     }
}
# Selection will run on mon/tues/thurs at 00:00/06:00/12:00/18:00
self.SetUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.Every(DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Thursday),
    self.TimeRules.Every(timedelta(hours = 12)),
    self.SelectSymbols # selection function in algorithm.
))

# Create selection function which returns symbol objects.
def SelectSymbols(self, dateTime):
     symbols = []
     symbols.append(Symbol.Create('SPY', SecurityType.Equity, Market.USA))
     symbols.append(Symbol.Create('SPY', SecurityType.Equity, Market.USA))
     return symbols

Creating Universe Models

Universe Models must implement a IUniverseSelectionModel interface. It has one method, CreateUniverses(QCAlgorithmFramework algorithm). The algorithm object is passed into the method to give you access to the QuantConnect API; and it should return an array of Universe objects.

// Algorithm framework model that defines the universes to be used by an algorithm
interface IUniverseSelectionModel
{
   // Creates the universes for this algorithm, called once after IAlgorithm.Initialize
   IEnumerable<Universe> CreateUniverses(QCAlgorithmFramework algorithm);
}

Generally you should be able to extend one of the universes described above so if you ever find yourself needing do something which doesn't fit into the categories above, please let us know and we'll create a new foundational type of universe model.

Configuring Securities

You can configure securities with the SetSecurityInitializer helper just like in a Classic Algorithm. Call this method from your Initialize() method and set a class or use the functional implementation for simple requests. For more information on this see the Configuring Universe Securities section.

//Most common request; requesting raw prices for universe securities.
SetSecurityInitializer(x => x.SetDataNormalizationMode(DataNormalizationMode.Raw));
# Most common request; requesting raw prices for universe securities.
self.SetSecurityInitializer(lambda x: x.SetDataNormalizationMode(DataNormalizationMode.Raw))

You can also see our Tutorials and Videos. You can also get in touch with us via Chat.

Did you find this page Helpful ?