Stop loss and profit taker orders, which price get hit first? (add example, previous the questions was confusing)
-
Example:
two prices that falls on the same 4H Candle. e,g, stop=10.5, profit=11, and the next candle OHLC is (10.8, 12, 10, 10.8), this is a candle with zero body, so do you know which price it hits first? 10.5 or 11? It could have been opened at 10.8 and go down to 10, go up to 12, then close down at 10.8, this will hit the stop.
Or it could have been opened at 10.8 and go up to 12, go down to 10, then go up and close at 10.8, this will hit the profit target.Re: Stop loss and take profit triggered at same time?
I found this situation happens in my 4h inside bar strategy too, where the take profit price and stoploss fall on the same candle stick because of large candle bars due to the momentum. Anyone confirms if adding two data frames could help this? Orders placed based on the higher timeframe, will they be executed based on the lower timeframe? In that case, I can add the 1 min bar which helps backtrader identify, which order should be filled first?
-
@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
I found this situation happens in my 4h inside bar strategy too, where the take profit price and stoploss fall on the same candle stick because of large candle bars due to the momentum
No. It happens because
- Your orders are not OCO and don't cancel each other out
- Because the timeframe you use is large and a bar comprises a large amount of price development
@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
Anyone confirms if adding two data frames could help this?
You can add 1000 ... it won't work by simply adding dataframes.
@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
Orders placed based on the higher timeframe, will they be executed based on the lower timeframe?
You should read the reference of
buy
/sell
. You decide the target. Docs - Strategy@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
In that case, I can add the 1 min bar which helps backtrader identify, which order should be filled first?
Orders are filled like in real life: FIFO style.
-
@backtrader Thanks for your reply.
But sorry English is my 3rd language so I didn't make myself clear. I do use OCO order, so I don't see both the orders getting filled.My orders are placed at the same time. My issue is that orders are not filling the same way as it is in a real-life tick-by-tick data stream. For example, the profit order price should get hit first so the stop order is canceled, but backtrader hit my stop order and cancels my profit order.
The reason is if both my stop and profit price fall on a 4h candle, there's no way to decided which price get hit first, because the direction of price move on that candle is not clear. Some back testing framework handles this situation in a conservative way - hit stop order and mark this trade as loss.
That's why I want to add a lower timeframe data, will that help backtrader decide if price hit the stop or target first?
-
@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
My orders are placed at the same time. My issue is that orders are not filling the same way as it is in a real-life tick-by-tick data stream. For example, the profit order price should get hit first so the stop order is canceled, but backtrader hit my stop order and cancels my profit order.
This is simply plainly wrong. Your expectation is that you get the same behavior from a tick-by-tick stream as you get from backtesting with a higher timeframe.
Orders are not executed according to the nature you assign to them (
take-profit
andstop-loss
). They are executed in a FIFO order. Full stop. Just like a broker does. Your problem is "timeframe".@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
The reason is if both my stop and profit price fall on a 4h candle, there's no way to decided which price get hit first,
Yes there is FIFO. Your problem is "timeframe".
@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
Some back testing framework handles this situation in a conservative way - hit stop order and mark this trade as loss
Wrong biased behavior.
@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
That's why I want to add a lower timeframe data, will that help backtrader decide if price hit the stop or target first?
Your point of view is biased because of the biased implementations you have seen (according to your previous point).
backtrader doesn't get help (or need it) to decide how to execute orders. Orders are executed using FIFO.
A smaller timeframe CAN HELP YOU in achieving a simulation which is closer to the reality.
Sorry if I am blunt, but thinking that trying to help the system decide which order gets hit first ... is a wrong way to approach backtesting.
-
@backtrader Understood my problem is the timeframe, and FIFO execution of orders. But here the focus is not about issue a buy / sell on the market price.
I placed the bracket order, the main order is filled, and it is waiting for the next candle bars to arrive to either fill its stoploss order or profit order.
My question rephrased is that are orders in backtrader tied to a specific data timeframe? If I added 4h timeframe and 1 min timeframe, will backtrader choose to execute orders on 1min timeframe ? Which data timeframe the order execution relies on?
-
@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
My question rephrased is that are orders in backtrader tied to a specific data timeframe? If I added 4h timeframe and 1 min timeframe, will backtrader choose to execute orders on 1min timeframe ? Which data timeframe the order execution relies on?
When you issue the order, you specify the data feed for the order execution. So the time frame of this data feed will define your order time frame.
-
@ab_trader thanks for the reply! there is a data parameter for buy/sell api which I was not aware of.. that solves my issue
def buy(self, data=None, size=None, price=None, plimit=None, exectype=None, valid=None, tradeid=0, oco=None, trailamount=None, trailpercent=None, parent=None, transmit=True, **kwargs):
-
@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
there is a data parameter for buy/sell api which I was not aware of
Docs - Orders - Order creation
You may want to read docs, can save you time in the future, and make you aware of other useful staff. :)
-
@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
there is a data parameter for buy/sell api which I was not aware of.. that solves my issue
@backtrader said in Stop loss and profit taker orders triggered on the same candlestick?:
You should read the reference of buy / sell. You decide the target. Docs - Strategy
-
@han-chen said in Stop loss and profit taker orders triggered on the same candlestick?:
My question rephrased is that are orders in backtrader tied to a specific data timeframe? If I added 4h timeframe and 1 min timeframe, will backtrader choose to execute orders on 1min timeframe ? Which data timeframe the order execution relies on?
Let me comment on this, because you stated above that "other platforms" and "influence the decision" and "help backtrader decide"
I don't know what other platforms do (actually I have used some but I prefer to abstract myself from them), but backtrader doesn't make decisions for you. It has some defaults which allow to use it quickly without having to think about which is the target, like in the case in which you only have 1 data feed and it would seem rather pointless to have to specify what's the target of an operation.
And you cannot "influence" or "help" backtrader. You can add bits and pieces (like smaller timeframe data, your own commission schemes, your Sizers and others) to try to model your backtesting and get it as close as possible to what real execution would be. But how backtrader works is to stick to simple rules as to how things actually work in the real world.
If you send 2 orders to a broker they will be EVALUATED with a FIFO approach. Notice the EVALUATED term. Because depending on the nature of the orders, the 2nd one could actually be executed first. But evaluation will always be FIFO based.
-
@backtrader Give you an example that helps understand my original question since I'm asking it on Tradingview too.. I got my answer from @ab_trader though..
two prices that falls on the same 4H Candle. e,g, stop=10.5, profit=11, and the next candle OHLC is (10.8, 12, 10, 10.8), this is a candle with zero body, so do you know which price it hits first? 10.5 or 11? It could have been opened at 10.8 and go down to 10, go up to 12, then close down at 10.8, this will hit the stop.
Or it could have been opened at 10.8 and go up to 12, go down to 10, then go up and close at 10.8, this will hit the profit target. -
Your original question is understood. It doesn't matter if you ask in in tradingview or in Hawaii.
The answer is already above, but it seems you fail to understand it.
@han-chen said in Stop loss and profit taker orders, which price get hit first? (add example, previous the questions was confusing):
so do you know which price it hits first? 10.5 or 11?
FIFO order execution. Order introduction in the system is not given. No answer is possible.
@han-chen said in Stop loss and profit taker orders, which price get hit first? (add example, previous the questions was confusing):
It could have been opened at 10.8 and go down to 10, go up to 12, then close down at 10.8, this will hit the stop.
Or it could have been opened at 10.8 and go up to 12, go down to 10, then go up and close at 10.8, this will hit the profit target.Fantastic ... you have perfectly identified and diagnosed your problem:
4-hour
bars don't have the resolution you need for the small price differences which you use for your stop-loss/take-profit orders. If the4-hour
bars are the one giving you the signals, you need a smaller timeframe for actual order execution.Anyone giving you any other answer is giving you false and wrong advice.
Many other people experience the same problem with daily bars and believe that
cheat-on-close
and/orcheat-on-open
is the solution, which it isn't.