/* * 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; using Newtonsoft.Json; using QuantConnect.Util; using QuantConnect.Data.Market; namespace QuantConnect { /// /// Single candlestick for a candlestick chart /// [JsonConverter(typeof(CandlestickJsonConverter))] public class Candlestick : ISeriesPoint { private bool _openSet; private decimal? _open; private decimal? _high; private decimal? _low; private decimal? _close; /// /// The candlestick time /// public DateTime Time { get; set; } /// /// The candlestick time in seconds since Unix Epoch /// public long LongTime { get { return (long)QuantConnect.Time.DateTimeToUnixTimeStamp(Time); } } /// /// The candlestick open price /// public decimal? Open { get { return _open; } set { _open = value.SmartRounding(); } } /// /// The candlestick high price /// public decimal? High { get { return _high; } set { _high = value.SmartRounding(); } } /// /// The candlestick low price /// public decimal? Low { get { return _low; } set { _low = value.SmartRounding(); } } /// /// The candlestick close price /// public decimal? Close { get { return _close; } set { _close = value.SmartRounding(); } } /// /// Default constructor /// public Candlestick() { } /// /// Constructor taking the candlestick values /// /// Candlestick time in seconds since Unix Epoch /// Candlestick open price /// Candlestick high price /// Candlestick low price /// Candlestick close price public Candlestick(long time, decimal? open, decimal? high, decimal? low, decimal? close) : this(QuantConnect.Time.UnixTimeStampToDateTime(time), open, high, low, close) { } /// /// Constructor taking candlestick values and time in DateTime format /// /// Candlestick time in seconds /// Candlestick open price /// Candlestick high price /// Candlestick low price /// Candlestick close price public Candlestick(DateTime time, decimal? open, decimal? high, decimal? low, decimal? close) { Time = time; Open = open; High = high; Low = low; Close = close; } /// /// Constructor taking candlestick values and time in DateTime format /// /// Bar which data will be used to create the candlestick public Candlestick(TradeBar bar) : this(bar.EndTime, bar.Open, bar.High, bar.Low, bar.Close) { } /// /// Constructor taking candlestick values and time in DateTime format /// /// Candlestick time in seconds /// Bar which data will be used to create the candlestick public Candlestick(DateTime time, Bar bar) : this(time, bar.Open, bar.High, bar.Low, bar.Close) { } /// /// Copy constructor /// /// Candlestick to copy from public Candlestick(Candlestick candlestick) : this(candlestick.Time, candlestick.Open, candlestick.High, candlestick.Low, candlestick.Close) { } /// /// Provides a readable string representation of this instance. /// public override string ToString() { return Messages.Candlestick.ToString(this); } /// /// Clones this instance /// /// Clone of this instance public ISeriesPoint Clone() { return new Candlestick(this); } /// /// Updates the candlestick with a new value. This will aggregate the OHLC bar /// /// The new value public void Update(decimal? value) { if (value.HasValue) { Update(value.Value); } } /// /// Updates the candlestick with a new value. This will aggregate the OHLC bar /// /// The new value public void Update(decimal value) { if (!_openSet) { Open = High = Low = Close = value; _openSet = true; } else if (value > High) High = value; else if (value < Low) Low = value; Close = value; } } }