4

I would like to create a zigzag indicator for stocks. I'm working with python and my english is bad so my appologies for that. I took part of my code from: Pandas: Zigzag segmentation of data based on local minima-maxima

The problem is the zigzag I would like is this (Metastock zigzag indicator): enter image description here And my zigzag code looks like(note you can change the percent with a filter): enter image description here

from pandas_datareader import data
import pandas as pd
from datetime import date
from pandas_datareader.nasdaq_trader import get_nasdaq_symbols
from scipy import signal
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
np.random.seed(0)

def filter(values, percentage):
    previous = values[0] 
    mask = [True]
    for value in values[1:]: 
        relative_difference = np.abs(value - previous)/previous
        if relative_difference > percentage:
            previous = value
            mask.append(True)
        else:
            mask.append(False)
    return mask


def main(stock=None, start_date=None, end_date=None):
    df = data.DataReader(
        stock, 
        start=start_date, end=end_date,
        data_source='yahoo'
    )
    return df


if __name__ == '__main__':
    today = '{}'.format(date.today())
    stocks = ['BLL']
    cont = 0
    for stock in stocks:
        cont += 1
        try:
            serie = main(stock=stock, start_date='2018-1-1', end_date=today)
            serie.insert(loc=0, column='Date', value=serie.index)
            serie = serie.reset_index(drop=True)
            # Create zigzag trendline.
            ########################################
            # Find peaks(max).
            data_x = serie.index.values
            data_y = serie['Close'].values
            peak_indexes = signal.argrelextrema(data_y, np.greater)
            peak_indexes = peak_indexes[0]
            # Find valleys(min).
            valley_indexes = signal.argrelextrema(data_y, np.less)
            valley_indexes = valley_indexes[0]          
            # Merge peaks and valleys data points using pandas.
            df_peaks = pd.DataFrame({'date': data_x[peak_indexes], 'zigzag_y': data_y[peak_indexes]})
            df_valleys = pd.DataFrame({'date': data_x[valley_indexes], 'zigzag_y': data_y[valley_indexes]})
            df_peaks_valleys = pd.concat([df_peaks, df_valleys], axis=0, ignore_index=True, sort=True)
            # Sort peak and valley datapoints by date.
            df_peaks_valleys = df_peaks_valleys.sort_values(by=['date'])
            p = 0.1 # 20% 
            filter_mask = filter(df_peaks_valleys.zigzag_y, p)
            filtered = df_peaks_valleys[filter_mask]

             # Instantiate axes.
            (fig, ax) = plt.subplots(figsize=(10,10))
            # Plot zigzag trendline.
            ax.plot(df_peaks_valleys['date'].values, df_peaks_valleys['zigzag_y'].values, 
                                                                    color='red', label="Extrema")
            # Plot zigzag trendline.
            ax.plot(filtered['date'].values, filtered['zigzag_y'].values, 
                                                                    color='blue', label="ZigZag")
            # Plot original line.
            ax.plot(data_x, data_y, linestyle='dashed', color='black', label="Org. line", linewidth=1)
            plt.show()
            print('{} - {}| success'.format(cont, stock))
        except Exception:
            print('{} - {}| ERROR'.format(cont, stock))
Martin Facts
  • 301
  • 2
  • 5
  • 12
  • unless you know exactly how the first was implemented it's impossible to reproduce. If you dont know, how in earth you expect us to help you? Stil my gut tell me it's dependent on the start point, you have ~600 I guess the above one has more points. – imbr Jun 18 '20 at 16:06
  • The points are the same. Both starts in 2018-01-01 and end in 2020-06-17. maybe it's help you to understand how zigzag works: https://www.investopedia.com/terms/z/zig_zag_indicator.asp @eusoubrasileiro – Martin Facts Jun 18 '20 at 16:18

2 Answers2

1

Here is an example on github:zigzag

cimport cython
import numpy as np
from numpy cimport ndarray, int_t

DEF PEAK = 1
DEF VALLEY = -1


