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/

    Bracketorder : Order gets executed at open instead of limit price.

    General Code/Help
    1
    3
    28
    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.
    • Devang Bhanushali
      Devang Bhanushali last edited by

      Quoting @Robin-Dhillon : "the purpose of a bracket order is as follows:

      A mainside order: Basically the set price you want to enter in, could be Limit Price or StopLimit order
      A lowside order: basically an order to make sure you get out a trade in the event it goes against your initial analysis
      A highside order: When you enter, you want to make sure that you get out in time, given the trade goes in the favorable decision."

      I'm facing an error where my main side order doesn't get executed at limit price but instead the open of the candle ( where my limit price hits and is under valid days) which contradicts what @Robin-Dhillon said and in general the actual motive of limit orders.

      Here's my code:

                          price = self.datas[0].low[0]
      
                          valid = datetime.timedelta(minutes = self.params.limmins)
                          self.log('BUY CREATE, %.2f' % price)
                          sl= self.datas[0].high[0] * 1.0001
                          tgt = price - 2*(sl- price)
      
                          # Keep track of the created order to avoid a 2nd order
                          
                          
                          os = self.sell_bracket(plimit = price,exectype=bt.Order.Limit,
                          stopprice=sl,stopexec=bt.Order.Stop,
                          limitprice=tgt, limitexec=bt.Order.Limit,
                          valid=valid,
                          stopargs=dict(valid=None),limitargs=dict(valid=None),)
      

      Please suggest me where I'm wrong, Thanks

      Devang Bhanushali 1 Reply Last reply Reply Quote 0
      • Devang Bhanushali
        Devang Bhanushali @Devang Bhanushali last edited by

        @backtrader any help would be appreciated

        Devang Bhanushali 1 Reply Last reply Reply Quote 0
        • Devang Bhanushali
          Devang Bhanushali @Devang Bhanushali last edited by

          @devang-bhanushali As the document says :

          "Limit
          Execution:

          The price set at order creation if the data touches it, starting with the next price bar.

          The order will be canceled if valid is set and the time point is reached

          Price Matching:

          backtrader tries to provide most realistic execution price for Limit orders.

          Using the 4 price spots (Open/High/Low/Close) it can be partially inferred if the requested price can be improved.

          For Buy Orders

          Case 1:

          If the open price of the bar is below the limit price the order executes immediately with the open price. The order has been swept during the opening phase of the session

          Case 2:

          If the open price has not penetrated below the limit price but the low price is below the limit price, then the limit price has been seen during the session and the order can be executed

          The logic is obviously inverted for Sell orders."

          The limit order will be executed for the best possible price and since open was the best, it got executed on it.

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