18 using System.Collections.Concurrent;
19 using System.Diagnostics;
32 private readonly
string _leanLocation;
33 private readonly
string _rootResultDirectory;
34 private readonly
string _extraLeanArguments;
35 private readonly ConcurrentDictionary<string, Process> _processByBacktestId;
43 _processByBacktestId =
new ConcurrentDictionary<string, Process>();
45 _rootResultDirectory = Configuration.Config.Get(
"results-destination-folder",
46 Path.Combine(Directory.GetCurrentDirectory(), $
"opt-{nodePacket.OptimizationId}"));
47 Directory.CreateDirectory(_rootResultDirectory);
49 _leanLocation = Configuration.Config.Get(
"lean-binaries-location",
50 Path.Combine(Directory.GetCurrentDirectory(),
"../../../Launcher/bin/Debug/QuantConnect.Lean.Launcher"));
52 var closeLeanAutomatically = Configuration.Config.GetBool(
"optimizer-close-automatically",
true);
53 _extraLeanArguments = $
"--close-automatically {closeLeanAutomatically}";
55 var algorithmTypeName = Configuration.Config.Get(
"algorithm-type-name");
56 if (!
string.IsNullOrEmpty(algorithmTypeName))
58 _extraLeanArguments += $
" --algorithm-type-name \"{algorithmTypeName}\"";
61 var algorithmLanguage = Configuration.Config.Get(
"algorithm-language");
62 if (!
string.IsNullOrEmpty(algorithmLanguage))
64 _extraLeanArguments += $
" --algorithm-language \"{algorithmLanguage}\"";
67 var algorithmLocation = Configuration.Config.Get(
"algorithm-location");
68 if (!
string.IsNullOrEmpty(algorithmLocation))
70 _extraLeanArguments += $
" --algorithm-location \"{algorithmLocation}\"";
82 var backtestId = Guid.NewGuid().ToString();
85 var resultDirectory = Path.Combine(_rootResultDirectory, backtestId);
86 Directory.CreateDirectory(resultDirectory);
89 var startInfo =
new ProcessStartInfo
91 FileName = _leanLocation,
92 WorkingDirectory = Directory.GetParent(_leanLocation).FullName,
93 Arguments = $
"--results-destination-folder \"{resultDirectory}\" --algorithm-id \"{backtestId}\" --optimization-id \"{optimizationId}\" --parameters {parameterSet} --backtest-name \"{backtestName}\" {_extraLeanArguments}",
94 WindowStyle = ProcessWindowStyle.Minimized
97 var process =
new Process
99 StartInfo = startInfo,
100 EnableRaisingEvents =
true
102 _processByBacktestId[backtestId] = process;
104 process.Exited += (sender, args) =>
112 _processByBacktestId.TryRemove(backtestId, out process);
113 var backtestResult = $
"{backtestId}.json";
114 var resultJson = Path.Combine(_rootResultDirectory, backtestId, backtestResult);
115 NewResult(File.Exists(resultJson) ? File.ReadAllText(resultJson) :
null, backtestId);
116 process.DisposeSafely();
131 if (_processByBacktestId.TryRemove(backtestId, out process))
134 process.DisposeSafely();
148 var message = $
"ConsoleLeanOptimizer.SendUpdate(): {currentEstimate} {string.Join(",
", stats.Select(pair => $"{pair.Key}:{pair.Value}
"))}";
149 var currentBestBacktest =
Strategy.Solution;
150 if (currentBestBacktest !=
null)
152 message += $
". Best id:'{currentBestBacktest.BacktestId}'. {OptimizationTarget}. Parameters ({currentBestBacktest.ParameterSet})";