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  }
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 (_cacheDate == exchangeDate)
98  {
99  return Unchanged;
100  }
101 
102  var availableContracts = data.Data.Select(x => x.Symbol);
103  // we will only update unique strikes when there is an exchange date change
104  _optionFilterUniverse.Refresh(availableContracts, data.Underlying, localEndTime);
105 
106  var results = Option.ContractFilter.Filter(_optionFilterUniverse);
107  _cacheDate = exchangeDate;
108 
109  // always prepend the underlying symbol
110  return _underlyingSymbol.Concat(results);
111  }
112 
113  /// <summary>
114  /// Adds the specified security to this universe
115  /// </summary>
116  /// <param name="utcTime">The current utc date time</param>
117  /// <param name="security">The security to be added</param>
118  /// <param name="isInternal">True if internal member</param>
119  /// <returns>True if the security was successfully added,
120  /// false if the security was already in the universe</returns>
121  internal override bool AddMember(DateTime utcTime, Security security, bool isInternal)
122  {
123  // never add members to disposed universes
124  if (DisposeRequested)
125  {
126  return false;
127  }
128 
129  if (Securities.ContainsKey(security.Symbol))
130  {
131  return false;
132  }
133 
134  // method take into account the case, when the option has experienced an adjustment
135  // we update member reference in this case
136  if (Securities.Any(x => x.Value.Security == security))
137  {
138  Member member;
139  Securities.TryRemove(security.Symbol, out member);
140  }
141 
142  return Securities.TryAdd(security.Symbol, new Member(utcTime, security, isInternal));
143  }
144 
145  /// <summary>
146  /// Gets the subscription requests to be added for the specified security
147  /// </summary>
148  /// <param name="security">The security to get subscriptions for</param>
149  /// <param name="currentTimeUtc">The current time in utc. This is the frontier time of the algorithm</param>
150  /// <param name="maximumEndTimeUtc">The max end time</param>
151  /// <param name="subscriptionService">Instance which implements <see cref="ISubscriptionDataConfigService"/> interface</param>
152  /// <returns>All subscriptions required by this security</returns>
153  public override IEnumerable<SubscriptionRequest> GetSubscriptionRequests(Security security, DateTime currentTimeUtc, DateTime maximumEndTimeUtc,
154  ISubscriptionDataConfigService subscriptionService)
155  {
156  if (Option.Symbol.Underlying == security.Symbol)
157  {
158  Option.Underlying = security;
160  }
161  else
162  {
163  // set the underlying security and pricing model from the canonical security
164  var option = (Option)security;
165  option.PriceModel = Option.PriceModel;
166  }
167 
168  return base.GetSubscriptionRequests(security, currentTimeUtc, maximumEndTimeUtc, subscriptionService);
169  }
170  }
171 }