This returns available cash for buying. It includes cash needed to take care of any pending buy orders, even partial fills.
Note this doesn't take shorting into account.
def cash_calc(context, data): # Calculate free cash for buying (not tied up in pending orders)
cash_tied_up = 0 # Tied up in current open orders
for orders in get_open_orders().values():
for o in orders:
if o.amount < 0: continue # Skip sell and short sell
if not data.can_trade(o.sid): continue
cash_tied_up += (o.amount - o.filled) * data.current(o.sid, 'price')
return max(0, context.portfolio.cash - cash_tied_up)