Lean icon indicating copy to clipboard operation
Lean copied to clipboard

Implement ZigZag Indicator

Open AlexCatarino opened this issue 6 years ago • 1 comments

Expected Behavior

ZigZag is supported.

Actual Behavior

ZigZag is not supported.

Potential Solution

Implement ZigZag indicator.

This indicator is available in Python with this package: https://github.com/jbn/ZigZag We could just add it to the Foundation, but it would only be available for Python algorithms and wouldn't benefit from automatic updates and warm up.

Checklist

  • [x] I have completely filled out this template
  • [x] I have confirmed that this issue exists on the current master branch
  • [x] I have confirmed that this is not a duplicate issue by searching issues

AlexCatarino avatar May 09 '19 13:05 AlexCatarino

Also available as a Python talipp implementation

femtotrader avatar Jul 05 '24 14:07 femtotrader

spy_zigzag.csv

JosueNina avatar Dec 10 '24 17:12 JosueNina

from talipp.indicators import ZigZag
from talipp.ohlcv import OHLCVFactory
import pandas as pd

history = pd.read_csv("https://github.com/QuantConnect/Lean/raw/master/Data/equity/usa/daily/spy.zip",
                      index_col=0, names=["open", "high", "low", "close", "volume"])

ohlcv = OHLCVFactory.from_matrix2([
    history.open.values,  
    history.high.values,  
    history.low.values, 
    history.close.values,  
    history.volume.values   
])

# ZigZag
sensitivity = 0.05  
minTrendLength = 10
zigzag = ZigZag(sensitivity, minTrendLength, ohlcv)

history['zigzag'] = None

for x in zigzag:
    matching_row = history[
        (history['open'] == x.ohlcv.open) & 
        (history['high'] == x.ohlcv.high) & 
        (history['low'] == x.ohlcv.low) & 
        (history['close'] == x.ohlcv.close)
    ]
    if not matching_row.empty:
        if x.type.value == 2:  # HIGH
            history.loc[matching_row.index[0], 'zigzag'] = x.ohlcv.high
        elif x.type.value == 1:  # LOW
            history.loc[matching_row.index[0], 'zigzag'] = x.ohlcv.low

output_csv = "spy_zigzag.csv"
history.to_csv(output_csv, index_label="date")


JosueNina avatar Dec 10 '24 17:12 JosueNina

#8454 A key detail to know is that the values are calculated one at a time, so we don't know if a higher or lower point might exist before finding the next pivot. The exposed attributes LowPivot/HighPivot will hold the values of the latest LowPivot and HighPivot, updating these values on each iteration if a lower or higher point is found or if it's time to calculate the next pivot.

JosueNina avatar Dec 10 '24 18:12 JosueNina