计算锯齿线的 slope/derivative 值
Compute slope/derivative values of zigzag line
我想计算每条锯齿线的 slope/derivative,并向他们展示 above/below 锯齿线。如何做到这一点?
锯齿形示例:
//@version=4
study("ZigZagSlope", overlay = true, max_bars_back = 500, max_lines_count = 300)
prd = input(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 = highestbars(high, prd) == 0 ? high : na
float pl = lowestbars(low, prd) == 0 ? low : na
var dir = 0
dir := iff(ph and na(pl), 1, iff(pl and na(ph), -1, dir))
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)=> round(value / syminfo.mintick) * syminfo.mintick
dirchanged = 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 = round(array.get(zigzag, 1))
if change(val) or change(point)
float val1 = array.get(zigzag, 2)
int point1 = round(array.get(zigzag, 3))
if change(val1) == 0 and change(point1) == 0
line.delete(zzline)
zzline := line.new(x1 = point, y1 = val, x2 = point1, y2 = val1, color = dir == 1 ? upcol : dncol, width = 2)
导数计算示例:
https://www.tradingview.com/script/1Ea3KEUq-MaxWarren-s-Pine-Acceleration-2nd-Derivative/
坡度超过 运行,在本例中是 (val - val1) / (point - point1)
。这是 bar_index
差价。
您可以按如下方式将其添加到您的脚本中,我已将变量值包含在标签中,因此您可以手动确认它们。
将当前斜率与同方向的第二个斜率进行比较,我们需要首先通过再次检查数组大小来检查它是否存在于数组中。然后我们可以从数组中检索价格和 bar_index 值并进行相同的 rise/run 计算并将现有之字折线的斜率与前第二个之字折线的斜率进行比较。
//@version=4
study("ZigZagSlope", overlay = true, max_bars_back = 500, max_lines_count = 300)
prd = input(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 = highestbars(high, prd) == 0 ? high : na
float pl = lowestbars(low, prd) == 0 ? low : na
var dir = 0
dir := iff(ph and na(pl), 1, iff(pl and na(ph), -1, dir))
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)=> round(value / syminfo.mintick) * syminfo.mintick
dirchanged = 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
var label slope_label = na
float val = array.get(zigzag, 0)
int point = round(array.get(zigzag, 1))
if change(val) or change(point)
float val1 = array.get(zigzag, 2)
int point1 = round(array.get(zigzag, 3))
if change(val1) == 0 and change(point1) == 0
line.delete(zzline)
label.delete(slope_label)
zzline := line.new(x1 = point, y1 = val, x2 = point1, y2 = val1, color = dir == 1 ? upcol : dncol, width = 2)
slope = (val - val1) / (point - point1)
debug_vals_text = "\nval : " + tostring(val) + "\nval1 : " + tostring(val1) + "\npoint : " + tostring(point) + "\npoint1 : " + tostring(point1)
slope_text = "slope : " + tostring(slope)
string slope2_text = na
float slope2 = na
if array.size(zigzag) >= 8
val2 = array.get(zigzag, 4)
point2 = array.get(zigzag, 5)
val3 = array.get(zigzag, 6)
point3 = array.get(zigzag, 7)
slope2 := (val2 - val3) / (point2 - point3)
slope2_text := "\nval2 : " + tostring(val2) + "\nval3 : " + tostring(val3) + "\npoint2 : " + tostring(point2) + "\npoint3 : " + tostring(point3) + "\nprev slope : " + tostring(slope2)
slope_diff_text = "\n\nslope diff : " + tostring(slope - slope2)
slope_label := label.new(x = point, y = val, text = slope_text + "\n\n" + debug_vals_text + slope2_text + slope_diff_text, style = val > val1 ? label.style_label_down : label.style_label_up, size = size.small)
我想计算每条锯齿线的 slope/derivative,并向他们展示 above/below 锯齿线。如何做到这一点?
锯齿形示例:
//@version=4
study("ZigZagSlope", overlay = true, max_bars_back = 500, max_lines_count = 300)
prd = input(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 = highestbars(high, prd) == 0 ? high : na
float pl = lowestbars(low, prd) == 0 ? low : na
var dir = 0
dir := iff(ph and na(pl), 1, iff(pl and na(ph), -1, dir))
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)=> round(value / syminfo.mintick) * syminfo.mintick
dirchanged = 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 = round(array.get(zigzag, 1))
if change(val) or change(point)
float val1 = array.get(zigzag, 2)
int point1 = round(array.get(zigzag, 3))
if change(val1) == 0 and change(point1) == 0
line.delete(zzline)
zzline := line.new(x1 = point, y1 = val, x2 = point1, y2 = val1, color = dir == 1 ? upcol : dncol, width = 2)
导数计算示例:
https://www.tradingview.com/script/1Ea3KEUq-MaxWarren-s-Pine-Acceleration-2nd-Derivative/
坡度超过 运行,在本例中是 (val - val1) / (point - point1)
。这是 bar_index
差价。
您可以按如下方式将其添加到您的脚本中,我已将变量值包含在标签中,因此您可以手动确认它们。
将当前斜率与同方向的第二个斜率进行比较,我们需要首先通过再次检查数组大小来检查它是否存在于数组中。然后我们可以从数组中检索价格和 bar_index 值并进行相同的 rise/run 计算并将现有之字折线的斜率与前第二个之字折线的斜率进行比较。
//@version=4
study("ZigZagSlope", overlay = true, max_bars_back = 500, max_lines_count = 300)
prd = input(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 = highestbars(high, prd) == 0 ? high : na
float pl = lowestbars(low, prd) == 0 ? low : na
var dir = 0
dir := iff(ph and na(pl), 1, iff(pl and na(ph), -1, dir))
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)=> round(value / syminfo.mintick) * syminfo.mintick
dirchanged = 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
var label slope_label = na
float val = array.get(zigzag, 0)
int point = round(array.get(zigzag, 1))
if change(val) or change(point)
float val1 = array.get(zigzag, 2)
int point1 = round(array.get(zigzag, 3))
if change(val1) == 0 and change(point1) == 0
line.delete(zzline)
label.delete(slope_label)
zzline := line.new(x1 = point, y1 = val, x2 = point1, y2 = val1, color = dir == 1 ? upcol : dncol, width = 2)
slope = (val - val1) / (point - point1)
debug_vals_text = "\nval : " + tostring(val) + "\nval1 : " + tostring(val1) + "\npoint : " + tostring(point) + "\npoint1 : " + tostring(point1)
slope_text = "slope : " + tostring(slope)
string slope2_text = na
float slope2 = na
if array.size(zigzag) >= 8
val2 = array.get(zigzag, 4)
point2 = array.get(zigzag, 5)
val3 = array.get(zigzag, 6)
point3 = array.get(zigzag, 7)
slope2 := (val2 - val3) / (point2 - point3)
slope2_text := "\nval2 : " + tostring(val2) + "\nval3 : " + tostring(val3) + "\npoint2 : " + tostring(point2) + "\npoint3 : " + tostring(point3) + "\nprev slope : " + tostring(slope2)
slope_diff_text = "\n\nslope diff : " + tostring(slope - slope2)
slope_label := label.new(x = point, y = val, text = slope_text + "\n\n" + debug_vals_text + slope2_text + slope_diff_text, style = val > val1 ? label.style_label_down : label.style_label_up, size = size.small)