Universes

Dataless Scheduled Universes

Introduction

A dataless scheduled universe let's you select a set of assets on a specific schedule. You can control which days the other types of universe run by adjusting the Schedule universe setting. However, the Scheduleschedule universe setting doesn't accept a TimeRule argument, so you can't control the time of the day they run. In contrast, a dataless scheduled universe accepts a TimeRule argument, but its selection function only receives the algorithm time.

Create Universes

To add a dataless scheduled universe, in the Initializeinitialize method, call the AddUniverseadd_universe method with a ScheduledUniverse object.

public class MyUniverseAlgorithm : QCAlgorithm {
    private Universe _universe;
    public override void Initialize()
    {
        UniverseSettings.Asynchronous = true;
        _universe = AddUniverse(new ScheduledUniverse(DateRules.MonthStart(), TimeRules.At(8, 0), SelectSymbols));
    }

        
    private IEnumerable<Symbol> SelectSymbols(DateTime dt) 
    {
        return new[] { QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA) };
    }
}
class MyUniverseAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.universe_settings.asynchronous = True
        self._universe = self.add_universe(
            ScheduledUniverse(
                self.date_rules.month_start(), 
                self.time_rules.at(8, 0), 
                self._select_symbols
            )
        )
        
    def _select_symbols(self, dt):
        return [Symbol.create("SPY", SecurityType.EQUITY, Market.USA)]

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
dateRuledate_ruleIDateRuleDate rule that defines what days the universe selection function runs
timeRuletime_ruleITimeRuleTime rule that defines what times on each day selected by date rule the universe selection function runs
Func<DateTime, IEnumerable< Symbol>> Callable[[datetime], List[Symbol]]selectorSelector function that accepts the current date time and returns the universe's Symbol objects
settingsUniverseSettingsThe universe settings. If you don't provide an argument, the model uses the algorithm.UniverseSettingsalgorithm.universe_settings by default.nullNone

Date Rules

The following table describes the supported DateRules:

