/*
* 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.IO;
using QuantConnect.Util;
using QuantConnect.Interfaces;
using System.Collections.Concurrent;
namespace QuantConnect.Data.Auxiliary
{
///
/// Provides an implementation of that searches the local disk
///
public class LocalDiskFactorFileProvider : IFactorFileProvider
{
private IMapFileProvider _mapFileProvider;
private IDataProvider _dataProvider;
private readonly ConcurrentDictionary _cache;
///
/// Creates a new instance of the
///
public LocalDiskFactorFileProvider()
{
_cache = new ConcurrentDictionary();
}
///
/// Initializes our FactorFileProvider by supplying our mapFileProvider
/// and dataProvider
///
/// MapFileProvider to use
/// DataProvider to use
public void Initialize(IMapFileProvider mapFileProvider, IDataProvider dataProvider)
{
_mapFileProvider = mapFileProvider;
_dataProvider = dataProvider;
}
///
/// Gets a instance for the specified symbol, or null if not found
///
/// The security's symbol whose factor file we seek
/// The resolved factor file, or null if not found
public IFactorProvider Get(Symbol symbol)
{
symbol = symbol.GetFactorFileSymbol();
IFactorProvider factorFile;
if (_cache.TryGetValue(symbol, out factorFile))
{
return factorFile;
}
// we first need to resolve the map file to get a permtick, that's how the factor files are stored
var mapFileResolver = _mapFileProvider.Get(AuxiliaryDataKey.Create(symbol));
if (mapFileResolver == null)
{
return GetFactorFile(symbol, symbol.Value);
}
var mapFile = mapFileResolver.ResolveMapFile(symbol);
if (mapFile.IsNullOrEmpty())
{
return GetFactorFile(symbol, symbol.Value);
}
return GetFactorFile(symbol, mapFile.Permtick);
}
///
/// Checks that the factor file exists on disk, and if it does, loads it into memory
///
private IFactorProvider GetFactorFile(Symbol symbol, string permtick)
{
var basePath = Globals.GetDataFolderPath(FactorFileZipHelper.GetRelativeFactorFilePath(symbol.ID.Market, symbol.SecurityType));
var path = Path.Combine(basePath, permtick.ToLowerInvariant() + ".csv");
var factorFile = PriceScalingExtensions.SafeRead(permtick, _dataProvider.ReadLines(path), symbol.SecurityType);
_cache.AddOrUpdate(symbol, factorFile, (s, c) => factorFile);
return factorFile;
}
}
}