Summary
ThinkScript’s ZigZagHighLow
function identifies significant swing points by filtering out minor price movements. Python doesn’t have a direct equivalent in the standard library, but you can replicate this indicator using pandas or a third‑party library. This article explains the algorithm and shows how to implement a ZigZag in Python.
Understanding the ZigZag indicator
The ZigZag indicator plots lines between swing highs and swing lows when price changes exceed a percentage threshold. In ThinkScript, you might see something like:
def EI = ZigZagHighLow(
"price h" = priceh,
"price l" = pricel,
"percentage reversal" = percentAmount,
"absolute reversal" = revAmount,
"atr length" = atrLength,
"atr reversal" = atrReversal
);
The parameters specify the high/low prices, reversal thresholds, and ATR settings. In Python you can achieve the same result by computing pivot points when the price reverses by a certain percentage or by a multiple of ATR.
Option 1: Use the stock_indicators library
The Stock Indicators for Python library includes a ready‑made Zig Zag function. The API call looks like this:
from stock_indicators import indicators
from stock_indicators import EndType
# quotes should be a list of Quote objects or a DataFrame with date/high/low/close/volume columns.
results = indicators.get_zig_zag(
quotes,
end_type=EndType.HIGH_LOW, # use high/low for ZigZagHighLow
percent_change=5 # percent change needed to mark a new point
)
Key parameters:
- quotes: a pandas DataFrame or iterable of
Quote
objects. - end_type: choose
EndType.HIGH_LOW
to use high/low prices. - percent_change: the percentage change required to form a new pivot (e.g.,
3
for 3 %).
The function returns a list of objects with fields like zig_zag
and point_type
(H for high, L for low). You can plot these points to replicate ThinkScript’s ZigZagHighLow.
Option 2: Roll your own using pandas
If you prefer not to use an external library, you can implement a simple ZigZag algorithm:
- Load your price data into a pandas DataFrame with
high
andlow
columns. - Choose a reversal threshold (percent change or ATR multiple).
- Iterate through the data, tracking the current trend (up or down). When the price reverses by more than your threshold, record a pivot point.
- Store the pivot points and connect them with lines.
An example pseudo‑code:
import pandas as pd
def zigzag(df, threshold_pct):
pivots = []
last_pivot = df.iloc[0]['close']
direction = 0 # 1=uptrend, -1=downtrend, 0=unknown
for idx, row in df.iterrows():
price = row['high'] if direction <= 0 else row['low']
change = (price - last_pivot) / last_pivot * 100
if direction <= 0 and change >= threshold_pct:
pivots.append(('low', row.name, row['low']))
last_pivot = row['low']
direction = 1
elif direction >= 0 and change <= -threshold_pct:
pivots.append(('high', row.name, row['high']))
last_pivot = row['high']
direction = -1
return pivots
This simplified example uses percent change; you can modify it to use ATR or absolute values.
Conclusion
ThinkScript’s ZigZagHighLow
indicator filters price swings using reversal thresholds. In Python you can either rely on the stock_indicators
library, which offers a get_zig_zag
function with high/low
inputs, or write your own algorithm in pandas. Specify the reversal threshold and high/low data to generate swing points, and plot them to visualize market structure.