Lean  $LEAN_TAG$
OptionChains.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 Python.Runtime;
17 using QuantConnect.Python;
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 
23 {
24  /// <summary>
25  /// Collection of <see cref="OptionChain"/> keyed by canonical option symbol
26  /// </summary>
27  public class OptionChains : DataDictionary<OptionChain>
28  {
29  private static readonly IEnumerable<string> _indexNames = new[] { "canonical", "symbol" };
30 
31  private readonly Lazy<PyObject> _dataframe;
32 
33  /// <summary>
34  /// Creates a new instance of the <see cref="OptionChains"/> dictionary
35  /// </summary>
36  public OptionChains()
37  : this(default)
38  {
39  }
40 
41  /// <summary>
42  /// Creates a new instance of the <see cref="OptionChains"/> dictionary
43  /// </summary>
44  public OptionChains(DateTime time)
45  : base(time)
46  {
47  _dataframe = new Lazy<PyObject>(InitializeDataFrame, isThreadSafe: false);
48  }
49 
50  /// <summary>
51  /// The data frame representation of the option chains
52  /// </summary>
53  public PyObject DataFrame => _dataframe.Value;
54 
55  /// <summary>
56  /// Gets or sets the OptionChain with the specified ticker.
57  /// </summary>
58  /// <returns>
59  /// The OptionChain with the specified ticker.
60  /// </returns>
61  /// <param name="ticker">The ticker of the element to get or set.</param>
62  /// <remarks>Wraps the base implementation to enable indexing in python algorithms due to pythonnet limitations</remarks>
63  public new OptionChain this[string ticker] { get { return base[ticker]; } set { base[ticker] = value; } }
64 
65  /// <summary>
66  /// Gets or sets the OptionChain with the specified Symbol.
67  /// </summary>
68  /// <returns>
69  /// The OptionChain with the specified Symbol.
70  /// </returns>
71  /// <param name="symbol">The Symbol of the element to get or set.</param>
72  /// <remarks>Wraps the base implementation to enable indexing in python algorithms due to pythonnet limitations</remarks>
73  public new OptionChain this[Symbol symbol] { get { return base[symbol]; } set { base[symbol] = value; } }
74 
75  private PyObject InitializeDataFrame()
76  {
77  var dataFrames = this.Select(kvp => kvp.Value.DataFrame).ToList();
78  var canonicalSymbols = this.Select(kvp => kvp.Key);
79 
80  return PandasConverter.ConcatDataFrames(dataFrames, keys: canonicalSymbols, names: _indexNames, sort: false);
81  }
82  }
83 }