Feature Request: OCO bracket orders?
Thanks for the excellent framework. I haven't found a way to do this yet, but maybe I missed something. I'd like to be able to close a position given both a Limit and Stop order, and possibly manage that order after-the-fact.
For instance, I enter a Buy at a Stop on the Highest of the last 'n' bars, and I want to exit via a Sell on either entry price + 3 current ATR, or entry price - 1.5 current ATR. I realize that I can create the initial order with buy_bracket, but as I understand it, the two sell orders are fixed, with the only option being for me to cancel those orders, and create a new single sell order.
Second, it would be nice to be able to implement a breakout/breakdown strategy, where there is a simultaneous Buy STOP at Highest of 'n' bars, and Sell STOP at Lowest of 'n' bars. From what I can tell, I don't see a way to have these two simultaneous entry orders open. FWIW, not all brokers may support this, IB does for sure. Regardless, I'm more interested in having this for backtesting than live trading at the moment.
- OCO Bracket
The problem is that the entry price is only known after the order is executed and the brokers out there don't know that you are using the
ATRto calculate the stop-loss and take-profit prices.
It should be rather easy to do what you are describing:
Wait for the notification of your (for example)
buyorder execution. The execution price can be found under
OCOorders, one with
Stopand the other with
Limit. The prices can be then calculated because the entry price is already known.
- Buy STOP at Highest and Sell STOP at Loest
- @beejaysea said in Feature Request: OCO bracket orders?:
from what I can tell, I don't see a way to have these two simultaneous entry orders open
Can you elaborate on what's not working and what the expectation would be?
Thanks for the highly-detailed response. This looks exactly like what I'm looking for. For some reason, I kept landing on the bracket docs. I'll give this a shot.
@backtrader - Just wanted to circle back. OCO is working as expected. Sorry for the alarm.
Edu last edited by
Hi, since I have a doubt that is similar to this one I'll post it here instead of opening another topic..
First thank you as well for the framework, it is, indeed, excellent.
Now, in my case I want to open a trade (with stoploss but no takeprofit) when some signal is sent and then close it when another signal is received. Example:
Prices cross SMA up = Buy with 10% stoploss
Priss cross SMA down = Sell
I have managed to do that without stoploss using the regular buy and sell. But if I try to use buy_bracket it seems the takeprofit is activated even if I use None as its input. I've gone thought all the docs but couldn't manage to make it work.
I apologize if this is a trivial issue, I'm new to this.
buy_bracketis meant to create a bracket. Even if it seems redundant to say so, it's only a bracket if you put orders around the main order. If only one side is covered it's not a bracket.
Your case use OCOs to automate the linking of your dynamic
sellorder and the stop-loss order
- You send a
buyinto the market
- When this order is notified as
notify_order, you issue a
StopTrailwith a percentage of
- If you receive the
sellsignal you issue a
Sellwith the indication:
- This prevents that both orders (the sell and the stop-loss) are executed at the same time, because execution of one cancels the other
- You send a
Edu last edited by
Thank you!! It worked out perfectly!