/* * 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 Python.Runtime; using QuantConnect.Data; using QuantConnect.Data.Market; using QuantConnect.Data.UniverseSelection; using QuantConnect.Securities; namespace QuantConnect.Algorithm.Framework.Selection { /// /// Provides an implementation of that simply /// subscribes to the specified set of symbols /// public class CustomUniverseSelectionModel : UniverseSelectionModel { private static readonly MarketHoursDatabase MarketHours = MarketHoursDatabase.FromDataFolder(); private readonly Symbol _symbol; private readonly Func> _selector; private readonly UniverseSettings _universeSettings; private readonly TimeSpan _interval; /// /// Initializes a new instance of the class /// for and /// using the algorithm's universe settings /// /// A unique name for this universe /// Function delegate that accepts a DateTime and returns a collection of string symbols public CustomUniverseSelectionModel(string name, Func> selector) : this(SecurityType.Equity, name, Market.USA, selector, null, Time.OneDay) { } /// /// Initializes a new instance of the class /// for and /// using the algorithm's universe settings /// /// A unique name for this universe /// Function delegate that accepts a DateTime and returns a collection of string symbols public CustomUniverseSelectionModel(string name, PyObject selector) : this(SecurityType.Equity, name, Market.USA, selector, null, Time.OneDay) { } /// /// Initializes a new instance of the class /// /// The security type of the universe /// A unique name for this universe /// The market of the universe /// Function delegate that accepts a DateTime and returns a collection of string symbols /// The settings used when adding symbols to the algorithm, specify null to use algorithm.UniverseSettings /// The interval at which selection should be performed public CustomUniverseSelectionModel(SecurityType securityType, string name, string market, Func> selector, UniverseSettings universeSettings, TimeSpan interval) { _interval = interval; _selector = selector; _universeSettings = universeSettings; _symbol = Symbol.Create($"{name}-{securityType}-{market}", securityType, market); } /// /// Initializes a new instance of the class /// /// The security type of the universe /// A unique name for this universe /// The market of the universe /// Function delegate that accepts a DateTime and returns a collection of string symbols /// The settings used when adding symbols to the algorithm, specify null to use algorithm.UniverseSettings /// The interval at which selection should be performed public CustomUniverseSelectionModel(SecurityType securityType, string name, string market, PyObject selector, UniverseSettings universeSettings, TimeSpan interval) : this( securityType, name, market, selector.ConvertToDelegate>().ConvertToUniverseSelectionStringDelegate(), universeSettings, interval ) { } /// /// Creates the universes for this algorithm. Called at algorithm start. /// /// The universes defined by this model public override IEnumerable CreateUniverses(QCAlgorithm algorithm) { var universeSettings = _universeSettings ?? algorithm.UniverseSettings; var entry = MarketHours.GetEntry(_symbol.ID.Market, (string) null, _symbol.SecurityType); var config = new SubscriptionDataConfig( universeSettings.Resolution == Resolution.Tick ? typeof(Tick) : typeof(TradeBar), _symbol, universeSettings.Resolution, entry.DataTimeZone, entry.ExchangeHours.TimeZone, universeSettings.FillForward, universeSettings.ExtendedMarketHours, true ); yield return new CustomUniverse(config, universeSettings, _interval, dt => Select(algorithm, dt)); } /// /// /// /// /// /// public virtual IEnumerable Select(QCAlgorithm algorithm, DateTime date) { if (_selector == null) { throw new ArgumentNullException(nameof(_selector)); } return _selector(date); } /// /// Returns a string that represents the current object /// public override string ToString() => _symbol.Value; } }