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

Entry and exit in the same bar using bracket orders



  • I wanted to see if there is a way to have trade entry and exit (whether stop loss or take profit) processed on the same bar.

    Hypothetical situation:

    • I am using bracket BUY order with stop entry at 300, take profit at 400 and stop loss at 250
    • previous bar closed at 280
    • next bar has OHLC as following (280, 410, 240, 320)

    Despite the fact that in realtime trading, clearly after entry order was executed, either stop or take profit would be hit in same bar (which one depends on whether high comes first or low comes first after entry) - when I am backtesting in backtrader, I get only entry order executed on this bar.

    I am assuming that reason for that is that broker "doesn't know" whether High came firs tor Low. Is there a way to set a broker in a way that it makes an assumption (e.g. TradingView Pine Script assumes that price goes first to the end that is "closer" to the open - e.g. if high - open < open - low, it will go to high)? Is there such broker emulator in backtrader?

    If not - is there a way to simulate that myself ? What would be the best way to go about it?

    Thank you for help in advance!



  • https://community.backtrader.com/topic/2994/order-execution-issue

    @Piotr_B said in Entry and exit in the same bar using bracket orders:

    If not - is there a way to simulate that myself ? What would be the best way to go about it?

    Issue orders, track their execution, cancel redundant orders.



  • Hmmm.... Well issue here is that if I use bracket order - the stop / take profit placement is delayed to the next bar (where in reality these orders may have been hit on the same bar - e.g. I use hourly data - my entry order hits in minute 5 of the hour and my stop would hit in minute 20 of the hour).

    If I use "regular" orders - then stop order / take profit order could be hit first before the entry order (which is also not close to reality because those orders would be placed only after entry order was executed)

    I agree that I can track if entry order was executed - but in order to simulate "real" behavior, I would have to use market order so that it is executed on the same bar (which is also not accurate - because my stop or take profit may have been issued at different price)

    So my question is whether there is a way to simulate it in similar way to TradingView pine script - where you can have both an entry order and then "conditional orders" (stop orders or tp orders that are placed only after entry has been completed) executed at the same bar. In TV this will happen even if you don't put "process_orders_on_close=True". It's because of how broker emulator works (it assumes how price was moving with a bar and if this assumption says that entry was first hit and then stop was hit - both will be executed in the same exact bar).





  • Link I provided above gives the solution. Let me repeat it here one more time -

    @ab_trader said in Order execution issue:

    To get quicker notifications move to lower timeframe.
    Or you can try some hacks such as quicknotify (Docs - Cerebro - Reference) or cheat-on-open (Docs - Broker - Cheat-on-open).



  • Thank you both for responses. Let me read on quicknotify and cheat-on-open - hopefully these can solve the issue.

    Moving to lower timeframe is not exactly great solution imho. For first 0 you still could get rapid price movement within the entry bar - and then execution of stop / take-profit will have inaccurate price if executed on next bar.

    Additionally - if I were to simulate the behavior of higher timeframe - I would have to "rewrite" all the indicators I am using to account for difference in timeframe I am solving for and data I am using (e.g. 20 period MA on hourly data is not the same as 80 period MA on 15 minute data)



  • I'm curious what you would propose as a solution that would allow for knowledge of the intrabar price movement? How can you know if the entry is filled before the tp or stop? And in what order is the tp or stop if after the entry bar?

    Each bar is a black box. We cannot know what is going on inside between open and close. Any assumptions you make inside that bar will not neccessarily get you closer to reality.



  • Sure - it will be an assumption on how price moved within a bar - but over longer period of time it should roughly net out (sometimes assumption will be correct, sometimes it will be incorrect in your favor, sometimes it will be incorrect against you).

    But I strongly believe that it would imitate actual real-time performance of the algorithm much better - than it does in current format. Especially when your entry, stop loss and take profit are triggered immediately when level was "touched" / "breached" not based on closing prices.

    For this particular purpose - I really like the way TradingView PineScript handles it (one of very few things I like more about TV PineScript vs BackTrader - overally think that BT is much more powerful tool). TradingView PineScript broker emulator assumes how price was moving within a bar based on relation of OHLC prices - to be precise:

    • IF high - open < open - low THEN assumption is that price moved open -> high -> low -> close
    • IF high - open > open - low THEN assumption is that price moved open -> low-> high -> close

    That allows you to have both entry order (whether it is entry using limit or stop market order type) and stop / take profit order conditional on entry (whether it is entry using limit or stop market order type) executed on the same bar.



  • Maybe replaydata with a lower timeframe like @ab_trader said may help you with this.



  • yeah I am reading up on quicknotify, cheat-on-open and replaydata to see if I can get closer to desired behavior.

    Will circle back here if I figure out a way to simulate intrabar behavior in a similar fashion to PineScript broker emulator.

    Appreciate everyone's input, thank you.



  • TV is an array-processing backtester, and bt as event-driven backtester. Therefore bt can't take a look inside the bar by design, it uses delivery of the bar as event to trigger algo. TV can do whatever end user programmed in it, and has a lot more possibility to fall into future lookahead bias.

    By the way, TVs assumption can be really dangerous. I am surprised that they take that responsibility on them. Usually all backtesters give an opportunity to decide on what signal to take on the same bar to end user.


Log in to reply
 

});