/*
* 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.Runtime.CompilerServices;
using ProtoBuf;
namespace QuantConnect.Data.Market
{
///
/// Base Bar Class: Open, High, Low, Close and Period.
///
[ProtoContract(SkipConstructor = true)]
public class Bar : IBar
{
private bool _openSet;
///
/// Opening price of the bar: Defined as the price at the start of the time period.
///
[ProtoMember(1)]
public virtual decimal Open { get; set; }
///
/// High price of the bar during the time period.
///
[ProtoMember(2)]
public virtual decimal High { get; set; }
///
/// Low price of the bar during the time period.
///
[ProtoMember(3)]
public virtual decimal Low { get; set; }
///
/// Closing price of the bar. Defined as the price at Start Time + TimeSpan.
///
[ProtoMember(4)]
public virtual decimal Close { get; set; }
///
/// Default initializer to setup an empty bar.
///
public Bar()
{
}
///
/// Initializer to setup a bar with a given information.
///
/// Decimal Opening Price
/// Decimal High Price of this bar
/// Decimal Low Price of this bar
/// Decimal Close price of this bar
public Bar(decimal open, decimal high, decimal low, decimal close)
{
_openSet = open != 0;
Open = open;
High = high;
Low = low;
Close = close;
}
///
/// Updates the bar with a new value. This will aggregate the OHLC bar
///
/// The new value
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Update(decimal value)
{
Update(ref value);
}
///
/// Updates the bar with a new value. This will aggregate the OHLC bar
///
/// The new value
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Update(ref decimal value)
{
// Do not accept zero as a new value
if (value == 0) return;
if (!_openSet)
{
Open = High = Low = Close = value;
_openSet = true;
}
else if (value > High) High = value;
else if (value < Low) Low = value;
Close = value;
}
///
/// Returns a clone of this bar
///
public Bar Clone()
{
return new Bar(Open, High, Low, Close);
}
/// Returns a string that represents the current object.
/// A string that represents the current object.
/// 2
public override string ToString()
{
return $"O: {Open.SmartRounding()} " +
$"H: {High.SmartRounding()} " +
$"L: {Low.SmartRounding()} " +
$"C: {Close.SmartRounding()}";
}
}
}