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

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.

  • administrators

    1. 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 ATR to 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 order.executed.price (received in notify_order(order)

    • Create 2 OCO orders, one with sell and Stop and the other with sell and Limit. The prices can be then calculated because the entry price is already known.

    For OCO orders see:

    1. Buy STOP at Highest and Sell STOP at Loest

    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.

  • 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.

    Thank you

  • administrators

    buy_bracket is 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 sell order and the stop-loss order

    • You send a buy into the market
    • When this order is notified as Completed in notify_order, you issue a StopTrail with a percentage of 10%
    • If you receive the sell signal you issue a Sell with the indication: oco=stop-loss-order
    • This prevents that both orders (the sell and the stop-loss) are executed at the same time, because execution of one cancels the other

  • Thank you!! It worked out perfectly!
    Much appreciated!

Log in to reply