/* * 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 System.Linq; using Python.Runtime; namespace QuantConnect.Data.UniverseSelection { /// /// ConstituentsUniverse allows to perform universe selection based on an /// already preselected set of . /// /// Using this class allows a performance improvement, since there is no /// runtime logic computation required for selecting the public class ConstituentsUniverse : FuncUniverse where T : BaseData { /// /// Creates a new instance of the /// /// The universe symbol /// The universe settings to use /// User-provided function to filter constituents universe with public ConstituentsUniverse( Symbol symbol, UniverseSettings universeSettings, Func, IEnumerable> constituentsFilter = null) : this(new SubscriptionDataConfig(typeof(T), symbol, Resolution.Daily, TimeZones.NewYork, TimeZones.NewYork, false, false, true, true), universeSettings, constituentsFilter) { } /// /// Creates a new instance of the /// /// The universe symbol /// The universe settings to use /// User-provided function to filter constituents universe with public ConstituentsUniverse( Symbol symbol, UniverseSettings universeSettings, PyObject constituentsFilter = null) : this(symbol, universeSettings, constituentsFilter.ConvertPythonUniverseFilterFunction()) { } /// /// Creates a new instance of the /// /// The universe configuration to use /// The universe settings to use /// User-provided function to filter constituents universe with public ConstituentsUniverse( SubscriptionDataConfig subscriptionDataConfig, UniverseSettings universeSettings, Func, IEnumerable> constituentsFilter = null) : base(subscriptionDataConfig, universeSettings, constituentsFilter ?? (constituents => { var symbols = constituents.Select(baseData => baseData.Symbol).ToList(); // for performance, just compare to Symbol.None if we have 1 Symbol if (symbols.Count == 1 && symbols[0] == Symbol.None) { // no symbol selected return Enumerable.Empty(); } return symbols; })) { if (!subscriptionDataConfig.IsCustomData) { throw new InvalidOperationException($"{typeof(T).Name} {nameof(SubscriptionDataConfig)}" + $" only supports custom data property set to 'true'"); } } /// /// Constituent universe for a Python function /// /// The universe configuration to use /// The universe settings to use /// User-provided function to filter constituents universe with public ConstituentsUniverse( SubscriptionDataConfig subscriptionDataConfig, UniverseSettings universeSettings, PyObject constituentsFilter = null) : this(subscriptionDataConfig, universeSettings, constituentsFilter.ConvertPythonUniverseFilterFunction()) { } } /// /// ConstituentsUniverse allows to perform universe selection based on an /// already preselected set of . /// /// Using this class allows a performance improvement, since there is no /// runtime logic computation required for selecting the public class ConstituentsUniverse : ConstituentsUniverse { /// /// Creates a new instance of the /// /// The universe symbol /// The universe settings to use /// The constituents filter function public ConstituentsUniverse(Symbol symbol, UniverseSettings universeSettings, Func, IEnumerable> filterFunc) : base(symbol, universeSettings, filterFunc) { } /// /// Creates a new instance of the /// /// The universe symbol /// The universe settings to use public ConstituentsUniverse(Symbol symbol, UniverseSettings universeSettings) : base(symbol, universeSettings, (Func, IEnumerable>)null) { } /// /// Creates a new instance of the /// /// The universe symbol /// The universe settings to use /// The constituents filter function public ConstituentsUniverse(Symbol symbol, UniverseSettings universeSettings, PyObject filterFunc) : base(symbol, universeSettings, filterFunc) { } } }