Here are some modifications that may fix the problem:
for i, sid in enumerate(stocks):
if get_open_orders(sid):
pass
else:
order_target_value(sid, -W[i])
I also added code that should cancel all orders at the close, which is what Quantopian does for real-money trading (and presumably paper trading, as well) with Interactive Brokers.
schedule_function(func=cancel_everything,date_rule=date_rules.every_day(),
time_rule=time_rules.market_close())
def cancel_everything(context,data):
"""
Cancels all open orders for all sids.
"""
all_open_orders = get_open_orders()
if all_open_orders:
for security, oo_for_sid in all_open_orders.iteritems():
for order_obj in oo_for_sid:
log.info("%s: Cancelling order for %s of %s created on %s" %
(get_datetime(), order_obj.amount,
security.symbol, order_obj.created))
cancel_order(order_obj)
I lifted the function to cancel all orders from https://www.quantopian.com/posts/how-to-cancel-all-open-orders (see John Ricklefs posted Feb 19, 2014).
One thing to keep in mind is that there is the slippage defined by the slippage model, but there is also an inherent slippage, in that orders are filled only when an actual historical trade occurs (i.e. when there was a market for the security). Reportedly, this assumption falls apart for certain thinly traded ETFs, through a creation/redemption mechanism, which I don't pretend to understand fully.
Grant