Recently, I've encountered a problem with rebalancing using an IB account. Whenever a rebalance occurs, I get an error stating there's not enough cash. I think this is because the cash "required" is equal to the current equity holdings + the new purchase. For example:

I start with $125.

A stock is currently trading at $20. I previously bought and currently own 5 shares ($100 value). I now have $25 left in my account balance.

self.SetHoldings(sid, some_percent)

The SetHoldings method does a rebalance, to a total of 6 shares. This means it should only need to purchase one additional share for $20, requiring $20 in cash. However, the error received looks like this (prices changed to match example):

New Order Event: Time: 11/1/2017 1:40:12 PM OrderID: 3 Symbol: AGG Status: Invalid Message: 201 - Order rejected - reason:CASH AVAILABLE (SETTLED CASH): 25; CASH NEEDED FOR THIS ORDER AND OTHER PENDING ORDERS: 120. Origin: IBPlaceOrder: STK AGG USD Smart

It appears that the cash requirement is set to all 6 shares, even though 5 are already bought.

Of course, in real life the stock would change in value from $20. This is only fixed in the example for simplicity.

This does not happen if positions are first liquidated.

Any ideas on how to fix this problem?