Created
July 1, 2021 05:05
-
-
Save NVentimiglia/60b8f06c0814ff4abef68085d7f1d7ca to your computer and use it in GitHub Desktop.
Zig Zag Percent TradingView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@version=4 | |
study("Zig Zag Percent [Nic]", overlay = true, max_bars_back = 500) | |
prd1 = input(defval = 20, title="ZigZag Period 1", minval = 2, maxval = 1000) | |
showlabel = input(defval = true, title = "Show Label") | |
showhline = input(defval = true, title = "Show Pivot Line") | |
showline = input(defval = true, title = "Show Zig Line") | |
upcol1 = input(defval = color.green, title = "Zig Zag Up Color") | |
dncol1 = input(defval = color.red, title = "Zig Zag Down Color") | |
txtcol = input(defval = color.white, title = "Text Color") | |
zz1style = input(defval = "Dashed", title = "Zig Zag Line Style", options = ["Dashed", "Dotted"]) | |
zz1width = input(defval = 2, title = "Zig zag Line Width", minval = 1, maxval = 4) | |
zz2color = input(defval = #2072cb, title = "Pivot Color") | |
zz2style = input(defval = "Dashed", title = "Zig Zag Line Style", options = ["Dashed", "Dotted"]) | |
zz2width = input(defval = 1, title = "Zig zag Line Width", minval = 1, maxval = 4) | |
float ph1 = highestbars(high, prd1) == 0 ? high : na | |
float pl1 = lowestbars(low, prd1) == 0 ? low : na | |
var dir1 = 0 | |
dir1 := iff(ph1 and na(pl1), 1, iff(pl1 and na(ph1), -1, dir1)) | |
truncate(number, decimals) => | |
factor = pow(10, decimals) | |
int(number * factor) / factor | |
percent(n1, n2) => | |
((n1 - n2) / n2) * 100 | |
var max_array_size = 10 // [5, 2] matrix | |
var zigzag1 = array.new_float(0) | |
var zigzag2 = array.new_float(0) | |
add_to_zigzag(pointer, value, bindex)=> | |
array.unshift(pointer, bindex) | |
array.unshift(pointer, value) | |
if array.size(pointer) > max_array_size | |
array.pop(pointer) | |
array.pop(pointer) | |
update_zigzag(pointer, value, bindex, dir)=> | |
if array.size(pointer) == 0 | |
add_to_zigzag(pointer, value, bindex) | |
else | |
if (dir == 1 and value > array.get(pointer, 0)) or (dir == -1 and value < array.get(pointer, 0)) | |
array.set(pointer, 0, value) | |
array.set(pointer, 1, bindex) | |
0. | |
dir1changed = change(dir1) | |
if ph1 or pl1 | |
if dir1changed | |
add_to_zigzag(zigzag1, dir1 == 1 ? ph1 : pl1, bar_index) | |
else | |
update_zigzag(zigzag1, dir1 == 1 ? ph1 : pl1, bar_index, dir1) | |
if array.size(zigzag1) >= 6 | |
var line zzline1 = na | |
var line zzline2 = na | |
var label zzlabel1 = na | |
float val = array.get(zigzag1, 0) | |
int point = round(array.get(zigzag1, 1)) | |
float val1 = array.get(zigzag1, 2) | |
if (change(val) or change(point) ) | |
int point1 = round(array.get(zigzag1, 3)) | |
plabel = tostring(truncate(percent(val,val1), 3)) + "%" | |
if change(val1) == 0 and change(point1) == 0 | |
line.delete(zzline1) | |
line.delete(zzline2) | |
label.delete(zzlabel1) | |
if(showline) | |
zzline1 := line.new(x1 = point, x2 = point1, y1 = val, y2 = val1, color = dir1 == 1 ? upcol1 : dncol1, width = zz1width, style = zz1style == "Dashed" ? line.style_dashed : line.style_dotted) | |
labelcol = dir1 == 1 ? array.get(zigzag1, 0) > array.get(zigzag1, 4) ? upcol1 : dncol1 : array.get(zigzag1, 0) < array.get(zigzag1, 4) ? dncol1 : upcol1 | |
if(showhline) | |
zzline2 := line.new(bar_index[1], val, point, val, color = zz2color, width = zz2width, style = zz2style == "Dashed" ? line.style_dashed : line.style_dotted) | |
line.set_extend(id=zzline2, extend=extend.right) | |
if showlabel | |
zzlabel1 := label.new(x = point, y = val, text = plabel, color = labelcol, textcolor = txtcol, style = dir1 == 1 ? label.style_label_down : label.style_label_up) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment