I write a custom portfolio construction model, which has a dictionary that computes the weight of each target symbol, and return the normalized percentage for the targets
targets = []
for symbol, weight in self.symbol_weights.items():
targets.append(PortfolioTarget.percent(algorithm, symbol, weight * 0.98 / sum(self.symbol_weights.values())))
However, it has a lot of Backtest Handled Error: Order Error: ids: [112414], Insufficient buying power to complete orders (Value:[292394.1552]), Reason: Id: 112414, Initial Margin: 146220.972598494, Free Margin: 90860.1985049576491822.
Even times the 0.98 does not help. This does not make sense to me, do I miss some basic thing here? ImmediateExecutionModel is used following custom PCM
Mia Alissi
The error message you're seeing is related to buying power. It means that the algorithm doesn't have enough cash to execute the orders. The ImmediateExecutionModel places trades as soon as it receives the targets from the Portfolio Construction Model, but it doesn't check if there's enough buying power to execute these trades.
The weights you're calculating in your Portfolio Construction Model are used to determine the proportion of the portfolio's value that should be invested in each security. However, if the sum of these weights exceeds the available buying power, you'll get an "Insufficient buying power" error.
One thing you could try is to add a check in your Portfolio Construction Model to ensure that the sum of the weights doesn't exceed the portfolio's buying power. You can get the portfolio's buying power using the
algorithm.Portfolio.GetBuyingPower(symbol, OrderDirection.Buy)
method.Here's an example of how you might modify your code:
This code scales down the weights if their sum exceeds the portfolio's buying power, which should prevent the "Insufficient buying power" error.
Please note that this is just a suggestion and you might need to adjust the code to fit your specific needs.
Sources:
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.
Chuanjiang Luo
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!