Lean  $LEAN_TAG$
DataHistory.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.Text;
18 using System.Linq;
19 using Python.Runtime;
20 using QuantConnect.Util;
21 using System.Collections;
22 using System.Collections.Generic;
23 
24 namespace QuantConnect.Data
25 {
26  /// <summary>
27  /// Historical data abstraction
28  /// </summary>
29  /// <typeparam name="T">The data this collection can enumerate</typeparam>
30  public class DataHistory<T> : IEnumerable<T>
31  {
32  private readonly Lazy<int> _count;
33  private readonly Lazy<PyObject> _dataframe;
34 
35  /// <summary>
36  /// The data we hold
37  /// </summary>
38  protected IEnumerable<T> Data { get; }
39 
40  /// <summary>
41  /// The current data point count
42  /// </summary>
43  public int Count => _count.Value;
44 
45  /// <summary>
46  /// This data pandas data frame
47  /// </summary>
48  public PyObject DataFrame => _dataframe.Value;
49 
50  /// <summary>
51  /// Creates a new instance
52  /// </summary>
53  public DataHistory(IEnumerable<T> data, Lazy<PyObject> dataframe)
54  {
55  Data = data.Memoize();
56  _dataframe = dataframe;
57  // let's be lazy
58  _count = new(() => Data.Count());
59  }
60 
61  /// <summary>
62  /// Default to string implementation
63  /// </summary>
64  public override string ToString()
65  {
66  var builder = new StringBuilder();
67  foreach (var dataPoint in Data)
68  {
69  builder.AppendLine(dataPoint.ToString());
70  }
71  return builder.ToString();
72  }
73 
74  /// <summary>
75  /// Returns an enumerator for the data
76  /// </summary>
77  public IEnumerator<T> GetEnumerator()
78  {
79  return Data.GetEnumerator();
80  }
81 
82  IEnumerator IEnumerable.GetEnumerator()
83  {
84  return GetEnumerator();
85  }
86  }
87 }