Lean  $LEAN_TAG$
BaseChainUniverseData.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 using System;
17 using System.IO;
18 using QuantConnect.Python;
20 using QuantConnect.Util;
21 
23 {
24  /// <summary>
25  /// Represents a chain universe.
26  /// Intended as a base for options and futures universe data.
27  /// </summary>
29  {
30  /// <summary>
31  /// Csv line to get the values from
32  /// </summary>
33  /// <remarks>We keep the properties as they are in the csv file to reduce memory usage (strings vs decimals)</remarks>
34  protected string CsvLine { get; }
35 
36  /// <summary>
37  /// The security identifier of the option symbol
38  /// </summary>
39  [PandasIgnore]
41 
42  /// <summary>
43  /// Price of the security
44  /// </summary>
45  [PandasIgnore]
46  public override decimal Value => Close;
47 
48  /// <summary>
49  /// Open price of the security
50  /// </summary>
51  public decimal Open
52  {
53  get
54  {
55  // Parse the values every time to avoid keeping them in memory
56  return CsvLine.GetDecimalFromCsv(0);
57  }
58  }
59 
60  /// <summary>
61  /// High price of the security
62  /// </summary>
63  public decimal High
64  {
65  get
66  {
67  return CsvLine.GetDecimalFromCsv(1);
68  }
69  }
70 
71  /// <summary>
72  /// Low price of the security
73  /// </summary>
74  public decimal Low
75  {
76  get
77  {
78  return CsvLine.GetDecimalFromCsv(2);
79  }
80  }
81 
82  /// <summary>
83  /// Close price of the security
84  /// </summary>
85  public decimal Close
86  {
87  get
88  {
89  return CsvLine.GetDecimalFromCsv(3);
90  }
91  }
92 
93  /// <summary>
94  /// Volume value of the security
95  /// </summary>
96  public decimal Volume
97  {
98  get
99  {
100  return CsvLine.GetDecimalFromCsv(4);
101  }
102  }
103 
104  /// <summary>
105  /// Open interest value
106  /// </summary>
107  public virtual decimal OpenInterest
108  {
109  get
110  {
111  return CsvLine.GetDecimalFromCsv(5);
112  }
113  }
114 
115  /// <summary>
116  /// Time that the data became available to use
117  /// </summary>
118  public override DateTime EndTime
119  {
120  get { return Time + QuantConnect.Time.OneDay; }
121  set { Time = value - QuantConnect.Time.OneDay; }
122  }
123 
124  /// <summary>
125  /// Creates a new instance of the <see cref="BaseChainUniverseData"/> class
126  /// </summary>
128  {
129  }
130 
131  /// <summary>
132  /// Creates a new instance of the <see cref="BaseChainUniverseData"/> class
133  /// </summary>
134  protected BaseChainUniverseData(DateTime date, Symbol symbol, string csv)
135  : base(date, date, symbol, null, null)
136  {
137  CsvLine = csv;
138  }
139 
140  /// <summary>
141  /// Creates a new instance of the <see cref="BaseChainUniverseData"/> class as a copy of the given instance
142  /// </summary>
144  : base(other)
145  {
146  CsvLine = other.CsvLine;
147  }
148 
149  /// <summary>
150  /// Return the URL string source of the file. This will be converted to a stream
151  /// </summary>
152  /// <param name="config">Configuration object</param>
153  /// <param name="date">Date of this source file</param>
154  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
155  /// <returns>String URL of source file.</returns>
156  public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
157  {
158  var path = GetUniverseFullFilePath(config.Symbol, date);
159  return new SubscriptionDataSource(path, SubscriptionTransportMedium.LocalFile, FileFormat.FoldingCollection);
160  }
161 
162  /// <summary>
163  /// Generates the file path for a universe data file based on the given symbol and date.
164  /// Optionally, creates the directory if it does not exist.
165  /// </summary>
166  /// <param name="symbol">The financial symbol for which the universe file is generated.</param>
167  /// <param name="date">The date associated with the universe file.</param>
168  /// <returns>The full file path to the universe data file.</returns>
169  public static string GetUniverseFullFilePath(Symbol symbol, DateTime date)
170  {
171  return Path.Combine(LeanData.GenerateUniversesDirectory(Globals.DataFolder, symbol), $"{date:yyyyMMdd}.csv");
172  }
173 
174  /// <summary>
175  /// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method, and returns a new instance of the object
176  /// each time it is called.
177  /// </summary>
178  /// <param name="config">Subscription data config setup object</param>
179  /// <param name="stream">Stream reader of the source document</param>
180  /// <param name="date">Date of the requested data</param>
181  /// <param name="symbol">The symbol read and parsed from the current line in the stream</param>
182  /// <param name="remainingLine">The remaining string after reading the symbol from the current line in the stream</param>
183  /// <returns>Whether a valid line starting with a symbol was read</returns>
184  protected static bool TryRead(SubscriptionDataConfig config, StreamReader stream, DateTime date, out Symbol symbol, out string remainingLine)
185  {
186  symbol = null;
187  remainingLine = null;
188 
189  if (stream == null || stream.EndOfStream)
190  {
191  return false;
192  }
193 
194  var sidStr = stream.GetString();
195 
196  if (sidStr.StartsWith("#", StringComparison.InvariantCulture))
197  {
198  stream.ReadLine();
199  return false;
200  }
201 
202  var symbolValue = stream.GetString();
203  remainingLine = stream.ReadLine();
204 
205  var key = $"{sidStr}:{symbolValue}";
206 
207  if (!TryGetCachedSymbol(key, out symbol))
208  {
209  var sid = SecurityIdentifier.Parse(sidStr);
210 
211  if (sid.HasUnderlying)
212  {
213  // Let's try to get the underlying symbol from the cache
214  SymbolRepresentation.TryDecomposeOptionTickerOSI(symbolValue, sid.SecurityType,
215  out var _, out var underlyingValue, out var _, out var _, out var _);
216  var underlyingKey = $"{sid.Underlying}:{underlyingValue}";
217  var underlyingWasCached = TryGetCachedSymbol(underlyingKey, out var underlyingSymbol);
218 
219  symbol = Symbol.CreateOption(sid, symbolValue, underlyingSymbol);
220 
221  if (!underlyingWasCached)
222  {
223  CacheSymbol(underlyingKey, symbol.Underlying);
224  }
225  }
226  else
227  {
228  symbol = new Symbol(sid, symbolValue);
229  }
230 
231  CacheSymbol(key, symbol);
232  }
233 
234  return true;
235  }
236 
237  /// <summary>
238  /// Gets the default resolution for this data and security type
239  /// </summary>
240  /// <remarks>This is a method and not a property so that python
241  /// custom data types can override it</remarks>
242  public override Resolution DefaultResolution()
243  {
244  return Resolution.Daily;
245  }
246 
247  /// <summary>
248  /// Gets the symbol of the option
249  /// </summary>
250  public Symbol ToSymbol()
251  {
252  return Symbol;
253  }
254  }
255 }