@cython.boundscheck(False)
@cython.wraparound(False)
cpdef int_t identify_initial_pivot(double [:] X,
                                   double up_thresh,
                                   double down_thresh):
    cdef:
        double x_0 = X[0]
        double x_t = x_0

        double max_x = x_0
        double min_x = x_0

        int_t max_t = 0
        int_t min_t = 0

    up_thresh += 1
    down_thresh += 1

    for t in range(1, len(X)):
        x_t = X[t]

        if x_t / min_x >= up_thresh:
            return VALLEY if min_t == 0 else PEAK

        if x_t / max_x <= down_thresh:
            return PEAK if max_t == 0 else VALLEY

        if x_t > max_x:
            max_x = x_t
            max_t = t

        if x_t < min_x:
            min_x = x_t
            min_t = t

    t_n = len(X)-1
    return VALLEY if x_0 < X[t_n] else PEAK


@cython.boundscheck(False)
@cython.wraparound(False)
cpdef peak_valley_pivots(double [:] X,
                         double up_thresh,
                         double down_thresh):
    """
    Find the peaks and valleys of a series.

    :param X: the series to analyze
    :param up_thresh: minimum relative change necessary to define a peak
    :param down_thesh: minimum relative change necessary to define a valley
    :return: an array with 0 indicating no pivot and -1 and 1 indicating
        valley and peak


    The First and Last Elements
    ---------------------------
    The first and last elements are guaranteed to be annotated as peak or
    valley even if the segments formed do not have the necessary relative
    changes. This is a tradeoff between technical correctness and the
    propensity to make mistakes in data analysis. The possible mistake is
    ignoring data outside the fully realized segments, which may bias
    analysis.
    """
    if down_thresh > 0:
        raise ValueError('The down_thresh must be negative.')

    cdef:
        int_t initial_pivot = identify_initial_pivot(X,
                                                     up_thresh,
                                                     down_thresh)
        int_t t_n = len(X)
        ndarray[int_t, ndim=1] pivots = np.zeros(t_n, dtype=np.int_)
        int_t trend = -initial_pivot
        int_t last_pivot_t = 0
        double last_pivot_x = X[0]
        double x, r

    pivots[0] = initial_pivot

    # Adding one to the relative change thresholds saves operations. Instead
    # of computing relative change at each point as x_j / x_i - 1, it is
    # computed as x_j / x_1. Then, this value is compared to the threshold + 1.
    # This saves (t_n - 1) subtractions.
    up_thresh += 1
    down_thresh += 1

    for t in range(1, t_n):
        x = X[t]
        r = x / last_pivot_x

        if trend == -1:
            if r >= up_thresh:
                pivots[last_pivot_t] = trend
                trend = PEAK
                last_pivot_x = x
                last_pivot_t = t
            elif x < last_pivot_x:
                last_pivot_x = x
                last_pivot_t = t
        else:
            if r <= down_thresh:
                pivots[last_pivot_t] = trend
                trend = VALLEY
                last_pivot_x = x
                last_pivot_t = t
            elif x > last_pivot_x:
                last_pivot_x = x
                last_pivot_t = t

    if last_pivot_t == t_n-1:
        pivots[last_pivot_t] = trend
    elif pivots[t_n-1] == 0:
        pivots[t_n-1] = -trend

    return pivots


@cython.boundscheck(False)
@cython.wraparound(False)
cpdef double max_drawdown(ndarray[double, ndim=1] X):
    """
    Compute the maximum drawdown of some sequence.

    :return: 0 if the sequence is strictly increasing.
        otherwise the abs value of the maximum drawdown
        of sequence X
    """
    cdef:
        double mdd = 0
        double peak = X[0]
        double x, dd

    for x in X:
        if x > peak:
            peak = x

        dd = (peak - x) / peak

        if dd > mdd:
            mdd = dd

    return mdd if mdd != 0.0 else 0.0


@cython.boundscheck(False)
@cython.wraparound(False)
def pivots_to_modes(int_t [:] pivots):
    """
    Translate pivots into trend modes.

    :param pivots: the result of calling ``peak_valley_pivots``
    :return: numpy array of trend modes. That is, between (VALLEY, PEAK] it
    is 1 and between (PEAK, VALLEY] it is -1.
    """

    cdef:
        int_t x, t
        ndarray[int_t, ndim=1] modes = np.zeros(len(pivots),
                                                dtype=np.int_)
        int_t mode = -pivots[0]

    modes[0] = pivots[0]

    for t in range(1, len(pivots)):
        x = pivots[t]
        if x != 0:
            modes[t] = mode
            mode = -x
        else:
            modes[t] = mode

    return modes


