Hi,
I'm attempting to run LEAN locally, and when I connect to an existing live/paper trading account's portfolio, LEAN loads the existing positions, but, as my portfolio contains different types of assets (forex, equities, options, etc.) the margin leverage calculation done by LEAN on them is wrong (it assumes a leverage of 1.0 for all securities in the portfolio).
I would have expected LEAN to fetch IBKR's margin account rules (i.e. equities can have 2-3.33x leverage while forex has 50x and options have lower requirements).
This makes it so that the remaining margin calculation in LEAN is erroneous, and the discrepancy can be observed by printing it and comparing it to the account window in TWS.
Here are screenshots of the account window on a toy paper account where I bought random positions ( the small differences are due to the timing before i could switch from the LEAN engine to TWS to visualize the information).
https://ibb.co/bJSFMGd
https://ibb.co/vjM2T2N
https://ibb.co/zQ40GKJ
The following code printed the following lines:
def OnData(self, data):
self.Debug(self.Portfolio.MarginRemaining)
self.Debug(self.Portfolio.TotalPortfolioValue)
self.Debug(self.Portfolio.TotalMarginUsed)
# output:
# 885849.407553494
# 900860.566092334
# 15011.1585388406
One can clearly see that Margin used: 15.011K makes no sense with a position of 25K in EURUSD (leverage is 50x as calculated by IBKR account window).
Is there something I am missing? I could solve this by manually setting the leverage in the positions with a for loop around the Portfolio securities, but could not get LEAN to do it automatically on startup.
Best,
Derek Melchin
Hi Marc,
We were unable to reproduce the issue. To test it, we allocated part of an IB paper trading account to EURUSD. We then deployed the attached algorithm (after commenting out line 11) and the logs showed:
Leverage: 50.0 Margin ratio: 50.002511910642966
If the issue continues to occur, we recommend setting the leverage of each security in the OnSecuritiesChanged method.
def OnSecuritiesChanged(self, changes): for security in cahnges.AddedSecurities: if security.Type == SecurityType.Forex: security.SetLeverage(50)
Best,
Derek Melchin
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.
Marc Demers
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!