diff --git a/03 Writing Algorithms/12 Universes/11 Dataless Scheduled Universes/99 Examples.html b/03 Writing Algorithms/12 Universes/11 Dataless Scheduled Universes/99 Examples.html index 51d2c822d4..f6310606bd 100644 --- a/03 Writing Algorithms/12 Universes/11 Dataless Scheduled Universes/99 Examples.html +++ b/03 Writing Algorithms/12 Universes/11 Dataless Scheduled Universes/99 Examples.html @@ -1,83 +1,101 @@ +
The following examples demonstrate some common practices for dataless scheduled universes.
+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.
+The following example selects SPY on the last month of each quarter. For the remaining months, it selects no assets.
public override void Initialize() +public class DatalessScheduledUniverseDemoAlgorithm : QCAlgorithm { - AddUniverse(new ScheduledUniverse(DateRules.MonthStart(), TimeRules.At(8, 0), (dt) => + public override void Initialize() { - if (dt.Month % 3 == 0) - { - return new[] { Symbol.Create("SPY", SecurityType.Equity, Market.USA) }; - } - return Enumrable.Empty<Symbol>(); - })); + AddUniverse(new ScheduledUniverse( + DateRules.MonthStart(), + TimeRules.At(8, 0), + (dt) => + { + if (dt.Month % 3 == 0) + { + return new[] { QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA) }; + } + return Enumerable.Empty<Symbol>(); + } + )); + } }-def initialize(self): - self.add_universe( - ScheduledUniverse( - self.date_rules.month_start(), - self.time_rules.at(8, 0), - self._select_symbols +class DatalessScheduledUniverseDemoAlgorithm(QCAlgorithm): + + 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 []+ + def _select_symbols(self, dt: datetime) -> List[Symbol]: + if dt.month % 3 == 0: + return [Symbol.create("SPY", SecurityType.EQUITY, Market.USA)] + return []
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.
+Standard Options expire at end of the third week of each month. The following algorithm selects VIX-related products on the third week to trade the foreseeable increasing volatility:
public class SampleAlgorithm : QCAlgorithm +public class DatalessScheduledUniverseDemoAlgorithm : 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) + AddUniverse(new ScheduledUniverse( + DateRules.WeekStart(), + TimeRules.At(8, 0), + (dt) => { - if (++_week == 3) + if (dt.Month == _month) { - return new[] { Symbol.Create("VXZ", SecurityType.Equity, Market.USA) }; + if (++_week == 3) + { + return new[] { QuantConnect.Symbol.Create("VXZ", SecurityType.Equity, Market.USA) }; + } + return Enumerable.Empty<Symbol>(); } - return Enumrable.Empty<Symbol>(); + _month = dt.Month; + _week = 0; + return Enumerable.Empty-(); } - - _month = dt.Month; - _week = 0; - return Enumrable.Empty<Symbol>(); - })); + )); } } def initialize(self): - self.month = -1 - self.week = -1 +class DatalessScheduledUniverseDemoAlgorithm(QCAlgorithm): - self.add_universe( - ScheduledUniverse( - self.date_rules.week_start(), - self.time_rules.at(8, 0), - self._select_symbols + 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 []+ + def _select_symbols(self, dt: datetime) -> List[Symbol]: + 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 []
For more examples, see the following algorithms: