Lean  $LEAN_TAG$
Series.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.Drawing;
19 using System.Linq;
20 using System.Runtime.Serialization;
21 using Newtonsoft.Json;
22 using Newtonsoft.Json.Converters;
23 using QuantConnect.Util;
24 
25 namespace QuantConnect
26 {
27  /// <summary>
28  /// Chart Series Object - Series data and properties for a chart:
29  /// </summary>
30  [JsonConverter(typeof(SeriesJsonConverter))]
31  public class Series : BaseSeries
32  {
33  /// <summary>
34  /// Color the series
35  /// </summary>
36  [JsonConverter(typeof(ColorJsonConverter))]
37  public Color Color { get; set; } = Color.Empty;
38 
39  /// <summary>
40  /// Shape or symbol for the marker in a scatter plot
41  /// </summary>
43 
44  /// <summary>
45  /// Default constructor for chart series
46  /// </summary>
47  public Series() : base() { }
48 
49  /// <summary>
50  /// Constructor method for Chart Series
51  /// </summary>
52  /// <param name="name">Name of the chart series</param>
53  public Series(string name)
54  : base(name, SeriesType.Line)
55  {
56  }
57 
58  /// <summary>
59  /// Foundational constructor on the series class
60  /// </summary>
61  /// <param name="name">Name of the series</param>
62  /// <param name="type">Type of the series</param>
63  /// <param name="index">Index position on the chart of the series</param>
64  public Series(string name, SeriesType type, int index)
65  : this(name, type, index, "$")
66  {
67  }
68 
69  /// <summary>
70  /// Foundational constructor on the series class
71  /// </summary>
72  /// <param name="name">Name of the series</param>
73  /// <param name="type">Type of the series</param>
74  /// <param name="index">Index position on the chart of the series</param>
75  /// <param name="unit">Unit for the series axis</param>
76  public Series(string name, SeriesType type, int index, string unit)
77  : this(name, type, unit, Color.Empty, ScatterMarkerSymbol.None)
78  {
79  Index = index;
80  }
81 
82  /// <summary>
83  /// Constructor method for Chart Series
84  /// </summary>
85  /// <param name="name">Name of the chart series</param>
86  /// <param name="type">Type of the chart series</param>
87  /// <param name="unit">Unit of the series</param>
88  public Series(string name, SeriesType type = SeriesType.Line, string unit = "$")
89  : this(name, type, unit, Color.Empty)
90  {
91  }
92 
93  /// <summary>
94  /// Constructor method for Chart Series
95  /// </summary>
96  /// <param name="name">Name of the chart series</param>
97  /// <param name="type">Type of the chart series</param>
98  /// <param name="unit">Unit of the series</param>
99  /// <param name="color">Color of the series</param>
100  public Series(string name, SeriesType type, string unit, Color color)
101  : this(name, type, unit, color, ScatterMarkerSymbol.None)
102  {
103  }
104 
105  /// <summary>
106  /// Constructor method for Chart Series
107  /// </summary>
108  /// <param name="name">Name of the chart series</param>
109  /// <param name="type">Type of the chart series</param>
110  /// <param name="unit">Unit of the series</param>
111  /// <param name="color">Color of the series</param>
112  /// <param name="symbol">Symbol for the marker in a scatter plot series</param>
113  public Series(string name, SeriesType type, string unit, Color color, ScatterMarkerSymbol symbol = ScatterMarkerSymbol.None)
114  : base(name, type, 0, unit)
115  {
116  Color = color;
117  ScatterMarkerSymbol = symbol;
118  }
119 
120  /// <summary>
121  /// Add a new point to this series
122  /// </summary>
123  /// <param name="time">Time of the chart point</param>
124  /// <param name="value">Value of the chart point</param>
125  public void AddPoint(DateTime time, decimal value)
126  {
127  ISeriesPoint point;
128  if (SeriesType == SeriesType.Scatter)
129  {
130  point = new ScatterChartPoint(time, value);
131  }
132  else
133  {
134  point = new ChartPoint(time, value);
135  }
136  AddPoint(point);
137  }
138 
139  /// <summary>
140  /// Add a new point to this series
141  /// </summary>
142  /// <param name="point">The data point to add</param>
143  public override void AddPoint(ISeriesPoint point)
144  {
145  if (point as ChartPoint == null)
146  {
147  throw new ArgumentException("Series.AddPoint requires a ChartPoint object");
148  }
149 
150  base.AddPoint(point);
151  }
152 
153  /// <summary>
154  /// Add a new point to this series
155  /// </summary>
156  /// <param name="time">The time of the data point</param>
157  /// <param name="values">The values of the data point</param>
158  public override void AddPoint(DateTime time, List<decimal> values)
159  {
160  if (values.Count > 1)
161  {
162  throw new ArgumentException("Series.AddPoint requires a single value");
163  }
164 
165  AddPoint(time, values.Count > 0 ? values[0] : 0);
166  }
167 
168  /// <summary>
169  /// Will sum up all chart points into a new single value, using the time of latest point
170  /// </summary>
171  /// <returns>The new chart point</returns>
173  {
174  if (Values.Count <= 0) return null;
175 
176  var sum = 0m;
177  foreach (ChartPoint point in Values)
178  {
179  if(point.y.HasValue)
180  {
181  sum += point.y.Value;
182  }
183  }
184 
185  var lastPoint = (ChartPoint)Values.Last();
186  return new ChartPoint(lastPoint.x, sum);
187  }
188 
189  /// <summary>
190  /// Return a new instance clone of this object
191  /// </summary>
192  /// <returns></returns>
193  public override BaseSeries Clone(bool empty = false)
194  {
195  var series = new Series(Name, SeriesType, Index, Unit)
196  {
197  Color = Color,
198  ZIndex = ZIndex,
199  Tooltip = Tooltip,
202  };
203 
204  if (!empty)
205  {
206  series.Values = CloneValues();
207  }
208 
209  return series;
210  }
211  }
212 
213  /// <summary>
214  /// Shape or symbol for the marker in a scatter plot
215  /// </summary>
216  [JsonConverter(typeof(StringEnumConverter))]
218  {
219  /// Circle symbol (0)
220  [EnumMember(Value = "none")]
221  None,
222  /// Circle symbol (1)
223  [EnumMember(Value = "circle")]
224  Circle,
225  /// Square symbol (2)
226  [EnumMember(Value = "square")]
227  Square,
228  /// Diamond symbol (3)
229  [EnumMember(Value = "diamond")]
230  Diamond,
231  /// Triangle symbol (4)
232  [EnumMember(Value = "triangle")]
233  Triangle,
234  /// Triangle-down symbol (5)
235  [EnumMember(Value = "triangle-down")]
237  }
238 }