Universe Selection

Scheduled Universes

Introduction

A Scheduled Universe Selection model selects assets at fixed, regular intervals. In live trading, you can use this type of model to fetch tickers from Dropbox or to perform periodic historical analysis and select some assets.

Scheduled Universe Selection

The ScheduledUniverseSelectionModel selects assets on the schedule you provide. To use this model, provide a DateRule, TimeRule, and a selector function. The DateRule and TimeRule define the selection schedule. The selector function receives a datetimeDateTime object and returns a list of Symbol objects. The Symbol objects you return from the selector function are the constituents of the universe.

AddUniverseSelection(new ScheduledUniverseSelectionModel(dateRule, timeRule, selector));
self.AddUniverseSelection(ScheduledUniverseSelectionModel(dateRule, timeRule, selector))

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
dateRuleIDateRuleDate rule defines what days the universe selection function will be invoked
timeRuleITimeRuleTime rule defines what times on each day selected by date rule the universe selection function will be invoked
selectorFunc<DateTime, IEnumerable<Symbol>>Callable[[datetime], List[Symbol]]Selector function accepting the date time firing time and returning the universe selected symbols
settingsUniverseSettingsUniverse settings for subscriptions added via this universe, null will default to algorithm's universe settingsnullNone

If you don't provide a settings argument, the algorithm.UniverseSettings is used by default.

The model assumes the timeRule argument is in Coordinated Universal Time (UTC). To use a different timezone, pass a timeZone argument of type DateTimeZone before the dateRule argument.

To return the current universe constituents from the selector function, return Universe.Unchanged.

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

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

# Create selection function which returns symbol objects.
def SelectSymbols(self, dateTime: datetime) -> List[Symbol]:
    tickers = ['SPY', 'AAPL', 'IBM']
    return [Symbol.Create(ticker, SecurityType.Equity, Market.USA)
        for ticker in tickers]

To view the implementation of this model, see the LEAN GitHub repository.

Date Rules

The following table describes the supported DateRules:

MemberDescription
self.DateRules.On(year: int, month: int, day: int) DateRules.On(int year, int month, int day)Trigger an event on a specific date.
self.DateRules.EveryDay()Trigger an event every day.
self.DateRules.EveryDay(symbol: Symbol) DateRules.EveryDay(Symbol symbol)Trigger an event every day a specific symbol is trading.
self.DateRules.Every(days: list[DayOfWeek]) DateRules.Every(params DayOfWeek[] days)Trigger an event on specific days throughout the week. To view the DayOfWeek enum members, see DayOfWeek Enum in the .NET documentation.
self.DateRules.MonthStart(daysOffset: int = 0) DateRules.MonthStart(int daysOffset = 0)Trigger an event on the first day of each month plus an offset.
self.DateRules.MonthStart(symbol: Symbol, daysOffset: int = 0) DateRules.MonthStart(Symbol symbol, int daysOffset = 0)Trigger an event on the first tradable date of each month for a specific symbol plus an offset.
self.DateRules.MonthEnd(daysOffset: int = 0) DateRules.MonthEnd(int daysOffset = 0)Trigger an event on the last day of each month minus an offset.
self.DateRules.MonthEnd(symbol: Symbol, daysOffset: int = 0) DateRules.MonthEnd(Symbol symbol, int daysOffset = 0)Trigger an event on the last tradable date of each month for a specific symbol minus an offset.
self.DateRules.WeekStart(daysOffset: int = 0) DateRules.WeekStart(int daysOffset = 0)Trigger an event on the first day of each week plus an offset.
self.DateRules.WeekStart(symbol: Symbol, daysOffset: int = 0) DateRules.WeekStart(Symbol symbol, int daysOffset = 0)Trigger an event on the first tradable date of each week for a specific symbol plus an offset.
self.DateRules.WeekEnd(daysOffset: int = 0) DateRules.WeekEnd(int daysOffset = 0)Trigger an event on the last day of each week minus an offset.
self.DateRules.WeekEnd(symbol: Symbol, daysOffset: int = 0) DateRules.WeekEnd(Symbol symbol, int daysOffset = 0)Trigger an event on the last tradable date of each week for a specific symbol minus an offset.
self.DateRules.TodayTrigger an event once today in the time zone of the algorithm.
self.DateRules.TomorrowTrigger an event once tomorrow in the time zone of the algorithm.

Time Rules

The following table describes the supported TimeRules:

MemberDescription
self.TimeRules.At(hour: int, minute: int, second: int = 0) TimeRules.At(int hour, int minute, int second = 0)Trigger an event at a specific time of day (e.g. 13:10).
self.TimeRules.AfterMarketOpen(symbol: Symbol, minutesAfterOpen: float = 0, extendedMarketOpen: bool = False) TimeRules.AfterMarketOpen(Symbol symbol, double minutesAfterOpen = 0, bool extendedMarketOpen = false)Trigger an event a few minutes after market open for a specific symbol (default is 0). This rule doesn't work for Crypto securities or custom data.
self.TimeRules.BeforeMarketClose(symbol: Symbol, minutesBeforeClose: float = 0, extendedMarketOpen: bool = False) TimeRules.BeforeMarketClose(Symbol symbol, double minutesBeforeClose = 0, bool extendedMarketOpen = false)Trigger an event a few minutes before market close for a specific symbol (default is 0). This rule doesn't work for Crypto securities or custom data.
self.TimeRules.Every(interval: timedelta) TimeRules.Every(TimeSpan interval)Trigger an event every period interval.
self.TimeRules.NowTrigger an event at the current time.
self.TimeRules.MidnightTrigger an event at midnight in the algorithm time zone.
self.TimeRules.NoonTrigger an event at noon in the algorithm time zone.

