/* * 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 RenkoBar : BaseRenkoBar { /// /// Gets the end time of this renko bar or the most recent update time if it /// [Obsolete("RenkoBar.End is obsolete. Please use RenkoBar.EndTime property instead.")] public DateTime End { get { return EndTime; } set { EndTime = value; } } /// /// The trend of the bar (i.e. Rising, Falling or NoDelta) /// public BarDirection Direction { get { if (Open < Close) return BarDirection.Rising; else if (Open > Close) return BarDirection.Falling; else return BarDirection.NoDelta; } } /// /// The "spread" of the bar /// public decimal Spread { get { return Math.Abs(Close - Open); } } /// /// Initializes a new default instance of the class. /// public RenkoBar() { } /// /// Initializes a new instance of the class with the specified values /// /// The symbol of this data /// The start time of the bar /// The size of each renko brick /// The opening price for the new bar /// Any initial volume associated with the data public RenkoBar(Symbol symbol, DateTime time, decimal brickSize, decimal open, decimal volume) { Type = RenkoType.Classic; Symbol = symbol; Start = time; EndTime = time; BrickSize = brickSize; Open = open; Close = open; Volume = volume; High = open; Low = open; } /// /// Initializes a new instance of the class with the specified values /// /// The symbol of this data /// The start time of the bar /// The end time of the bar /// The size of each wicko brick /// 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 public RenkoBar(Symbol symbol, DateTime start, DateTime endTime, decimal brickSize, decimal open, decimal high, decimal low, decimal close) { Type = RenkoType.Wicked; Symbol = symbol; Start = start; EndTime = endTime; BrickSize = brickSize; Open = open; Close = close; Volume = 0; High = high; Low = low; } /// /// Updates this with the specified values and returns whether or not this bar is closed /// /// The current time /// The current value /// The volume since the last update called on this instance /// True if this bar public bool Update(DateTime time, decimal currentValue, decimal volumeSinceLastUpdate) { if (Type == RenkoType.Wicked) throw new InvalidOperationException("A \"Wicked\" RenkoBar cannot be updated!"); // can't update a closed renko bar if (IsClosed) return true; if (Start == DateTime.MinValue) Start = time; EndTime = time; // compute the min/max closes this renko bar can have decimal lowClose = Open - BrickSize; decimal highClose = Open + BrickSize; Close = Math.Min(highClose, Math.Max(lowClose, currentValue)); Volume += volumeSinceLastUpdate; // determine if this data caused the bar to close if (currentValue <= lowClose || currentValue >= highClose) { IsClosed = true; } if (Close > High) High = Close; if (Close < Low) Low = Close; return IsClosed; } /// /// 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 RenkoBar { Type = Type, BrickSize = BrickSize, Open = Open, Volume = Volume, Close = Close, EndTime = EndTime, High = High, IsClosed = IsClosed, Low = Low, Time = Time, Value = Value, Symbol = Symbol, DataType = DataType }; } } }