/* * 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.Collections.Generic; using System; using QuantConnect.Data.Market; using Newtonsoft.Json; using QuantConnect.Util; namespace QuantConnect { /// /// Candlestick Chart Series Object - Series data and properties for a candlestick chart /// [JsonConverter(typeof(SeriesJsonConverter))] public class CandlestickSeries : BaseSeries { /// /// Default constructor for chart series /// public CandlestickSeries() : base() { SeriesType = SeriesType.Candle; } /// /// Constructor method for Chart Series /// /// Name of the chart series public CandlestickSeries(string name) : base(name, SeriesType.Candle) { } /// /// Foundational constructor on the series class /// /// Name of the series /// Index position on the chart of the series public CandlestickSeries(string name, int index) : this(name, index, "$") { } /// /// Foundational constructor on the series class /// /// Name of the series /// Index position on the chart of the series /// Unit for the series axis public CandlestickSeries(string name, int index, string unit) : this(name, unit) { Index = index; } /// /// Constructor method for Chart Series /// /// Name of the chart series /// Unit of the series public CandlestickSeries(string name, string unit) : base(name, SeriesType.Candle, unit) { } /// /// Add a new point to this series /// /// Time of the chart point /// Candlestick open price /// Candlestick high price /// Candlestick low price /// Candlestick close price public void AddPoint(DateTime time, decimal open, decimal high, decimal low, decimal close) { base.AddPoint(new Candlestick(time, open, high, low, close)); } /// /// Add a new point to this series /// public void AddPoint(TradeBar bar) { base.AddPoint(new Candlestick(bar)); } /// /// Add a new point to this series /// /// The data point to add public override void AddPoint(ISeriesPoint point) { if (point as Candlestick == null) { throw new ArgumentException("CandlestickSeries.AddPoint requires a Candlestick object"); } base.AddPoint(point); } /// /// Add a new point to this series /// /// The time of the data point /// The values of the data point public override void AddPoint(DateTime time, List values) { if (values.Count != 4) { throw new ArgumentException("CandlestickSeries.AddPoint requires 4 values (open, high, low, close)"); } base.AddPoint(new Candlestick(time, values[0], values[1], values[2], values[3])); } /// /// Will sum up all candlesticks into a new single one, using the time of latest point /// /// The new candlestick public override ISeriesPoint ConsolidateChartPoints() { if (Values.Count <= 0) return null; decimal? openSum = null; decimal? highSum = null; decimal? lowSum = null; decimal? closeSum = null; foreach (Candlestick point in Values) { if (point.Open.HasValue) { openSum ??= 0; openSum += point.Open.Value; } if (point.High.HasValue) { highSum ??= 0; highSum += point.High.Value; } if (point.Low.HasValue) { lowSum ??= 0; lowSum += point.Low.Value; } if (point.Close.HasValue) { closeSum ??= 0; closeSum += point.Close.Value; } } var lastCandlestick = Values[Values.Count - 1]; return new Candlestick(lastCandlestick.Time, openSum, highSum, lowSum, closeSum); } /// /// Return a new instance clone of this object /// /// public override BaseSeries Clone(bool empty = false) { var series = new CandlestickSeries(Name, Index, Unit) { ZIndex = ZIndex, IndexName = IndexName, Tooltip = Tooltip }; if (!empty) { series.Values = CloneValues(); } return series; } } }