//@version=4
study("Zig Zag", overlay=true)
dev_threshold = input(title="Deviation (%)", type=input.float, defval=5, minval=1, maxval=100)
depth = input(title="Depth", type=input.integer, defval=10, minval=1)
pivots(src, length, isHigh) =>
p = nz(src[length])
if length == 0
[bar_index, p]
else
isFound = true
for i = 0 to 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
[bar_index[length], p]
else
[int(na), float(na)]
[iH, pH] = pivots(high, floor(depth / 2), true)
[iL, pL] = pivots(low, floor(depth / 2), false)
calc_dev(base_price, price) =>
100 * (price - base_price) / base_price
var line lineLast = 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
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)
[lineLast, isHighLast, false]
else
[line(na), bool(na), false]
else // reverse the direction (or create the very first line)
if na(lineLast)
id = line.new(index, price, index, price, color=color.red, width=2)
[id, isHigh, true]
else
// price move is significant
if abs(dev) >= dev_threshold
id = line.new(iLast, pLast, index, price, color=color.red, width=2)
[id, isHigh, true]
else
[line(na), bool(na), false]
if not na(iH) and not na(iL) and iH == iL
dev1 = calc_dev(pLast, pH)
[id2, isHigh2, isNew2] = pivotFound(dev1, true, iH, pH)
if isNew2
linesCount := linesCount + 1
if not na(id2)
lineLast := id2
isHighLast := isHigh2
iLast := iH
pLast := pH
dev2 = calc_dev(pLast, pL)
[id1, isHigh1, isNew1] = pivotFound(dev2, false, iL, pL)
if isNew1
linesCount := linesCount + 1
if not na(id1)
lineLast := id1
isHighLast := isHigh1
iLast := iL
pLast := pL
else
if not na(iH)
dev1 = calc_dev(pLast, pH)
[id, isHigh, isNew] = pivotFound(dev1, true, iH, pH)
if isNew
linesCount := linesCount + 1
if not na(id)
lineLast := id
isHighLast := isHigh
iLast := iH
pLast := pH
else
if not na(iL)
dev2 = calc_dev(pLast, pL)
[id, isHigh, isNew] = pivotFound(dev2, false, iL, pL)
if isNew
linesCount := linesCount + 1
if not na(id)
lineLast := id
isHighLast := isHigh
iLast := iL
pLast := pL
Overview
The script identifies local pivot highs and lows using a custom loop mechanism based on the depth setting, then connects them with drawing lines when price movement exceeds the specified percentage deviation threshold. The main settings adjust the depth lookback window and the minimum percentage deviation required for dynamic line creation. Note that pivot lines update retroactively as new price extremes form.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Deviation (%)
float
5
dev_threshold
Depth
int
10
depth
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
This indicator detects equal highs and lows by comparing prices within user-defined left and right bars using a tolerance percentage. It plots horizontal lines on the price chart for equal highs in one color and lows in another. A screener allows input of mult
This indicator fits a linear regression through stored pivot highs and lows and draws the resulting channel on the price chart. A function returns predicted values, maximum and minimum deviations, and the average absolute deviation. Confirmed two-bar pivots up
AI Trend is an indicator that plots pivot points, trend lines, and support and resistance levels directly on the price chart. It marks high and low pivots based on user-defined length, draws trend lines that check for violations at specified points, and identi
This indicator plots Ichimoku Cloud elements on the price chart, including Tenkan-sen, Kijun-sen, Senkou Span A and B lines with optional Chikou Span. It signals trend changes via Tenkan-sen and Kijun-sen crosses and places arrows at Kijun-sen direction shifts
This indicator identifies local swing highs and swing lows to construct a Zig Zag pattern on the main price chart. It evaluates pivot points based on a customizable number of surrounding left and right bars. Using persistent variables, the script tracks previo
Dynamic Linear Regression calculates and plots linear regression trendlines and standard deviation channels directly on the price chart over a user-selected time period. Traders can configure static channels locked at the end time, dynamic channels that contin