Hi! I've been trying to improve the IB brokerage model, as it doesn't take into account slippage and leverage interests I want to add them to the model. I add a constant slippage manually in initialize and there is no problem but when I try to make a custom model and import it to the main code it runs but it doesn't change the results of the backtest so thats the fisrt issue. For the leverage interest costs I tried to reserve some liquid cash by manually setting requiredFreeBuyingPowerPercent in BuyingPowerModel as suggested in this thread but it gives me this error:
Backtest Handled Error: Order Error: id: 2869, Insufficient buying power to complete order (Value:142336.5897), Reason: Id: 2869, Initial Margin: 142339.744675525, Free Margin: 100000
Im working with leverage. Any help is appreciated, I attached the backtest & code.
Thank you in advance!
Fred Painchaud
Hi Renata,
I cloned your algo to test it. You only needed to use:
self.Securities[self.spy].MarginModel = BuyingPowerModel(2.0)
instead of:
self.Securities[self.spy].MarginModel = BuyingPowerModel(requiredFreeBuyingPowerPercent = 0.05)
So, I used the constructor with the "leverage" param and I setted it to 2.0. I don't have access to my dev computer (again) so I could not really understand the meaning of the params for the constructor you used… (by looking at the constructor's code, I would most likely understand what it is doing exactly with the params).
It works now. Basically, your leverage was not properly setting so your algo could not trade as your first trade costs more than the 100k you have in your account.
F
P.S. From my limited understanding, you could also use this instead:
self.Securities[self.spy].MarginModel = BuyingPowerModel(2.0, 0.05)
As I get it, that would set your leverage to 2x AND your required cash balance to always be above 5%, as you wanted it seems. I am still unsure it is 5% of what, like, 5% of 100k (your un-leveraged capital) or 5% of 200k…
Yes, just tested it and this second option also works. Same amount of trades so it looks the 5% is not reached anyway.
Renata
Thank you so much, Fred! I'm going to try it right now.
Fred Painchaud
Glad I could help! 👍
So, decided to read on margin account, initial margin, maintenance margin, etc. To go back to your code, you needed to feed all 3 params of the BuyingPowerModel, like this:
self.Securities[self.spy].MarginModel = BuyingPowerModel(0.5, 0.25, 0.05)
That would mean an initial margin of 50% (2:1 leverage), a maintenance margin of 25% (for instance), and minimal cash of 5%.
F
Renata
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!