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/

    Adding MA to Equity Curve

    General Code/Help
    2
    4
    439
    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.
    • M
      Mango Loco last edited by

      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):
              self.l.ma = bt.indicators.MovingAverageSimple(self.l.value, period=20)
      
          def next(self):
              self.l.value[0] = self._owner.broker.getvalue()
      

      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!

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

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

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

        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.

        1 Reply Last reply Reply Quote 1
        • M
          Mango Loco last edited by

          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 = self._owner.broker.get_value()
          
                  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?

          1 Reply Last reply Reply Quote 0
          • M
            Mango Loco last edited by

            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] = self._owner.broker.getvalue()
                    self.l.ma[0] = np.average(self.l.value.get(size=self.p.ma_len))
            
            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors