In OnData, I set a LimitOrder for a limit price of 40. The current Close price is 63.40.

I'm calculating the quantity with this formula: Portfolio.Cash * 0.9 / 40. So, I'm trying to put a limit order that will be worth 90% of my current available cash when it will be executed at the limit price.

But I get this error: 

Backtest Handled Error: Order Error: id: 1, Insufficient buying power to complete order (Value:90000), Reason: Id: 1, Initial Margin: 142661.25, Free Margin: 100000

When I divide the number from Initial Margin (142661.25) by the calculated quantity of 2250, I get exactly 63.405, which is the Close price of the finished bar.

Is this a bug? Why would I need margin based on the current price?