Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Bracket order with SL and TP: what happens when 1 bar hits both?

    General Code/Help
    4
    7
    194
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • leecallen
      leecallen last edited by

      If I open a bracket order with SL and TP, and both the SL and the TP are hit in a single bar, which will BT process first? I am using the BT internal broker.

      This must be a FAQ but I can't find it.

      1 Reply Last reply Reply Quote 1
      • vladisld
        vladisld last edited by

        I think it was discussed in the following post:

        https://community.backtrader.com/topic/3046/entry-and-exit-in-the-same-bar-using-bracket-orders/8

        1 Reply Last reply Reply Quote 0
        • leecallen
          leecallen last edited by

          I agree the topic is discussed in that thread (and the one it refers to), but it does not answer the question of how BT currently behaves. I can think of 4 possibilities:

          • SL is always processed first - I consider this 'conservative' behavior, it's how I would code it
          • TP is always processed first - optimistic
          • The SL & TP bracket orders are evaluated in the order they are created
          • Random, unpredictable

          My testing suggests it's the first one - SL is processed first. But it's hard to be sure.

          D 1 Reply Last reply Reply Quote 0
          • A
            ab_trader last edited by

            @leecallen said in Bracket order with SL and TP: what happens when 1 bar hits both?:

            But it's hard to be sure.

            bt processes orders based on the FIFO basis. If you don't trust your test results, than the only way to get confidence is to look into the bt code and verify what sequence is actually programmed in the code.

            • If my answer helped, hit reputation up arrow at lower right corner of the post.
            • Python Debugging With Pdb
            • New to python and bt - check this out
            1 Reply Last reply Reply Quote 0
            • vladisld
              vladisld last edited by

              As far as I can follow the source code in bbroker.py - in case the buy/sell_bracket is used the orders are created in the following order: main, stop (SL), limit (TP).

              Only main order will be active.

              Once the main order is executed both stop and limit orders will activate, but will remain in the same order in the broker's queue. This is important since broker will try to execute them in that order - first stop and then limit.

              Note: I'm not sure the main order and stop/limit orders in the bracket could be executed in the same bar though - it seems that if the main order is executed on bar N, the stop/limit orders may be executed only starting from bar N+1. But I could be mistaken here. I anybody knows otherwise - please correct me.

              So if my understanding is correct - you are right and bbroker will process the SL first and then TP. There is no more complicated logic involved - at least I couldn't find such - to predict/deduct the order of high low prices in a single bar according the proximity of the open price.

              If anybody find/experienced a different behavior - please correct me if I'm wrong.

              1 Reply Last reply Reply Quote 1
              • D
                dasch @leecallen last edited by

                @leecallen said in Bracket order with SL and TP: what happens when 1 bar hits both?:

                SL is always processed first - I consider this 'conservative' behavior, it's how I would code it
                TP is always processed first - optimistic
                The SL & TP bracket orders are evaluated in the order they are created
                Random, unpredictable

                as @vladisld said, when using buy/sell_bracket, then the order is main/stop/limit.

                SL is always processed first - I consider this 'conservative' behavior, it's how I would code it

                additionally as a note, when you add the orders manually with pref, then you could define a different order by creating the TP order before the SL order

                The SL & TP bracket orders are evaluated in the order they are created

                leecallen 1 Reply Last reply Reply Quote 1
                • leecallen
                  leecallen @dasch last edited by

                  @dasch @vladisld Thank you! This explains why my tests are more profitable on lower timeframes. Because the SL and TP get hit in the order they occur, so sometimes TP occurs first. Whereas on the higher timeframe, sometimes they both occur in the same bar, and BT will execute the SL first.

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors