Lean  $LEAN_TAG$
IndicatorHistory.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.Linq;
18 using Python.Runtime;
20 using System.Collections.Generic;
21 
22 namespace QuantConnect.Data
23 {
24  /// <summary>
25  /// Provides historical values of an indicator
26  /// </summary>
27  public class IndicatorHistory : DataHistory<IndicatorDataPoints>
28  {
29  private readonly Dictionary<string, List<IndicatorDataPoint>> _pointsPerName;
30 
31  /// <summary>
32  /// The indicators historical values
33  /// </summary>
34  public List<IndicatorDataPoint> Current => this["current"];
35 
36  /// <summary>
37  /// Creates a new instance
38  /// </summary>
39  /// <param name="indicatorsDataPointsByTime">Indicators data points by time</param>
40  /// <param name="indicatorsDataPointPerProperty">Indicators data points by property name</param>
41  /// <param name="dataframe">The lazy data frame constructor</param>
42  public IndicatorHistory(List<IndicatorDataPoints> indicatorsDataPointsByTime, List<InternalIndicatorValues> indicatorsDataPointPerProperty, Lazy<PyObject> dataframe)
43  : base(indicatorsDataPointsByTime, dataframe)
44  {
45  // for the index accessor we enforce uniqueness by name
46  _pointsPerName = indicatorsDataPointPerProperty.DistinctBy(x => x.Name.ToLowerInvariant()).ToDictionary(x => x.Name.ToSnakeCase(), x => x.Values);
47  }
48 
49  /// <summary>
50  /// Access the historical indicator values per indicator property name
51  /// </summary>
52  public List<IndicatorDataPoint> this[string name]
53  {
54  get
55  {
56  if (_pointsPerName.TryGetValue(name.ToSnakeCase().ToLowerInvariant(), out var result))
57  {
58  return result;
59  }
60  return null;
61  }
62  }
63  }
64 }