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 Arnaud Legoux Moving Average (Vectorised or Nan-Vec): Help Needed

    General Code/Help
    3
    4
    710
    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.
    • Ender Kina
      Ender Kina last edited by

      Have anyone done an indicator on this that they would be willing to share? I'm interested in both Normal and Vectorised approaches.
      (https://stackoverflow.com/questions/46990098/arnaud-legoux-moving-average-and-numpy)
      I am not experienced in sub-classing/inheritance in Python
      Any help would be highly appreciated. Thx in advance!

      Mark Lester Bolotaolo 1 Reply Last reply Reply Quote 0
      • Ender Kina
        Ender Kina last edited by

        Anybody has any custom indicator examples which includes numpy calculations? I would be appretiated if you share so i can figure out how to convert the code below.

        def NPALMA(pnp_array, a) :
        
            length = a
            # just some number (6.0 is useful)
            sigma = 6
            # sensisitivity (close to 1) or smoothness (close to 0)
            offset = 0.85
        
            asize = length - 1
            m = offset * asize
            s = length  / sigma
            dss = 2 * s * s
        
            alma = np.zeros(pnp_array.shape)
            wtd_sum = np.zeros(pnp_array.shape)
        
            for l in range(len(pnp_array)):
                if l >= asize:
                    for i in range(length):
                        im = i - m
                        wtd = np.exp( -(im * im) / dss)
                        alma[l] += pnp_array[l - length + i] * wtd
                        wtd_sum[l] += wtd
                    alma[l] = alma[l] / wtd_sum[l]
        
            return alma
        
        Mariano Volpedo 1 Reply Last reply Reply Quote 0
        • Mark Lester Bolotaolo
          Mark Lester Bolotaolo @Ender Kina last edited by

          @Ender-Kina were you able to solve this?

          1 Reply Last reply Reply Quote 0
          • Mariano Volpedo
            Mariano Volpedo @Ender Kina last edited by

            @Ender-Kina this my ALMA implementation as backtrader indicator

            class ALMA(bt.Indicator):
                lines = ('alma',)
            
                params = dict(
                                period=40,
                                sigma=6,
                                offset=1,
                            )
                
                '''
                ALMA - Arnaud Legoux Moving Average,
                http://www.financial-hacker.com/trend-delusion-or-reality/
                https://github.com/darwinsys/Trading_Strategies/blob/master/ML/Features.py
                  ''' 
                def __init__(self):
            
                    self.asize = self.p.period - 1
                    self.m = self.p.offset * self.asize
                    self.s = self.p.period  / self.p.sigma
                    self.dss = 2 * self.s * self.s
            
                def next(self):
                    try:
                        wtd_sum = 0
                        self.l.alma[0] = 0
                        if len(self) >= self.asize:
                            for i in range(self.p.period):
                                im = i - self.m
                                wtd = np.exp( -(im * im) / self.dss)
                                self.l.alma[0] += self.data[0 - self.p.period + i] * wtd
                                wtd_sum += wtd
                            self.l.alma[0] = self.l.alma[0] / wtd_sum
                            print(self.l.alma[0])
            
                    except TypeError:
                        self.l.alma[0] = 0
                        return
            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors