Back to Community
Limit Orders Not Backtesting Properly?

Hello all,

Is there anything I can do to make sure limit orders are being filled properly in backtesting?

Here is a link to a screenshot:

https://pbs.twimg.com/media/CbiSJmGVAAADA3s.png:large

Many of my limit (buy) orders are being filled above the limit, and this is happening with sell orders too. Backtesting is useless without being able to accurately plot reasonable entries and exits!

Thank you for your help!

5 responses

bump?

There's a known bug in zipline whereby the slippage model is applied after order trigger for both market and limit orders. No idea on ETA for fix.

Is this actually a bug, or simply that by design, there is built in slippage in that the next available minutely closing price is used?

Definitely a bug. https://www.quantopian.com/posts/limit-order-execution-at-varying-prices

If I want to buy limit 10.00, that is the most that I will pay (+ commission). The way it currently works is that if the price is > limit, the order is filled at limit + slippage, which is not correct.

Looking at class VolumeShareSlippage(SlippageModel) on https://github.com/quantopian/zipline/blob/master/zipline/finance/slippage.py, the comments and code suggest that this case should be handled:

if order.limit:  
            # this is tricky! if an order with a limit price has reached  
            # the limit price, we will try to fill the order. do not fill  
            # these shares if the impacted price is worse than the limit  
            # price. return early to avoid creating the transaction.

            # buy order is worse if the impacted price is greater than  
            # the limit price. sell order is worse if the impacted price  
            # is less than the limit price  
            if (order.direction > 0 and impacted_price > order.limit) or \  
                    (order.direction < 0 and impacted_price < order.limit):  
                return  

This would seem to do the trick, no? It just skips processing the transaction if the limit order conditions are not met, using a hypothetical price if the order were to go through.

Interestingly, the code under FixedSlippage(SlippageModel) does not appear to handle limit orders in the same fashion--it has no awareness of the order type, and would transact all orders with slippage applied to the price.

Either way, unless we're talking about a contest entry here (for which the default slippage must be used), wouldn't it be possible simply to write a custom slippage model so that the desired execution of limit orders is effected?