/*
* 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;
namespace QuantConnect.Data.Market
{
///
/// 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)
///
public class RangeBar: TradeBar
{
///
/// Gets the range of the bar.
///
public decimal RangeSize { get; private set; }
///
/// Gets whether or not this bar is considered closed.
///
public bool IsClosed { get; private set; }
///
/// Initialize a new default instance of class.
///
public RangeBar()
{
}
///
/// Initializes a new instance of the class with the specified values
///
/// The symbol of this data
/// The end time of the bar
/// The size of each range bar
/// The opening price for the new bar
/// The high price for the new bar
/// The low price for the new bar
/// The closing price for the new bar
/// The volume value for the new bar
public RangeBar(Symbol symbol, DateTime endTime,
decimal rangeSize, decimal open, decimal? high = null, decimal? low = null, decimal? close = null, decimal volume = 0)
{
Symbol = symbol;
EndTime = endTime;
RangeSize = rangeSize;
Open = open;
Close = close ?? open;
Volume = volume;
High = high ?? open;
Low = low ?? open;
}
///
/// Updates this with the specified values
///
/// The current time
/// The current value
/// The volume since the last update called on this instance
public void Update(DateTime time, decimal currentValue, decimal volumeSinceLastUpdate)
{
EndTime = time;
if (currentValue < Low)
{
if ((High - currentValue) > RangeSize)
{
IsClosed = true;
Low = High - RangeSize;
Close = Low;
return;
}
else
{
Low = currentValue;
}
}
else if (currentValue > High)
{
if ((currentValue - Low) > RangeSize)
{
IsClosed = true;
High = Low + RangeSize;
Close = High;
return;
}
else
{
High = currentValue;
}
}
Volume += volumeSinceLastUpdate;
}
///
/// Return a new instance clone of this object, used in fill forward
///
///
/// This base implementation uses reflection to copy all public fields and properties
///
/// A clone of the current object
public override BaseData Clone()
{
return new RangeBar
{
RangeSize = RangeSize,
Open = Open,
Volume = Volume,
Close = Close,
EndTime = EndTime,
High = High,
IsClosed = IsClosed,
Low = Low,
Time = Time,
Value = Value,
Symbol = Symbol,
DataType = DataType
};
}
}
}