Back to Community
Backtest - Difference between the price in limit order and the executed price (transaction detail)

I am using a limit order in order to prevent slippage.

def placeLimitOrder(context, data, order, firstUnderlyingTarget):  
    if order != 0:  
        print("firstUnderlyingTarget: " + str(firstUnderlyingTarget))  
        order_target(context.firstUnderlying, order * 500, style=LimitOrder(firstUnderlyingTarget))

In the log output, I see
2011-01-07 00:11 PRINT firstUnderlyingTarget: 127.03

However, in the Transaction detail, I see
12:12AM SPY SELL $127.15 500 ($63575)

Wondering why there is a price difference. And the difference is so large.

6 responses

it could be this bug

Tony,

Are you sure the results are not correct? When you place a sell limit order at 127.03 , that means you are willing to sell for any price at that level or higher.
If it sold at 127.15 you should be happy, you got a better fill than you were willing to take.

Now if the order had been to buy , you should not have gotten filled.

@Serge d'Adesky. The bug is explained in the link above. If you place a limit order either the current price is better than the limit price and you get a better filled price or the current price is worse than the limit price. In this latter case, there is no way the market gives you a better price and when the limit price is crossed by the market you get a filled price equal to the limit price.

Luca,

There may indeed be a bug in the system, which you reference I was just pointing out that:
if the price is above 127.03 and you place a sell limit order at 127.03 YOU WILL GET FILLED , as long as price is above 127.03.

So if the market's at bid/ask 127.17 / 127.19 and you are selling limit 127.03 you will get filled at a price of 127.17

And if you want to only sell below a certain price, obviously stop limit orders are indicated.

Compare default slippage vs set_slippage(slippage.FixedSlippage(spread=0.0))

Thanks for the help and explanation. Now, I think the result is correct.

But the result seems unlikely happen in real world.