17 using Newtonsoft.Json;
27 using System.Collections.Generic;
30 using System.Reflection;
40 private static readonly TextWriter Console = System.Console.Out;
42 private const string PaperBrokerageTypeName =
"PaperBrokerage";
43 private const string DefaultHistoryProvider =
"SubscriptionDataReaderHistoryProvider";
44 private const string DefaultDataQueueHandler =
"LiveDataQueue";
45 private const string DefaultDataChannelProvider =
"DataChannelProvider";
47 private readonly
string AlgorithmTypeName =
Config.
Get(
"algorithm-type-name");
57 if (_language ==
null)
59 string algorithmLanguage =
Config.
Get(
"algorithm-language");
60 if (
string.IsNullOrEmpty(algorithmLanguage))
62 var extension = Path.GetExtension(AlgorithmLocation).ToLower();
72 throw new ArgumentException($
"Unknown extension, algorithm extension was {extension}");
77 _language = (
Language)Enum.Parse(typeof(
Language), algorithmLanguage, ignoreCase:
true);
89 private string AlgorithmLocation {
get; } =
Config.
Get(
"algorithm-location",
"QuantConnect.Algorithm.CSharp.dll");
109 x.FullName !=
null &&
110 x.FullName.EndsWith(dataQueueHandler, StringComparison.InvariantCultureIgnoreCase) &&
113 if (dataQueueHandlerType !=
null)
116 brokerageFactory = (
BrokerageFactory)Activator.CreateInstance(attribute.Type);
118 return brokerageFactory;
127 algorithmPath = GetAlgorithmLocation();
129 Log.
Trace($
"JobQueue.NextJob(): Selected {algorithmPath}");
132 var parameters =
new Dictionary<string, string>();
134 var parametersConfigString =
Config.
Get(
"parameters");
135 if (!
string.IsNullOrEmpty(parametersConfigString))
137 parameters = JsonConvert.DeserializeObject<Dictionary<string, string>>(parametersConfigString);
142 MinuteLimit =
Config.
GetInt(
"symbol-minute-limit", 10000),
143 SecondLimit =
Config.
GetInt(
"symbol-second-limit", 10000),
145 RamAllocation =
int.MaxValue,
146 MaximumDataPointsPerChartSeries =
Config.
GetInt(
"maximum-data-points-per-chart-series", 1000000),
147 MaximumChartSeries =
Config.
GetInt(
"maximum-chart-series", 30),
148 StorageLimit =
Config.GetValue(
"storage-limit", 10737418240L),
149 StorageFileCount =
Config.
GetInt(
"storage-file-count", 10000),
153 var algorithmId =
Config.
Get(
"algorithm-id", AlgorithmTypeName);
158 var dataHandlers =
Config.
Get(
"data-queue-handler", DefaultDataQueueHandler);
162 Algorithm =
File.ReadAllBytes(AlgorithmLocation),
164 HistoryProvider =
Config.
Get(
"history-provider", DefaultHistoryProvider),
165 DataQueueHandler = dataHandlers,
166 DataChannelProvider =
Config.
Get(
"data-channel-provider", DefaultDataChannelProvider),
173 DeployId = algorithmId,
174 Parameters = parameters,
177 PythonVirtualEnvironment =
Config.
Get(
"python-venv"),
181 Type brokerageName =
null;
187 liveJob.BrokerageData = brokerageFactory.BrokerageData;
189 catch (Exception err)
191 Log.
Error(err, $
"Error resolving BrokerageData for live job for brokerage {liveJob.Brokerage}");
194 var brokerageBasedHistoryProvider = liveJob.HistoryProvider.DeserializeList().Select(x =>
197 return brokerageName;
198 }).Where(x => x !=
null);
200 foreach (var dataHandlerName
in dataHandlers.DeserializeList().Concat(brokerageBasedHistoryProvider).Distinct())
203 if (brokerageFactoryForDataHandler ==
null)
205 Log.
Trace($
"JobQueue.NextJob(): Not able to fetch brokerage factory with name: {dataHandlerName}");
208 if (brokerageFactoryForDataHandler.BrokerageType == brokerageName)
213 foreach (var data
in brokerageFactoryForDataHandler.BrokerageData)
215 if (data.Key ==
"live-holdings" || data.Key ==
"live-cash-balance")
221 liveJob.BrokerageData.TryAdd(data.Key, data.Value);
227 var optimizationId =
Config.
Get(
"optimization-id");
232 Algorithm =
File.ReadAllBytes(AlgorithmLocation),
233 HistoryProvider =
Config.
Get(
"history-provider", DefaultHistoryProvider),
240 BacktestId = algorithmId,
242 Parameters = parameters,
244 PythonVirtualEnvironment =
Config.
Get(
"python-venv"),
248 var outOfSampleMaxEndDate =
Config.
Get(
"out-of-sample-max-end-date");
249 if (!
string.IsNullOrEmpty(outOfSampleMaxEndDate))
251 backtestJob.OutOfSampleMaxEndDate =
Time.
ParseDate(outOfSampleMaxEndDate);
253 backtestJob.OutOfSampleDays =
Config.
GetInt(
"out-of-sample-days");
256 if (!optimizationId.IsNullOrEmpty())
258 backtestJob.OptimizationId = optimizationId;
268 private string GetAlgorithmLocation()
272 if (!
File.Exists(AlgorithmLocation))
274 throw new FileNotFoundException($
"JobQueue.TryCreatePythonAlgorithm(): Unable to find py file: {AlgorithmLocation}");
278 var pythonFile =
new FileInfo(AlgorithmLocation);
282 return AlgorithmLocation;
292 Console.WriteLine(
"Engine.Main(): Analysis Complete.");
296 Console.WriteLine(
"Engine.Main(): Press any key to continue.");
297 System.Console.Read();