Lean  $LEAN_TAG$
CandlestickSeries.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.Collections.Generic;
17 using System;
19 using Newtonsoft.Json;
20 using QuantConnect.Util;
21 
22 namespace QuantConnect
23 {
24  /// <summary>
25  /// Candlestick Chart Series Object - Series data and properties for a candlestick chart
26  /// </summary>
27  [JsonConverter(typeof(SeriesJsonConverter))]
29  {
30  /// <summary>
31  /// Default constructor for chart series
32  /// </summary>
33  public CandlestickSeries() : base()
34  {
35  SeriesType = SeriesType.Candle;
36  }
37 
38  /// <summary>
39  /// Constructor method for Chart Series
40  /// </summary>
41  /// <param name="name">Name of the chart series</param>
42  public CandlestickSeries(string name)
43  : base(name, SeriesType.Candle)
44  {
45  }
46 
47  /// <summary>
48  /// Foundational constructor on the series class
49  /// </summary>
50  /// <param name="name">Name of the series</param>
51  /// <param name="index">Index position on the chart of the series</param>
52  public CandlestickSeries(string name, int index)
53  : this(name, index, "$")
54  {
55  }
56 
57  /// <summary>
58  /// Foundational constructor on the series class
59  /// </summary>
60  /// <param name="name">Name of the series</param>
61  /// <param name="index">Index position on the chart of the series</param>
62  /// <param name="unit">Unit for the series axis</param>
63  public CandlestickSeries(string name, int index, string unit)
64  : this(name, unit)
65  {
66  Index = index;
67  }
68 
69  /// <summary>
70  /// Constructor method for Chart Series
71  /// </summary>
72  /// <param name="name">Name of the chart series</param>
73  /// <param name="unit">Unit of the series</param>
74  public CandlestickSeries(string name, string unit)
75  : base(name, SeriesType.Candle, unit)
76  {
77  }
78 
79  /// <summary>
80  /// Add a new point to this series
81  /// </summary>
82  /// <param name="time">Time of the chart point</param>
83  /// <param name="open">Candlestick open price</param>
84  /// <param name="high">Candlestick high price</param>
85  /// <param name="low">Candlestick low price</param>
86  /// <param name="close">Candlestick close price</param>
87  public void AddPoint(DateTime time, decimal open, decimal high, decimal low, decimal close)
88  {
89  base.AddPoint(new Candlestick(time, open, high, low, close));
90  }
91 
92  /// <summary>
93  /// Add a new point to this series
94  /// </summary>
95  public void AddPoint(TradeBar bar)
96  {
97  base.AddPoint(new Candlestick(bar));
98  }
99 
100  /// <summary>
101  /// Add a new point to this series
102  /// </summary>
103  /// <param name="point">The data point to add</param>
104  public override void AddPoint(ISeriesPoint point)
105  {
106  if (point as Candlestick == null)
107  {
108  throw new ArgumentException("CandlestickSeries.AddPoint requires a Candlestick object");
109  }
110 
111  base.AddPoint(point);
112  }
113 
114  /// <summary>
115  /// Add a new point to this series
116  /// </summary>
117  /// <param name="time">The time of the data point</param>
118  /// <param name="values">The values of the data point</param>
119  public override void AddPoint(DateTime time, List<decimal> values)
120  {
121  if (values.Count != 4)
122  {
123  throw new ArgumentException("CandlestickSeries.AddPoint requires 4 values (open, high, low, close)");
124  }
125 
126  base.AddPoint(new Candlestick(time, values[0], values[1], values[2], values[3]));
127  }
128 
129  /// <summary>
130  /// Will sum up all candlesticks into a new single one, using the time of latest point
131  /// </summary>
132  /// <returns>The new candlestick</returns>
134  {
135  if (Values.Count <= 0) return null;
136 
137  decimal? openSum = null;
138  decimal? highSum = null;
139  decimal? lowSum = null;
140  decimal? closeSum = null;
141  foreach (Candlestick point in Values)
142  {
143  if (point.Open.HasValue)
144  {
145  openSum ??= 0;
146  openSum += point.Open.Value;
147  }
148  if (point.High.HasValue)
149  {
150  highSum ??= 0;
151  highSum += point.High.Value;
152  }
153  if (point.Low.HasValue)
154  {
155  lowSum ??= 0;
156  lowSum += point.Low.Value;
157  }
158  if (point.Close.HasValue)
159  {
160  closeSum ??= 0;
161  closeSum += point.Close.Value;
162  }
163  }
164 
165  var lastCandlestick = Values[Values.Count - 1];
166  return new Candlestick(lastCandlestick.Time, openSum, highSum, lowSum, closeSum);
167  }
168 
169  /// <summary>
170  /// Return a new instance clone of this object
171  /// </summary>
172  /// <returns></returns>
173  public override BaseSeries Clone(bool empty = false)
174  {
175  var series = new CandlestickSeries(Name, Index, Unit) { ZIndex = ZIndex, IndexName = IndexName, Tooltip = Tooltip };
176 
177  if (!empty)
178  {
179  series.Values = CloneValues();
180  }
181 
182  return series;
183  }
184  }
185 }