简体   繁体   English

计算锯齿线的斜率/导数

[英]Compute slope/derivative values of zigzag line

I would like to compute the slope/derivative of each zigzag line, and show them above/below the zigzag line.我想计算每条锯齿线的斜率/导数,并将它们显示在锯齿线的上方/下方。 How to do this?这该怎么做?

Example of zigzag:之字形示例:

//@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)

Example of computation of derivatives:导数计算示例:

https://www.tradingview.com/script/1Ea3KEUq-MaxWarren-s-Pine-Acceleration-2nd-Derivative/ https://www.tradingview.com/script/1Ea3KEUq-MaxWarren-s-Pine-Acceleration-2nd-Derivative/

Slope is rise over run, in this case it is by (val - val1) / (point - point1) .斜率上升超过运行,在这种情况下它是由(val - val1) / (point - point1) Which is the price difference over the the bar_index difference.这是bar_index差异的价格差异。

You can add it to your script as follows, I've included the variable values in the label so you can confirm them manually.您可以按如下方式将其添加到您的脚本中,我已将变量值包含在标签中,以便您可以手动确认它们。

//@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)
        slope_label := label.new(x = point, y = val, text = slope_text + "\n\n" + debug_vals_text , style = val > val1 ? label.style_label_down : label.style_label_up, size = size.small)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM