19 using Newtonsoft.Json;
26 using System.Collections.Generic;
58 algorithm?.SetDeploymentTarget(job.DeploymentTarget);
60 Log.
Trace($
"BaseSetupHandler.Setup({job.DeploymentTarget}): UID: {job.UserId.ToStringInvariant()}, " +
61 $
"PID: {job.ProjectId.ToStringInvariant()}, Version: {job.Version}, Source: {job.RequestSource}"
80 IReadOnlyCollection<string> currenciesToUpdateWhiteList =
null)
87 Func<Cash, bool> cashToUpdateFilter = currenciesToUpdateWhiteList ==
null
88 ? (x) => x.CurrencyConversion !=
null && x.ConversionRate == 0
89 : (x) => currenciesToUpdateWhiteList.Contains(x.Symbol);
92 var securitiesToUpdate = cashToUpdate
93 .SelectMany(x => x.CurrencyConversion.ConversionRateSecurities)
98 var historyRequests =
new List<HistoryRequest>();
99 foreach (var security
in securitiesToUpdate)
101 var configs = algorithm
103 .SubscriptionDataConfigService
104 .GetSubscriptionDataConfigs(security.Symbol,
105 includeInternalConfigs:
true);
109 var configToUse = configs.OrderBy(x => x.TickType).First();
110 var hours = security.Exchange.Hours;
112 var resolution = configs.GetHighestResolution();
113 var startTime = historyRequestFactory.GetStartTimeAlgoTz(
118 configToUse.DataTimeZone,
120 var endTime = algorithm.Time;
122 historyRequests.Add(historyRequestFactory.CreateHistoryRequest(
126 security.Exchange.Hours,
131 var slices = algorithm.HistoryProvider.GetHistory(historyRequests, algorithm.TimeZone);
132 slices.PushThrough(data =>
134 foreach (var security
in securitiesToUpdate.Where(x => x.Symbol == data.Symbol))
136 security.SetMarketPrice(data);
140 foreach (var cash
in cashToUpdate)
146 var unassignedCash = cashToUpdate.Where(x => x.ConversionRate == 0).ToList();
147 if (unassignedCash.Any())
150 $
"Failed to assign conversion rates for the following cash: {string.Join(",
", unassignedCash.Select(x => x.Symbol))}." +
151 $
" Attempting to request daily resolution history to resolve conversion rate");
153 var unassignedCashSymbols = unassignedCash
154 .SelectMany(x => x.SecuritySymbols)
157 var replacementHistoryRequests =
new List<HistoryRequest>();
158 foreach (var request
in historyRequests.Where(x =>
159 unassignedCashSymbols.Contains(x.Symbol) && x.Resolution <
Resolution.Daily))
161 var newRequest =
new HistoryRequest(request.EndTimeUtc.AddDays(-10), request.EndTimeUtc,
163 request.Symbol,
Resolution.Daily, request.ExchangeHours, request.DataTimeZone,
164 request.FillForwardResolution,
165 request.IncludeExtendedMarketHours, request.IsCustomData, request.DataNormalizationMode,
168 replacementHistoryRequests.Add(newRequest);
171 slices = algorithm.HistoryProvider.GetHistory(replacementHistoryRequests, algorithm.TimeZone);
172 slices.PushThrough(data =>
174 foreach (var security
in securitiesToUpdate.Where(x => x.Symbol == data.Symbol))
176 security.SetMarketPrice(data);
180 foreach (var cash
in unassignedCash)
186 Log.
Trace($
"BaseSetupHandler.SetupCurrencyConversions():{Environment.NewLine}" +
187 $
"Account Type: {algorithm.BrokerageModel.AccountType}{Environment.NewLine}{Environment.NewLine}{algorithm.Portfolio.CashBook}");
189 algorithm.Portfolio.LogMarginInformation();
200 return isolator.ExecuteWithTimeLimit(TimeSpan.FromMinutes(5),
202 DebuggerHelper.Initialize(algorithmNodePacket.Language, out var workersInitializationCallback);
204 if(workersInitializationCallback != null)
207 WeightedWorkScheduler.Instance.AddSingleCallForAll(workersInitializationCallback);
210 algorithmNodePacket.RamAllocation,
211 sleepIntervalMillis: 100,
212 workerThread: workerThread);
227 kvp.Value.SetAmount(0);
252 var dataFeedsConfigString =
Config.
Get(
"security-data-feeds");
254 if (!dataFeedsConfigString.IsNullOrEmpty())
256 var dataFeeds = JsonConvert.DeserializeObject<Dictionary<SecurityType, List<TickType>>>(dataFeedsConfigString);
273 if (algorithm ==
null)
275 throw new ArgumentNullException(nameof(algorithm));