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/

    Highest high since position was opened

    Indicators/Strategies/Analyzers
    indicators
    2
    5
    2067
    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
      Benoît Zuber last edited by

      Hi,
      As a newbie I am looking for a simple/elegant way to get the highest high of a data feed since I opened a position in that asset.
      Thanks

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

        Listen for the nofitication of a trade, write down the bar number and initialize a highest tracking variable

        For the available attributes in a Trade see: Docs - Trade

        def notify_trade(self, trade):
            if trade.justopened:
                self.position_bar = trade.baropen
                self.position_highest = float('-inf')
        

        check during the regular next cycle

        def next(self):
            if self.position_bar:
                self.position_highest = max(self.position_highest, self.data.high[0])
        

        You need to, of course, initialize position_bar and manage what happens when you close the trade.

        B 1 Reply Last reply Reply Quote 1
        • B
          Benoît Zuber @backtrader last edited by

          @backtrader Thank you for your quick reply!

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

            See this for a more general solution: Blog - A Dynamic Indicator

            1 Reply Last reply Reply Quote 0
            • B
              Benoît Zuber last edited by

              @backtrader Actually I have a problem running this, following the solution in the blog post that you wrote. I am not sure if I am missing something but it seems that the indicator is computed all at once during strategy initialization. This is a problem since the indicator should be influenced by trades, which are computed later. This is my indicator code:

              class DynamicHighest(bt.Indicator):
              """indicates the highest high since a trade was open."""
              lines = ('dyn_highest',)
              
              def __init__(self):
                  self._tradeopen = False
                  self.addminperdiod = 2
              
              def tradeopen(self, isopen):
                  self._tradeopen = isopen
                  print("tradeopen {isopen}".format(isopen=isopen))
              
              def next(self):
                  if self._tradeopen:
                      print("trade is open")
                      self.lines.dyn_highest[0] = max(self.data[0].high, self.dyn_highest[-1])
                  else:
                      print("trade is close")
                      self.lines.dyn_highest[0] = 0 
              

              now when I run cerebro I get this output:

              trade is close
              trade is close
              trade is close
              .
              .
              .
              trade is close
              trade is close
              trade is close
              trade is close
              trade is close
              dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.13
              BUY
              tradeopen True
              dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.15
              dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.19
              dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.17
              dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.19
              dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.20
              dyn_highest: 0.00 | stoplimit: -0.04 | close: 0.19
              dyn_highest: 0.00 | stoplimit: -0.04 | close: 0.20
              dyn_highest: 0.00 | stoplimit: -0.04 | close: 0.19
              dyn_highest: 0.00 | stoplimit: -0.04 | close: 0.19
              dyn_highest: 0.00 | stoplimit: -0.04 | close: 0.23
              .
              .
              .
              

              So, as we see dyn_highest stays 0 the whole time.
              Is there a way to prevent precalculation of the indicator?

              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(); }); }