Lean  $LEAN_TAG$
LeanParser.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.Collections.Generic;
18 using System.IO;
19 using QuantConnect.Data;
21 using QuantConnect.Util;
22 
23 namespace QuantConnect.ToolBox
24 {
25  /// <summary>
26  /// Provides an implementation of <see cref="IStreamParser"/> that reads files in the lean format
27  /// </summary>
28  public class LeanParser : IStreamParser
29  {
30  /// <summary>
31  /// Parses the specified input stream into an enumerable of data
32  /// </summary>
33  /// <param name="source">The source file corresponding the the stream</param>
34  /// <param name="stream">The input stream to be parsed</param>
35  /// <returns>An enumerable of base data</returns>
36  public IEnumerable<BaseData> Parse(string source, Stream stream)
37  {
38  var pathComponents = LeanDataPathComponents.Parse(source);
39  var tickType = pathComponents.Filename.ToLowerInvariant().Contains("_trade")
40  ? TickType.Trade
41  : TickType.Quote;
42 
43  var dataType = GetDataType(pathComponents.SecurityType, pathComponents.Resolution, tickType);
44  var factory = (BaseData) Activator.CreateInstance(dataType);
45 
46  // ignore time zones here, i.e, we're going to emit data in the data time zone
47  var config = new SubscriptionDataConfig(dataType, pathComponents.Symbol, pathComponents.Resolution, TimeZones.Utc, TimeZones.Utc, false, true, false);
48  using (var reader = new StreamReader(stream))
49  {
50  string line;
51  while ((line = reader.ReadLine()) != null)
52  {
53  yield return factory.Reader(config, line, pathComponents.Date, false);
54  }
55  }
56  }
57 
58  /// <summary>
59  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
60  /// </summary>
61  public void Dispose()
62  {
63  }
64 
65  private Type GetDataType(SecurityType securityType, Resolution resolution, TickType tickType)
66  {
67  if (resolution == Resolution.Tick)
68  {
69  return typeof (Tick);
70  }
71 
72  switch (securityType)
73  {
74  case SecurityType.Base:
75  case SecurityType.Equity:
76  return typeof (TradeBar);
77 
78  case SecurityType.Cfd:
79  case SecurityType.Forex:
80  case SecurityType.Crypto:
81  return typeof (QuoteBar);
82 
83  case SecurityType.Option:
84  case SecurityType.FutureOption:
85  case SecurityType.IndexOption:
86  if (tickType == TickType.Trade) return typeof (TradeBar);
87  if (tickType == TickType.Quote) return typeof (QuoteBar);
88  break;
89  }
90  var parameters = string.Join(" | ", securityType, resolution, tickType);
91  throw new NotImplementedException("LeanParser.GetDataType has not yet implemented: " + parameters);
92  }
93  }
94 }