/* * 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.Linq; using Python.Runtime; using System.Collections.Generic; namespace QuantConnect.Data.UniverseSelection { /// /// Defines a universe that reads coarse us equity data /// public class CoarseFundamentalUniverse : Universe { private readonly Func, IEnumerable> _selector; /// /// Initializes a new instance of the class /// /// The settings used for new subscriptions generated by this universe /// Returns the symbols that should be included in the universe public CoarseFundamentalUniverse(UniverseSettings universeSettings, Func, IEnumerable> selector) : base(CreateConfiguration(FundamentalUniverseFactory.SymbolFactory.UniverseSymbol())) { UniverseSettings = universeSettings; _selector = selector; } /// /// Initializes a new instance of the class /// /// The settings used for new subscriptions generated by this universe /// Returns the symbols that should be included in the universe public CoarseFundamentalUniverse(UniverseSettings universeSettings, PyObject selector) : this(FundamentalUniverseFactory.SymbolFactory.UniverseSymbol(), universeSettings, selector) { } /// /// Initializes a new instance of the class /// /// Defines the symbol to use for this universe /// The settings used for new subscriptions generated by this universe /// Returns the symbols that should be included in the universe public CoarseFundamentalUniverse(Symbol symbol, UniverseSettings universeSettings, Func, IEnumerable> selector) : base(CreateConfiguration(symbol)) { UniverseSettings = universeSettings; _selector = selector; } /// /// Initializes a new instance of the class /// /// Defines the symbol to use for this universe /// The settings used for new subscriptions generated by this universe /// Returns the symbols that should be included in the universe public CoarseFundamentalUniverse(Symbol symbol, UniverseSettings universeSettings, PyObject selector) : base(CreateConfiguration(symbol)) { UniverseSettings = universeSettings; Func, object> func; if (selector.TryConvertToDelegate(out func)) { _selector = func.ConvertToUniverseSelectionSymbolDelegate(); } } /// /// 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 _selector(data.Data.OfType()); } /// /// Creates a subscription configuration for the US-equity market /// /// The symbol used in the returned configuration /// A coarse fundamental subscription configuration with the specified symbol public static SubscriptionDataConfig CreateConfiguration(Symbol symbol) { return FundamentalUniverseFactory.CreateConfiguration(symbol); } } }