/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using NodaTime; using Python.Runtime; using QuantConnect.Data.UniverseSelection; using QuantConnect.Interfaces; using QuantConnect.Scheduling; using QuantConnect.Securities; namespace QuantConnect.Algorithm.Framework.Selection { /// /// Defines a universe selection model that invokes a selector function on a specific scheduled given by an and an /// public class ScheduledUniverseSelectionModel : UniverseSelectionModel { private readonly IDateRule _dateRule; private readonly ITimeRule _timeRule; private readonly Func> _selector; private readonly DateTimeZone _timeZone; private readonly UniverseSettings _settings; /// /// Initializes a new instance of the class using the algorithm's time zone /// /// Date rule defines what days the universe selection function will be invoked /// Time rule defines what times on each day selected by date rule the universe selection function will be invoked /// Selector function accepting the date time firing time and returning the universe selected symbols /// Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings public ScheduledUniverseSelectionModel(IDateRule dateRule, ITimeRule timeRule, Func> selector, UniverseSettings settings = null) { _dateRule = dateRule; _timeRule = timeRule; _selector = selector; _settings = settings; } /// /// Initializes a new instance of the class /// /// The time zone the date/time rules are in /// Date rule defines what days the universe selection function will be invoked /// Time rule defines what times on each day selected by date rule the universe selection function will be invoked /// Selector function accepting the date time firing time and returning the universe selected symbols /// Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings public ScheduledUniverseSelectionModel(DateTimeZone timeZone, IDateRule dateRule, ITimeRule timeRule, Func> selector, UniverseSettings settings = null) { _timeZone = timeZone; _dateRule = dateRule; _timeRule = timeRule; _selector = selector; _settings = settings; } /// /// Initializes a new instance of the class using the algorithm's time zone /// /// Date rule defines what days the universe selection function will be invoked /// Time rule defines what times on each day selected by date rule the universe selection function will be invoked /// Selector function accepting the date time firing time and returning the universe selected symbols /// Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings public ScheduledUniverseSelectionModel(IDateRule dateRule, ITimeRule timeRule, PyObject selector, UniverseSettings settings = null) : this(null, dateRule, timeRule, selector, settings) { } /// /// Initializes a new instance of the class /// /// The time zone the date/time rules are in /// Date rule defines what days the universe selection function will be invoked /// Time rule defines what times on each day selected by date rule the universe selection function will be invoked /// Selector function accepting the date time firing time and returning the universe selected symbols /// Universe settings for subscriptions added via this universe, null will default to algorithm's universe settings public ScheduledUniverseSelectionModel(DateTimeZone timeZone, IDateRule dateRule, ITimeRule timeRule, PyObject selector, UniverseSettings settings = null) { Func func; selector.TryConvertToDelegate(out func); _timeZone = timeZone; _dateRule = dateRule; _timeRule = timeRule; _selector = func.ConvertSelectionSymbolDelegate(); _settings = settings; } /// /// Creates the universes for this algorithm. Called once after /// /// The algorithm instance to create universes for /// The universes to be used by the algorithm public override IEnumerable CreateUniverses(QCAlgorithm algorithm) { yield return new ScheduledUniverse( // by default ITimeRule yields in UTC _timeZone ?? TimeZones.Utc, _dateRule, _timeRule, _selector, _settings ?? algorithm.UniverseSettings ); } } }