Interactive Brokers has a setting to choose which positions to liquidate last on a margin call. Is it possible to configure this in a QuantConnect algortihm for both a backtest and live trading? Essentially, what I'd like to do is have some control over which positions get liquidated in the event of a margin call.
Shile Wen
Hi Mirza,
We have an OnMarginCall method for this purpose.
Best,
Shile Wen
Mirza1238u4353
Oh, can I use OnMarginCall to choose what gets sold? How do I do that? I was under the impression that it gives me a chance to know what the engine chose to sell for me, but I didn't realize that I could act on it.
Jared Broad
You can either write your own MarginCallModel (your original question) to chose the "requested orders for liquidation" or you can handle the order requests which come through the OnMarginCall event handler. The engine liquidates the requests in order to get back to meet the minimum margin requirements again.Â
This is on the more advanced side of the engine -- so kudos =D But apologies it's not documented. Setting a MarginCallModel is on the portfolio object; DefaultMarginCallModel,Â
Â
Â
Example of using the user-layer event handler:
https://github.com/QuantConnect/Lean/blob/master/Algorithm.CSharp/MarginCallEventsAlgorithm.csÂ
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.
Mirza1238u4353
Thanks! How does this work in live trading? Does IB notify QC and give it a chance to run the OnMarginCall handler? Does the initial OnMarginCall request list reflect IB's selection? Or is QC trying to anticipate a margin call before IB (and is there a race?)? If I setup my own MarginCallModel, does that interact with live trading in some way?
Jared Broad
>Or is QC trying to anticipate a margin call before IB
This one -- we're trying to make our fictional models as realistic as possible. There's no race as IB is very slow at issuing margin calls and updating their portfolio margin math in general. In live-trading, the portfolio is updated at least minutely whether you're using hour-daily data or not. I think there's a "non-zero" chance of a race but we've never seen it happen in reality.
If that were to happen it would be an untracked order event -- when an order event comes in which isn't tracked by LEAN the default behavior is to shutdown.
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.
Mirza1238u4353
Does that mean that if my live Interactive Brokers account uses Portfolio Margin (which QuantConnect doesn't model as far as I know), then QuantConnect will model Reg-T and margin call me when my broker would not (because Portfolio Margin gives me lower maintenance margin requirements than Reg-T margin)?
Also, what happens to QuantConnect's model in live trading when my broker increases margin requirements? E.g. today Interactive Brokers announced that they're gradually increasing maintenance margin by 35% before the election. Will QuantConnect reflect that increase by updating the model each day? Or will QuantConnect use a stale model - which means I would get margin called by the broker before QuantConnect would realize it?
I'm experimenting with an algorithm which uses high leverage, and I want to track Maintenance Margin requirements / Excess Liquidity and adjust my holdings dynamically. It seems to me that I can't really use QuantConnect for this, because I have no way of pulling the true data from the broker, I can only try to reproduce the broker's margin model and hope it matches. Is that right? There's no way in QuantConnect to query the broker for the account's true Maintenance Margin and Excess Liquidity?
Jared Broad
You should be able to use LEAN just fine. You just might need to create some custom models.Â
Generally speaking, I'd recommend thinking of LEAN as a starting point with plugins at all the places people need specialization. The default models handle 80% of people's requirements and the models provide an extension for the remaining 20% of users. Quants are an incredibly diverse bunch and the only way to support the 6M strategies people have built on QC is to provide a lot of flexibility!
If are sensitive to the limits of margin then I'd recommend just disabling the margin call models we provide and leaning on IB's margin calls. This is the NullMarginCallModel which disables the LEAN margin calls.Â
The only pending todo is the margin model plugin point which is on the todo tomorrow (lucky timing!). This hasn't been a priority since you can bypass it by setting security.SetLeverage(1000) and you'll have infinite margin.Â
Â
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.
Mirza1238u4353
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!