Lean  $LEAN_TAG$
RangeBar.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 RangeBar: TradeBar
24  {
25  /// <summary>
26  /// Gets the range of the bar.
27  /// </summary>
28  public decimal RangeSize { get; private set; }
29 
30  /// <summary>
31  /// Gets whether or not this bar is considered closed.
32  /// </summary>
33  public bool IsClosed { get; private set; }
34 
35  /// <summary>
36  /// Initialize a new default instance of <see cref="RangeBar"/> class.
37  /// </summary>
38  public RangeBar()
39  {
40  }
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="RangeBar"/> class with the specified values
44  /// </summary>
45  /// <param name="symbol">The symbol of this data</param>
46  /// <param name="endTime">The end time of the bar</param>
47  /// <param name="rangeSize">The size of each range bar</param>
48  /// <param name="open">The opening price for the new bar</param>
49  /// <param name="high">The high price for the new bar</param>
50  /// <param name="low">The low price for the new bar</param>
51  /// <param name="close">The closing price for the new bar</param>
52  /// <param name="volume">The volume value for the new bar</param>
53  public RangeBar(Symbol symbol, DateTime endTime,
54  decimal rangeSize, decimal open, decimal? high = null, decimal? low = null, decimal? close = null, decimal volume = 0)
55  {
56  Symbol = symbol;
57  EndTime = endTime;
58  RangeSize = rangeSize;
59  Open = open;
60  Close = close ?? open;
61  Volume = volume;
62  High = high ?? open;
63  Low = low ?? open;
64  }
65 
66  /// <summary>
67  /// Updates this <see cref="RangeBar"/> with the specified values
68  /// </summary>
69  /// <param name="time">The current time</param>
70  /// <param name="currentValue">The current value</param>
71  /// <param name="volumeSinceLastUpdate">The volume since the last update called on this instance</param>
72  public void Update(DateTime time, decimal currentValue, decimal volumeSinceLastUpdate)
73  {
74  EndTime = time;
75 
76  if (currentValue < Low)
77  {
78  if ((High - currentValue) > RangeSize)
79  {
80  IsClosed = true;
81  Low = High - RangeSize;
82  Close = Low;
83  return;
84  }
85  else
86  {
87  Low = currentValue;
88  }
89  }
90  else if (currentValue > High)
91  {
92  if ((currentValue - Low) > RangeSize)
93  {
94  IsClosed = true;
95  High = Low + RangeSize;
96  Close = High;
97  return;
98  }
99  else
100  {
101  High = currentValue;
102  }
103  }
104 
105  Volume += volumeSinceLastUpdate;
106  }
107 
108  /// <summary>
109  /// Return a new instance clone of this object, used in fill forward
110  /// </summary>
111  /// <remarks>
112  /// This base implementation uses reflection to copy all public fields and properties
113  /// </remarks>
114  /// <returns>A clone of the current object</returns>
115  public override BaseData Clone()
116  {
117  return new RangeBar
118  {
120  Open = Open,
121  Volume = Volume,
122  Close = Close,
123  EndTime = EndTime,
124  High = High,
125  IsClosed = IsClosed,
126  Low = Low,
127  Time = Time,
128  Value = Value,
129  Symbol = Symbol,
131  };
132  }
133  }
134 }