/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2023 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.Collections.Generic;
using System.Runtime.CompilerServices;
namespace QuantConnect.Data.Fundamental
{
///
/// Per symbol we will have a fundamental class provider so the instances can be reused
///
public class FundamentalInstanceProvider
{
private static readonly Dictionary _cache = new();
private readonly FundamentalTimeProvider _timeProvider;
private readonly FinancialStatements _financialStatements;
private readonly OperationRatios _operationRatios;
private readonly SecurityReference _securityReference;
private readonly CompanyReference _companyReference;
private readonly CompanyProfile _companyProfile;
private readonly AssetClassification _assetClassification;
private readonly ValuationRatios _valuationRatios;
private readonly EarningRatios _earningRatios;
private readonly EarningReports _earningReports;
///
/// Get's the fundamental instance provider for the requested symbol
///
/// The requested symbol
/// The unique instance provider
public static FundamentalInstanceProvider Get(Symbol symbol)
{
FundamentalInstanceProvider result = null;
lock (_cache)
{
_cache.TryGetValue(symbol.ID, out result);
}
if (result == null)
{
// we create the fundamental instance provider without holding the cache lock, this is because it uses the pygil
// Deadlock case: if the main thread has PyGil and wants to take lock on cache (security.Fundamentals use case) and the data
// stack thread takes the lock on the cache (creating new fundamentals) and next wants the pygil deadlock!
result = new FundamentalInstanceProvider(symbol);
lock (_cache)
{
_cache[symbol.ID] = result;
}
}
return result;
}
///
/// Creates a new fundamental instance provider
///
/// The target symbol
private FundamentalInstanceProvider(Symbol symbol)
{
_timeProvider = new();
_financialStatements = new(_timeProvider, symbol.ID);
_operationRatios = new(_timeProvider, symbol.ID);
_securityReference = new(_timeProvider, symbol.ID);
_companyReference = new(_timeProvider, symbol.ID);
_companyProfile = new(_timeProvider, symbol.ID);
_assetClassification = new(_timeProvider, symbol.ID);
_valuationRatios = new(_timeProvider, symbol.ID);
_earningRatios = new(_timeProvider, symbol.ID);
_earningReports = new(_timeProvider, symbol.ID);
}
///
/// Returns the ValuationRatios instance
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ValuationRatios GetValuationRatios(DateTime time)
{
_timeProvider.Time = time;
return _valuationRatios;
}
///
/// Returns the EarningRatios instance
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EarningRatios GetEarningRatios(DateTime time)
{
_timeProvider.Time = time;
return _earningRatios;
}
///
/// Returns the EarningReports instance
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EarningReports GetEarningReports(DateTime time)
{
_timeProvider.Time = time;
return _earningReports;
}
///
/// Returns the OperationRatios instance
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public OperationRatios GetOperationRatios(DateTime time)
{
_timeProvider.Time = time;
return _operationRatios;
}
///
/// Returns the FinancialStatements instance
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public FinancialStatements GetFinancialStatements(DateTime time)
{
_timeProvider.Time = time;
return _financialStatements;
}
///
/// Returns the SecurityReference instance
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public SecurityReference GetSecurityReference(DateTime time)
{
_timeProvider.Time = time;
return _securityReference;
}
///
/// Returns the CompanyReference instance
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CompanyReference GetCompanyReference(DateTime time)
{
_timeProvider.Time = time;
return _companyReference;
}
///
/// Returns the CompanyProfile instance
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CompanyProfile GetCompanyProfile(DateTime time)
{
_timeProvider.Time = time;
return _companyProfile;
}
///
/// Returns the AssetClassification instance
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public AssetClassification GetAssetClassification(DateTime time)
{
_timeProvider.Time = time;
return _assetClassification;
}
private class FundamentalTimeProvider : ITimeProvider
{
public DateTime Time;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DateTime GetUtcNow() => Time;
}
}
}