Lean  $LEAN_TAG$
OptionContractUniverse.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.Linq;
18 using QuantConnect.Data;
19 using System.Collections.Generic;
20 using System.Collections.Specialized;
22 
24 {
25  /// <summary>
26  /// This universe will hold single option contracts and their underlying, managing removals and additions
27  /// </summary>
29  {
30  private readonly HashSet<Symbol> _symbols;
31 
32  /// <summary>
33  /// Creates a new empty instance
34  /// </summary>
35  /// <param name="configuration">The universe configuration to use</param>
36  /// <param name="universeSettings">The universe settings to use</param>
37  public OptionContractUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings)
38  : base(configuration, universeSettings, Time.EndOfTimeTimeSpan,
39  // Argument isn't used since we override 'SelectSymbols'
40  Enumerable.Empty<Symbol>())
41  {
42  _symbols = new HashSet<Symbol>();
43  }
44 
45  /// <summary>
46  /// Returns the symbols defined by the user for this universe
47  /// </summary>
48  /// <param name="utcTime">The current utc time</param>
49  /// <param name="data">The symbols to remain in the universe</param>
50  /// <returns>The data that passes the filter</returns>
51  public override IEnumerable<Symbol> SelectSymbols(DateTime utcTime, BaseDataCollection data)
52  {
53  return _symbols;
54  }
55 
56  /// <summary>
57  /// Event invocator for the <see cref="UserDefinedUniverse.CollectionChanged"/> event
58  /// </summary>
59  /// <param name="args">The notify collection changed event arguments</param>
60  protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
61  {
62  if (args.Action == NotifyCollectionChangedAction.Remove)
63  {
64  var removedSymbol = (Symbol)args.OldItems[0];
65  _symbols.Remove(removedSymbol);
66 
67  // the option has been removed! This can happen when the user manually removed the option contract we remove the underlying
68  // but only if there isn't any other option selected using the same underlying!
69  if (removedSymbol.SecurityType.IsOption()
70  && !_symbols.Any(symbol => symbol.SecurityType.IsOption() && symbol.Underlying == removedSymbol.Underlying))
71  {
72  Remove(removedSymbol.Underlying);
73  }
74  }
75  else if (args.Action == NotifyCollectionChangedAction.Add)
76  {
77  // QCAlgorithm.AddOptionContract will add both underlying and option contract
78  _symbols.Add((Symbol)args.NewItems[0]);
79  }
80 
81  base.OnCollectionChanged(args);
82  }
83 
84  /// <summary>
85  /// Creates a user defined universe symbol
86  /// </summary>
87  /// <param name="market">The market</param>
88  /// <param name="securityType">The underlying option security type</param>
89  /// <returns>A symbol for user defined universe of the specified security type and market</returns>
90  public static Symbol CreateSymbol(string market, SecurityType securityType)
91  {
92  var ticker = $"qc-universe-optioncontract-{securityType.SecurityTypeToLower()}-{market.ToLowerInvariant()}";
93  var underlying = Symbol.Create(ticker, securityType, market);
94  var sid = SecurityIdentifier.GenerateOption(SecurityIdentifier.DefaultDate, underlying.ID, market, 0, 0, 0);
95 
96  return new Symbol(sid, ticker);
97  }
98  }
99 }