Lean  $LEAN_TAG$
FuturesChainUniverse.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 
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
23 
25 {
26  /// <summary>
27  /// Defines a universe for a single futures chain
28  /// </summary>
30  {
31  private DateTime _cacheDate;
32 
33  /// <summary>
34  /// True if this universe filter can run async in the data stack
35  /// </summary>
36  public override bool Asynchronous
37  {
38  get
39  {
40  if (UniverseSettings.Asynchronous.HasValue)
41  {
42  return UniverseSettings.Asynchronous.Value;
43  }
45  }
46  }
47 
48  /// <summary>
49  /// Initializes a new instance of the <see cref="FuturesChainUniverse"/> class
50  /// </summary>
51  /// <param name="future">The canonical future chain security</param>
52  /// <param name="universeSettings">The universe settings to be used for new subscriptions</param>
54  UniverseSettings universeSettings)
55  : base(future.SubscriptionDataConfig)
56  {
57  Future = future;
58  UniverseSettings = universeSettings;
59  }
60 
61  /// <summary>
62  /// The canonical future chain security
63  /// </summary>
64  public Future Future { get; }
65 
66  /// <summary>
67  /// Gets the settings used for subscriptons added for this universe
68  /// </summary>
69  public override UniverseSettings UniverseSettings
70  {
71  set
72  {
73  if (value != null)
74  {
75  // make sure data mode is raw
77  }
78  }
79  }
80 
81  /// <summary>
82  /// Performs universe selection using the data specified
83  /// </summary>
84  /// <param name="utcTime">The current utc time</param>
85  /// <param name="data">The symbols to remain in the universe</param>
86  /// <returns>The data that passes the filter</returns>
87  public override IEnumerable<Symbol> SelectSymbols(DateTime utcTime, BaseDataCollection data)
88  {
89  // date change detection needs to be done in exchange time zone
90  var localEndTime = utcTime.ConvertFromUtc(Future.Exchange.TimeZone);
91  var exchangeDate = localEndTime.Date;
92  if (_cacheDate == exchangeDate)
93  {
94  return Unchanged;
95  }
96 
97  var availableContracts = data.Data.Select(x => x.Symbol);
98  var results = Future.ContractFilter.Filter(new FutureFilterUniverse(availableContracts, localEndTime));
99  _cacheDate = exchangeDate;
100 
101  return results;
102  }
103 
104  /// <summary>
105  /// Gets the subscription requests to be added for the specified security
106  /// </summary>
107  /// <param name="security">The security to get subscriptions for</param>
108  /// <param name="currentTimeUtc">The current time in utc. This is the frontier time of the algorithm</param>
109  /// <param name="maximumEndTimeUtc">The max end time</param>
110  /// <param name="subscriptionService">Instance which implements <see cref="ISubscriptionDataConfigService"/> interface</param>
111  /// <returns>All subscriptions required by this security</returns>
112  public override IEnumerable<SubscriptionRequest> GetSubscriptionRequests(Security security, DateTime currentTimeUtc, DateTime maximumEndTimeUtc,
113  ISubscriptionDataConfigService subscriptionService)
114  {
115  if (Future.Symbol.Underlying == security.Symbol)
116  {
117  Future.Underlying = security;
118  }
119 
120  return base.GetSubscriptionRequests(security, currentTimeUtc, maximumEndTimeUtc, subscriptionService);
121  }
122  }
123 }