I have setup a  backtest using IB model.

My algorithm is designed to use 2x leverage. 

In an attempt to give myself more control and prevent a margin call, in the OnMarginCallWarning() method I try to liquidate a long position of my choice, but I get an error in


What I don't understand is why doesn't this order go through fine.

I would have expected that when selling a long position, no margin check would be required.

If selling short margin would be required, but selling a long position should always go through.

Am I misunderstanding how margin accounts work? Or is this a corner case that needs to be updated in the lean engine?