/* * 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 QuantConnect.Data.Market; namespace QuantConnect.Data.UniverseSelection; /// /// Represents derivative market data including trade and open interest information. /// public class DerivativeUniverseData { private readonly Symbol _symbol; private decimal _open; private decimal _high; private decimal _low; private decimal _close; private decimal _volume; private decimal? _openInterest; /// /// Initializes a new instance of using open interest data. /// /// The open interest data. public DerivativeUniverseData(OpenInterest openInterest) { _symbol = openInterest.Symbol; _openInterest = openInterest.Value; } /// /// Initializes a new instance of using trade bar data. /// /// The trade bar data. public DerivativeUniverseData(TradeBar tradeBar) { _symbol = tradeBar.Symbol; _open = tradeBar.Open; _high = tradeBar.High; _low = tradeBar.Low; _close = tradeBar.Close; _volume = tradeBar.Volume; } /// /// Initializes a new instance of using quote bar data. /// /// The quote bar data. public DerivativeUniverseData(QuoteBar quoteBar) { _symbol = quoteBar.Symbol; _open = quoteBar.Open; _high = quoteBar.High; _low = quoteBar.Low; _close = quoteBar.Close; } /// /// Updates the instance with new trade bar data. /// /// The new trade bar data. /// Thrown when tradeBar is null. public void UpdateByTradeBar(TradeBar tradeBar) { // If price data has already been initialized (likely from a QuoteBar) if (_open != 0 || _high != 0 || _low != 0 || _close != 0) { _volume = tradeBar.Volume; return; } _open = tradeBar.Open; _high = tradeBar.High; _low = tradeBar.Low; _close = tradeBar.Close; } /// /// Updates the instance with new quote bar data. /// /// The new quote bar data. public void UpdateByQuoteBar(QuoteBar quoteBar) { _open = quoteBar.Open; _high = quoteBar.High; _low = quoteBar.Low; _close = quoteBar.Close; } /// /// Updates the instance with new open interest data. /// /// The new open interest data. /// Thrown when openInterest is null. public void UpdateByOpenInterest(OpenInterest openInterest) { _openInterest = openInterest.Value; } /// /// Converts the current data to a CSV format string. /// /// A CSV formatted string representing the data. public string ToCsv() { return OptionUniverse.ToCsv(_symbol, _open, _high, _low, _close, _volume, _openInterest, null, NullGreeks.Instance); } }