Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Ways to avoid partially filled orders

Dear Quantopians,

I am developing an algo and ran into an issue with partial fill of orders when doing backtesting (I guess in the real trading one can order "fill or kill" order type but not in backtesting).

I will try to place not default market orders, but rather limit orders with some 'breathing space' for order price to allow full fill. I have also read that some solutions are: (1) adjust trade size to available volume and (2) call cancel_order() function.

Any other ideas how to secure full order fill?

3 responses

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.

Dan, thanks for very helpful post.

To your questions. I am trying to backtest using a small and realistic (for my foreseeable future) deposit (~$35,000-$50,000) and I want to achieve the following:

(1) be quick at entry/exit (2) ideally, fully fill an order during 1 min post signal triggering (during the next 1 min bar) (3) if full order fill is impossible during the 1 min post signal bar, even partial fill is OK unless it gets filled with 'odd lots' (97 instead of 100 shares) b/c I would like to be liquid at exit which is usually better when a number of shares is a multiplier by 10s or 100s.

Set_slippage is a good method to play.
Using only most liquid stock universe, round lot sizes (10s, 100s) and a limit order price a bit offset to the current price should help as well

Also thinking of tracking real size of a position for a given asset to get it rounded asap (97 shares after one min - get another 3 to make 100 and stop)

Any further feedback on the above ideas would be appreciated.

For quick in and quick out then you are on the right track to limit your trading universe to those stocks with high average dollar-volume per minute. Figuring out that threshold would probably be by trial and error. Using full lot sizes can potentially help in live trading (but not in the backtester). In both the backtester and real life however there is no guarantee that your order will fill complete.

Not sure I'd bother to cancel and round up the order. Just leave the existing order active. It will get the remaining shares as needed.

Not sure exactly how much you want to invest on each trade but there may not be a lot of stocks out there that meet your criteria of guaranteed fill in the next minute. Attached is a notebook with an estimate. Assume $50k trade and a guess that you may not want to trade more than 2.5% of the average (for fear of impacting price). Only about 25 stocks meet that criteria. However, if you spread the total dollars across several trades this number increases.