Lean  $LEAN_TAG$
FutureHistory.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 
24 {
25  /// <summary>
26  /// Class to manage information from History Request of Futures
27  /// </summary>
28  public class FutureHistory : 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="FutureHistory"/>.
36  /// </summary>
37  /// <param name="data"></param>
38  public FutureHistory(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 expity dates in the future history
56  /// </summary>
57  /// <returns></returns>
58  public PyObject GetExpiryDates()
59  {
60  var expiry = _data.SelectMany(x => x.FuturesChains.SelectMany(y => y.Value.Contracts.Keys.Select(z => z.ID.Date).Distinct()));
61  using (Py.GIL())
62  {
63  return expiry.Distinct().ToList().ToPython();
64  }
65  }
66 
67  /// <summary>
68  /// Returns a string that represent the current object
69  /// </summary>
70  /// <returns></returns>
71  public override string ToString()
72  {
73  return _converter.ToString();
74  }
75 
76  #region IEnumerable implementation
77 
78  public IEnumerator<Slice> GetEnumerator()
79  {
80  return _data.GetEnumerator();
81  }
82 
83  IEnumerator IEnumerable.GetEnumerator()
84  {
85  return GetEnumerator();
86  }
87 
88  #endregion
89  }
90 }