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

ZigZag Indicator, Sometime Showing Incorrect Waves for Deviation: 0.1% or more #856

Open
divyankm opened this issue Nov 27, 2024 · 6 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@divyankm
Copy link

divyankm commented Nov 27, 2024

Which version are you running? The lastest version is on Github. Pip is for major releases.

import pandas_ta as ta
print(ta.version)
0.4.19b0

Have you tried the development version? Did it resolve the issue?

$ pip install -U git+https://github.com/twopirllc/pandas-ta.git@development
Yes

Describe the bug
Sometime the ZigZag Waves are not formed correctly for Deviation: 0.1% or greater

To Reproduce

!pip install -U git+https://github.com/twopirllc/pandas-ta.git@development
from pandas_ta import zigzag
import pandas_ta as ta
import pandas as pd
import numpy as np
import plotly.io as pio
from datetime import date
from datetime import datetime, timedelta
from pytz import timezone
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
import plotly.io as pio
import yfinance as yf
# Define the stock ticker symbol (replace 'ES=F' with your desired stock symbol)
ticker = 'ES=F'
# Fetch the last 1 day of data with 1-minute intervals
data = yf.download(ticker, interval='1m', period='5d')
# Convert the datetime index from UTC to Eastern Time (US/Eastern)
data.index = data.index.tz_convert('US/Eastern')
df2 = data.copy()
# Flatten the MultiIndex and remove the ticker part (symbol name)
df2.columns = df2.columns.get_level_values('Price')

# Reset index to get a default integer-based index
df2.reset_index(drop=False, inplace=True)

# Using the Pandas-TA zigzag indicator (or other equivalent library like ta-lib or ta)
zigzag_result = ta.zigzag(
    high=df2['High'],      # High prices of the asset
    low=df2['Low'],        # Low prices of the asset
    close=df2['Close'],    # Closing prices of the asset
    legs=3,                # Minimum number of price points required for a significant trend change
    deviation=0.01,        # Minimum 1% price movement to form a new zigzag segment (1% deviation)
    retrace=True,          # Consider retraces before identifying new points
    last_extreme=True,     # Consider the last extreme point in the calculation
    offset=0               # No time offset, meaning Zigzag points will be calculated from the current time
)

df2 = df2.join(zigzag_result)

zigzag_result2 = ta.zigzag(
    high=df2['High'],
    low=df2['Low'],
    close=df2['Close'],
    legs=3,
    deviation=0.1, #0.07
    retrace=True,
    last_extreme=True,
    offset=0
)

df2 = df2.join(zigzag_result2,rsuffix='_zigzag')
df2 = df2.iloc[:-1]

# Filter non-NaN values in ZIGZAGv_0.01%_3 for plotting
valid_zigzag_data = df2[df2['ZIGZAGv_0.01%_3'].notna()]

# Create the figure
fig = go.Figure()

# Adding Candlestick trace
fig.add_trace(go.Candlestick(
    x=df2['Datetime'],  # Assuming `Datetime` is set as index
    open=df2['Open'],
    high=df2['High'],
    low=df2['Low'],
    close=df2['Close'],
    name='Candlesticks'
))

# Adding Zigzag line trace with dots
fig.add_trace(go.Scatter(
    x=valid_zigzag_data['Datetime'],  # Non-NaN indices
    y=valid_zigzag_data['ZIGZAGv_0.01%_3'],  # Non-NaN Zigzag values
    mode='markers+lines',  # Both markers (dots) and lines
    line=dict(color='rgba(0, 0, 255, 0.9)', width=2.5),  # Customize the line color and width
    marker=dict(color='red', size=4),  # Customize the dots color and size
    name='Wave Formation 0.01% DEV'
))

# Filter non-NaN values in ZIGZAGv_0.01%_3 for plotting
valid_zigzag_data2 = df2[df2['ZIGZAGv_0.1%_3'].notna()]
# Adding Zigzag line trace with dots
fig.add_trace(go.Scatter(
    x=valid_zigzag_data2['Datetime'],  # Non-NaN indices
    y=valid_zigzag_data2['ZIGZAGv_0.1%_3'],  # Non-NaN Zigzag values
    mode='markers+lines',  # Both markers (dots) and lines
    line=dict(color='yellow', width=2.5),  # Customize the line color and width
    marker=dict(color='red', size=4),  # Customize the dots color and size
    name='Wave Formation 0.1% DEV'
))


# Set up layout
fig.update_layout(
    title="Price with Higher High and Lower Low Patterns",
    xaxis_title="Date",
    xaxis=dict(
        rangeslider=dict(
            visible=True  # Set to True to make the range slider visible
        )),
    yaxis=dict(
        autorange=True,  # Enable dynamic Y-axis range adjustment
        fixedrange=False  # Allow zooming on the y-axis
    ),
    yaxis_title="Price",
    template="plotly_dark",
    showlegend=True,
    legend=dict(
        orientation='h',  # Horizontal legend
        yanchor='top',
        y=1.5,  # Position legend slightly above the plot
        xanchor='left',
        x=0.5,  # Center the legend horizontally
        font=dict(
            color="grey"  # Change the legend text color here (e.g., 'white', 'black', or any valid CSS color)
        )
    )
)

# Show plot
fig.show()

# Optionally, save the figure to an HTML file
pio.write_html(fig, file='Wave Pattern Formation 5d.html', auto_open=True)

Expected behavior
As Data is coming on 1min Basis, sometimes zigzag are plotted correctly as below.
image

Screenshots
While Rest of Time Zigzag is not plotted correctly, like below.
image

@divyankm divyankm added the bug Something isn't working label Nov 27, 2024
@Ish2K
Copy link

Ish2K commented Nov 28, 2024

Could you share the dataset which results in inconsistent results? Perhaps you can make a temporary csv file to store results. My intuition is that maybe there are null values reported in dataset by yfinance. Did you try using other tickers? If yes, did you face occasional inconsistent results there too?

@divyankm
Copy link
Author

divyankm commented Nov 28, 2024

DataSet ES=F:
df2_output.csv

Output ScreenShot (Yellow Wave: Deviation: 0.1%):

My intuition is that maybe there are null values reported in dataset by yfinance.
No Null Values in OHLC Data, on df2_output.csv file

Datetime	0
Adj Close	0
Close	0
High	0
Low	0
Open	0
Volume	0
ZIGZAGs_0.01%_3	3460
ZIGZAGv_0.01%_3	3460
ZIGZAGd_0.01%_3	3460
ZIGZAGs_0.1%_3	5007
ZIGZAGv_0.1%_3	5007
ZIGZAGd_0.1%_3	5007

Current ScreenShot of ZigZag Waves on 0.1% Deviation for ES=F:
image

Did you try using other tickers? If yes, did you face occasional inconsistent results there too?

Yes, I check on Other Ticker: YM=F (Dow Futures) on 1 min interval, Same issue coming there.
Dow Futures DataSet Attached as well.
image

ds=f_output.csv

@twopirllc twopirllc removed their assignment Nov 30, 2024
@twopirllc twopirllc added the help wanted Extra attention is needed label Nov 30, 2024
@twopirllc
Copy link
Owner

Hopefully @aligheshlaghi97 has some insight on this or how to fix perhaps? 🤷🏼‍♂️

@aligheshlaghi97
Copy link

Hi @twopirllc
Thanks for mentioning me! I’ll take a look and try to address it during the next week.

@aligheshlaghi97
Copy link

Hi there everyone. After reviewing the data provided by @divyankm, I’ve come to the following conclusions:

1. Low legs values lead to inaccurate swing detection

When legs is set to very low values (e.g., legs=3), it often results in poor swing detection. For example, a minor bottom might be identified while the chart values are in their peak (or vice versa). Therefore, using very small legs values is not recommended.

2. Right-to-left swing calculation

My code, like TradingView's script, calculates swings from right to left. If the issue in point 1 occurs, the algorithm might fail to identify swings needed to form a proper zigzag. This can lead to large gaps in the chart. However, I couldn’t replicate this specific behavior using TradingView.

The data shared by @divyankm highlights an example of such gaps:

ex1 (1)

As shown in the chart, no valid peaks are possible after that bottom, leading to the gap.

Lastly, I couldn’t review all of the code in detail, but I would like to ask @twopirllc if there have been any major changes made since the previous code review that might be relevant to this issue.

@twopirllc
Copy link
Owner

@aligheshlaghi97

Usually I touch up PRs before pushing to development. Hopefully I didn’t make a mistake in that process. 😅

I’ll take a deeper look next week and compare the commits.

Thanks for helping. 😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants