You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何计算zigzag锯齿线的斜率/导数并在其上下方显示对应数值

ZigZag锯齿线斜率计算与显示实现方案

实现逻辑

单段锯齿线的斜率计算公式为:(后拐点价格 - 前拐点价格) / 两个拐点间隔的K线数量,得到每根K线对应的平均价格变动幅度,也就是该段锯齿线的斜率。我们会在每段锯齿线的中点位置显示斜率数值,上升段数值显示在线段上方,下降段显示在线段下方避免遮挡。

完整可运行代码

//@version=4
study("ZigZagSlope", overlay = true, max_bars_back = 500, max_lines_count = 300, max_labels_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")
// 新增斜率显示相关参数
showslope = input(defval = true, title = "Show Slope Value")
slope_text_size = input(defval = size.small, title = "Slope Text Size", options = [size.tiny, size.small, size.normal, size.large])
text_offset = input(defval = 0.1, title = "Slope Text Offset", tooltip = "调整文字与线段的垂直距离,单位为最小报价单位")

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)
        
        // 新增斜率计算与显示逻辑
        if showslope
            var label slp_label = na
            // 计算斜率
            price_diff = val - val1
            bar_diff = point - point1
            slope = price_diff / bar_diff
            // 确定文本显示在中点位置
            mid_x = (point + point1) / 2
            mid_y = (val + val1) / 2
            // 上升段显示在上,下降段显示在下
            text_y = mid_y + (dir == 1 ? syminfo.mintick * text_offset : -syminfo.mintick * text_offset)
            // 删除旧文本,创建新文本
            label.delete(slp_label)
            slp_label := label.new(
                 x = mid_x, 
                 y = text_y, 
                 text = str.tostring(Round_it(slope)), 
                 color = color.gray, 
                 textcolor = dir == 1 ? upcol : dncol, 
                 size = slope_text_size, 
                 style = label.style_label_center
            )

自定义调整说明

  • 如果需要显示百分比斜率,将斜率计算行替换为slope = (val / val1 - 1) * 100 / bar_diff,即可得到每根K线的平均涨跌幅斜率
  • 可以通过调整Slope Text Offset参数修改文字和锯齿线的距离,避免重叠
  • 可以修改文本颜色、背景色参数适配你的图表配色

内容的提问来源于stack exchange,提问作者User4536124

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 03:45:03