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/

    Custom Indicator

    General Code/Help
    3
    7
    1530
    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
      narik11 last edited by

      Hi All,

      created a custom class for calculating Keltner Channels and had the below issue:
      ATR() takes at least 3 positional arguments

      Here is the sample code:

      class KeltnerStrategy(bt.Strategy):
      
          params = (('debug', False)
                    ,('perc1', 0.05))
      
          def __init__(self):
              self.keltnerChannel = keltnerChannel(self.data)
              
      class keltnerChannel(bt.Indicator):
          lines = ('upper', 'basis', 'lower',)
          params = (('ema', 20), ('period', 10),)
          
          def __init__(self):
      #        self.addminperiod(self.p.period)
              self.basis = bt.talib.EMA(self.data, period=self.p.ema)
              atr = bt.talib.ATR(self.data, period=self.p.period)
              self.upper = self.basis + 1 * atr
              self.lower = self.basis - 1 * atr
      

      Appreciate if someone could help. Thanks in advance

      A B 2 Replies Last reply Reply Quote 0
      • A
        ab_trader @narik11 last edited by

        @narik11 talib.ATR requires high, low, close and timeperiod as input. Read the docs please.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators @narik11 last edited by backtrader

          Or instead of ...

          @narik11 said in Custom Indicator:

              atr = bt.talib.ATR(self.data, period=self.p.period)
          

          do

          atr = bt.ind.ATR(self.data, period=self.p.period)
          
          N 1 Reply Last reply Reply Quote 1
          • N
            narik11 last edited by

            Thanks @ab_trader

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

              @backtrader after changing the above line, the values got populated, but with constant ones. here is the complete code..

              class KeltnerStrategy(bt.Strategy):
              
                  params = (('debug', False)
                            ,('perc1', 0.05))
              
                  def log(self, txt, dt=None):
                      dt = dt or self.datas[0].datetime.datetime(0)
                      print('%s: %s' % (dt.isoformat(), txt))
              
                  def __init__(self):
                      self.keltnerChannel = keltnerChannel(self.data)
                  
                  
                  def next(self):
                      self.log('Open: %.2f, High: %.2f, Low: %.2f, Close: %.2f, u: %.5f, b: %.5f, l: %.5f'
                               %(self.data.open[0], self.data.high[0], 
                                 self.data.low[0], self.data.close[0],
                                 self.keltnerChannel.upper[0], self.keltnerChannel.basis[0],
                                 self.keltnerChannel.lower[0]
                                ))
              
                      
              class keltnerChannel(bt.Indicator):
                  lines = ('upper', 'basis', 'lower',)
                  params = (('ema', 20), ('period', 10),)
                  
                  def __init__(self):
              #        self.addminperiod(self.p.period)
                      self.basis = bt.indicators.EMA(self.data, period=self.p.ema)
                      atr = bt.indicators.ATR(self.data, period=self.p.period)
                      self.upper = self.basis + 1 * atr
                      self.lower = self.basis - 1 * atr
              
              2019-03-20T04:45:00: Open: 3992.38, High: 3997.65, Low: 3991.58, Close: 3993.97, u: 3988.11740, b: 3975.04052, l: 3961.96363
              2019-03-20T05:00:00: Open: 3993.18, High: 3997.00, Low: 3993.18, Close: 3995.50, u: 3988.11740, b: 3975.04052, l: 3961.96363
              2019-03-20T05:15:00: Open: 3994.34, High: 3999.00, Low: 3994.02, Close: 3996.67, u: 3988.11740, b: 3975.04052, l: 3961.96363
              2019-03-20T05:30:00: Open: 3995.50, High: 4000.97, Low: 3995.50, Close: 3999.63, u: 3988.11740, b: 3975.04052, l: 3961.96363
              2019-03-20T05:45:00: Open: 3997.57, High: 4008.89, Low: 3997.57, Close: 4003.81, u: 3988.11740, b: 3975.04052, l: 3961.96363
              2019-03-20T06:00:00: Open: 4000.69, High: 4008.28, Low: 4000.36, Close: 4004.89, u: 3988.11740, b: 3975.04052, l: 3961.96363
              2019-03-20T06:15:00: Open: 4002.79, High: 4006.81, Low: 4002.79, Close: 4004.85, u: 3988.11740, b: 3975.04052, l: 3961.96363
              

              trying to replicate the same template as posted for MACD @ https://www.backtrader.com/docu/inddev.html?highlight=custom

              could you please let me know what is causing the issue??

              B 1 Reply Last reply Reply Quote 0
              • N
                narik11 last edited by

                changing the variables to self.lines.* in __init() worked..

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

                  @narik11 said in Custom Indicator:

                          self.upper = self.basis + 1 * atr
                          self.lower = self.basis - 1 * atr
                  

                  Because these are attributes you created and were not targeting the output lines

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