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> _flattenedDfIndexNames = new[] { "canonical", "symbol" };
30 
31  private readonly Lazy<PyObject> _dataframe;
32  private readonly bool _flatten;
33 
34  /// <summary>
35  /// Creates a new instance of the <see cref="OptionChains"/> dictionary
36  /// </summary>
37  public OptionChains()
38  : this(default, true)
39  {
40  }
41 
42  /// <summary>
43  /// Creates a new instance of the <see cref="OptionChains"/> dictionary
44  /// </summary>
45  public OptionChains(bool flatten)
46  : this(default, flatten)
47  {
48  }
49 
50  /// <summary>
51  /// Creates a new instance of the <see cref="OptionChains"/> dictionary
52  /// </summary>
53  public OptionChains(DateTime time, bool flatten = true)
54  : base(time)
55  {
56  _flatten = flatten;
57  _dataframe = new Lazy<PyObject>(InitializeDataFrame, isThreadSafe: false);
58  }
59 
60  /// <summary>
61  /// The data frame representation of the option chains
62  /// </summary>
63  public PyObject DataFrame => _dataframe.Value;
64 
65  /// <summary>
66  /// Gets or sets the OptionChain with the specified ticker.
67  /// </summary>
68  /// <returns>
69  /// The OptionChain with the specified ticker.
70  /// </returns>
71  /// <param name="ticker">The ticker 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[string ticker] { get { return base[ticker]; } set { base[ticker] = value; } }
74 
75  /// <summary>
76  /// Gets or sets the OptionChain with the specified Symbol.
77  /// </summary>
78  /// <returns>
79  /// The OptionChain with the specified Symbol.
80  /// </returns>
81  /// <param name="symbol">The Symbol of the element to get or set.</param>
82  /// <remarks>Wraps the base implementation to enable indexing in python algorithms due to pythonnet limitations</remarks>
83  public new OptionChain this[Symbol symbol] { get { return base[symbol]; } set { base[symbol] = value; } }
84 
85  private PyObject InitializeDataFrame()
86  {
87  if (!PythonEngine.IsInitialized)
88  {
89  return null;
90  }
91 
92  var dataFrames = this.Select(kvp => kvp.Value.DataFrame).ToList();
93 
94  if (_flatten)
95  {
96  var canonicalSymbols = this.Select(kvp => kvp.Key);
97  return PandasConverter.ConcatDataFrames(dataFrames, keys: canonicalSymbols, names: _flattenedDfIndexNames, sort: false);
98  }
99 
100  return PandasConverter.ConcatDataFrames(dataFrames, sort: false);
101  }
102  }
103 }