diff --git a/Common/Data/Market/FuturesContract.cs b/Common/Data/Market/FuturesContract.cs index 09d136df98de..81957d95124e 100644 --- a/Common/Data/Market/FuturesContract.cs +++ b/Common/Data/Market/FuturesContract.cs @@ -22,6 +22,117 @@ namespace QuantConnect.Data.Market /// public class FuturesContract : BaseContract { + private TradeBar _tradeBar; + private QuoteBar _quoteBar; + private Tick _tradeTick; + private Tick _quoteTick; + private Tick _openInterest; + + /// + /// Gets the open interest + /// + public override decimal OpenInterest => _openInterest?.Value ?? decimal.Zero; + + /// + /// Gets the last price this contract traded at + /// + public override decimal LastPrice + { + get + { + if (_tradeBar == null && _tradeTick == null) + { + return decimal.Zero; + } + if (_tradeBar != null) + { + return _tradeTick != null && _tradeTick.EndTime > _tradeBar.EndTime ? _tradeTick.Price : _tradeBar.Close; + } + return _tradeTick.Price; + } + } + + /// + /// Gets the last volume this contract traded at + /// + public override long Volume => (long)(_tradeBar?.Volume ?? 0); + + /// + /// Get the current bid price + /// + public override decimal BidPrice + { + get + { + if (_quoteBar == null && _quoteTick == null) + { + return decimal.Zero; + } + if (_quoteBar != null) + { + return _quoteTick != null && _quoteTick.EndTime > _quoteBar.EndTime ? _quoteTick.BidPrice : _quoteBar.Bid.Close; + } + return _quoteTick.BidPrice; + } + } + + /// + /// Get the current bid size + /// + public override long BidSize + { + get + { + if (_quoteBar == null && _quoteTick == null) + { + return 0; + } + if (_quoteBar != null) + { + return (long)(_quoteTick != null && _quoteTick.EndTime > _quoteBar.EndTime ? _quoteTick.BidSize : _quoteBar.LastBidSize); + } + return (long)_quoteTick.BidSize; + } + } + + /// + /// Gets the current ask price + /// + public override decimal AskPrice + { + get + { + if (_quoteBar == null && _quoteTick == null) + { + return decimal.Zero; + } + if (_quoteBar != null) + { + return _quoteTick != null && _quoteTick.EndTime > _quoteBar.EndTime ? _quoteTick.AskPrice : _quoteBar.Ask.Close; + } + return _quoteTick.AskPrice; + } + } + + /// + /// Get the current ask size + /// + public override long AskSize + { + get + { + if (_quoteBar == null && _quoteTick == null) + { + return 0; + } + if (_quoteBar != null) + { + return (long)(_quoteTick != null && _quoteTick.EndTime > _quoteBar.EndTime ? _quoteTick.AskSize : _quoteBar.LastAskSize); + } + return (long)_quoteTick.AskSize; + } + } + /// /// Initializes a new instance of the class /// @@ -62,51 +173,23 @@ internal override void Update(BaseData data) switch (data) { case TradeBar tradeBar: - if (tradeBar.Close != 0m) - { - LastPrice = tradeBar.Close; - Volume = (long)tradeBar.Volume; - } - break; + _tradeBar = tradeBar; + break; case QuoteBar quoteBar: - if (quoteBar.Ask != null && quoteBar.Ask.Close != 0m) - { - AskPrice = quoteBar.Ask.Close; - AskSize = (long)quoteBar.LastAskSize; - } - if (quoteBar.Bid != null && quoteBar.Bid.Close != 0m) - { - BidPrice = quoteBar.Bid.Close; - BidSize = (long)quoteBar.LastBidSize; - } + _quoteBar = quoteBar; + break; + + case Tick tick when tick.TickType == TickType.Trade: + _tradeTick = tick; + break; + + case Tick tick when tick.TickType == TickType.Quote: + _quoteTick = tick; break; - case Tick tick: - if (tick.TickType == TickType.Trade) - { - LastPrice = tick.Price; - } - else if (tick.TickType == TickType.Quote) - { - if (tick.AskPrice != 0m) - { - AskPrice = tick.AskPrice; - AskSize = (long)tick.AskSize; - } - if (tick.BidPrice != 0m) - { - BidPrice = tick.BidPrice; - BidSize = (long)tick.BidSize; - } - } - else if (tick.TickType == TickType.OpenInterest) - { - if (tick.Value != 0m) - { - OpenInterest = tick.Value; - } - } + case Tick tick when tick.TickType == TickType.OpenInterest: + _openInterest = tick; break; } }