/* * 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.Concurrent; using System.Collections.Generic; using System.Linq; using QuantConnect.Interfaces; using QuantConnect.Securities; namespace QuantConnect.Data.UniverseSelection { /// /// Provides an implementation of that redirects all calls to a /// wrapped (or decorated) universe. This provides scaffolding for other decorators who /// only need to override one or two methods. /// /// Requires special handling due to `this != this.Universe` /// public abstract class UniverseDecorator : Universe { /// /// The decorated universe instance /// protected Universe Universe { get; init; } /// /// Gets the settings used for subscriptions added for this universe /// public override UniverseSettings UniverseSettings { get { return Universe.UniverseSettings; } set { Universe.UniverseSettings = value; } } /// /// Gets the internal security collection used to define membership in this universe /// public override ConcurrentDictionary Securities { get { return Universe.Securities; } } /// /// Initializes a new instance of the class /// /// The decorated universe. All overridable methods delegate to this instance. protected UniverseDecorator(Universe universe) : base(universe.Configuration) { Universe = universe; } /// /// Gets the subscription requests to be added for the specified security /// /// The security to get subscriptions for /// The current time in utc. This is the frontier time of the algorithm /// The max end time /// All subscriptions required by this security [Obsolete("This overload is obsolete and will not be called. It was not capable of creating new SubscriptionDataConfig due to lack of information")] public override IEnumerable GetSubscriptionRequests(Security security, DateTime currentTimeUtc, DateTime maximumEndTimeUtc) { return Universe.GetSubscriptionRequests(security, currentTimeUtc, maximumEndTimeUtc); } /// /// Gets the subscription requests to be added for the specified security /// /// The security to get subscriptions for /// The current time in utc. This is the frontier time of the algorithm /// The max end time /// Instance which implements interface /// All subscriptions required by this security public override IEnumerable GetSubscriptionRequests(Security security, DateTime currentTimeUtc, DateTime maximumEndTimeUtc, ISubscriptionDataConfigService subscriptionService) { var result = Universe.GetSubscriptionRequests( security, currentTimeUtc, maximumEndTimeUtc, subscriptionService).ToList(); for (var i = 0; i < result.Count; i++) { // This is required because the system tracks which universe // is requesting to add or remove each SubscriptionRequest. // UniverseDecorator is a special case because // `this != UniverseDecorator.Universe` result[i] = new SubscriptionRequest(result[i], universe: this); } return result; } /// /// Determines whether or not the specified security can be removed from /// this universe. This is useful to prevent securities from being taken /// out of a universe before the algorithm has had enough time to make /// decisions on the security /// /// The current utc time /// The security to check if its ok to remove /// True if we can remove the security, false otherwise public override bool CanRemoveMember(DateTime utcTime, Security security) { return Universe.CanRemoveMember(utcTime, security); } /// /// Creates and configures a security for the specified symbol /// /// The symbol of the security to be created /// The algorithm instance /// The market hours database /// The symbol properties database /// The newly initialized security object [Obsolete("CreateSecurity is obsolete and will not be called. The system will create the required Securities based on selected symbols")] public override Security CreateSecurity(Symbol symbol, IAlgorithm algorithm, MarketHoursDatabase marketHoursDatabase, SymbolPropertiesDatabase symbolPropertiesDatabase) { return Universe.CreateSecurity(symbol, algorithm, marketHoursDatabase, symbolPropertiesDatabase); } /// /// Performs universe selection using the data specified /// /// The current utc time /// The symbols to remain in the universe /// The data that passes the filter public override IEnumerable SelectSymbols(DateTime utcTime, BaseDataCollection data) { return Universe.SelectSymbols(utcTime, data); } } }