Lean  $LEAN_TAG$
BaseSeries.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.Collections.Generic;
18 using System.Linq;
19 using Newtonsoft.Json;
20 using QuantConnect.Logging;
21 using QuantConnect.Util;
22 
23 namespace QuantConnect
24 {
25  /// <summary>
26  /// Chart Series Object - Series data and properties for a chart:
27  /// </summary>
28  [JsonConverter(typeof(SeriesJsonConverter))]
29  public abstract class BaseSeries
30  {
31  /// The index of the last fetch update request to only retrieve the "delta" of the previous request.
32  private int _updatePosition;
33 
34  /// <summary>
35  /// Name of the series.
36  /// </summary>
37  public string Name { get; set; }
38 
39  /// <summary>
40  /// Axis for the chart series.
41  /// </summary>
42  public string Unit { get; set; }
43 
44  /// <summary>
45  /// Index/position of the series on the chart.
46  /// </summary>
47  public int Index { get; set; }
48 
49  /// <summary>
50  /// Axis name for the chart series.
51  /// </summary>
52  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
53  public string IndexName { get; set; }
54 
55  /// <summary>
56  /// Defines the visual Z index of the series on the chart.
57  /// </summary>
58  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
59  public int? ZIndex { get; set; }
60 
61  /// <summary>
62  /// Chart type for the series:
63  /// </summary>
64  public SeriesType SeriesType { get; set; }
65 
66  /// <summary>
67  /// An optional tooltip template
68  /// </summary>
69  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
70  public string Tooltip { get; set; }
71 
72  /// <summary>
73  /// The series list of values.
74  /// These values are assumed to be in ascending time order (first points earliest, last points latest)
75  /// </summary>
76  public List<ISeriesPoint> Values { get; set; }
77 
78  /// <summary>
79  /// Default constructor for chart series
80  /// </summary>
81  protected BaseSeries()
82  {
83  Unit = "$";
84  Values = new List<ISeriesPoint>();
85  }
86 
87  /// <summary>
88  /// Constructor method for Chart Series
89  /// </summary>
90  /// <param name="name">Name of the chart series</param>
91  /// <param name="type">Type of the series</param>
92  protected BaseSeries(string name, SeriesType type)
93  : this()
94  {
95  Name = name;
96  SeriesType = type;
97  }
98 
99  /// <summary>
100  /// Foundational constructor on the series class
101  /// </summary>
102  /// <param name="name">Name of the series</param>
103  /// <param name="type">Type of the series</param>
104  /// <param name="index">Series index position on the chart</param>
105  protected BaseSeries(string name, SeriesType type, int index)
106  : this(name, type)
107  {
108  Index = index;
109  }
110 
111  /// <summary>
112  /// Foundational constructor on the series class
113  /// </summary>
114  /// <param name="name">Name of the series</param>
115  /// <param name="type">Type of the series</param>
116  /// <param name="index">Series index position on the chart</param>
117  /// <param name="unit">Unit for the series axis</param>
118  protected BaseSeries(string name, SeriesType type, int index, string unit)
119  : this(name, type, index)
120  {
121  Unit = unit;
122  }
123 
124  /// <summary>
125  /// Constructor method for Chart Series
126  /// </summary>
127  /// <param name="name">Name of the chart series</param>
128  /// <param name="type">Type of the chart series</param>
129  /// <param name="unit">Unit of the series</param>
130  protected BaseSeries(string name, SeriesType type, string unit)
131  : this(name, type, 0, unit)
132  {
133  }
134 
135  /// <summary>
136  /// Add a new point to this series
137  /// </summary>
138  /// <param name="point">The data point to add</param>
139  public virtual void AddPoint(ISeriesPoint point)
140  {
141  if (Values.Count > 0 && Values[Values.Count - 1].Time == point.Time)
142  {
143  // duplicate points at the same time, overwrite the value
144  Values[Values.Count - 1] = point;
145  }
146  else
147  {
148  Values.Add(point);
149  }
150  }
151 
152  /// <summary>
153  /// Add a new point to this series
154  /// </summary>
155  /// <param name="time">The time of the data point</param>
156  /// <param name="values">The values of the data point</param>
157  public abstract void AddPoint(DateTime time, List<decimal> values);
158 
159  /// <summary>
160  /// Get the updates since the last call to this function.
161  /// </summary>
162  /// <returns>List of the updates from the series</returns>
164  {
165  var copy = Clone(empty: true);
166 
167  try
168  {
169  //Add the updates since the last
170  for (var i = _updatePosition; i < Values.Count; i++)
171  {
172  copy.Values.Add(Values[i]);
173  }
174  //Shuffle the update point to now:
175  _updatePosition = Values.Count;
176  }
177  catch (Exception err)
178  {
179  Log.Error(err);
180  }
181  return copy;
182  }
183 
184  /// <summary>
185  /// Removes the data from this series and resets the update position to 0
186  /// </summary>
187  public void Purge()
188  {
189  Values.Clear();
190  _updatePosition = 0;
191  }
192 
193  /// <summary>
194  /// Will sum up all chart points into a new single value, using the time of latest point
195  /// </summary>
196  /// <returns>The new chart point</returns>
197  public abstract ISeriesPoint ConsolidateChartPoints();
198 
199  /// <summary>
200  /// Return a new instance clone of this object
201  /// </summary>
202  /// <returns></returns>
203  public abstract BaseSeries Clone(bool empty = false);
204 
205  /// <summary>
206  /// Return a list of cloned values
207  /// </summary>
208  /// <returns></returns>
209  protected List<ISeriesPoint> CloneValues()
210  {
211  var clone = new List<ISeriesPoint>(Values.Count);
212  foreach (var point in Values)
213  {
214  clone.Add(point.Clone());
215  }
216  return clone;
217  }
218 
219  /// <summary>
220  /// Returns an enumerable of the values of the series cast to the specified type
221  /// </summary>
222  /// <returns>An enumerable of the values of the series cast to the specified type</returns>
223  public IEnumerable<T> GetValues<T>()
224  where T : ISeriesPoint
225  {
226  return Values.Cast<T>();
227  }
228 
229  /// <summary>
230  /// Creates a series according to the specified type.
231  /// </summary>
232  /// <param name="seriesType">The series type</param>
233  /// <param name="name">The name of the series</param>
234  /// <param name="index">Series index position on the chart</param>
235  /// <param name="unit">Unit for the series axis</param>
236  /// <returns>
237  /// A <see cref="CandlestickSeries"/> if <paramref name="seriesType"/> is <see cref="SeriesType.Candle"/>.
238  /// A <see cref="Series"/> otherwise.
239  /// </returns>
240  public static BaseSeries Create(SeriesType seriesType, string name, int index = 0, string unit = "$")
241  {
242  if (!Enum.IsDefined(typeof(SeriesType), seriesType))
243  {
244  throw new ArgumentOutOfRangeException(nameof(seriesType), "Series type out of range");
245  }
246 
247  if (seriesType == SeriesType.Candle)
248  {
249  return new CandlestickSeries(name, index, unit);
250  }
251 
252  return new Series(name, seriesType, index, unit);
253  }
254  }
255 
256  /// <summary>
257  /// Available types of chart series
258  /// </summary>
259  public enum SeriesType
260  {
261  /// Line Plot for Value Types (0)
262  Line,
263  /// Scatter Plot for Chart Distinct Types (1)
264  Scatter,
265  /// Charts (2)
266  Candle,
267  /// Bar chart (3)
268  Bar,
269  /// Flag indicators (4)
270  Flag,
271  /// 100% area chart showing relative proportions of series values at each time index (5)
272  StackedArea,
273  /// Pie chart (6)
274  Pie,
275  /// Treemap Plot (7)
276  Treemap,
277  /// Heatmap Plot (9) -- NOTE: 8 is reserved
278  Heatmap = 9,
279  /// Scatter 3D Plot (10)
280  Scatter3d
281  }
282 }