Lean  $LEAN_TAG$
RenkoBar.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 
19 {
20  /// <summary>
21  /// Represents a bar sectioned not by time, but by some amount of movement in a value (for example, Closing price moving in $10 bar sizes)
22  /// </summary>
23  public class RenkoBar : BaseRenkoBar
24  {
25  /// <summary>
26  /// Gets the end time of this renko bar or the most recent update time if it <see cref="BaseRenkoBar.IsClosed"/>
27  /// </summary>
28  [Obsolete("RenkoBar.End is obsolete. Please use RenkoBar.EndTime property instead.")]
29  public DateTime End
30  {
31  get { return EndTime; }
32  set { EndTime = value; }
33  }
34 
35  /// <summary>
36  /// The trend of the bar (i.e. Rising, Falling or NoDelta)
37  /// </summary>
38  public BarDirection Direction
39  {
40  get
41  {
42  if (Open < Close)
43  return BarDirection.Rising;
44  else if (Open > Close)
45  return BarDirection.Falling;
46  else
47  return BarDirection.NoDelta;
48  }
49  }
50 
51  /// <summary>
52  /// The "spread" of the bar
53  /// </summary>
54  public decimal Spread
55  {
56  get
57  {
58  return Math.Abs(Close - Open);
59  }
60  }
61 
62  /// <summary>
63  /// Initializes a new default instance of the <see cref="RenkoBar"/> class.
64  /// </summary>
65  public RenkoBar()
66  {
67  }
68 
69  /// <summary>
70  /// Initializes a new instance of the <see cref="RenkoBar"/> class with the specified values
71  /// </summary>
72  /// <param name="symbol">The symbol of this data</param>
73  /// <param name="time">The start time of the bar</param>
74  /// <param name="brickSize">The size of each renko brick</param>
75  /// <param name="open">The opening price for the new bar</param>
76  /// <param name="volume">Any initial volume associated with the data</param>
77  public RenkoBar(Symbol symbol, DateTime time, decimal brickSize,
78  decimal open, decimal volume)
79  {
80  Type = RenkoType.Classic;
81 
82  Symbol = symbol;
83  Start = time;
84  EndTime = time;
85  BrickSize = brickSize;
86  Open = open;
87  Close = open;
88  Volume = volume;
89  High = open;
90  Low = open;
91  }
92 
93  /// <summary>
94  /// Initializes a new instance of the <see cref="RenkoBar"/> class with the specified values
95  /// </summary>
96  /// <param name="symbol">The symbol of this data</param>
97  /// <param name="start">The start time of the bar</param>
98  /// <param name="endTime">The end time of the bar</param>
99  /// <param name="brickSize">The size of each wicko brick</param>
100  /// <param name="open">The opening price for the new bar</param>
101  /// <param name="high">The high price for the new bar</param>
102  /// <param name="low">The low price for the new bar</param>
103  /// <param name="close">The closing price for the new bar</param>
104  public RenkoBar(Symbol symbol, DateTime start, DateTime endTime,
105  decimal brickSize, decimal open, decimal high, decimal low, decimal close)
106  {
107  Type = RenkoType.Wicked;
108 
109  Symbol = symbol;
110  Start = start;
111  EndTime = endTime;
112  BrickSize = brickSize;
113  Open = open;
114  Close = close;
115  Volume = 0;
116  High = high;
117  Low = low;
118  }
119 
120  /// <summary>
121  /// Updates this <see cref="RenkoBar"/> with the specified values and returns whether or not this bar is closed
122  /// </summary>
123  /// <param name="time">The current time</param>
124  /// <param name="currentValue">The current value</param>
125  /// <param name="volumeSinceLastUpdate">The volume since the last update called on this instance</param>
126  /// <returns>True if this bar <see cref="BaseRenkoBar.IsClosed"/></returns>
127  public bool Update(DateTime time, decimal currentValue, decimal volumeSinceLastUpdate)
128  {
129  if (Type == RenkoType.Wicked)
130  throw new InvalidOperationException("A \"Wicked\" RenkoBar cannot be updated!");
131 
132  // can't update a closed renko bar
133  if (IsClosed) return true;
134  if (Start == DateTime.MinValue) Start = time;
135  EndTime = time;
136 
137  // compute the min/max closes this renko bar can have
138  decimal lowClose = Open - BrickSize;
139  decimal highClose = Open + BrickSize;
140 
141  Close = Math.Min(highClose, Math.Max(lowClose, currentValue));
142  Volume += volumeSinceLastUpdate;
143 
144  // determine if this data caused the bar to close
145  if (currentValue <= lowClose || currentValue >= highClose)
146  {
147  IsClosed = true;
148  }
149 
150  if (Close > High) High = Close;
151  if (Close < Low) Low = Close;
152 
153  return IsClosed;
154  }
155 
156  /// <summary>
157  /// Return a new instance clone of this object, used in fill forward
158  /// </summary>
159  /// <remarks>
160  /// This base implementation uses reflection to copy all public fields and properties
161  /// </remarks>
162  /// <returns>A clone of the current object</returns>
163  public override BaseData Clone()
164  {
165  return new RenkoBar
166  {
167  Type = Type,
169  Open = Open,
170  Volume = Volume,
171  Close = Close,
172  EndTime = EndTime,
173  High = High,
174  IsClosed = IsClosed,
175  Low = Low,
176  Time = Time,
177  Value = Value,
178  Symbol = Symbol,
180  };
181  }
182  }
183 }