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

How to achieve more realistic stop behaviour



  • Please see the image below. There is a short entry, submitted on the first bear candle, executed at the open of the bull candle. This is in simulation/backtest mode.

    I would expect price on the bull candle to go something like open -> low -> high -> close. Therefore, the stop order would have been executed intra bar, and not on the next bar as appears to be done in the backtest.

    Is there a way to achieve a more realistic stop loss behaviour?

    0_1519386893528_02ba0f5a-e1d6-4505-822e-6318b3ba5e77-image.png



  • [REMOVED]



  • [REMOVED]



  • [REMOVED]


  • administrators

    A Stop order is executed as soon as the trigger price is met (or exceeded) Although the zoomed-in bars are nice, they actually, imho, obscure the picture, because actual order submission, prices and other things are not seen.

    Let's see your first case. From the narrative the following seems to be happening:

    • A sell order is submitted during the 1st bar (red)
    • The order is executed with the opening price of the 2nd bar (gray)
    • As soon as the order is executed a Stop order is submitted
    • The Stop order is submitted (with a price in the range of the gray bar)

    Let's point out something assuming the order of the actions above is right

    • The Stop order cannot be executed during the gray bar, because this bar is already closed (it has already happend)

    Carrying on:

    • The Stop order is executed with the opening price of the 3rd bar (red), which is right because the open already triggers the Stop and it is the 1st price to be seen.

    With all this in mind:

    • Is this how orders and things happened?

    If yes:

    • What's wrong then?


  • Up till here, I think you are correct:

    @backtrader said in How to achieve more realistic stop behaviour:

    Let's point out something assuming the order of the actions above is right

    The Stop order cannot be executed during the gray bar, because this bar is already closed (it has already happend)

    I believe this part is incorrect: the order in the real market is filled at the open of the bar, the stop order is also live at the same time. This means that in the real world, the original order can fill and be stopped out in the same bar which is what I believe should happen here during a backtest.

    If what you're saying is that backtrader doesn't do this, then yes I agree. However, the real world can (and does) do this so it would be nice if backtrader reflected the real world on this.



  • @backtrader I believe this should be the standard behaviour for backtesters: https://www.tradingview.com/wiki/Strategies#Broker_emulator:

    1. If opening price of bar is closer to highest price of the same bar, the broker emulator assumes that intrabar price was moving this way: Open → High → Low → Close.
    2. If opening price of bar is closer to lowest price of the same bar, the broker emulator assumes that intrabar price was moving this way: Open → Low → High → Close.
    3. Broker emulator assumes that there were no gaps inside bar, meaning all intrabar prices are available for order execution.

  • administrators

    @cheez said in How to achieve more realistic stop behaviour:

    I believe this part is incorrect: the order in the real market is filled at the open of the bar, the stop order is also live at the same time. This means that in the real world, the original order can fill and be stopped out in the same bar which is what I believe should happen here during a backtest.
    If what you're saying is that backtrader doesn't do this, then yes I agree. However, the real world can (and does) do this so it would be nice if backtrader reflected the real world on this.

    This is the point where the obscurity brought by the bars and the lack of actual data is relevant. In your narrative there is something you are not confirming.

    • When is the Stop order issued?


  • @backtrader Sorry about that! The stop order is issued using buy/sell_bracket, so at the same time as the parent order.


  • administrators

    @cheez said in How to achieve more realistic stop behaviour:

    @backtrader Sorry about that! The stop order is issued using buy/sell_bracket, so at the same time as the parent order.

    Yes and no. The order is issued at the same time but it won't become active until the parent order is executed. It becomes active for the next evaluation cycle. The rationale being:

    • Once the parent order has been executed, if you executed a child order it could happen with a price which has already happened.

    With regards to:

    @cheez said in How to achieve more realistic stop behaviour:

    1. If opening price of bar is closer to highest price of the same bar, the broker emulator assumes that intrabar price was moving this way: Open → High → Low → Close.
    2. If opening price of bar is closer to lowest price of the same bar, the broker emulator assumes that intrabar price was moving this way: Open → Low → High → Close.
    3. Broker emulator assumes that there were no gaps inside bar, meaning all intrabar prices are available for order execution.

    The key words is: assumes. There is no room for assumption when you are backtesting. There are some situations in which you know that the part of the price range in a bar had to follow a certain path and you can then use that part to match.

    In most cases the major problem when executing backtesting is: lack of resolution. When trading one sees the construction of the 1-day bar in real-time. When bbacktesting one applies a single 1-day bar and expects miracles from the software which is going to says how orders could be matched. One should use a smaller timeframe and replay the reconstruction of the 1-day bar.



  • @backtrader said in How to achieve more realistic stop behaviour:

    Yes and no. The order is issued at the same time but it won't become active until the parent order is executed. It becomes active for the next evaluation cycle. The rationale being:

    Once the parent order has been executed, if you executed a child order it could happen with a price which has already happened.

    That may be the case, but that depends on the asset, don't you think? For example, thinly traded assets, you can assume will have gaps and jump around. High volume assets would likely touch every price between two points.

    This is to say that my particular assets exhibit this behaviour: I can be stopped out in the same bar as the entry in specifically that bar. It would be nice if I could at least configure backtrader to handle this somehow.


  • administrators

    @cheez said in How to achieve more realistic stop behaviour:

    @backtrader said in How to achieve more realistic stop behaviour:

    Yes and no. The order is issued at the same time but it won't become active until the parent order is executed. It becomes active for the next evaluation cycle. The rationale being:

    Once the parent order has been executed, if you executed a child order it could happen with a price which has already happened.

    That may be the case, but that depends on the asset, don't you think? For example, thinly traded assets, you can assume will have gaps and jump around. High volume assets would likely touch every price between two points.

    This is to say that my particular assets exhibit this behaviour: I can be stopped out in the same bar as the entry in specifically that bar. It would be nice if I could at least configure backtrader to handle this somehow.

    You need more bars (ie: a smaller timeframe) and not assumptions inside a bar. I didn't talk about gaps between prices in the bar or whether each an every price point will be touched. There are simply no assumptions to be made. If the logic of the path allows for part of it to be considered for a match, it will be used. If nothing can be inferred 100% it won't be used.

    Shall a configurable behavior be applied to Stop orders which are activated as child orders or for all Stop orders? What happens with the Stop orders which are issued several bars after the strategy is in the market? Should they evaluated with the current bar? (Even if the prices don't exist any more ... let's add the cheat-on-stop behavior)



  • @backtrader OK. Is there an example of making a decision on timeframe A and trading on a smaller timeframe? When I try to do that using different compression, I get very odd behaviour. Still profitable, but overlaying orders on the lower timeframe chart looks odd. For example, see below. Whether or not I use the lower timeframe as the data for the order, I still get this odd behaviour.

    0_1519496976165_127f444a-04ad-4bb3-ac43-665d145aeedd-image.png


  • administrators

    The problem with the charts, as expressed above, is that they bring nothing but obscurity

    • What is the timeframe of the data?
    • How does the data actually look like?
    • How is the data added to the system?
    • When are the orders being issued?
    • How are the orders being issued?
    • What are the execution types for the orders? (and with which parameters)
    • What's being notified to the strategy?

    This may all be clear to you, but without an excerpt of the data, working code and some sensible lines of logging, it is just pure black magic for the rest of us.



  • Ok I'll try and get a standalone test case for you.