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,