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/

    Plotting StopLoss orders

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

      When I use stoploss orders, these are plotted when they are accepted, not when they are actually executed. This results in double buy/sell markers in my plots, when an order is placed with a stoploss. See the screen below (with both long and short orders for opening a position with a stoploss, resulting in a double marker).

      Wouldn't it make more sense to actually plot an order when it is executed? The stoploss orders would then just be plotted when the position is closed.

      Thanks for the great work on BT!

      stoploss double order arrows

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

        @reynard80 said in Plotting StopLoss orders:

        When I use stoploss orders

        A stoploss order doesn't exist. It's a concept you use to try to limit your losses. You implement the concept using the available execution types.

        You may wish to share the code you use to create that, else there is no way to know what you mean.

        reynard80 1 Reply Last reply Reply Quote 0
        • reynard80
          reynard80 @backtrader last edited by

          @backtrader I use a StopTrail or StopTrailLimit order for a stoploss. For example, when a Limit or Market order is completed, a stoploss is set with the following code:

                      if order.getordername() in ['Market', 'Limit']:
                          # For regular orders
                          self.order = None
          
                          # Create stoploss order if applicable
                          if order.status == order.Completed:
                              if self.p.stoptype:
                                  if self.getposition(data=self.posdata):
                                      # Open position (and no open orders, above)
                                      # Evaluate postion exit for stoploss
          
                                      possize = self.getposition(data=self.posdata).size
                                      if possize > 0:
                                          # Long position
                                          price = None
                                          plimit = None
                                          trailpercent = None
          
                                          if self.p.stoptype == bt.Order.StopTrailLimit:
                                              price = self.middata.close[0]  # Base price for trailing stop
                                              plimit = self.middata.close[0] + self.p.limitoffset  # Price of limit order
                                              trailpercent = self.p.trailpercent  # Percentage of trailing stop
          
                                          elif self.p.stoptype == bt.Order.StopTrail:
                                              price = self.middata.close[0]  # Base price for trailing stop
                                              trailpercent = self.p.trailpercent  # Percentage of trailing stop
          
                                          stoporderexitkwargs = dict(
                                              data=self.compdata,
                                              size=self.p.stake,
                                              exectype=self.p.stoptype,
                                              price=price,
                                              plimit=plimit,
                                              trailpercent=trailpercent,
                                              valid=self.p.valid
                                          )
          
                                          # print('Add stoploss order')
                                          self.stoporder = self.sell(**stoporderexitkwargs)
                                          self.orderid.append(self.stoporder)
          
          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors