forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCandlestick.cs
195 lines (177 loc) · 6.17 KB
/
Candlestick.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using Newtonsoft.Json;
using QuantConnect.Util;
using QuantConnect.Data.Market;
namespace QuantConnect
{
/// <summary>
/// Single candlestick for a candlestick chart
/// </summary>
[JsonConverter(typeof(CandlestickJsonConverter))]
public class Candlestick : ISeriesPoint
{
private bool _openSet;
private decimal? _open;
private decimal? _high;
private decimal? _low;
private decimal? _close;
/// <summary>
/// The candlestick time
/// </summary>
public DateTime Time { get; set; }
/// <summary>
/// The candlestick time in seconds since Unix Epoch
/// </summary>
public long LongTime
{
get
{
return (long)QuantConnect.Time.DateTimeToUnixTimeStamp(Time);
}
}
/// <summary>
/// The candlestick open price
/// </summary>
public decimal? Open
{
get { return _open; }
set { _open = value.SmartRounding(); }
}
/// <summary>
/// The candlestick high price
/// </summary>
public decimal? High
{
get { return _high; }
set { _high = value.SmartRounding(); }
}
/// <summary>
/// The candlestick low price
/// </summary>
public decimal? Low
{
get { return _low; }
set { _low = value.SmartRounding(); }
}
/// <summary>
/// The candlestick close price
/// </summary>
public decimal? Close
{
get { return _close; }
set { _close = value.SmartRounding(); }
}
/// <summary>
/// Default constructor
/// </summary>
public Candlestick() { }
/// <summary>
/// Constructor taking the candlestick values
/// </summary>
/// <param name="time">Candlestick time in seconds since Unix Epoch</param>
/// <param name="open">Candlestick open price</param>
/// <param name="high">Candlestick high price</param>
/// <param name="low">Candlestick low price</param>
/// <param name="close">Candlestick close price</param>
public Candlestick(long time, decimal? open, decimal? high, decimal? low, decimal? close)
: this(QuantConnect.Time.UnixTimeStampToDateTime(time), open, high, low, close)
{
}
/// <summary>
/// Constructor taking candlestick values and time in DateTime format
/// </summary>
/// <param name="time">Candlestick time in seconds</param>
/// <param name="open">Candlestick open price</param>
/// <param name="high">Candlestick high price</param>
/// <param name="low">Candlestick low price</param>
/// <param name="close">Candlestick close price</param>
public Candlestick(DateTime time, decimal? open, decimal? high, decimal? low, decimal? close)
{
Time = time;
Open = open;
High = high;
Low = low;
Close = close;
}
/// <summary>
/// Constructor taking candlestick values and time in DateTime format
/// </summary>
/// <param name="bar">Bar which data will be used to create the candlestick</param>
public Candlestick(TradeBar bar)
: this(bar.EndTime, bar.Open, bar.High, bar.Low, bar.Close)
{
}
/// <summary>
/// Constructor taking candlestick values and time in DateTime format
/// </summary>
/// <param name="time">Candlestick time in seconds</param>
/// <param name="bar">Bar which data will be used to create the candlestick</param>
public Candlestick(DateTime time, Bar bar)
: this(time, bar.Open, bar.High, bar.Low, bar.Close)
{
}
/// <summary>
/// Copy constructor
/// </summary>
/// <param name="candlestick">Candlestick to copy from</param>
public Candlestick(Candlestick candlestick)
: this(candlestick.Time, candlestick.Open, candlestick.High, candlestick.Low, candlestick.Close)
{
}
/// <summary>
/// Provides a readable string representation of this instance.
/// </summary>
public override string ToString()
{
return Messages.Candlestick.ToString(this);
}
/// <summary>
/// Clones this instance
/// </summary>
/// <returns>Clone of this instance</returns>
public ISeriesPoint Clone()
{
return new Candlestick(this);
}
/// <summary>
/// Updates the candlestick with a new value. This will aggregate the OHLC bar
/// </summary>
/// <param name="value">The new value</param>
public void Update(decimal? value)
{
if (value.HasValue)
{
Update(value.Value);
}
}
/// <summary>
/// Updates the candlestick with a new value. This will aggregate the OHLC bar
/// </summary>
/// <param name="value">The new value</param>
public void Update(decimal value)
{
if (!_openSet)
{
Open = High = Low = Close = value;
_openSet = true;
}
else if (value > High) High = value;
else if (value < Low) Low = value;
Close = value;
}
}
}