24 using System.Collections.Generic;
44 public List<Exception>
Errors {
get;
set; }
77 Errors =
new List<Exception>();
102 var complete = loader.TryCreateAlgorithmInstanceWithIsolator(assemblyPath, algorithmNodePacket.
RamAllocation, out algorithm, out error);
103 if (!complete)
throw new AlgorithmSetupException($
"During the algorithm initialization, the following exception has occurred: {error}");
132 throw new ArgumentException(
"Expected BacktestNodePacket but received " + parameters.
AlgorithmNodePacket.GetType().Name);
137 if (algorithm ==
null)
143 algorithm.Name = job.Name;
146 if (job.PeriodStart ==
default(DateTime))
152 var controls = job.Controls;
154 var initializeComplete = isolator.ExecuteWithTimeLimit(TimeSpan.FromMinutes(5), () =>
158 parameters.ResultHandler.SendStatusUpdate(AlgorithmStatus.Initializing,
"Initializing algorithm...");
160 algorithm.SetParameters(job.Parameters);
161 algorithm.SetAvailableDataTypes(BaseSetupHandler.GetConfiguredDataFeeds());
164 algorithm.SetAlgorithmMode(job.AlgorithmMode);
167 algorithm.Schedule.SetEventSchedule(parameters.RealTimeHandler);
170 algorithm.SetOptionChainProvider(new CachingOptionChainProvider(new BacktestingOptionChainProvider(parameters.DataCacheProvider, parameters.MapFileProvider)));
173 algorithm.SetFutureChainProvider(new CachingFutureChainProvider(new BacktestingFutureChainProvider(parameters.DataCacheProvider)));
176 BaseSetupHandler.LoadBacktestJobAccountCurrency(algorithm, job);
179 algorithm.Initialize();
182 if (job.PeriodStart.HasValue)
184 algorithm.SetStartDate(job.PeriodStart.Value);
186 if (job.PeriodFinish.HasValue)
188 algorithm.SetEndDate(job.PeriodFinish.Value);
191 if(job.OutOfSampleMaxEndDate.HasValue)
193 if(algorithm.EndDate > job.OutOfSampleMaxEndDate.Value)
195 Log.Trace($
"BacktestingSetupHandler.Setup(): setting end date to {job.OutOfSampleMaxEndDate.Value:yyyyMMdd}");
196 algorithm.SetEndDate(job.OutOfSampleMaxEndDate.Value);
198 if (algorithm.StartDate > algorithm.EndDate)
200 algorithm.SetStartDate(algorithm.EndDate);
206 BaseSetupHandler.LoadBacktestJobCashAmount(algorithm, job);
209 BaseSetupHandler.SetBrokerageTradingDayPerYear(algorithm);
212 algorithm.PostInitialize();
214 catch (Exception err)
218 }, controls.RamAllocation,
219 sleepIntervalMillis: 100,
229 if (!initializeComplete)
return false;
231 MaximumRuntime = TimeSpan.FromMinutes(job.Controls.MaximumRuntimeMinutes);
233 BaseSetupHandler.SetupCurrencyConversions(algorithm, parameters.UniverseSelection);
237 MaxOrders = job.Controls.BacktestingMaxOrders;
244 Log.
Trace(
"SetUp Backtesting: User: " + job.UserId +
" ProjectId: " + job.ProjectId +
" AlgoId: " + job.AlgorithmId);
245 Log.
Trace($
"Dates: Start: {algorithm.StartDate.ToStringInvariant("d
")} " +
246 $
"End: {algorithm.EndDate.ToStringInvariant("d
")} " +
247 $
"Cash: {StartingPortfolioValue.ToStringInvariant("C
")} " +
248 $
"MaximumRuntime: {MaximumRuntime} " +
249 $
"MaxOrders: {MaxOrders}");
251 return initializeComplete;