The question was previously answered at this page, but the answer is in version 4. Compute slope/derivative values of zigzag line but it was written for version 4.
I would appreciate any help in getting the code in version 5. Thank you.
The question was previously answered at this page, but the answer is in version 4. Compute slope/derivative values of zigzag line but it was written for version 4.
I would appreciate any help in getting the code in version 5. Thank you.
You can paste this code in your pinescript panel, then go to the up rigth of this panel and choose 'convert to V5':
Here is the result :
//@version=5
indicator('ZigZagSlope', overlay=true, max_bars_back=500, max_lines_count=300)
prd = input.int(defval=3, title='ZigZag Period', minval=2, maxval=50)
showzigzag = input(defval=true, title='Show Zig Zag')
upcol = input(defval=color.black, title='Zigzag Up Color')
dncol = input(defval=color.black, title='Zigzag Down Color')
float ph = ta.highestbars(high, prd) == 0 ? high : na
float pl = ta.lowestbars(low, prd) == 0 ? low : na
var dir = 0
iff_1 = pl and na(ph) ? -1 : dir
dir := ph and na(pl) ? 1 : iff_1
var max_array_size = 10
var zigzag = array.new_float(0)
add_to_zigzag(value, bindex) =>
array.unshift(zigzag, bindex)
array.unshift(zigzag, value)
if array.size(zigzag) > max_array_size
array.pop(zigzag)
array.pop(zigzag)
update_zigzag(value, bindex) =>
if array.size(zigzag) == 0
add_to_zigzag(value, bindex)
else
if dir == 1 and value > array.get(zigzag, 0) or dir == -1 and value < array.get(zigzag, 0)
array.set(zigzag, 0, value)
array.set(zigzag, 1, bindex)
0.
Round_it(value) =>
math.round(value / syminfo.mintick) * syminfo.mintick
dirchanged = ta.change(dir)
if ph or pl
if dirchanged
add_to_zigzag(dir == 1 ? ph : pl, bar_index)
else
update_zigzag(dir == 1 ? ph : pl, bar_index)
if showzigzag and array.size(zigzag) >= 4
var line zzline = na
float val = array.get(zigzag, 0)
int point = math.round(array.get(zigzag, 1))
if ta.change(val) or ta.change(point)
float val1 = array.get(zigzag, 2)
int point1 = math.round(array.get(zigzag, 3))
if ta.change(val1) == 0 and ta.change(point1) == 0
line.delete(zzline)
zzline := line.new(x1=point, y1=val, x2=point1, y2=val1, color=dir == 1 ? upcol : dncol, width=2)
zzline