/* * 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 { /// /// Provides a functional implementation of /// /// The BaseData type to provide to the user defined universe filter public class FuncUniverse : Universe where T : BaseData { private readonly Func, IEnumerable> _universeSelector; /// /// Initializes a new instance of the class /// /// The configuration used to resolve the data for universe selection /// The settings used for new subscriptions generated by this universe /// Returns the symbols that should be included in the universe public FuncUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, Func, IEnumerable> universeSelector) : base(configuration) { _universeSelector = universeSelector; _universeSelector ??= (dataPoints) => dataPoints.Select(x => x.Symbol); UniverseSettings = universeSettings; } /// /// Initializes a new instance of the class for a filter function loaded from Python /// /// The configuration used to resolve the data for universe selection /// The settings used for new subscriptions generated by this universe /// Function that returns the symbols that should be included in the universe public FuncUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, PyObject universeSelector) : this(configuration, universeSettings, universeSelector.ConvertPythonUniverseFilterFunction()) { } /// /// Performs an initial, coarse filter /// /// The current utc time /// The coarse fundamental data /// The data that passes the filter public override IEnumerable SelectSymbols(DateTime utcTime, BaseDataCollection data) { return _universeSelector(data.Data.Cast()); } } /// /// Provides a functional implementation of /// public class FuncUniverse : FuncUniverse { /// /// Initializes a new instance of the class /// /// The configuration used to resolve the data for universe selection /// The settings used for new subscriptions generated by this universe /// Returns the symbols that should be included in the universe public FuncUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, Func, IEnumerable> universeSelector) : base(configuration, universeSettings, universeSelector) { } /// /// Initializes a new instance of the class /// /// The configuration used to resolve the data for universe selection /// The settings used for new subscriptions generated by this universe /// Returns the symbols that should be included in the universe public FuncUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, PyObject universeSelector) : base(configuration, universeSettings, universeSelector) { } } }