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?



  • 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.





  • 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.



  • @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.



  • 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.



  • @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



  • @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.


Log in to reply
 

});