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

Zigzag indicator



  • I'm new, Can anyone help me ? /zigzag indicator/
    http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:zigzag
    https://github.com/joshuaulrich/TTR/blob/master/src/zigzag.c
    @backtrader @ab_trader

    class Zigzag(bt.Indicator):
        
        lines = ('zigzag',)
        plotinfo = dict(subplot=False)
        params = (
            ('change', 40),
            ('use_percent', False),
            ('use_retrace', False),
            ('use_last_ex', False),
        )
    
        def __init__(self):
    
            self.reference = {
                'price': None,
                'index': None,
            }
            self.inflection = {
                'price': None,
                'index': None,
            }
            if self.params.use_percent:
                self.change = self.params.change / 100.0
            else:
                self.change = self.params.change
            
            self.reference['price'] = (self.data.high.array[0] + self.data.low.array[0]) / 2
            self.reference['index'] = 0
            self.inflection['price'] = (self.data.high.array[0] + self.data.low.array[0]) / 2
            self.inflection['index'] = 1
    
            self.extreme_min = 0.0
            self.extreme_max = 0.0
            self.local_min = 0.0
            self.local_max = 0.0        
            self.signal = 0
            self.i = 0
            
       
        def next(self):
            
    
            self.i = len(self.data.open)-1
            # Initialize all zigzag values to NA 
            self.zigzag.array.append(NAN)
            
            if self.params.use_percent:
                #If % change given (absolute move)      
                self.extreme_min = self.inflection['price'] * (1.0 - self.change)
                self.extreme_max = self.inflection['price'] * (1.0 + self.change)
            else:
                #If $ change given (only absolute moves make sense) 
                self.extreme_min = self.inflection['price'] - self.change
                self.extreme_max = self.inflection['price'] + self.change
    
            #Find local maximum and minimum
    
            
            self.local_max = self.inflection['price'] if self.inflection['price'] > self.data.high.array[self.i] else self.data.high.array[self.i]
            self.local_min = self.inflection['price'] if self.inflection['price'] < self.data.low.array[self.i] else self.data.low.array[self.i]
            
            ##Find first trend 
            if self.signal == 0:
                if self.params.use_retrace:
                    #Retrace prior move 
                    self.signal = 1 if (self.inflection['price'] >= self.reference['price']) else -1
                else:
                    #Absolute move
                    if self.local_min <= self.extreme_min:
                        #Confirmed Downtrend
                        self.signal = -1
    
    
                    if self.local_max >= self.extreme_max:
                        #Confirmed Uptrend 
                        self.signal = 1
    
            ##Downtrend
            if self.signal == -1:
                #New Minimum
                if self.data.low.array[self.i] == self.local_min:
                    #Last Extreme
                    if self.params.use_last_ex:
                        self.inflection['price'] = self.data.low.array[self.i]
                        self.inflection['index'] = self.i
                    else:
                        #First Extreme 
                        if (self.data.low.array[self.i] != self.data.low[-1]):
                            self.inflection['price'] = self.data.low.array[self.i]
                            self.inflection['index'] = self.i
    
                #Retrace prior move
                if self.params.use_retrace:
                    self.extreme_max = self.inflection['price'] +((self.reference['price'] - self.inflection['price']) * self.change)
    
                #Trend Reversal 
                if self.data.high.array[self.i] >= self.extreme_max:
                    self.zigzag[self.reference['index']] = self.reference['price']
                    self.reference = copy.copy(self.inflection)
                    self.inflection['price'] = self.data.high.array[self.i]
                    self.inflection['index'] = self.i
                    self.signal = 1
    
    
            #Uptrend 
            if self.signal == 1:
    
                #New Maximum
                if self.data.high.array[self.i] == self.local_max:
                    #Last Extreme 
                    if self.params.use_last_ex:
                        self.inflection['price'] = self.data.high.array[self.i]
                        self.inflection['index'] = self.i
                    else:
                        #First Extreme 
                        if (self.data.high.array[self.i] != self.data.high[-1]):
                            self.inflection['price'] = self.data.high.array[self.i]
                            self.inflection['index'] = self.i
    
                #Retrace prior move 
                if self.params.use_retrace:
                    self.extreme_min = self.inflection['price'] -((self.inflection['price'] - self.reference['price']) * self.change)
    
                #Trend Reversal
                if (self.data.low.array[self.i] <= self.extreme_min):
                    self.zigzag[self.reference['index']] = self.reference['price']
                    self.reference = copy.copy(self.inflection)
                    self.inflection['price'] = self.data.low.array[self.i]
                    self.inflection['index'] = self.i
                    self.signal = -1
    
                self.i = self.i + 1
                
            self.zigzag[self.reference['index']] = self.reference['price']
            self.zigzag[self.inflection['index']] = self.inflection['price']
    

    0_1513851289023_Simple-ZigZag-indicator-for-MetaTrader-5.png


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.