Lean  $LEAN_TAG$
OptionChainUniverse.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.Linq;
21 using System.Collections.Generic;
23 
25 {
26  /// <summary>
27  /// Defines a universe for a single option chain
28  /// </summary>
30  {
31  private readonly OptionFilterUniverse _optionFilterUniverse;
32  // as an array to make it easy to prepend to selected symbols
33  private readonly Symbol[] _underlyingSymbol;
34  private DateTime _cacheDate;
35 
36  /// <summary>
37  /// True if this universe filter can run async in the data stack
38  /// </summary>
39  public override bool Asynchronous
40  {
41  get
42  {
43  if (UniverseSettings.Asynchronous.HasValue)
44  {
45  return UniverseSettings.Asynchronous.Value;
46  }
47  return Option.ContractFilter.Asynchronous;
48  }
49  }
50 
51  /// <summary>
52  /// Initializes a new instance of the <see cref="OptionChainUniverse"/> class
53  /// </summary>
54  /// <param name="option">The canonical option chain security</param>
55  /// <param name="universeSettings">The universe settings to be used for new subscriptions</param>
56  public OptionChainUniverse(Option option,
57  UniverseSettings universeSettings)
58  : base(option.SubscriptionDataConfig)
59  {
60  Option = option;
61  UniverseSettings = universeSettings;
62  _underlyingSymbol = new[] { Option.Symbol.Underlying };
63  _optionFilterUniverse = new OptionFilterUniverse(Option);
64  }
65 
66  /// <summary>
67  /// The canonical option chain security
68  /// </summary>
69  public Option Option { get; }
70 
71  /// <summary>
72  /// Gets the settings used for subscriptons added for this universe
73  /// </summary>
74  public override UniverseSettings UniverseSettings
75  {
76  set
77  {
78  if (value != null)
79  {
80  // make sure data mode is raw
82  }
83  }
84  }
85 
86  /// <summary>
87  /// Performs universe selection using the data specified
88  /// </summary>
89  /// <param name="utcTime">The current utc time</param>
90  /// <param name="data">The symbols to remain in the universe</param>
91  /// <returns>The data that passes the filter</returns>
92  public override IEnumerable<Symbol> SelectSymbols(DateTime utcTime, BaseDataCollection data)
93  {
94  // date change detection needs to be done in exchange time zone
95  var localEndTime = utcTime.ConvertFromUtc(Option.Exchange.TimeZone);
96  var exchangeDate = localEndTime.Date;
97  if (data.Symbol.SecurityType == SecurityType.FutureOption)
98  {
99  if (_cacheDate == exchangeDate)
100  {
101  return Unchanged;
102  }
103  _cacheDate = exchangeDate;
104  }
105 
106  // Temporary: this can be removed when future options universe selection is also file-based
107  var availableContractsData = data.Symbol.SecurityType != SecurityType.FutureOption
108  ? data.Data.Cast<OptionUniverse>()
109  : data.Data.Select(x => new OptionUniverse()
110  {
111  Symbol = x.Symbol,
112  Underlying = data.Underlying,
113  Time = x.Time
114  });
115 
116  // we will only update unique strikes when there is an exchange date change
117  _optionFilterUniverse.Refresh(availableContractsData, data.Underlying, localEndTime);
118 
119  var results = Option.ContractFilter.Filter(_optionFilterUniverse);
120 
121  // always prepend the underlying symbol
122  return _underlyingSymbol.Concat(results.Select(x => x.Symbol));
123  }
124 
125  /// <summary>
126  /// Adds the specified security to this universe
127  /// </summary>
128  /// <param name="utcTime">The current utc date time</param>
129  /// <param name="security">The security to be added</param>
130  /// <param name="isInternal">True if internal member</param>
131  /// <returns>True if the security was successfully added,
132  /// false if the security was already in the universe</returns>
133  internal override bool AddMember(DateTime utcTime, Security security, bool isInternal)
134  {
135  // never add members to disposed universes
136  if (DisposeRequested)
137  {
138  return false;
139  }
140 
141  if (Securities.ContainsKey(security.Symbol))
142  {
143  return false;
144  }
145 
146  // method take into account the case, when the option has experienced an adjustment
147  // we update member reference in this case
148  if (Securities.Any(x => x.Value.Security == security))
149  {
150  Member member;
151  Securities.TryRemove(security.Symbol, out member);
152  }
153 
154  return Securities.TryAdd(security.Symbol, new Member(utcTime, security, isInternal));
155  }
156 
157  /// <summary>
158  /// Gets the subscription requests to be added for the specified security
159  /// </summary>
160  /// <param name="security">The security to get subscriptions for</param>
161  /// <param name="currentTimeUtc">The current time in utc. This is the frontier time of the algorithm</param>
162  /// <param name="maximumEndTimeUtc">The max end time</param>
163  /// <param name="subscriptionService">Instance which implements <see cref="ISubscriptionDataConfigService"/> interface</param>
164  /// <returns>All subscriptions required by this security</returns>
165  public override IEnumerable<SubscriptionRequest> GetSubscriptionRequests(Security security, DateTime currentTimeUtc, DateTime maximumEndTimeUtc,
166  ISubscriptionDataConfigService subscriptionService)
167  {
168  if (Option.Symbol.Underlying == security.Symbol)
169  {
170  Option.Underlying = security;
172  }
173  else
174  {
175  // set the underlying security and pricing model from the canonical security
176  var option = (Option)security;
177  option.PriceModel = Option.PriceModel;
178  }
179 
180  return base.GetSubscriptionRequests(security, currentTimeUtc, maximumEndTimeUtc, subscriptionService);
181  }
182  }
183 }