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, 0, unit)
76  {
77  SeriesType = SeriesType.Candle;
78  }
79 
80  /// <summary>
81  /// Add a new point to this series
82  /// </summary>
83  /// <param name="time">Time of the chart point</param>
84  /// <param name="open">Candlestick open price</param>
85  /// <param name="high">Candlestick high price</param>
86  /// <param name="low">Candlestick low price</param>
87  /// <param name="close">Candlestick close price</param>
88  public void AddPoint(DateTime time, decimal open, decimal high, decimal low, decimal close)
89  {
90  base.AddPoint(new Candlestick(time, open, high, low, close));
91  }
92 
93  /// <summary>
94  /// Add a new point to this series
95  /// </summary>
96  public void AddPoint(TradeBar bar)
97  {
98  base.AddPoint(new Candlestick(bar));
99  }
100 
101  /// <summary>
102  /// Add a new point to this series
103  /// </summary>
104  /// <param name="point">The data point to add</param>
105  public override void AddPoint(ISeriesPoint point)
106  {
107  if (point as Candlestick == null)
108  {
109  throw new ArgumentException("CandlestickSeries.AddPoint requires a Candlestick object");
110  }
111 
112  base.AddPoint(point);
113  }
114 
115  /// <summary>
116  /// Add a new point to this series
117  /// </summary>
118  /// <param name="time">The time of the data point</param>
119  /// <param name="values">The values of the data point</param>
120  public override void AddPoint(DateTime time, List<decimal> values)
121  {
122  if (values.Count != 4)
123  {
124  throw new ArgumentException("CandlestickSeries.AddPoint requires 4 values (open, high, low, close)");
125  }
126 
127  base.AddPoint(new Candlestick(time, values[0], values[1], values[2], values[3]));
128  }
129 
130  /// <summary>
131  /// Will sum up all candlesticks into a new single one, using the time of latest point
132  /// </summary>
133  /// <returns>The new candlestick</returns>
135  {
136  if (Values.Count <= 0) return null;
137 
138  decimal? openSum = null;
139  decimal? highSum = null;
140  decimal? lowSum = null;
141  decimal? closeSum = null;
142  foreach (Candlestick point in Values)
143  {
144  if (point.Open.HasValue)
145  {
146  openSum ??= 0;
147  openSum += point.Open.Value;
148  }
149  if (point.High.HasValue)
150  {
151  highSum ??= 0;
152  highSum += point.High.Value;
153  }
154  if (point.Low.HasValue)
155  {
156  lowSum ??= 0;
157  lowSum += point.Low.Value;
158  }
159  if (point.Close.HasValue)
160  {
161  closeSum ??= 0;
162  closeSum += point.Close.Value;
163  }
164  }
165 
166  var lastCandlestick = Values[Values.Count - 1];
167  return new Candlestick(lastCandlestick.Time, openSum, highSum, lowSum, closeSum);
168  }
169 
170  /// <summary>
171  /// Return a new instance clone of this object
172  /// </summary>
173  /// <returns></returns>
174  public override BaseSeries Clone(bool empty = false)
175  {
176  var series = new CandlestickSeries(Name, Index, Unit) { ZIndex = ZIndex, IndexName = IndexName, Tooltip = Tooltip };
177 
178  if (!empty)
179  {
180  series.Values = CloneValues();
181  }
182 
183  return series;
184  }
185  }
186 }