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/

    How to notify SL/TP orders created with buy_bracket?

    General Code/Help
    2
    3
    288
    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.
    • uelel
      uelel last edited by

      I'm searching for an order attribute that could identify whether an executed order is SL/TP. Something like:

      def notify_order(self, order):
      
         if order.status in [order.Completed]:
              if order.isStopLoss():
                  self.log('STOP LOSS TRIGGERED, Price: %.5f' % (order.executed.price))
      
              if order.isTargetProfit():            
                  self.log('TARGET PROFIT TRIGGERED, Price: %.5f' % (order.executed.price))
      

      The SL/TP is created with buy_bracket method:

      self.order = self.buy_bracket(size=100, price=price, limitprice=profitTargetPrice, stopprice=stopLossPrice)
      

      Is it possible to notify the execution of SL/TP orders?

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

        The status (execution, cancelation, ...) of all orders is properly and notified in due time.

        TargetProfit and StopLoss are concepts used by the trader. Such orders DO NOT exist.

        Your options:

        • When issuing the bracket keep a reference to the orders you apply to those concepts (in a list or dict or in a simple variable) and compare then to the actual notified orders

        • Add information to the orders when creating them with the method addinfo and check later the info member attribute (a dict)

        Docs - Order - https://www.backtrader.com/docu/order/

        1 Reply Last reply Reply Quote 2
        • uelel
          uelel last edited by

          @backtrader Thank you for your explanation.
          Thanks,

          if order.ref == self.order[1].ref:
              # notify stop loss
          elif order.ref == self.order[1].ref:
              # notify target profit
          

          did the trick.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors