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



  • 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



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


  • administrators

    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)
    


  • Thanks @ab_trader



  • @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??



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


  • administrators

    @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


Log in to reply
 

});