Lean  $LEAN_TAG$
OptionHistory.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.Data;
18 using QuantConnect.Python;
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.Linq;
22 
23 namespace QuantConnect.Research
24 {
25  /// <summary>
26  /// Class to manage information from History Request of Options
27  /// </summary>
28  public class OptionHistory : IEnumerable<Slice>
29  {
30  private IEnumerable<Slice> _data;
31  private PandasConverter _converter;
32  private PyObject _dataframe;
33 
34  /// <summary>
35  /// Create a new instance of <see cref="OptionHistory"/>.
36  /// </summary>
37  /// <param name="data"></param>
38  public OptionHistory(IEnumerable<Slice> data)
39  {
40  _data = data;
41  _converter = new PandasConverter();
42  _dataframe = _converter.GetDataFrame(_data);
43  }
44 
45  /// <summary>
46  /// Gets all data from the History Request that are written in a pandas.DataFrame
47  /// </summary>
48  /// <returns></returns>
49  public PyObject GetAllData()
50  {
51  return _dataframe;
52  }
53 
54  /// <summary>
55  /// Gets all strikes in the option history
56  /// </summary>
57  /// <returns></returns>
58  public PyObject GetStrikes()
59  {
60  var strikes = _data.SelectMany(x => x.OptionChains.SelectMany(y => y.Value.Contracts.Keys.Select(z => (double)z.ID.StrikePrice).Distinct()));
61  using (Py.GIL())
62  {
63  return strikes.Distinct().ToList().ToPython();
64  }
65  }
66 
67  /// <summary>
68  /// Gets all expiry dates in the option history
69  /// </summary>
70  /// <returns></returns>
71  public PyObject GetExpiryDates()
72  {
73  var expiry = _data.SelectMany(x => x.OptionChains.SelectMany(y => y.Value.Contracts.Keys.Select(z => z.ID.Date).Distinct()));
74  using (Py.GIL())
75  {
76  return expiry.Distinct().ToList().ToPython();
77  }
78  }
79 
80  /// <summary>
81  /// Returns a string that represent the current object
82  /// </summary>
83  /// <returns></returns>
84  public override string ToString()
85  {
86  return _converter.ToString();
87  }
88 
89  #region IEnumerable implementation
90 
91  public IEnumerator<Slice> GetEnumerator()
92  {
93  return _data.GetEnumerator();
94  }
95 
96  IEnumerator IEnumerable.GetEnumerator()
97  {
98  return GetEnumerator();
99  }
100 
101  #endregion
102  }
103 }