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.
Petter Hansson
Judging by the quiet I decided to go fix it. See code in attached backtest. My problem backtests run way faster with this (as in minuts rather than several hours).
To use the code attached, you can inherit from FixedSetHoldingsAlgorithm rather than QCAlgorithm and call FixedSetHoldings rather than SetHoldings. What the fix does is to replace that linear time loop with a binary search. It assumes monotonic order value/fee models, similar with margin requirements, but from what I can see the original code also makes this assumption.
Of course, it would be preferable to integrate the fix in Lean proper (don't got a Git branch right now as I'm on SVN, but plan to switch).
Jared Broad
Awesome Petter, nice work! Is the logic that for FX assets it is slow since FX quantities are higher? We will try to tidy and pull this into LEAN. If possible please try making a PR so your name will be listed on the contributors page :)
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Jared Broad
Posted issue here: https://github.com/QuantConnect/Lean/issues/590
Potentially you can use the symbol lot size to fast track the search.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Petter Hansson
Yeah, my understanding is you really start to note this with large quantity orders (more likely in FX). Will look at possibility of making formal PRs in the future, for now feel free to manually include.
Petter Hansson
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!