def compute_segment_returns(X, pivots):
    """
    :return: numpy array of the pivot-to-pivot returns for each segment."""
    pivot_points = X[pivots != 0]
    return pivot_points[1:] / pivot_points[:-1] - 1.0
kai
  • 1,640
  • 18
  • 11
0

Tradingview has an excellent zigzag. You can see the code in Pine (tradingView language) but you can translate it into python.

//@version=5
indicator("Zig Zag", overlay=true, max_lines_count=500, max_labels_count=500)
dev_threshold = input.float(title="Deviation (%)", defval=5.0, minval=0.00001, maxval=100.0)
depth = input.int(title="Depth", defval=10, minval=1)
line_color = input(title="Line Color", defval=#2962FF)
extend_to_last_bar = input(title="Extend to Last Bar", defval=true)
display_reversal_price = input(title="Display Reversal Price", defval=true)
display_cumulative_volume = input(title="Display Cumulative Volume", defval=true)
display_reversal_price_change = input(title="Display Reversal Price Change", defval=true, inline="price rev")
difference_price = input.string("Absolute", "", options=["Absolute", "Percent"], inline="price rev")

pivots(src, length, isHigh) =>
    p = nz(src[length])
    if length == 0
        [time, p]
    else
        isFound = true
        for i = 0 to math.abs(length - 1)
            if isHigh and src[i] > p
                isFound := false
            if not isHigh and src[i] < p
                isFound := false
        for i = length + 1 to 2 * length
            if isHigh and src[i] >= p
                isFound := false
            if not isHigh and src[i] <= p
                isFound := false
        if isFound and length * 2 <= bar_index
            [time[length], p]
        else
            [int(na), float(na)]

[iH, pH] = pivots(high, math.floor(depth / 2), true)
[iL, pL] = pivots(low, math.floor(depth / 2), false)

calc_dev(base_price, price) =>
    100 * (price - base_price) / base_price

price_rotation_aggregate(price_rotation, pLast, cum_volume) =>
    str = ""
    if display_reversal_price
        str += str.tostring(pLast, format.mintick) + " "
    if display_reversal_price_change
        str += price_rotation + " "
    if display_cumulative_volume
        str += "\n" + cum_volume
    str
    
caption(isHigh, iLast, pLast, price_rotation, cum_volume) =>
    price_rotation_str = price_rotation_aggregate(price_rotation, pLast, cum_volume)
    if display_reversal_price or display_reversal_price_change or display_cumulative_volume
        if not isHigh
            label.new(iLast, pLast, text=price_rotation_str, style=label.style_none, xloc=xloc.bar_time, yloc=yloc.belowbar, textcolor=color.red)
        else
            label.new(iLast, pLast, text=price_rotation_str, style=label.style_none, xloc=xloc.bar_time, yloc=yloc.abovebar, textcolor=color.green)

price_rotation_diff(pLast, price) =>
    if display_reversal_price_change
        tmp_calc = price - pLast
        str = difference_price == "Absolute"? (math.sign(tmp_calc) > 0? "+" : "") + str.tostring(tmp_calc, format.mintick) : (math.sign(tmp_calc) > 0? "+" : "-") + str.tostring((math.abs(tmp_calc) * 100)/pLast, format.percent)
        str := "(" + str  + ")"
        str
    else
        ""

var line lineLast = na
var label labelLast = na
var int iLast = 0
var float pLast = 0
var bool isHighLast = true // otherwise the last pivot is a low pivot
var int linesCount = 0
var float sumVol = 0
var float sumVolLast = 0

pivotFound(dev, isHigh, index, price) =>
    if isHighLast == isHigh and not na(lineLast)
        // same direction
        if isHighLast ? price > pLast : price < pLast
            if linesCount <= 1
                line.set_xy1(lineLast, index, price)
            line.set_xy2(lineLast, index, price)
            label.set_xy(labelLast, index, price)
            label.set_text(labelLast, price_rotation_aggregate(price_rotation_diff(line.get_y1(lineLast), price), price, str.tostring(sumVol + sumVolLast, format.volume)))
            [lineLast, labelLast, isHighLast, false, sumVol + sumVolLast]
        else
            [line(na), label(na), bool(na), false, float(na)]
    else // reverse the direction (or create the very first line)
        if na(lineLast)
            id = line.new(index, price, index, price, xloc=xloc.bar_time, color=line_color, width=2)
            lb = caption(isHigh, index, price, price_rotation_diff(pLast, price),  str.tostring(sumVol, format.volume))
            [id, lb, isHigh, true, sumVol]
        else
            // price move is significant
            if math.abs(dev) >= dev_threshold
                id = line.new(iLast, pLast, index, price, xloc=xloc.bar_time, color=line_color, width=2)
                lb = caption(isHigh, index, price, price_rotation_diff(pLast, price),  str.tostring(sumVol, format.volume))
                [id, lb, isHigh, true, sumVol]
            else
                [line(na), label(na), bool(na), false, float(na)]

sumVol += nz(volume[math.floor(depth / 2)])

if not na(iH) and not na(iL) and iH == iL
    dev1 = calc_dev(pLast, pH)
    [id2, lb2, isHigh2, isNew2, sum2] = pivotFound(dev1, true, iH, pH)
    if isNew2
        linesCount := linesCount + 1
    if not na(id2)
        lineLast := id2
        labelLast := lb2
        isHighLast := isHigh2
        iLast := iH
        pLast := pH
        sumVolLast := sum2
        sumVol := 0
    dev2 = calc_dev(pLast, pL)
    [id1, lb1, isHigh1, isNew1, sum1] = pivotFound(dev2, false, iL, pL)
    if isNew1
        linesCount := linesCount + 1
    if not na(id1)
        lineLast := id1
        labelLast := lb1
        isHighLast := isHigh1
        iLast := iL
        pLast := pL
        sumVolLast := sum1
        sumVol := 0
else
    if not na(iH)
        dev1 = calc_dev(pLast, pH)
        [id, lb, isHigh, isNew, sum] = pivotFound(dev1, true, iH, pH)
        if isNew
            linesCount := linesCount + 1
        if not na(id)
            lineLast := id
            labelLast := lb
            isHighLast := isHigh
            iLast := iH
            pLast := pH
            sumVolLast := sum
            sumVol := 0
    else
        if not na(iL)
            dev2 = calc_dev(pLast, pL)
            [id, lb, isHigh, isNew, sum] = pivotFound(dev2, false, iL, pL)
            if isNew
                linesCount := linesCount + 1
            if not na(id)
                lineLast := id
                labelLast := lb
                isHighLast := isHigh
                iLast := iL
                pLast := pL
                sumVolLast := sum
                sumVol := 0

var line extend_line = na
var label extend_label = na
if extend_to_last_bar == true and barstate.islast == true
    isHighLastPoint = not isHighLast
    curSeries = isHighLastPoint ? high : low
    if na(extend_line) and na(extend_label)
        extend_line := line.new(line.get_x2(lineLast), line.get_y2(lineLast), time, curSeries, xloc=xloc.bar_time, color=line_color, width=2)
        extend_label := caption(not isHighLast, time, curSeries,  price_rotation_diff(line.get_y2(lineLast), curSeries), str.tostring(sumVol, format.volume))  
    line.set_xy1(extend_line, line.get_x2(lineLast), line.get_y2(lineLast))
    line.set_xy2(extend_line, time, curSeries)
    
    price_rotation = price_rotation_diff(line.get_y1(extend_line), curSeries)                                                                                                  
    remaingRealTimeVol = 0.
    for i = math.abs(math.floor(depth / 2) - 1) to 0
        remaingRealTimeVol += volume[i]
    label.set_xy(extend_label, time, curSeries)    
    label.set_text(extend_label, price_rotation_aggregate(price_rotation, curSeries, str.tostring(sumVol+remaingRealTimeVol, format.volume)))
    label.set_textcolor(extend_label, isHighLastPoint? color.green : color.red)
    label.set_yloc(extend_label, yloc= isHighLastPoint? yloc.abovebar : yloc.belowbar)