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 bracket function works for stop-loss and take-profit in the same candle?

    General Code/Help
    4
    4
    248
    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.
    • Behnam Heydari
      Behnam Heydari last edited by

      Hello All
      I've used the following code to buy or sell after the signal created in the "next" function.

      if position_type == 'buy':
          brackets = self.buy_bracket(limitprice=position.take_profit, price=position.start, stopprice=position.stop_loss)
      elif position_type == 'sell':
          brackets = self.sell_bracket(limitprice=position.take_profit, price=position.start, stopprice=position.stop_loss)
      

      the problem is when take-profit and stop-loss occurred in the same candle, observation shows for example if the signal is buying, Backtrader will buy in this candle and sell in the opening of next candle, that this it not suitable for me.
      the best scenario in this situation is to check the lower time-frame to find out which is occurred first, is this possible to customize it someway to achieve this requirement?
      Thanks in advance.

      1 Reply Last reply Reply Quote 0
      • R
        rajanprabu last edited by

        I exactly don't understand your question. But if you want the signal and trade to be on same candle you can use cheat-on-close. Also use self.close to close any existing orders ( if it was the intended use ).

        1 Reply Last reply Reply Quote 0
        • run-out
          run-out last edited by

          Research replay and resample

          RunBacktest.com

          1 Reply Last reply Reply Quote 0
          • vladisld
            vladisld last edited by

            Please take a look at the following posts for possible clues:

            https://community.backtrader.com/topic/3094/bracket-order-with-sl-and-tp-what-happens-when-1-bar-hits-both
            https://community.backtrader.com/topic/3046/entry-and-exit-in-the-same-bar-using-bracket-orders

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