MemberDescription
self.date_rules.set_default_time_zone(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.date_rules.on(year: int, month: int, day: int) DateRules.On(int year, int month, int day)Trigger an event on a specific date.
self.date_rules.every_day() DateRules.EveryDay()Trigger an event every day.
self.date_rules.every_day(symbol: Symbol, extended_market_hours: bool = False) DateRules.EveryDay(Symbol symbol, bool extendedMarketHours = false)Trigger an event every day a specific symbol is trading.
self.date_rules.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.date_rules.month_start(days_offset: int = 0) DateRules.MonthStart(int daysOffset = 0)Trigger an event on the first day of each month plus an offset.
self.date_rules.month_start(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.date_rules.month_end(days_offset: int = 0) DateRules.MonthEnd(int daysOffset = 0)Trigger an event on the last day of each month minus an offset.
self.date_rules.month_end(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.date_rules.week_start(days_offset: int = 0) DateRules.WeekStart(int daysOffset = 0)Trigger an event on the first day of each week plus an offset.
self.date_rules.week_start(symbol: Symbol, days_offset: 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.date_rules.week_end(days_offset: int = 0) DateRules.WeekEnd(int daysOffset = 0)Trigger an event on the last day of each week minus an offset.
self.date_rules.week_end(symbol: Symbol, days_offset: 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.date_rules.year_start(days_offset: int = 0) DateRules.YearStart(int daysOffset = 0)Trigger an event on the first day of each year plus an offset.
self.date_rules.year_start(symbol: Symbol, days_offset: int = 0) DateRules.YearStart(Symbol symbol, int daysOffset = 0)Trigger an event on the first tradable date of each year for a specific symbol plus an offset.
self.date_rules.year_end(days_offset: int = 0) DateRules.YearEnd(int daysOffset = 0)Trigger an event on the last day of each year minus an offset.
self.date_rules.year_end(symbol: Symbol, days_offset: int = 0) DateRules.YearEnd(Symbol symbol, int daysOffset = 0)Trigger an event on the last tradable date of each year for a specific symbol minus an offset.
self.date_rules.todayDateRules.TodayTrigger an event once today.
self.date_rules.tomorrowDateRules.TomorrowTrigger an event once tomorrow.

To define custom date rules, create a FuncDateRule object. The FuncDateRule constructor expects a name argument of type stringstr and a getDatesFunctionget_dates_function argument of type Func<DateTime, DateTime, IEnumerable<DateTime>>Callable[[datetime, datetime], List[datetime]]. The getDatesFunctionget_dates_function function receives the start and end dates of the algorithm and returns a list of dates for the date rule. In live trading, the end date is 12/31/2025. The following example demonstrates how to define a date rule that represents the 10th day of each month:

// Create a date rule that specifies the 10th day of each month.
var dateRule = new FuncDateRule(
    name: "10th_day_of_the_month",
    getDatesFunction: (start, end) => Enumerable.Range(start.Year, end.Year - start.Year + 1)
        .SelectMany(year => Enumerable.Range(1, 12).Select(month => new DateTime(year, month, 10)))
);
# Create a date rule that specifies the 10th day of each month.
date_rule = FuncDateRule(
    name="10th_day_of_the_month", 
    get_dates_function=lambda start, end: [
        datetime(year, month, 10) 
        for year in range(start.year, end.year) for month in range(1,12)
    ]
) 

Time Rules

The following table describes the supported TimeRules:

MemberDescription
self.time_rules.set_default_time_zone(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.time_rules.after_market_open(symbol: Symbol, minutes_after_open: float = 0, extended_market_open: 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.time_rules.before_market_close(symbol: Symbol, minutes_after_open: float = 0, extended_market_open: 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.time_rules.every(interval: timedelta) TimeRules.Every(TimeSpan interval)Trigger an event every period interval starting at midnight.
self.time_rules.nowTimeRules.NowTrigger an event at the current time of day.
self.time_rules.midnightTimeRules.MidnightTrigger an event at midnight.
self.time_rules.noonTimeRules.NoonTrigger an event at noon.
self.time_rules.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.time_rules.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).

To define custom time rules, create a FuncTimeRule object. The FuncTimeRule constructor expects a name argument of type stringstr and a createUtcEventTimesFunctioncreate_utc_event_times_function argument of type Func<IEnumerable<DateTime>, IEnumerable<DateTime>>Callable[[List[datetime]], List[datetime]]. The function receives the list of dates from the date rule and then returns a list of DateTimedatetime that define the time rule.

var timeRule = new FuncTimeRule(
    name: "CustomTimeRule",
    createUtcEventTimesFunction: dates => dates.Select(d => d.AddHours(10)));
time_rule = FuncTimeRule(
    name="CustomTimeRule", 
    create_utc_event_times_function=lambda dates: [d + timedelta(hours=10) for d in dates]
)

Selection Frequency

Dataless scheduled universes run at whatever selection schedule you define by the dateRuledate_rule and timeRuletime_rule arguments.

Examples

Example 1: Quarter End Selection

The following example selects SPY if the current month is a quarter's end to ride on the possible quarter ending accounting period. Otherwise, it returns an empty universe.

public override void Initialize()
{
    AddUniverse(new ScheduledUniverse(DateRules.MonthStart(), TimeRules.At(8, 0), (dt) => 
    {
        if (dt.Month % 3 == 0)
        {
            return new[] { Symbol.Create("SPY", SecurityType.Equity, Market.USA) };
        }
        return Enumrable.Empty<Symbol>();
    }));
}
def initialize(self):
    self.add_universe(
        ScheduledUniverse(
            self.date_rules.month_start(), 
            self.time_rules.at(8, 0), 
            self._select_symbols
        )
    )
	
def _select_symbols(self, dt: datetime) -> List[Symbols]:
    if dt.month % 3 == 0:
        return [Symbol.create("SPY", SecurityType.EQUITY, Market.USA)]
    return []

Example 2: Third Week VIX

Standard Options expire at end of the the third week of each month. You can filter the universe to select VIX related products on the third week to trade the foreseeable increasing volatility.

public class SampleAlgorithm : QCAlgorithm
{
    private int _month = -1;
    private int _week = -1;
        
    public override void Initialize()
    {
        AddUniverse(new ScheduledUniverse(DateRules.WeekStart(), TimeRules.At(8, 0), (dt) => 
        {
            if (dt.Month == _month)
            {
                if (++_week == 3)
                {
                return new[] { Symbol.Create("VXZ", SecurityType.Equity, Market.USA) };
                }
                return Enumrable.Empty<Symbol>();
            }
            
            _month = dt.Month;
            _week = 0;
            return Enumrable.Empty<Symbol>();
        }));
    }
}
def initialize(self):
    self.month = -1
    self.week = -1

    self.add_universe(
        ScheduledUniverse(
            self.date_rules.week_start(), 
            self.time_rules.at(8, 0), 
            self._select_symbols
        )
    )
	
def _select_symbols(self, dt: datetime) -> List[Symbols]:
    if dt.month == self.month:
        self.week += 1
        if self.week == 3:
            return [Symbol.create("VXZ", SecurityType.EQUITY, Market.USA)]
        return []
    
    self.month = dt.month
    self.week = 0
    return []

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: