Lean  $LEAN_TAG$
VolumeRenkoBar.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 volume
22  /// </summary>
24  {
25  /// <summary>
26  /// Gets whether or not this bar is considered closed.
27  /// </summary>
28  public override bool IsClosed => Volume >= BrickSize;
29 
30  /// <summary>
31  /// Initializes a new default instance of the <see cref="RenkoBar"/> class.
32  /// </summary>
33  public VolumeRenkoBar()
34  {
35  }
36 
37  /// <summary>
38  /// Initializes a new instance of the <see cref="VolumeRenkoBar"/> class with the specified values
39  /// </summary>
40  /// <param name="symbol">symbol of the data</param>
41  /// <param name="start">The current data start time</param>
42  /// <param name="endTime">The current data end time</param>
43  /// <param name="brickSize">The preset volume capacity of this bar</param>
44  /// <param name="open">The current data open value</param>
45  /// <param name="high">The current data high value</param>
46  /// <param name="low">The current data low value</param>
47  /// <param name="close">The current data close value</param>
48  /// <param name="volume">The current data volume</param>
49  public VolumeRenkoBar(Symbol symbol, DateTime start, DateTime endTime, decimal brickSize, decimal open, decimal high, decimal low, decimal close, decimal volume)
50  {
51  Type = RenkoType.Classic;
52  BrickSize = brickSize;
53 
54  Symbol = symbol;
55  Start = start;
56  EndTime = endTime;
57  Open = open;
58  Close = close;
59  Volume = volume;
60  High = high;
61  Low = low;
62  }
63 
64  /// <summary>
65  /// Updates this <see cref="VolumeRenkoBar"/> with the specified values and returns whether or not this bar is closed
66  /// </summary>
67  /// <param name="time">The current data end time</param>
68  /// <param name="high">The current data high value</param>
69  /// <param name="low">The current data low value</param>
70  /// <param name="close">The current data close value</param>
71  /// <param name="volume">The current data volume</param>
72  /// <returns>The excess volume that the current bar cannot absorb</returns>
73  public decimal Update(DateTime time, decimal high, decimal low, decimal close, decimal volume)
74  {
75  // can't update a closed renko bar
76  if (IsClosed) return 0m;
77  EndTime = time;
78 
79  var excessVolume = Volume + volume - BrickSize;
80  if (excessVolume > 0)
81  {
82  Volume = BrickSize;
83  }
84  else
85  {
86  Volume += volume;
87  }
88 
89  Close = close;
90  if (high > High) High = high;
91  if (low < Low) Low = low;
92 
93  return excessVolume;
94  }
95 
96  /// <summary>
97  /// Create a new <see cref="VolumeRenkoBar"/> with previous information rollover
98  /// </summary>
100  {
101  return new VolumeRenkoBar
102  {
103  Type = Type,
105  Symbol = Symbol,
106  Open = Close, // rollover open is the previous close
107  High = Close,
108  Low = Close,
109  Close = Close,
110  Start = EndTime, // rollover start time is the previous end time
111  EndTime = EndTime,
112  Volume = 0m
113  };
114  }
115  }
116 }