Lean  $LEAN_TAG$
DerivativeUniverseData.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
19 
21 
22 /// <summary>
23 /// Represents derivative market data including trade and open interest information.
24 /// </summary>
26 {
27  private readonly Symbol _symbol;
28  private decimal _open;
29  private decimal _high;
30  private decimal _low;
31  private decimal _close;
32  private decimal _volume;
33  private decimal? _openInterest;
34 
35  /// <summary>
36  /// Initializes a new instance of <see cref="DerivativeUniverseData"/> using open interest data.
37  /// </summary>
38  /// <param name="openInterest">The open interest data.</param>
39  public DerivativeUniverseData(OpenInterest openInterest)
40  {
41  _symbol = openInterest.Symbol;
42  _openInterest = openInterest.Value;
43  }
44 
45  /// <summary>
46  /// Initializes a new instance of <see cref="DerivativeUniverseData"/> using trade bar data.
47  /// </summary>
48  /// <param name="tradeBar">The trade bar data.</param>
50  {
51  _symbol = tradeBar.Symbol;
52  _open = tradeBar.Open;
53  _high = tradeBar.High;
54  _low = tradeBar.Low;
55  _close = tradeBar.Close;
56  _volume = tradeBar.Volume;
57  }
58 
59  /// <summary>
60  /// Initializes a new instance of <see cref="DerivativeUniverseData"/> using quote bar data.
61  /// </summary>
62  /// <param name="quoteBar">The quote bar data.</param>
64  {
65  _symbol = quoteBar.Symbol;
66  _open = quoteBar.Open;
67  _high = quoteBar.High;
68  _low = quoteBar.Low;
69  _close = quoteBar.Close;
70  }
71 
72  /// <summary>
73  /// Updates the instance with new trade bar data.
74  /// </summary>
75  /// <param name="tradeBar">The new trade bar data.</param>
76  /// <exception cref="ArgumentNullException">Thrown when tradeBar is null.</exception>
77  public void UpdateByTradeBar(TradeBar tradeBar)
78  {
79  // If price data has already been initialized (likely from a QuoteBar)
80  if (_open != 0 || _high != 0 || _low != 0 || _close != 0)
81  {
82  _volume = tradeBar.Volume;
83  return;
84  }
85 
86  _open = tradeBar.Open;
87  _high = tradeBar.High;
88  _low = tradeBar.Low;
89  _close = tradeBar.Close;
90  }
91 
92  /// <summary>
93  /// Updates the instance with new quote bar data.
94  /// </summary>
95  /// <param name="quoteBar">The new quote bar data.</param>
96  public void UpdateByQuoteBar(QuoteBar quoteBar)
97  {
98  _open = quoteBar.Open;
99  _high = quoteBar.High;
100  _low = quoteBar.Low;
101  _close = quoteBar.Close;
102  }
103 
104  /// <summary>
105  /// Updates the instance with new open interest data.
106  /// </summary>
107  /// <param name="openInterest">The new open interest data.</param>
108  /// <exception cref="ArgumentNullException">Thrown when openInterest is null.</exception>
109  public void UpdateByOpenInterest(OpenInterest openInterest)
110  {
111  _openInterest = openInterest.Value;
112  }
113 
114  /// <summary>
115  /// Converts the current data to a CSV format string.
116  /// </summary>
117  /// <returns>A CSV formatted string representing the data.</returns>
118  public string ToCsv()
119  {
120  return OptionUniverse.ToCsv(_symbol, _open, _high, _low, _close, _volume, _openInterest, null, NullGreeks.Instance);
121  }
122 }