/*
* 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;
using QuantConnect.Data.Fundamental;
namespace QuantConnect.Data.UniverseSelection
{
///
/// Defines a universe that reads fundamental us equity data
///
public class FundamentalUniverseFactory : Universe
{
internal static readonly FundamentalUniverse SymbolFactory = new();
private readonly Func, IEnumerable> _selector;
///
/// Initializes a new instance of the class
///
/// The target market
/// The settings used for new subscriptions generated by this universe
/// Returns the symbols that should be included in the universe
public FundamentalUniverseFactory(string market, UniverseSettings universeSettings, Func, IEnumerable> selector)
: this(SymbolFactory.UniverseSymbol(market), universeSettings, selector)
{
}
///
/// Initializes a new instance of the class
///
/// The target market
/// The settings used for new subscriptions generated by this universe
/// Returns the symbols that should be included in the universe
public FundamentalUniverseFactory(string market, UniverseSettings universeSettings, PyObject selector)
: this(market, universeSettings, selector.ConvertToDelegate, object>>())
{
}
///
/// Initializes a new instance of the class
///
/// The target market
/// The settings used for new subscriptions generated by this universe
/// Returns the symbols that should be included in the universe
public FundamentalUniverseFactory(string market, UniverseSettings universeSettings, Func, object> selector)
: this(market, universeSettings, selector.ConvertToUniverseSelectionSymbolDelegate())
{
}
///
/// 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 FundamentalUniverseFactory(Symbol symbol, UniverseSettings universeSettings, Func, IEnumerable> selector)
: base(CreateConfiguration(symbol))
{
UniverseSettings = universeSettings;
_selector = selector;
}
///
/// 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 fundamental subscription configuration with the specified symbol
public static SubscriptionDataConfig CreateConfiguration(Symbol symbol)
{
return new SubscriptionDataConfig(typeof(Fundamental.FundamentalUniverse),
symbol: symbol,
resolution: Resolution.Daily,
dataTimeZone: TimeZones.NewYork,
exchangeTimeZone: TimeZones.NewYork,
fillForward: false,
extendedHours: false,
isInternalFeed: true,
isCustom: false,
tickType: null,
isFilteredSubscription: false
);
}
}
}