/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using static QuantConnect.StringExtensions;
namespace QuantConnect.Data.Auxiliary
{
///
/// Provides methods for reading factor file zips
///
public static class FactorFileZipHelper
{
///
/// Constructs the factor file path for the specified market and security type
///
/// The market this symbol belongs to
/// The security type
/// The relative file path
public static string GetRelativeFactorFilePath(string market, SecurityType securityType)
{
return Invariant($"{securityType.SecurityTypeToLower()}/{market}/factor_files");
}
///
/// Gets the factor file zip filename for the specified date
///
public static string GetFactorFileZipFileName(string market, DateTime date, SecurityType securityType)
{
return Path.Combine(Globals.DataFolder, GetRelativeFactorFilePath(market, securityType), $"factor_files_{date:yyyyMMdd}.zip");
}
///
/// Reads the zip bytes as text and parses as FactorFileRows to create FactorFiles
///
public static IEnumerable> ReadFactorFileZip(Stream file, MapFileResolver mapFileResolver, string market, SecurityType securityType)
{
if (file == null || file.Length == 0)
{
return new Dictionary();
}
var keyValuePairs = (
from kvp in Compression.Unzip(file)
let filename = kvp.Key
let lines = kvp.Value
let factorFile = PriceScalingExtensions.SafeRead(Path.GetFileNameWithoutExtension(filename), lines, securityType)
let mapFile = mapFileResolver.GetByPermtick(factorFile.Permtick)
where mapFile != null
select new KeyValuePair(GetSymbol(mapFile, market, securityType), factorFile)
);
return keyValuePairs;
}
private static Symbol GetSymbol(MapFile mapFile, string market, SecurityType securityType)
{
SecurityIdentifier sid;
switch (securityType)
{
case SecurityType.Equity:
sid = SecurityIdentifier.GenerateEquity(mapFile.FirstDate, mapFile.FirstTicker, market);
break;
case SecurityType.Future:
sid = SecurityIdentifier.GenerateFuture(SecurityIdentifier.DefaultDate, mapFile.Permtick, market);
break;
default:
throw new ArgumentOutOfRangeException(nameof(securityType), securityType, null);
}
return new Symbol(sid, mapFile.Permtick);
}
}
}