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/

    Indicator that calculates the n-candle max of another indicator

    General Code/Help
    3
    6
    99
    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.
    • N
      nathanl93 last edited by

      Hi everyone,

      I am writing an Indicator that takes another Indicator as its Data Feed. It should work generically on any Indicator that I pass it. The idea is to calculate the max out of the last n-candles and use that as the value.

      Here the indicator in question:

      class LastNMaxIndicator(Indicator):
          lines = ('example', )
          params = {'lookback_period': 3}
      
          def __init__(self):
              self.addminperiod(self.params.lookback_period)
      
          def next(self):
              self.lines.example[0] = max(self.data.get(size=self.params.lookback_period))
      

      In my strategy, I am passing the SimpleMovingAverage as its input:

      class TestStrategy(Strategy):
          def __init__(self):
              self.sma = MovingAverageSimple(self.datas[0])
              self.peak = LastNMaxIndicator(self.sma)
      
          def next(self):
              print('=== Date: {} ==='.format(self.datas[0].datetime.date(0))
      

      However, I get an error when running this saying that the result of self.data.get(size=self.params.lookback_period)) is empty:

        File "...", line 14, in next
          self.lines.example[0] = max(self.data.get(size=self.params.lookback_period))
      ValueError: max() arg is an empty sequence
      

      What am I missing?

      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        bt has Highest or MaxN indicator which returns what you want.

        1 Reply Last reply Reply Quote 0
        • N
          nathanl93 last edited by

          Thanks for answering. If I wanted to do it through the next method though, how would I do it?

          I am not sure what I am doing wrong with my code.

          1 Reply Last reply Reply Quote 0
          • N
            nathanl93 last edited by

            I am asking because, in the event that there is no off-the-shelf provided functionality by Backtrader, I would like to know how to do it through the next method :)

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

              There is probably an issue with the minperiod calculation in your indicator. Calling the super(LastNMaxIndicator, self).__init__() may help, before adding your own minperiod.

              N 1 Reply Last reply Reply Quote 0
              • N
                nathanl93 @vladisld last edited by

                @vladisld said in Indicator that calculates the n-candle max of another indicator:

                There is probably an issue with the minperiod calculation in your indicator. Calling the super(LastNMaxIndicator, self).__init__() may help, before adding your own minperiod.

                That was it. Thank you! For anyone wondering what the indicator looks like now:

                class LastNMaxIndicator(Indicator):
                    lines = ('example', )
                    params = {'lookback_period': 3}
                
                    def __init__(self):
                        super(LastNMaxIndicator, self).__init__()
                        self.addminperiod(self.params.lookback_period)
                
                    def next(self):
                        self.lines.example[0] = max(
                            self.data.get(size=self.params.lookback_period))
                
                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(); }); }