For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Execution of Limit Orders



  • Is there a way in backtrader such that limit orders only execute if price trades through the limit order price?

    I'm running a backtest with tick data for ES futures, so I would only want a sell limit at 2700 to execute if the price goes a tick higher to 2700.25.

    I believe the default behavior is to execute the limit order when price reaches 2700, which rarely happens in live trading.

    Thanks in advance.


  • administrators

    @toolkmit said in Execution of Limit Orders:

    I believe the default behavior is to execute the limit order when price reaches 2700, which rarely happens in live trading.

    backtrader is not about what rarely happens. It tries to model reality. Actually if you have a Limit order to sell at 2700, you are NOT going to be matched at 2700.25 because that would actually go AGAINST what you have specified as limit. Your broker would be breaching the contract it has with you not to executed your order at any price higher than 2700.

    You ar probably looking at these options:

    • Adding slippage: Docs - Slippage
    • Using StopLimit (i.e.: only having a Limit order active if a trigger price has been reached).

    You may also be looking for an order type which is not implemented in backtrader: Limit If Touched. When the price touches 2700 you will be looking to have a Limit order placed at 2700.25