Examples

// Select the universe on a specific date at a specific time 
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.On(2013, 10, 7),
    TimeRules.At(13, 0),
    SelectSymbols));

// Select the universe 10 minutes after SPY starts trading each day
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.EveryDay("SPY"),
    TimeRules.AfterMarketOpen("SPY", 10),
    SelectSymbols));

// Select the universe 10 minutes before SPY stops trading each day
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.EveryDay("SPY"),
    TimeRules.BeforeMarketClose("SPY", 10),
    SelectSymbols));

// Select the universe on Monday and Friday at noon
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.Every(DayOfWeek.Monday, DayOfWeek.Friday),
    TimeRules.At(12, 0),
    SelectSymbols));

// Select the universe now
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.Today,
    TimeRules.Now,
    SelectSymbols));

// Select the universe tomorrow at midnight
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.Tomorrow,
    TimeRules.Midnight,
    SelectSymbols));

// Select the universe today at noon
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.Today,
    TimeRules.Noon,
    SelectSymbols));

// Select the universe every 10 minutes on everyday 
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.EveryDay(), 
    TimeRules.Every(TimeSpan.FromMinutes(10)),
    SelectSymbols));
            
// Select the universe at the market open on the first day of the month the SPY starts trading
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.MonthStart("SPY"),
    TimeRules.AfterMarketOpen("SPY"),
    SelectSymbols));

// Select the universe at the market close on the last day of the month the SPY trades
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.MonthEnd("SPY"),
    TimeRules.BeforeMarketClose("SPY"),
    SelectSymbols));

// Select the universe 5 minutes after SPY starts trading each week
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.WeekStart("SPY"),
    TimeRules.AfterMarketOpen("SPY", 5),
    SelectSymbols));

// Select the universe 5 minutes before the SPY stops trading each week
AddUniverseSelection(new ScheduledUniverseSelectionModel(
    DateRules.WeekEnd("SPY"),
    TimeRules.BeforeMarketClose("SPY", 5),
    SelectSymbols));


// Define a selection function that returns Symbol objects
private IEnumerable<Symbol> SelectSymbols(DateTime dateTime)
{
    var tickers = new[] {"SPY", "AAPL", "IBM"};
    return tickers.Select(ticker =>
        QuantConnect.Symbol.Create(ticker, SecurityType.Equity, Market.USA));
}      
# Select the universe on a specific date at a specific time 
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.On(2013, 10, 7), 
    self.TimeRules.At(13, 0),
    self.select_symbols))
    
# Select the universe 10 minutes after SPY starts trading each day
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.EveryDay("SPY"), 
    self.TimeRules.AfterMarketOpen("SPY", 10),
    self.select_symbols))

# Select the universe 10 minutes before SPY stops trading each day
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.EveryDay("SPY"),
    self.TimeRules.BeforeMarketClose("SPY", 10),
    self.select_symbols))

# Select the universe on Monday and Friday at noon
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.Every(DayOfWeek.Monday, DayOfWeek.Friday),
    self.TimeRules.At(12, 0),
    self.select_symbols))

# Select the universe now
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.Today,
    self.TimeRules.Now,
    self.select_symbols))

# Select the universe tomorrow at midnight
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.Tomorrow,
    self.TimeRules.Midnight,
    self.select_symbols))
    
# Select the universe today at noon
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.Today,
    self.TimeRules.Noon,
    self.select_symbols))

# Select the universe every 10 minutes on everyday 
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.EveryDay(), 
    self.TimeRules.Every(timedelta(minutes=10)),
    self.select_symbols))

# Select the universe at the market open on the first day of the month the SPY starts trading
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.MonthStart("SPY"),
    self.TimeRules.AfterMarketOpen("SPY"),
    self.select_symbols))

# Select the universe at the market close on the last day of the month the SPY trades
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.MonthEnd("SPY"),
    self.TimeRules.BeforeMarketClose("SPY"),
    self.select_symbols))

# Select the universe 5 minutes after SPY starts trading each week
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.WeekStart("SPY"),
    self.TimeRules.AfterMarketOpen("SPY", 5),
    self.select_symbols))

# Select the universe 5 minutes before the SPY stops trading each week
self.AddUniverseSelection(ScheduledUniverseSelectionModel(
    self.DateRules.WeekEnd("SPY"),
    self.TimeRules.BeforeMarketClose("SPY", 5),
    self.select_symbols))


# Define a selection function that returns Symbol objects
def select_symbols(self, date_time: datetime) -> list[Symbol]:
    tickers = ['SPY', 'AAPL', 'IBM']
    return [Symbol.Create(ticker, SecurityType.Equity, Market.USA)
        for ticker in tickers]

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: