Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ZigZag Indicator #3179

Closed
3 tasks done
AlexCatarino opened this issue May 9, 2019 · 4 comments · Fixed by #8454
Closed
3 tasks done

Implement ZigZag Indicator #3179

AlexCatarino opened this issue May 9, 2019 · 4 comments · Fixed by #8454
Labels
feature good first issue If you're looking to get started as a LEAN contributor, check out these starter issues! indicator

Comments

@AlexCatarino
Copy link
Member

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

  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues
@jaredbroad jaredbroad added feature good first issue If you're looking to get started as a LEAN contributor, check out these starter issues! and removed up for grabs labels Feb 16, 2023
@femtotrader
Copy link
Contributor

Also available as a Python talipp implementation

@JosueNina
Copy link
Contributor

spy_zigzag.csv

@JosueNina
Copy link
Contributor

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
Copy link
Contributor

JosueNina commented Dec 10, 2024

#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature good first issue If you're looking to get started as a LEAN contributor, check out these starter issues! indicator
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants