For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Question about the example in the article Developing a Recursive Indicator (with a seed)



  • When I run the example code in the article “Developing a Recursive Indicator (with a seed)”
    https://www.backtrader.com/blog/posts/2018-01-27-recursive-indicators/recursive-indicator/
    ''''
    import backtrader as bt

    class EMA(bt.indicators.PeriodN):
    params = {'period': 30} # even if defined, we can redefine the default value
    lines = ('ema',) # our output line

    def __init__(self):
        self.alpha = 2.0 / (1.0 + self.p.period)  # period -> exp smoothing factor
    
    def nextstart(self):  # calculate here the seed value
        self.lines.ema[0] = self.data.get(size=self.p.period) / self.p.period
    
    def next(self):
        ema1 = self.lines.ema[-1]  # previous EMA value
        self.lines.ema[0] = ema1 * (1.0 - self.alpha) + self.data[0] * self.alpha
    

    ''''
    I met the following error
    ''''
    File "C:/Bob_home/exploration/backtrader/data-pandas.py", line 50, in nextstart
    self.lines.ema[0] = self.data.get(size=self.p.period) / self.p.period
    TypeError: unsupported operand type(s) for /: 'array.array' and 'int'
    ''''

    In the part of nextstart, from my understanding, ''''self.lines.ema[0] '''' is a digit, and ''''self.data.get(size=self.p.period) / self.p.period'''' is a series whose length is period.
    why there exists the error? many thx.


  • administrators

    '''' is not ```

    There is an obvious typo

    @da-zhang said in Question about the example in the article Developing a Recursive Indicator (with a seed):

    self.lines.ema[0] = self.data.get(size=self.p.period) / self.p.period
    TypeError: unsupported operand type(s) for /: 'array.array' and 'int'

    self.lines.ema[0] = sum(self.data.get(size=self.p.period)) / self.p.period
    

Log in to reply
 

});