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

Adding MA to Equity Curve

  • Hi,

    I'm wanting to add a MA to the Equity Curve. I have tried:

    class AcctValue(bt.Observer):
        alias = ('Value',)
        lines = ('value', 'ma')
        plotinfo = {"plot": True, "subplot": True}
        plotlines = dict(ma=dict(ls='-', color='black', _plotvalue=False, _plotvaluetag=False))
        def __init__(self):
   = bt.indicators.MovingAverageSimple(self.l.value, period=20)
        def next(self):
            self.l.value[0] =

    That results in:
    Screen Shot 2020-01-19 at 8.45.41 PM.png

    The "ma" tag shows up, but no MA.
    Any ideas for how I could achieve the desired outcome?

    Thanks in advance!

  • administrators

    @Mango-Loco said in Adding MA to Equity Curve:

    def __init__(self): = bt.indicators.MovingAverageSimple(self.l.value, period=20)
    def next(self):
        self.l.value[0] =

    There is a problem there. The moving average is calculated before you set the value. Which means that the moving average is constantly calculating the average of Nan values.

    You need to define an indicator which stores the value of the broker. You instantiate it just before the moving average and then do the moving average of that.

  • Thanks for your help. I have the following:

    class AcctValueSMA(bt.Indicator):
        lines = ('AcctValue', 'MA')
        params = dict(ma_len=20)
        def __init__(self, type):
            self.l.AcctValue =
            if type == 'SMA':
                self.l.MA = bt.ind.MovingAverageSimple(self.l.AcctValue, period=self.p.ma_len)
            elif type == 'EMA':
                self.l.MA = bt.ind.ExponentialMovingAverage(self.l.AcctValue, period=self.p.ma_len)

    But I get:
    TypeError: 'float' object is not callable

    Is there a specific way to store and subsequently use the value of the broker in this case?

  • Problem solved, using numpy to calculate and feed the average of account value.

    class AcctValue(bt.Observer):
        lines = ('value', 'ma')
        params = dict(ma_len=100)
        plotinfo = dict(plot=True, subplot=True)
        def next(self):
            self.l.value[0] =
  [0] = np.average(self.l.value.get(size=self.p.ma_len))

Log in to reply