SetHoldings is a useful function for protyping to not have to care about the particulars of orders until later, but when making a lot of trades with it one backtest gets stuck in slow motion.

My suspicion is that CalculateOrderQuantity is the culprit, because this issue is greater with FX it seems (high quantities). See implementation:

// compute the initial order quantity var orderQuantity = (int)(targetOrderValue / unitPrice); var iterations = 0; do { // decrease the order quantity if (iterations > 0) { // if fees are high relative to price, we reduce the order quantity faster if (feeToPriceRatio > 0) orderQuantity -= feeToPriceRatio; else orderQuantity--; } // generate the order var order = new MarketOrder(security.Symbol, orderQuantity, UtcTime); orderValue = order.GetValue(security); orderFees = security.FeeModel.GetOrderFee(security, order); feeToPriceRatio = (int)(orderFees / unitPrice); // calculate the margin required for the order marginRequired = security.MarginModel.GetInitialMarginRequiredForOrder(security, order); iterations++; } while (orderQuantity > 0 && (marginRequired > marginRemaining || orderValue + orderFees > targetOrderValue));

Has anyone else had this issue? Did anyone attempt to optimize it already?  I suppose it could also be the sheer amount of orders that is the issue, but I don't think so after trying to reduce the call frequency to SetHoldings to rather small values.

Attached example where I ripped out a bunch of stuff from a project with the issue to provide a reasonably small test case.