Lean  $LEAN_TAG$
Bar.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.Runtime.CompilerServices;
17 using ProtoBuf;
18 
20 {
21  /// <summary>
22  /// Base Bar Class: Open, High, Low, Close and Period.
23  /// </summary>
24  [ProtoContract(SkipConstructor = true)]
25  public class Bar : IBar
26  {
27  private bool _openSet;
28 
29  /// <summary>
30  /// Opening price of the bar: Defined as the price at the start of the time period.
31  /// </summary>
32  [ProtoMember(1)]
33  public virtual decimal Open { get; set; }
34 
35  /// <summary>
36  /// High price of the bar during the time period.
37  /// </summary>
38  [ProtoMember(2)]
39  public virtual decimal High { get; set; }
40 
41  /// <summary>
42  /// Low price of the bar during the time period.
43  /// </summary>
44  [ProtoMember(3)]
45  public virtual decimal Low { get; set; }
46 
47  /// <summary>
48  /// Closing price of the bar. Defined as the price at Start Time + TimeSpan.
49  /// </summary>
50  [ProtoMember(4)]
51  public virtual decimal Close { get; set; }
52 
53  /// <summary>
54  /// Default initializer to setup an empty bar.
55  /// </summary>
56  public Bar()
57  {
58  }
59 
60  /// <summary>
61  /// Initializer to setup a bar with a given information.
62  /// </summary>
63  /// <param name="open">Decimal Opening Price</param>
64  /// <param name="high">Decimal High Price of this bar</param>
65  /// <param name="low">Decimal Low Price of this bar</param>
66  /// <param name="close">Decimal Close price of this bar</param>
67  public Bar(decimal open, decimal high, decimal low, decimal close)
68  {
69  _openSet = open != 0;
70  Open = open;
71  High = high;
72  Low = low;
73  Close = close;
74  }
75 
76  /// <summary>
77  /// Updates the bar with a new value. This will aggregate the OHLC bar
78  /// </summary>
79  /// <param name="value">The new value</param>
80  [MethodImpl(MethodImplOptions.AggressiveInlining)]
81  public void Update(decimal value)
82  {
83  Update(ref value);
84  }
85 
86  /// <summary>
87  /// Updates the bar with a new value. This will aggregate the OHLC bar
88  /// </summary>
89  /// <param name="value">The new value</param>
90  [MethodImpl(MethodImplOptions.AggressiveInlining)]
91  public void Update(ref decimal value)
92  {
93  // Do not accept zero as a new value
94  if (value == 0) return;
95 
96  if (!_openSet)
97  {
98  Open = High = Low = Close = value;
99  _openSet = true;
100  }
101  else if (value > High) High = value;
102  else if (value < Low) Low = value;
103  Close = value;
104  }
105 
106  /// <summary>
107  /// Returns a clone of this bar
108  /// </summary>
109  public Bar Clone()
110  {
111  return new Bar(Open, High, Low, Close);
112  }
113 
114  /// <summary>Returns a string that represents the current object.</summary>
115  /// <returns>A string that represents the current object.</returns>
116  /// <filterpriority>2</filterpriority>
117  public override string ToString()
118  {
119  return $"O: {Open.SmartRounding()} " +
120  $"H: {High.SmartRounding()} " +
121  $"L: {Low.SmartRounding()} " +
122  $"C: {Close.SmartRounding()}";
123  }
124  }
125 }