Skip to content

Commit

Permalink
Consolidate Configuration.Get usages (QuantConnect#7796)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Molinero authored Feb 20, 2024
1 parent 6f6fbee commit 2a82c6f
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 75 deletions.
9 changes: 6 additions & 3 deletions Api/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace QuantConnect.Api
/// </summary>
public class Api : IApi, IDownloadProvider
{
private readonly HttpClient _client = new HttpClient();
private readonly Lazy<HttpClient> _client = new ();
private string _dataFolder;

/// <summary>
Expand Down Expand Up @@ -1010,7 +1010,7 @@ public bool DownloadData(string filePath, string organizationId)
{
// Download the file
var uri = new Uri(dataLink.Url);
using var dataStream = _client.GetStreamAsync(uri);
using var dataStream = _client.Value.GetStreamAsync(uri);

using var fileStream = new FileStream(filePath, FileMode.Create);
dataStream.Result.CopyTo(fileStream);
Expand Down Expand Up @@ -1128,7 +1128,10 @@ public virtual string Download(string address, IEnumerable<KeyValuePair<string,
/// <filterpriority>2</filterpriority>
public virtual void Dispose()
{
_client.DisposeSafely();
if (_client.IsValueCreated)
{
_client.Value.DisposeSafely();
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Common/Data/DataMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class DataMonitor : IDataMonitor
/// </summary>
public DataMonitor()
{
_resultsDestinationFolder = Config.Get("results-destination-folder", Directory.GetCurrentDirectory());
_resultsDestinationFolder = Globals.ResultsDestinationFolder;
_succeededDataRequestsFileName = GetFilePath("succeeded-data-requests.txt");
_failedDataRequestsFileName = GetFilePath("failed-data-requests.txt");
}
Expand Down
3 changes: 1 addition & 2 deletions Common/Data/InterestRateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ protected void LoadInterestRateProvider()
/// <returns>Dictionary of historical credit rate change events</returns>
public static Dictionary<DateTime, decimal> FromCsvFile(string file, out decimal firstInterestRate)
{
var dataProvider = Composer.Instance.GetExportedValueByTypeName<IDataProvider>(
Config.Get("data-provider", "DefaultDataProvider"));
var dataProvider = Composer.Instance.GetPart<IDataProvider>();

var firstInterestRateSet = false;
firstInterestRate = DefaultRiskFreeRate;
Expand Down
3 changes: 1 addition & 2 deletions Common/Data/Shortable/LocalDiskShortableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public class LocalDiskShortableProvider : IShortableProvider
/// <summary>
/// The data provider instance to use
/// </summary>
protected static IDataProvider DataProvider = Composer.Instance.GetExportedValueByTypeName<IDataProvider>(Config.Get("data-provider",
"DefaultDataProvider"), forceTypeNameOnExisting: false);
protected static IDataProvider DataProvider = Composer.Instance.GetPart<IDataProvider>();

private string _ticker;
private bool _scheduledCleanup;
Expand Down
3 changes: 1 addition & 2 deletions Common/Data/UniverseSelection/ContinuousContractUniverse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public ContinuousContractUniverse(Security security, UniverseSettings universeSe
_security = security;
_liveMode = liveMode;
UniverseSettings = universeSettings;
var mapFileProviderTypeName = Config.Get("map-file-provider", "LocalDiskMapFileProvider");
_mapFileProvider = Composer.Instance.GetExportedValueByTypeName<IMapFileProvider>(mapFileProviderTypeName);
_mapFileProvider = Composer.Instance.GetPart<IMapFileProvider>();

_config = new SubscriptionDataConfig(Configuration, dataMappingMode: UniverseSettings.DataMappingMode, symbol: _security.Symbol.Canonical);
}
Expand Down
38 changes: 38 additions & 0 deletions Common/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,41 @@ static Globals()
Reset();
}

/// <summary>
/// The user Id
/// </summary>
public static int UserId { get; set; }

/// <summary>
/// The project id
/// </summary>
public static int ProjectId { get; set; }

/// <summary>
/// The user token
/// </summary>
public static string UserToken { get; set; }

/// <summary>
/// The organization id
/// </summary>
public static string OrganizationID { get; set; }

/// <summary>
/// The results destination folder
/// </summary>
public static string ResultsDestinationFolder { get; set; }

/// <summary>
/// The root directory of the data folder for this application
/// </summary>
public static string DataFolder { get; private set; }

/// <summary>
/// True if running in live mode
/// </summary>
public static bool LiveMode { get; private set; }

/// <summary>
/// Resets global values with the Config data.
/// </summary>
Expand All @@ -54,6 +84,14 @@ public static void Reset ()
{
CacheDataFolder = cacheLocation;
}

LiveMode = Config.GetBool("live-mode");

UserId = Config.GetInt("job-user-id");
ProjectId = Config.GetInt("project-id");
UserToken = Config.Get("api-access-token");
OrganizationID = Config.Get("job-organization-id");
ResultsDestinationFolder = Config.Get("results-destination-folder", Directory.GetCurrentDirectory());
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions Common/Securities/Future/FutureMarginModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class FutureMarginModel : SecurityMarginModel
{
private static IDataProvider _dataProvider;
private static readonly object _locker = new();
private static readonly string DataProvider = Config.Get("data-provider", "DefaultDataProvider");
private static Dictionary<string, MarginRequirementsEntry[]> _marginRequirementsCache = new();

// historical database of margin requirements
Expand Down Expand Up @@ -263,7 +262,7 @@ private static MarginRequirementsEntry[] FromCsvFile(Symbol symbol)
if(_dataProvider == null)
{
ClearMarginCache();
_dataProvider = Composer.Instance.GetExportedValueByTypeName<IDataProvider>(DataProvider, forceTypeNameOnExisting: false);
_dataProvider = Composer.Instance.GetPart<IDataProvider>();
}

// skip the first header line, also skip #'s as these are comment lines
Expand Down
6 changes: 2 additions & 4 deletions Common/Securities/SecurityDefinitionSymbolResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ private SecurityDefinitionSymbolResolver(IDataProvider dataProvider = null, stri
{
_securitiesDefinitionKey = securitiesDefinitionKey ?? Path.Combine(Globals.GetDataFolderPath("symbol-properties"), "security-database.csv");

_dataProvider = dataProvider ??
Composer.Instance.GetExportedValueByTypeName<IDataProvider>(
Config.Get("data-provider", "QuantConnect.Lean.Engine.DataFeeds.DefaultDataProvider"));
_dataProvider = dataProvider ?? Composer.Instance.GetPart<IDataProvider>();

_mapFileProvider = Composer.Instance.GetExportedValueByTypeName<IMapFileProvider>(Config.Get("map-file-provider", "LocalDiskMapFileProvider"));
_mapFileProvider = Composer.Instance.GetPart<IMapFileProvider>();
_mapFileProvider.Initialize(_dataProvider);
}

Expand Down
5 changes: 1 addition & 4 deletions Common/SecurityIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ public class SecurityIdentifier : IEquatable<SecurityIdentifier>, IComparable<Se

private static readonly Dictionary<string, Type> TypeMapping = new();
private static readonly Dictionary<string, SecurityIdentifier> SecurityIdentifierCache = new();
private static readonly string MapFileProviderTypeName = Config.Get("map-file-provider", "LocalDiskMapFileProvider");
private static readonly char[] InvalidCharacters = {'|', ' '};
private static readonly Lazy<IMapFileProvider> MapFileProvider = new Lazy<IMapFileProvider>(
() => Composer.Instance.GetExportedValueByTypeName<IMapFileProvider>(MapFileProviderTypeName, forceTypeNameOnExisting: false)
);
private static readonly Lazy<IMapFileProvider> MapFileProvider = new(Composer.Instance.GetPart<IMapFileProvider>());

/// <summary>
/// Gets an instance of <see cref="SecurityIdentifier"/> that is empty, that is, one with no symbol specified
Expand Down
22 changes: 9 additions & 13 deletions Engine/DataFeeds/ApiDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ namespace QuantConnect.Lean.Engine.DataFeeds
/// </summary>
public class ApiDataProvider : BaseDownloaderDataProvider
{
private readonly int _uid = Config.GetInt("job-user-id", 0);
private readonly string _token = Config.Get("api-access-token", "1");
private readonly string _organizationId = Config.Get("job-organization-id");
private readonly string _dataPath = Config.Get("data-folder", "../../../Data/");
private decimal _purchaseLimit = Config.GetValue("data-purchase-limit", decimal.MaxValue); //QCC

private readonly HashSet<SecurityType> _unsupportedSecurityType;
private readonly DataPricesList _dataPrices;
private readonly Api.Api _api;
private readonly IApi _api;
private readonly bool _subscribedToIndiaEquityMapAndFactorFiles;
private readonly bool _subscribedToUsaEquityMapAndFactorFiles;
private readonly bool _subscribedToFutureMapAndFactorFiles;
Expand All @@ -51,21 +47,21 @@ public class ApiDataProvider : BaseDownloaderDataProvider
/// </summary>
public ApiDataProvider()
{
_api = new Api.Api();
_unsupportedSecurityType = new HashSet<SecurityType> { SecurityType.Future, SecurityType.FutureOption, SecurityType.Index, SecurityType.IndexOption };
_api.Initialize(_uid, _token, _dataPath);

_api = Composer.Instance.GetPart<IApi>();

// If we have no value for organization get account preferred
if (string.IsNullOrEmpty(_organizationId))
if (string.IsNullOrEmpty(Globals.OrganizationID))
{
var account = _api.ReadAccount();
_organizationId = account?.OrganizationId;
Log.Trace($"ApiDataProvider(): Will use organization Id '{_organizationId}'.");
Globals.OrganizationID = account?.OrganizationId;
Log.Trace($"ApiDataProvider(): Will use organization Id '{Globals.OrganizationID}'.");
}

// Read in data prices and organization details
_dataPrices = _api.ReadDataPrices(_organizationId);
var organization = _api.ReadOrganization(_organizationId);
_dataPrices = _api.ReadDataPrices(Globals.OrganizationID);
var organization = _api.ReadOrganization(Globals.OrganizationID);

foreach (var productItem in organization.Products.Where(x => x.Type == ProductType.Data).SelectMany(product => product.Items))
{
Expand Down Expand Up @@ -229,7 +225,7 @@ protected virtual bool DownloadData(string filePath)
Log.Debug($"ApiDataProvider.Fetch(): Attempting to get data from QuantConnect.com's data library for {filePath}.");
}

if (_api.DownloadData(filePath, _organizationId))
if (_api.DownloadData(filePath, Globals.OrganizationID))
{
Log.Trace($"ApiDataProvider.Fetch(): Successfully retrieved data for {filePath}.");
return true;
Expand Down
3 changes: 1 addition & 2 deletions Engine/DataFeeds/Queues/FakeDataQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
using QuantConnect.Interfaces;
using QuantConnect.Data.Market;
using System.Collections.Generic;
using QuantConnect.Configuration;
using Timer = System.Timers.Timer;
using QuantConnect.Lean.Engine.HistoricalData;

Expand Down Expand Up @@ -69,7 +68,7 @@ public FakeDataQueue(IDataAggregator dataAggregator, int dataPointsPerSecondPerS
_aggregator = dataAggregator;
_dataPointsPerSecondPerSymbol = dataPointsPerSecondPerSymbol;
_dataCacheProvider = new ZipDataCacheProvider(new DefaultDataProvider(), true);
var mapFileProvider = Composer.Instance.GetExportedValueByTypeName<IMapFileProvider>(Config.Get("map-file-provider", "LocalDiskMapFileProvider"), false);
var mapFileProvider = Composer.Instance.GetPart<IMapFileProvider>();
_optionChainProvider = new LiveOptionChainProvider(_dataCacheProvider, mapFileProvider);
_marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
_symbolExchangeTimeZones = new Dictionary<Symbol, TimeZoneOffsetProvider>();
Expand Down
2 changes: 1 addition & 1 deletion Engine/Initializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void Start()
#endif

Log.DebuggingEnabled = Config.GetBool("debug-mode");
var destinationDir = Config.Get("results-destination-folder");
var destinationDir = Globals.ResultsDestinationFolder;
if (!string.IsNullOrEmpty(destinationDir))
{
Directory.CreateDirectory(destinationDir);
Expand Down
5 changes: 2 additions & 3 deletions Engine/LeanEngineAlgorithmHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ public static LeanEngineAlgorithmHandlers FromConfiguration(Composer composer, b
var objectStoreTypeName = Config.Get("object-store", "LocalObjectStore");
var dataPermissionManager = Config.Get("data-permission-manager", "DataPermissionManager");

var liveMode = Config.GetBool("live-mode");
var result = new LeanEngineAlgorithmHandlers(
composer.GetExportedValueByTypeName<IResultHandler>(resultHandlerTypeName),
composer.GetExportedValueByTypeName<ISetupHandler>(setupHandlerTypeName),
Expand All @@ -217,7 +216,7 @@ public static LeanEngineAlgorithmHandlers FromConfiguration(Composer composer, b
composer.GetExportedValueByTypeName<IDataProvider>(dataProviderTypeName),
composer.GetExportedValueByTypeName<IObjectStore>(objectStoreTypeName),
composer.GetExportedValueByTypeName<IDataPermissionManager>(dataPermissionManager),
liveMode,
Globals.LiveMode,
researchMode
);

Expand All @@ -231,7 +230,7 @@ public static LeanEngineAlgorithmHandlers FromConfiguration(Composer composer, b
$" and {typeof(LocalZipMapFileProvider)}, please update 'config.json'");
}

FundamentalService.Initialize(result.DataProvider, liveMode);
FundamentalService.Initialize(result.DataProvider, Globals.LiveMode);

return result;
}
Expand Down
1 change: 0 additions & 1 deletion Engine/LeanEngineSystemHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public static LeanEngineSystemHandlers FromConfiguration(Composer composer)
/// </summary>
public void Initialize()
{
Api.Initialize(Config.GetInt("job-user-id", 0), Config.Get("api-access-token", ""), Config.Get("data-folder"));
Notify.Initialize(new MessagingHandlerInitializeParameters(Api));
JobQueue.Initialize(Api);
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/Results/BaseResultsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ protected BaseResultsHandler()
AlgorithmId = "";
ChartLock = new object();
LogStore = new List<LogEntry>();
ResultsDestinationFolder = Config.Get("results-destination-folder", Directory.GetCurrentDirectory());
ResultsDestinationFolder = Globals.ResultsDestinationFolder;
State = new Dictionary<string, string>
{
["StartTime"] = StartTime.ToStringInvariant(DateFormat.UI),
Expand Down
8 changes: 3 additions & 5 deletions Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,14 @@ static void Main(string[] args)
Config.MergeCommandLineArgumentsWithConfiguration(LeanArgumentParser.ParseArguments(args));
}

var liveMode = Config.GetBool("live-mode");
//Name thread for the profiler:
Thread.CurrentThread.Name = "Algorithm Analysis Thread";

Initializer.Start();
leanEngineSystemHandlers = Initializer.GetSystemHandlers();

//-> Pull job from QuantConnect job queue, or, pull local build:
string assemblyPath;
job = leanEngineSystemHandlers.JobQueue.NextJob(out assemblyPath);
job = leanEngineSystemHandlers.JobQueue.NextJob(out var assemblyPath);

leanEngineAlgorithmHandlers = Initializer.GetAlgorithmHandlers();

Expand Down Expand Up @@ -100,13 +98,13 @@ static void Main(string[] args)
Console.CancelKeyPress += new ConsoleCancelEventHandler(ExitKeyPress);

// Create the algorithm manager and start our engine
algorithmManager = new AlgorithmManager(liveMode, job);
algorithmManager = new AlgorithmManager(Globals.LiveMode, job);

leanEngineSystemHandlers.LeanManager.Initialize(leanEngineSystemHandlers, leanEngineAlgorithmHandlers, job, algorithmManager);

OS.Initialize();

var engine = new Engine.Engine(leanEngineSystemHandlers, leanEngineAlgorithmHandlers, liveMode);
var engine = new Engine.Engine(leanEngineSystemHandlers, leanEngineAlgorithmHandlers, Globals.LiveMode);
engine.Run(job, algorithmManager, assemblyPath, WorkerThread.Instance);
}
finally
Expand Down
25 changes: 10 additions & 15 deletions Queues/JobQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ public class JobQueue : IJobQueueHandler
private const string DefaultHistoryProvider = "SubscriptionDataReaderHistoryProvider";
private const string DefaultDataQueueHandler = "LiveDataQueue";
private const string DefaultDataChannelProvider = "DataChannelProvider";
private bool _liveMode = Config.GetBool("live-mode");
private static readonly string AccessToken = Config.Get("api-access-token");
private static readonly string Channel = Config.Get("data-channel");
private static readonly string OrganizationId = Config.Get("job-organization-id");
private static readonly int UserId = Config.GetInt("job-user-id", 0);
private static readonly int ProjectId = Config.GetInt("job-project-id", 0);
private readonly string AlgorithmTypeName = Config.Get("algorithm-type-name");
private Language? _language;

Expand Down Expand Up @@ -97,7 +92,7 @@ protected Language Language
/// </summary>
public void Initialize(IApi api)
{
//
api.Initialize(Globals.UserId, Globals.UserToken, Globals.DataFolder);
}

/// <summary>
Expand Down Expand Up @@ -157,7 +152,7 @@ public AlgorithmNodePacket NextJob(out string location)
var algorithmId = Config.Get("algorithm-id", AlgorithmTypeName);

//If this isn't a backtesting mode/request, attempt a live job.
if (_liveMode)
if (Globals.LiveMode)
{
var dataHandlers = Config.Get("data-queue-handler", DefaultDataQueueHandler);
var liveJob = new LiveNodePacket
Expand All @@ -169,10 +164,10 @@ public AlgorithmNodePacket NextJob(out string location)
DataQueueHandler = dataHandlers,
DataChannelProvider = Config.Get("data-channel-provider", DefaultDataChannelProvider),
Channel = Channel,
UserToken = AccessToken,
UserId = UserId,
ProjectId = ProjectId,
OrganizationId = OrganizationId,
UserToken = Globals.UserToken,
UserId = Globals.UserId,
ProjectId = Globals.ProjectId,
OrganizationId = Globals.OrganizationID,
Version = Globals.Version,
DeployId = algorithmId,
Parameters = parameters,
Expand Down Expand Up @@ -230,10 +225,10 @@ public AlgorithmNodePacket NextJob(out string location)
Algorithm = File.ReadAllBytes(AlgorithmLocation),
HistoryProvider = Config.Get("history-provider", DefaultHistoryProvider),
Channel = Channel,
UserToken = AccessToken,
UserId = UserId,
ProjectId = ProjectId,
OrganizationId = OrganizationId,
UserToken = Globals.UserToken,
UserId = Globals.UserId,
ProjectId = Globals.ProjectId,
OrganizationId = Globals.OrganizationID,
Version = Globals.Version,
BacktestId = algorithmId,
Language = Language,
Expand Down
Loading

0 comments on commit 2a82c6f

Please sign in to comment.