Bracket Orders Behavior
Do bracket orders only operate on close prices? If not, how does it know if low (stop price) or high (take profit) was hit first? Will it help if we give backtrader smaller timeframe data and resample to larger timeframe?
Robin Dhillon last edited by
the purpose of a bracket order is as follows:
- A mainside order: Basically the set price you want to enter in, could be Limit Price or StopLimit order
- A lowside order: basically an order to make sure you get out a trade in the event it goes against your initial analysis
- A highside order: When you enter, you want to make sure that you get out in time, given the trade goes in the favorable decision
All in all, it's not magic how these get executed, its all depends on which order gets hit first when you enter with your mainside order.
Do bracket orders only operate on close prices?
As pointed out by @Robin-Dhillon, you choose the order types (although only a few make actually sense, and what gets accepted is for sure broker dependent).
The general answer is no. Order execution depends on the order type.
If not, how does it know if low (stop price) or high (take profit) was hit first?
It doesn't know and it cannot know it. Orders, just like in real exchanges are executing on a FIFO basis.
Will it help if we give backtrader smaller timeframe data and resample to larger timeframe?
It's not about helping backtrader. You can only help yourself by using the smallest possible timeframe for execution. Anyone letting you provide help to decide which order is executed first is giving you something not realistic and biased.
@backtrader I may be using the wrong type of order. I basically want to simulate this real world scenario: I trade Forex on daily time frame. As you know, unlike the stock market which gives me the ability to place order before market open, etc, I just have to place order at a certain time in day to operate on daily. I want to simulate a real world scenario where I place an order at midnight (the daily time frame starting point in data bars) for a pair with takeprofit at +%1 and stoploss at -%0.5. In real world this pair will hit +%1 around noon and it will automatically be sold at a profit but goes down and will end up being at -%2 the midnight of the following day. So you see in Backtrader this would end up as a loss, since it only daily granularity, where as in real world this would have been a profit. I am trying to figure out if there is an easy way to do this in Backtrader without doing extra custom coding. Thank you in advance for your time.
Your problem is the same problem many other have: you want to do something with daily data which cannot be done with daily data.
There is no custom coding which can help you. You simply need more granularity.
@backtrader got it. It would be an awesome feature if we could give backtrader finer granularity data just for it to use in the background for smart execution of bracket orders, and give it a main data (daily in my case) to operate as it does right now, i.e. call next() with it, etc. Anyway, thank you for the information.
It would be an awesome feature if we could give backtrader finer granularity data just for it to use in the background for smart execution of bracket orders, and give it a main data (daily in my case) to operate as it does right now, i.e. call next() with it, etc.
Incredible as it may seem ... that awesome functionality is built-in from day
@backtrader if you are referring to Multiple Timeframes, I have tried it. next() gets called by the smaller timeframe. Or am I missing something?
P.S. I have gotten around it by keeping previous datetime of the daily timeframe and skipping unless it's a new datetime:
for _, d in enumerate(self.datas): if d._timeframe == 4 or (d in prev_dt.keys() and d.datetime == prev_dt[d]): continue prev_dt[d] = d.datetime # trading logic here
Please let me know if there is a better way to do this. Thanks!
https://www.backtrader.com/blog/2019-05-20-momentum-strategy/momentum-strategy/ and in several community threads