Lean  $LEAN_TAG$
Candlestick.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 Newtonsoft.Json;
18 using QuantConnect.Util;
20 
21 namespace QuantConnect
22 {
23  /// <summary>
24  /// Single candlestick for a candlestick chart
25  /// </summary>
26  [JsonConverter(typeof(CandlestickJsonConverter))]
27  public class Candlestick : ISeriesPoint
28  {
29  private bool _openSet;
30  private decimal? _open;
31  private decimal? _high;
32  private decimal? _low;
33  private decimal? _close;
34 
35  /// <summary>
36  /// The candlestick time
37  /// </summary>
38  public DateTime Time { get; set; }
39 
40  /// <summary>
41  /// The candlestick time in seconds since Unix Epoch
42  /// </summary>
43  public long LongTime
44  {
45  get
46  {
48  }
49  }
50 
51  /// <summary>
52  /// The candlestick open price
53  /// </summary>
54  public decimal? Open
55  {
56  get { return _open; }
57  set { _open = value.SmartRounding(); }
58  }
59 
60  /// <summary>
61  /// The candlestick high price
62  /// </summary>
63  public decimal? High
64  {
65  get { return _high; }
66  set { _high = value.SmartRounding(); }
67  }
68 
69  /// <summary>
70  /// The candlestick low price
71  /// </summary>
72  public decimal? Low
73  {
74  get { return _low; }
75  set { _low = value.SmartRounding(); }
76  }
77 
78  /// <summary>
79  /// The candlestick close price
80  /// </summary>
81  public decimal? Close
82  {
83  get { return _close; }
84  set { _close = value.SmartRounding(); }
85  }
86 
87  /// <summary>
88  /// Default constructor
89  /// </summary>
90  public Candlestick() { }
91 
92  /// <summary>
93  /// Constructor taking the candlestick values
94  /// </summary>
95  /// <param name="time">Candlestick time in seconds since Unix Epoch</param>
96  /// <param name="open">Candlestick open price</param>
97  /// <param name="high">Candlestick high price</param>
98  /// <param name="low">Candlestick low price</param>
99  /// <param name="close">Candlestick close price</param>
100  public Candlestick(long time, decimal? open, decimal? high, decimal? low, decimal? close)
101  : this(QuantConnect.Time.UnixTimeStampToDateTime(time), open, high, low, close)
102  {
103  }
104 
105  /// <summary>
106  /// Constructor taking candlestick values and time in DateTime format
107  /// </summary>
108  /// <param name="time">Candlestick time in seconds</param>
109  /// <param name="open">Candlestick open price</param>
110  /// <param name="high">Candlestick high price</param>
111  /// <param name="low">Candlestick low price</param>
112  /// <param name="close">Candlestick close price</param>
113  public Candlestick(DateTime time, decimal? open, decimal? high, decimal? low, decimal? close)
114  {
115  Time = time;
116  Open = open;
117  High = high;
118  Low = low;
119  Close = close;
120  }
121 
122  /// <summary>
123  /// Constructor taking candlestick values and time in DateTime format
124  /// </summary>
125  /// <param name="bar">Bar which data will be used to create the candlestick</param>
126  public Candlestick(TradeBar bar)
127  : this(bar.EndTime, bar.Open, bar.High, bar.Low, bar.Close)
128  {
129  }
130 
131  /// <summary>
132  /// Constructor taking candlestick values and time in DateTime format
133  /// </summary>
134  /// <param name="time">Candlestick time in seconds</param>
135  /// <param name="bar">Bar which data will be used to create the candlestick</param>
136  public Candlestick(DateTime time, Bar bar)
137  : this(time, bar.Open, bar.High, bar.Low, bar.Close)
138  {
139  }
140 
141  /// <summary>
142  /// Copy constructor
143  /// </summary>
144  /// <param name="candlestick">Candlestick to copy from</param>
145  public Candlestick(Candlestick candlestick)
146  : this(candlestick.Time, candlestick.Open, candlestick.High, candlestick.Low, candlestick.Close)
147  {
148  }
149 
150  /// <summary>
151  /// Provides a readable string representation of this instance.
152  /// </summary>
153  public override string ToString()
154  {
155  return Messages.Candlestick.ToString(this);
156  }
157 
158  /// <summary>
159  /// Clones this instance
160  /// </summary>
161  /// <returns>Clone of this instance</returns>
163  {
164  return new Candlestick(this);
165  }
166 
167  /// <summary>
168  /// Updates the candlestick with a new value. This will aggregate the OHLC bar
169  /// </summary>
170  /// <param name="value">The new value</param>
171  public void Update(decimal? value)
172  {
173  if (value.HasValue)
174  {
175  Update(value.Value);
176  }
177  }
178 
179  /// <summary>
180  /// Updates the candlestick with a new value. This will aggregate the OHLC bar
181  /// </summary>
182  /// <param name="value">The new value</param>
183  public void Update(decimal value)
184  {
185  if (!_openSet)
186  {
187  Open = High = Low = Close = value;
188  _openSet = true;
189  }
190  else if (value > High) High = value;
191  else if (value < Low) Low = value;
192  Close = value;
193  }
194  }
195 }