Lean  $LEAN_TAG$
OpenInterest.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 QuantConnect.Util;
18 using System;
19 using ProtoBuf;
20 
22 {
23  /// <summary>
24  /// Defines a data type that represents open interest for given security
25  /// </summary>
26  [ProtoContract(SkipConstructor = true)]
27  public class OpenInterest : Tick
28  {
29  /// <summary>
30  /// Initializes a new instance of the OpenInterest class
31  /// </summary>
32  public OpenInterest()
33  {
34  DataType = MarketDataType.Tick;
35  TickType = TickType.OpenInterest;
36  Value = 0;
37  Time = new DateTime();
39  }
40 
41  /// <summary>
42  /// Cloner constructor for fill forward engine implementation. Clone the original OI into this new one:
43  /// </summary>
44  /// <param name="original">Original OI we're cloning</param>
45  public OpenInterest(OpenInterest original)
46  {
47  DataType = MarketDataType.Tick;
48  TickType = TickType.OpenInterest;
49  Value = original.Value;
50  Time = original.Time;
51  Symbol = original.Symbol;
52  }
53 
54  /// <summary>
55  /// Initializes a new instance of the OpenInterest class with data
56  /// </summary>
57  /// <param name="time">Full date and time</param>
58  /// <param name="symbol">Underlying equity security symbol</param>
59  /// <param name="openInterest">Open Interest value</param>
60  public OpenInterest(DateTime time, Symbol symbol, decimal openInterest)
61  {
62  DataType = MarketDataType.Tick;
63  TickType = TickType.OpenInterest;
64  Time = time;
65  Symbol = symbol;
66  Value = openInterest;
67  }
68 
69  /// <summary>
70  /// Constructor for QuantConnect open interest data
71  /// </summary>
72  /// <param name="config">Subscription configuration</param>
73  /// <param name="symbol">Symbol for underlying asset</param>
74  /// <param name="line">CSV line of data from QC OI csv</param>
75  /// <param name="baseDate">The base date of the OI</param>
76  public OpenInterest(SubscriptionDataConfig config, Symbol symbol, string line, DateTime baseDate)
77  {
78  var csv = line.Split(',');
79  DataType = MarketDataType.Tick;
80  TickType = TickType.OpenInterest;
81  Symbol = symbol;
82 
83  Time = (config.Resolution == Resolution.Daily || config.Resolution == Resolution.Hour) ?
84  // hourly and daily have different time format, and can use slow, robust c# parser.
85  DateTime.ParseExact(csv[0], DateFormat.TwelveCharacter,
86  System.Globalization.CultureInfo.InvariantCulture)
87  .ConvertTo(config.DataTimeZone, config.ExchangeTimeZone)
88  :
89  // Using custom "ToDecimal" conversion for speed on high resolution data.
90  baseDate.Date.AddMilliseconds(csv[0].ToInt32()).ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
91 
92  Value = csv[1].ToDecimal();
93  }
94 
95  /// <summary>
96  /// Parse an open interest data line from quantconnect zip source files.
97  /// </summary>
98  /// <param name="line">CSV source line of the compressed source</param>
99  /// <param name="date">Base date for the open interest (date is stored as int milliseconds since midnight)</param>
100  /// <param name="config">Subscription configuration object</param>
101  public OpenInterest(SubscriptionDataConfig config, string line, DateTime date):
102  this(config, config.Symbol, line, date)
103  {
104  }
105 
106  /// <summary>
107  /// Tick implementation of reader method: read a line of data from the source and convert it to an open interest object.
108  /// </summary>
109  /// <param name="config">Subscription configuration object for algorithm</param>
110  /// <param name="line">Line from the datafeed source</param>
111  /// <param name="date">Date of this reader request</param>
112  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
113  /// <returns>New initialized open interest object</returns>
114  public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
115  {
116  if (isLiveMode)
117  {
118  // currently OIs don't come through the reader function
119  return new OpenInterest();
120  }
121 
122  return new OpenInterest(config, line, date);
123  }
124 
125  /// <summary>
126  /// Get source for OI data feed - not used with QuantConnect data sources implementation.
127  /// </summary>
128  /// <param name="config">Configuration object</param>
129  /// <param name="date">Date of this source request if source spread across multiple files</param>
130  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
131  /// <returns>String source location of the file to be opened with a stream</returns>
132  public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
133  {
134  if (isLiveMode)
135  {
136  // this data type is streamed in live mode
137  return new SubscriptionDataSource(string.Empty, SubscriptionTransportMedium.Streaming);
138  }
139 
140  var source = LeanData.GenerateZipFilePath(Globals.DataFolder, config.Symbol, date, config.Resolution, config.TickType);
141  if (config.SecurityType == SecurityType.Future || config.SecurityType.IsOption())
142  {
143  source += "#" + LeanData.GenerateZipEntryName(config.Symbol, date, config.Resolution, config.TickType);
144  }
145  return new SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile, FileFormat.Csv);
146  }
147 
148  /// <summary>
149  /// Clone implementation for open interest class:
150  /// </summary>
151  /// <returns>New tick object clone of the current class values.</returns>
152  public override BaseData Clone()
153  {
154  return new OpenInterest(this);
155  }
156  }
157 }