Lean  $LEAN_TAG$
InternalIndicatorValues.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.Linq;
17 using System.Reflection;
18 using QuantConnect.Data;
19 using System.Collections;
20 using System.Collections.Generic;
21 
23 {
24  /// <summary>
25  /// Collection of indicator data points for a given time
26  /// </summary>
28  {
29  /// <summary>
30  /// The indicator value at a given point
31  /// </summary>
33 
34  /// <summary>
35  /// The indicator value at a given point
36  /// </summary>
37  public override decimal Value => Current.Value;
38 
39  /// <summary>
40  /// Access the historical indicator values per indicator property name
41  /// </summary>
42  public IndicatorDataPoint this[string name]
43  {
44  get
45  {
46  return GetProperty(name) as IndicatorDataPoint;
47  }
48  }
49 
50  /// <summary>
51  /// String representation
52  /// </summary>
53  public override string ToString()
54  {
55  return $"{EndTime} {string.Join(", ", GetStorageDictionary().OrderBy(x => x.Key).Select(x => $"{x.Key}: {HandleObjectStorage(x.Value)}"))}";
56  }
57 
58  /// <summary>
59  /// Returns the current data value held within the instance
60  /// </summary>
61  /// <param name="instance">The DataPoint instance</param>
62  /// <returns>The current data value held within the instance</returns>
63  public static implicit operator decimal(IndicatorDataPoints instance)
64  {
65  return instance.Value;
66  }
67 
68  private static string HandleObjectStorage(object storedObject)
69  {
70  if (storedObject is IndicatorDataPoint point)
71  {
72  return point.Value.SmartRounding().ToStringInvariant();
73  }
74  return storedObject?.ToString() ?? string.Empty;
75  }
76  }
77 
78  /// <summary>
79  /// Internal carrier of an indicator values by property name
80  /// </summary>
81  public class InternalIndicatorValues : IEnumerable<IndicatorDataPoint>
82  {
83  /// <summary>
84  /// The name of the values associated to this dto
85  /// </summary>
86  public string Name { get; }
87 
88  /// <summary>
89  /// The indicator values
90  /// </summary>
91  public List<IndicatorDataPoint> Values { get; }
92 
93  /// <summary>
94  /// The target indicator
95  /// </summary>
96  protected IIndicator Indicator { get; }
97 
98  /// <summary>
99  /// Creates a new instance
100  /// </summary>
101  public InternalIndicatorValues(IIndicator indicator, string name)
102  {
103  Name = name;
104  Values = new();
105  Indicator = indicator;
106  }
107 
108  /// <summary>
109  /// Update with a new indicator point
110  /// </summary>
112  {
113  Values.Add(Indicator.Current);
114  return Indicator.Current;
115  }
116 
117  /// <summary>
118  /// Creates a new instance
119  /// </summary>
120  public static InternalIndicatorValues Create(IIndicator indicator, string name)
121  {
122  return new InternalIndicatorValues(indicator, name);
123  }
124 
125  /// <summary>
126  /// Creates a new instance
127  /// </summary>
128  public static InternalIndicatorValues Create(IIndicator indicator, PropertyInfo propertyInfo)
129  {
130  return new IndicatorPropertyValues(indicator, propertyInfo);
131  }
132 
133  /// <summary>
134  /// String representation
135  /// </summary>
136  public override string ToString()
137  {
138  return $"{Name} {Values.Count} indicator values";
139  }
140 
141  /// <summary>
142  /// Returns an enumerator for the indicator values
143  /// </summary>
144  public IEnumerator<IndicatorDataPoint> GetEnumerator()
145  {
146  return ((IEnumerable<IndicatorDataPoint>)Values).GetEnumerator();
147  }
148 
149  IEnumerator IEnumerable.GetEnumerator()
150  {
151  return ((IEnumerable)Values).GetEnumerator();
152  }
153 
154  private class IndicatorPropertyValues : InternalIndicatorValues
155  {
156  private readonly PropertyInfo _currentInfo;
157  private readonly PropertyInfo _propertyInfo;
158  public IndicatorPropertyValues(IIndicator indicator, PropertyInfo propertyInfo) : base(indicator, propertyInfo.Name)
159  {
160  _propertyInfo = propertyInfo;
161  _currentInfo = _propertyInfo.PropertyType.GetProperty("Current");
162  }
163  public override IndicatorDataPoint UpdateValue()
164  {
165  var value = _propertyInfo.GetValue(Indicator);
166  if (_currentInfo != null)
167  {
168  value = _currentInfo.GetValue(value);
169  }
170  var point = value as IndicatorDataPoint;
171  Values.Add(point);
172  return point;
173  }
174  }
175  }
176 }