31 using System.Collections.Generic;
54 private IEnumerable<Slice> _conversionSlices =
new List<Slice>();
80 historyProvider.Initialize(
new HistoryProviderInitializeParameters(
null,
null,
null, _cacheProvider, mapFileProvider, factorFileProvider, (_) => { },
false, dataPermissionManager,
Algorithm.
ObjectStore,
Algorithm.
Settings));
85 var feed =
new MockDataFeed();
95 symbolPropertiesDataBase,
100 dataPermissionManager,
107 dataPermissionManager);
111 symbolPropertiesDataBase,
133 _resultHandler.
Initialize(
new (job,
new Messaging.Messaging(),
new Api.Api(), transactions, mapFileProvider));
139 feed.Initialize(
Algorithm, job, _resultHandler,
null,
null,
null, _dataManager,
null,
null);
148 if (conversionSecurities.Any())
151 _conversionSlices = GetHistory(
Algorithm, conversionSecurities, resolution);
161 _cacheProvider.DisposeSafely();
162 _resultHandler.
Exit();
173 private static IEnumerable<Slice> GetHistory(
IAlgorithm algorithm, List<Security> securities,
Resolution resolution)
175 var historyRequests =
new List<Data.HistoryRequest>();
179 foreach (var security
in securities)
187 var configToUse = configs.OrderBy(x => x.TickType).First();
189 var startTime = historyRequestFactory.GetStartTimeAlgoTz(
193 security.Exchange.Hours,
194 configToUse.DataTimeZone,
196 var endTime = algorithm.
EndDate;
198 historyRequests.Add(historyRequestFactory.CreateHistoryRequest(
202 security.Exchange.Hours,
218 public static IEnumerable<Slice>
GetHistory(List<Symbol> symbols, DateTime start, DateTime end,
Resolution resolution)
222 var securities =
new List<Security>();
224 looper.Algorithm.SetStartDate(start);
225 looper.Algorithm.SetEndDate(end);
227 foreach (var symbol
in symbols)
229 var configs = looper.Algorithm.SubscriptionManager.SubscriptionDataConfigService.Add(symbol, resolution,
false,
false);
230 securities.Add(looper.Algorithm.Securities.CreateSecurity(symbol, configs));
233 return GetHistory(looper.Algorithm, securities, resolution);
248 if (!orders.Any() || equityCurve.IsEmpty)
254 var portfolioDeployments =
new List<List<Order>>();
258 var previousOrderId = 0;
259 var currentDeployment =
new List<Order>();
262 portfolioDeployments.Add(currentDeployment);
264 foreach (var order
in orders)
268 if (order.Id <= previousOrderId)
270 currentDeployment =
new List<Order>();
271 portfolioDeployments.Add(currentDeployment);
274 currentDeployment.Add(order);
275 previousOrderId = order.Id;
280 foreach (var deploymentOrders
in portfolioDeployments)
282 if (deploymentOrders.Count == 0)
284 Log.
Trace($
"PortfolioLooper.FromOrders(): Deployment contains no orders");
287 var startTime = deploymentOrders.First().Time;
288 var deployment = equityCurve.Where(kvp => kvp.Key <= startTime);
289 if (deployment.IsEmpty)
291 Log.
Trace($
"PortfolioLooper.FromOrders(): Equity series is empty after filtering with upper bound: {startTime}");
296 if (liveSeries && deploymentOrders.First().Time.Date == deploymentOrders.Last().Time.Date)
298 Log.
Trace(
"PortfolioLooper.FromOrders(): Filtering deployment because it has not been deployed for more than one day");
303 looper =
new PortfolioLooper(deployment.LastValue(), deploymentOrders, algorithmConfiguration: algorithmConfiguration);
305 foreach (var portfolio
in looper.ProcessOrders(deploymentOrders))
308 yield
return portfolio;
317 looper.DisposeSafely();
325 private IEnumerable<PointInTimePortfolio> ProcessOrders(IEnumerable<Order> orders)
328 foreach (var order
in orders)
333 DateTime lastFillTime;
335 if ((order.Type ==
OrderType.MarketOnOpen || order.Type ==
OrderType.MarketOnClose) &&
336 (order.Status ==
OrderStatus.Filled || order.Status ==
OrderStatus.PartiallyFilled) && order.LastFillTime ==
null)
338 lastFillTime = order.Time;
340 else if (order.LastFillTime ==
null)
342 Log.
Trace($
"Order with ID: {order.Id} has been skipped because of null LastFillTime");
347 lastFillTime = order.LastFillTime.Value;
350 var tick =
new Tick {
Quantity = order.Quantity, AskPrice = order.Price, BidPrice = order.Price, Value = order.Price, EndTime = lastFillTime };
357 Volume = order.Quantity,
361 Symbol = order.Symbol,
370 orderSecurity.SetMarketPrice(tradeBar);
371 orderSecurity.SetMarketPrice(tick);
377 if (baseCurrency !=
null)
380 var updateSlices = _conversionSlices.Where(x => x.Time <= order.Time).ToList();
383 if (updateSlices.Count != 0)
385 var updateSlice = updateSlices.Last();
387 foreach (var quoteBar
in updateSlice.QuoteBars.Values)
406 var orderEvent =
new OrderEvent(order, order.Time, Orders.Fees.OrderFee.Zero) { FillPrice = order.Price, FillQuantity = order.
Quantity };