Universe Selection
Scheduled Universes
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 datetime
DateTime
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:
Argument | Data Type | Description | Default Value |
---|---|---|---|
dateRule | IDateRule | Date rule defines what days the universe selection function will be invoked | |
timeRule | ITimeRule | Time rule defines what times on each day selected by date rule the universe selection function will be invoked | |
selector | Func<DateTime, IEnumerable<Symbol>> Callable[[datetime], List[Symbol]] | Selector function accepting the date time firing time and returning the universe selected symbols | |
settings | UniverseSettings | Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings | null None |
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
:
Member | Description |
---|---|
self.DateRules.SetDefaultTimeZone(time_zone: DateTimeZone)
DateRules.SetDefaultTimeZone(DateTimeZone timeZone); | Sets the time zone for the DateRules object used in all methods in this table. The default time zone is the algorithm time zone. |
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.Today | Trigger an event once today. |
self. DateRules.Tomorrow | Trigger an event once tomorrow. |
Time Rules
The following table describes the supported TimeRules
:
Member | Description |
---|---|
self.TimeRules.SetDefaultTimeZone(time_zone: DateTimeZone)
TimeRules.SetDefaultTimeZone(DateTimeZone timeZone) | Sets the time zone for the TimeRules object used in all methods in this table, except when a different time zone is given. The default time zone is the algorithm time zone. |
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 starting at midnight. |
self. TimeRules.Now | Trigger an event at the current time of day. |
self. TimeRules.Midnight | Trigger an event at midnight. |
self. TimeRules.Noon | Trigger an event at noon. |
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.At(hour: int, minute: int, second: int, time_zone: DateTimeZone)
TimeRules.At(int hour, int minute, int second, DateTimeZone timeZone) | Trigger an event at a specific time of day in the given time zone (e.g. 13:10 UTC). |
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]