Lean  $LEAN_TAG$
FactorFileZipHelper.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 System.Linq;
19 using System.Collections.Generic;
20 using static QuantConnect.StringExtensions;
21 
23 {
24  /// <summary>
25  /// Provides methods for reading factor file zips
26  /// </summary>
27  public static class FactorFileZipHelper
28  {
29  /// <summary>
30  /// Constructs the factor file path for the specified market and security type
31  /// </summary>
32  /// <param name="market">The market this symbol belongs to</param>
33  /// <param name="securityType">The security type</param>
34  /// <returns>The relative file path</returns>
35  public static string GetRelativeFactorFilePath(string market, SecurityType securityType)
36  {
37  return Invariant($"{securityType.SecurityTypeToLower()}/{market}/factor_files");
38  }
39 
40  /// <summary>
41  /// Gets the factor file zip filename for the specified date
42  /// </summary>
43  public static string GetFactorFileZipFileName(string market, DateTime date, SecurityType securityType)
44  {
45  return Path.Combine(Globals.DataFolder, GetRelativeFactorFilePath(market, securityType), $"factor_files_{date:yyyyMMdd}.zip");
46  }
47 
48  /// <summary>
49  /// Reads the zip bytes as text and parses as FactorFileRows to create FactorFiles
50  /// </summary>
51  public static IEnumerable<KeyValuePair<Symbol, IFactorProvider>> ReadFactorFileZip(Stream file, MapFileResolver mapFileResolver, string market, SecurityType securityType)
52  {
53  if (file == null || file.Length == 0)
54  {
55  return new Dictionary<Symbol, IFactorProvider>();
56  }
57 
58  var keyValuePairs = (
59  from kvp in Compression.Unzip(file)
60  let filename = kvp.Key
61  let lines = kvp.Value
62  let factorFile = PriceScalingExtensions.SafeRead(Path.GetFileNameWithoutExtension(filename), lines, securityType)
63  let mapFile = mapFileResolver.GetByPermtick(factorFile.Permtick)
64  where mapFile != null
65  select new KeyValuePair<Symbol, IFactorProvider>(GetSymbol(mapFile, market, securityType), factorFile)
66  );
67 
68  return keyValuePairs;
69  }
70 
71  private static Symbol GetSymbol(MapFile mapFile, string market, SecurityType securityType)
72  {
74  switch (securityType)
75  {
76  case SecurityType.Equity:
77  sid = SecurityIdentifier.GenerateEquity(mapFile.FirstDate, mapFile.FirstTicker, market);
78  break;
79  case SecurityType.Future:
81  break;
82  default:
83  throw new ArgumentOutOfRangeException(nameof(securityType), securityType, null);
84  }
85  return new Symbol(sid, mapFile.Permtick);
86  }
87  }
88 }