我想计算每条锯齿线的斜率/导数,并将它们显示在锯齿线上方/下方。如何做到这一点?
之字形示例:
//@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 值,并进行相同的上涨/运行计算,并将现有之字形的斜率与前第二个斜率进行比较。
//@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)
我希望您可以展示斜率比较的脚本变体,因为我自己无法编写代码。谢谢!