Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Feature Request: OCO bracket orders?

    General Discussion
    3
    7
    1898
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      beejaysea last edited by

      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.

      1 Reply Last reply Reply Quote 1
      • B
        backtrader administrators last edited by

        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:

        • Blog - OCO Orders
        • Docs - OCO Orders
        1. 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?

        1 Reply Last reply Reply Quote 0
        • B
          beejaysea last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • B
            beejaysea last edited by

            @backtrader - Just wanted to circle back. OCO is working as expected. Sorry for the alarm.

            1 Reply Last reply Reply Quote 0
            • E
              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.

              Thank you

              1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators last edited by backtrader

                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
                1 Reply Last reply Reply Quote 0
                • E
                  Edu last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                  $(document).ready(function () { app.coldLoad(); }); }