/* * 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 NodaTime; using Python.Runtime; using QuantConnect.Data; using QuantConnect.Data.Market; using QuantConnect.Interfaces; using QuantConnect.Securities; namespace QuantConnect.Algorithm.Framework.Selection { /// /// Selects contracts in a futures universe, sorted by open interest. This allows the selection to identifiy current /// active contract. /// public class OpenInterestFutureUniverseSelectionModel : FutureUniverseSelectionModel { private readonly int? _chainContractsLookupLimit; private readonly IAlgorithm _algorithm; private readonly int? _resultsLimit; private readonly MarketHoursDatabase _marketHoursDatabase; /// /// Creates a new instance of /// /// Algorithm /// Selects symbols from the provided future chain /// Limit on how many contracts to query for open interest /// Limit on how many contracts will be part of the universe public OpenInterestFutureUniverseSelectionModel(IAlgorithm algorithm, Func> futureChainSymbolSelector, int? chainContractsLookupLimit = 6, int? resultsLimit = 1) : base(TimeSpan.FromDays(1), futureChainSymbolSelector) { _marketHoursDatabase = MarketHoursDatabase.FromDataFolder(); if (algorithm == null) { throw new ArgumentNullException(nameof(algorithm)); } _algorithm = algorithm; _resultsLimit = resultsLimit; _chainContractsLookupLimit = chainContractsLookupLimit; } /// /// Creates a new instance of /// /// Algorithm /// Selects symbols from the provided future chain /// Limit on how many contracts to query for open interest /// Limit on how many contracts will be part of the universe public OpenInterestFutureUniverseSelectionModel(IAlgorithm algorithm, PyObject futureChainSymbolSelector, int? chainContractsLookupLimit = 6, int? resultsLimit = 1) : this(algorithm, ConvertFutureChainSymbolSelectorToFunc(futureChainSymbolSelector), chainContractsLookupLimit, resultsLimit) { } /// /// Defines the future chain universe filter /// protected override FutureFilterUniverse Filter(FutureFilterUniverse filter) { // Remove duplicated keys return filter.Contracts(FilterByOpenInterest( filter.DistinctBy(x => x).ToDictionary(x => x.Symbol, x => _marketHoursDatabase.GetEntry(x.ID.Market, x, x.ID.SecurityType)))); } /// /// Filters a set of contracts based on open interest. /// /// Contracts to filter /// Filtered set public IEnumerable FilterByOpenInterest(IReadOnlyDictionary contracts) { var symbols = new List(_chainContractsLookupLimit.HasValue ? contracts.Keys.OrderBy(x => x.ID.Date).Take(_chainContractsLookupLimit.Value) : contracts.Keys); var openInterest = symbols.GroupBy(x => contracts[x]).SelectMany(g => GetOpenInterest(g.Key, g.Select(i => i))).ToDictionary(x => x.Key, x => x.Value); if (openInterest.Count == 0) { _algorithm.Error( $"{nameof(OpenInterestFutureUniverseSelectionModel)}.{nameof(FilterByOpenInterest)}: Failed to get historical open interest, no symbol will be selected." ); return Enumerable.Empty(); } var filtered = openInterest.OrderByDescending(x => x.Value).ThenBy(x => x.Key.ID.Date).Select(x => x.Key); if (_resultsLimit.HasValue) { filtered = filtered.Take(_resultsLimit.Value); } return filtered; } private Dictionary GetOpenInterest(MarketHoursDatabase.Entry marketHours, IEnumerable symbols) { var current = _algorithm.UtcTime; var exchangeHours = marketHours.ExchangeHours; var endTime = Instant.FromDateTimeUtc(_algorithm.UtcTime).InZone(exchangeHours.TimeZone).ToDateTimeUnspecified(); var previousDay = Time.GetStartTimeForTradeBars(exchangeHours, endTime, Time.OneDay, 1, true, marketHours.DataTimeZone); var requests = symbols.Select( symbol => new HistoryRequest( previousDay, current, typeof(Tick), symbol, Resolution.Tick, exchangeHours, exchangeHours.TimeZone, null, true, false, DataNormalizationMode.Raw, TickType.OpenInterest ) ) .ToArray(); return _algorithm.HistoryProvider.GetHistory(requests, exchangeHours.TimeZone) .Where(s => s.HasData && s.Ticks.Keys.Count > 0) .SelectMany(s => s.Ticks.Select(x => new Tuple(x.Key, x.Value.LastOrDefault()))) .GroupBy(x => x.Item1) .ToDictionary(x => x.Key, x => x.OrderByDescending(i => i.Item2.Time).LastOrDefault().Item2.Value); } /// /// Converts future chain symbol selector, provided as a Python lambda function, to a managed func /// /// Python lambda function that selects symbols from the provided future chain /// Given Python future chain symbol selector as a func objet /// private static Func> ConvertFutureChainSymbolSelectorToFunc(PyObject futureChainSymbolSelector) { if (futureChainSymbolSelector.TryConvertToDelegate(out Func> futureSelector)) { return futureSelector; } else { using (Py.GIL()) { throw new ArgumentException($"FutureUniverseSelectionModel.ConvertFutureChainSymbolSelectorToFunc: {futureChainSymbolSelector.Repr()} is not a valid argument."); } } } } }