Stop loss and take profit triggered at same time?



  • Hi everyone,

    I get a problem when I backtest my strategy. At some point, I go short and it places automatically by stop_loss and take_profit orders. The problem happens a few candles later when the candle is so big that both orders are triggered at the same time. I thought it wasn't possible since I handle order cancellation. Here is the log :

    0_1511826180813_Capture d’écran 2017-11-28 à 00.36.30.png

    And here is my part of the code that handles order cancellation and places stop_loss/take_profit orders (inside notify_order) :

    0_1511826347358_Capture d’écran 2017-11-28 à 00.45.24.png

    Should I manage cancellation with OCO property instead of using broker.cancel(...)?



  • @Alex-0 said in Stop loss and take profit triggered at same time?:

    Should I manage cancellation with OCO property instead of using broker.cancel(...)?

    Yes!!! But even this is not accurate enough, see below. And see further below for an alternative

    @Alex-0 said in Stop loss and take profit triggered at same time?:

    the candle is so big that both orders are triggered at the same time. I thought it wasn't possible since I handle order cancellation

    It takes a while to understand it but try to grasp it: when you see things ... the bar (the OHLC prices) is gone, closed, you name it. The prices have already happened. If you were able to issue commands like an order cancellation, you'd be cheating.

    It seems your timeframe is daily. For a real simulation of your scenario you should get data with a smaller timeframe (1-minute data for example)

    As pointed out above even if you use OCO it isn't really accurate because orders are executed sequentially (very easy to spot in the logic in the source code of the broker) which means that even if the 2nd order to be sent to the system could be the 1st to be executed in the real world it will not be in the simulation, due to the huge timeframe (1-day)

    I also saw this added to Cerebro and never found a real use case for it until I read this today: quicknotify. In hindsight I guess they could have named it cheat-on-notify-order (like cheat-on-close and cheat-on-open), but the documentation makes a good point about it being useful for live brokers. It seems you will be notified of order execution before other orders are executed. Give it a try. You can read the doc in the link below.

    And the discussion that led to the introduction

    Again: this is not a substitute for using a smaller timeframe if you want accurate results.



  • There might be a case when you can stay on the same time frame, but this need to be tested.

    In order to perform conservative backtest, stop orders should be processed before take orders. bt normally processes orders as first come - first go, so every time you issue buy entry order, stop order should be issued before take order. Using this and quicknotify to cancel remaining order can work.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.