Lean  $LEAN_TAG$
OpenInterestFutureUniverseSelectionModel.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Collections.Generic;
18 using System.Linq;
19 using NodaTime;
20 using Python.Runtime;
21 using QuantConnect.Data;
25 
27 {
28  /// <summary>
29  /// Selects contracts in a futures universe, sorted by open interest. This allows the selection to identifiy current
30  /// active contract.
31  /// </summary>
33  {
34  private readonly int? _chainContractsLookupLimit;
35  private readonly IAlgorithm _algorithm;
36  private readonly int? _resultsLimit;
37  private readonly MarketHoursDatabase _marketHoursDatabase;
38 
39  /// <summary>
40  /// Creates a new instance of <see cref="OpenInterestFutureUniverseSelectionModel" />
41  /// </summary>
42  /// <param name="algorithm">Algorithm</param>
43  /// <param name="futureChainSymbolSelector">Selects symbols from the provided future chain</param>
44  /// <param name="chainContractsLookupLimit">Limit on how many contracts to query for open interest</param>
45  /// <param name="resultsLimit">Limit on how many contracts will be part of the universe</param>
46  public OpenInterestFutureUniverseSelectionModel(IAlgorithm algorithm, Func<DateTime, IEnumerable<Symbol>> futureChainSymbolSelector, int? chainContractsLookupLimit = 6,
47  int? resultsLimit = 1) : base(TimeSpan.FromDays(1), futureChainSymbolSelector)
48  {
49  _marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
50  if (algorithm == null)
51  {
52  throw new ArgumentNullException(nameof(algorithm));
53  }
54 
55  _algorithm = algorithm;
56  _resultsLimit = resultsLimit;
57  _chainContractsLookupLimit = chainContractsLookupLimit;
58  }
59 
60  /// <summary>
61  /// Creates a new instance of <see cref="OpenInterestFutureUniverseSelectionModel" />
62  /// </summary>
63  /// <param name="algorithm">Algorithm</param>
64  /// <param name="futureChainSymbolSelector">Selects symbols from the provided future chain</param>
65  /// <param name="chainContractsLookupLimit">Limit on how many contracts to query for open interest</param>
66  /// <param name="resultsLimit">Limit on how many contracts will be part of the universe</param>
67  public OpenInterestFutureUniverseSelectionModel(IAlgorithm algorithm, PyObject futureChainSymbolSelector, int? chainContractsLookupLimit = 6,
68  int? resultsLimit = 1) : this(algorithm, ConvertFutureChainSymbolSelectorToFunc(futureChainSymbolSelector), chainContractsLookupLimit, resultsLimit)
69  {
70  }
71 
72  /// <summary>
73  /// Defines the future chain universe filter
74  /// </summary>
76  {
77  return filter.Contracts(FilterByOpenInterest(filter.ToDictionary(x => x, x => _marketHoursDatabase.GetEntry(x.ID.Market, x, x.ID.SecurityType))));
78  }
79 
80  /// <summary>
81  /// Filters a set of contracts based on open interest.
82  /// </summary>
83  /// <param name="contracts">Contracts to filter</param>
84  /// <returns>Filtered set</returns>
85  public IEnumerable<Symbol> FilterByOpenInterest(IReadOnlyDictionary<Symbol, MarketHoursDatabase.Entry> contracts)
86  {
87  var symbols = new List<Symbol>(_chainContractsLookupLimit.HasValue ? contracts.Keys.OrderBy(x => x.ID.Date).Take(_chainContractsLookupLimit.Value) : contracts.Keys);
88  var openInterest = symbols.GroupBy(x => contracts[x]).SelectMany(g => GetOpenInterest(g.Key, g.Select(i => i))).ToDictionary(x => x.Key, x => x.Value);
89 
90  if (openInterest.Count == 0)
91  {
92  _algorithm.Error(
93  $"{nameof(OpenInterestFutureUniverseSelectionModel)}.{nameof(FilterByOpenInterest)}: Failed to get historical open interest, no symbol will be selected."
94  );
95  return Enumerable.Empty<Symbol>();
96  }
97 
98  var filtered = openInterest.OrderByDescending(x => x.Value).ThenBy(x => x.Key.ID.Date).Select(x => x.Key);
99  if (_resultsLimit.HasValue)
100  {
101  filtered = filtered.Take(_resultsLimit.Value);
102  }
103 
104  return filtered;
105  }
106 
107  private Dictionary<Symbol, decimal> GetOpenInterest(MarketHoursDatabase.Entry marketHours, IEnumerable<Symbol> symbols)
108  {
109  var current = _algorithm.UtcTime;
110  var exchangeHours = marketHours.ExchangeHours;
111  var endTime = Instant.FromDateTimeUtc(_algorithm.UtcTime).InZone(exchangeHours.TimeZone).ToDateTimeUnspecified();
112  var previousDay = Time.GetStartTimeForTradeBars(exchangeHours, endTime, Time.OneDay, 1, true, marketHours.DataTimeZone);
113  var requests = symbols.Select(
114  symbol => new HistoryRequest(
115  previousDay,
116  current,
117  typeof(Tick),
118  symbol,
119  Resolution.Tick,
120  exchangeHours,
121  exchangeHours.TimeZone,
122  null,
123  true,
124  false,
126  TickType.OpenInterest
127  )
128  )
129  .ToArray();
130  return _algorithm.HistoryProvider.GetHistory(requests, exchangeHours.TimeZone)
131  .Where(s => s.HasData && s.Ticks.Keys.Count > 0)
132  .SelectMany(s => s.Ticks.Select(x => new Tuple<Symbol, Tick>(x.Key, x.Value.LastOrDefault())))
133  .GroupBy(x => x.Item1)
134  .ToDictionary(x => x.Key, x => x.OrderByDescending(i => i.Item2.Time).LastOrDefault().Item2.Value);
135  }
136 
137  /// <summary>
138  /// Converts future chain symbol selector, provided as a Python lambda function, to a managed func
139  /// </summary>
140  /// <param name="futureChainSymbolSelector">Python lambda function that selects symbols from the provided future chain</param>
141  /// <returns>Given Python future chain symbol selector as a func objet</returns>
142  /// <exception cref="ArgumentException"></exception>
143  private static Func<DateTime, IEnumerable<Symbol>> ConvertFutureChainSymbolSelectorToFunc(PyObject futureChainSymbolSelector)
144  {
145  if (futureChainSymbolSelector.TryConvertToDelegate(out Func<DateTime, IEnumerable<Symbol>> futureSelector))
146  {
147  return futureSelector;
148  }
149  else
150  {
151  using (Py.GIL())
152  {
153  throw new ArgumentException($"FutureUniverseSelectionModel.ConvertFutureChainSymbolSelectorToFunc: {futureChainSymbolSelector.Repr()} is not a valid argument.");
154  }
155  }
156  }
157  }
158 }