What is the goal? Are you trying to get the backtest to always fill your orders, OR, are you trying to create code which would result in 'real life' orders always being 100% filled, OR, do you want to create code which deals with partial fills? If the latter, how long are you willing to wait for an order to fill? 1 minute? 5 Minutes? End of day?
If you simply want your code to run and have the backtester always 100% fill your order then set the slippage model to FixedSlippage
. See https://www.quantopian.com/help#ide-slippage .
set_slippage(slippage.FixedSlippage(spread=0.0))
Using limit orders won't solve the problem. In fact, limit orders may exacerbate the problem by placing a price constraint on the order. Using a market order doesn't place any constraints.
It would certainly help if the trade size was limited. This goes back to the question of how long are you willing to wait for your order to fill. If the average minute volume of a security is 1000 shares, the default slippage model will use 2.5% of that to fill your order (ie 25 shares). If you wanted to buy 100 shares it would take, on average, 4 minutes to fill (in 25 share increments). Note that the past volume, just like price, doesn't guarantee the future volume so it may be more or less than this.
If, you simply don't want to have partial orders hanging out there for some reason, then yes, use the 'cancel_order' method. You may want to check the amount actually filled first. You probably wouldn't want to cancel an order for 100 shares if 1 share had already been filled.
The bottom line is you can tell the backtester to always fill your orders, but this probably isn't realistic in the real world. In the real world, the only way to guarantee an order is filled 100% is issuing a 'All or None' order with your broker. However, not all brokers allow this type of order and it's not supported in live trading via Quantopian through either Robinhood or IB. It also could result in paying a premium price for the security.