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

请求将PineScript v4指标「Divergence for Many Indicators」升级至v5

PineScript v4 转 v5:「Divergence for Many Indicators」升级完成

已将LonesomeTheBlue开发的v4版本指标完整升级至PineScript v5,所有原有技术特性与功能均保留,包括RSI、MACD、Stochastic三类指标的顶/底背离检测、K线标记、指标窗口绘制等。

升级后完整代码

//@version=5
indicator("Divergence for Many Indicators v5", overlay=true, shorttitle="DivManyIndi v5", precision=2)

// ———— 输入设置 ————
src = input(close, title="Source")
length = input.int(14, title="Length")
rsiOB = input.int(70, title="RSI Overbought Level")
rsiOS = input.int(30, title="RSI Oversold Level")
macdFast = input.int(12, title="MACD Fast Length")
macdSlow = input.int(26, title="MACD Slow Length")
macdSignal = input.int(9, title="MACD Signal Length")
kLength = input.int(14, title="Stochastic %K Length")
dLength = input.int(3, title="Stochastic %D Length")
smoothK = input.int(3, title="Stochastic Smooth %K")
lookback = input.int(5, title="Divergence Lookback Period")

// ———— 指标计算 ————
// RSI
rsiVal = ta.rsi(src, length)
// MACD
[macdLine, signalLine, histLine] = ta.macd(src, macdFast, macdSlow, macdSignal)
// Stochastic
[kVal, dVal] = ta.stoch(src, high, low, kLength, dLength, smoothK)

// ———— 背离检测函数 ————
f_findDivergence(indVal, srcVal, lookback, isBearish) =>
    var float[] indPeaks = array.new_float()
    var float[] srcPeaks = array.new_float()
    var int[] peakBars = array.new_int()
    
    clear() =>
        array.clear(indPeaks)
        array.clear(srcPeaks)
        array.clear(peakBars)
    
    // 识别峰值/谷值
    isPeak = isBearish ? ta.pivothigh(indVal, lookback, lookback) : ta.pivotlow(indVal, lookback, lookback)
    if isPeak
        array.push(indPeaks, indVal)
        array.push(srcPeaks, srcVal)
        array.push(peakBars, bar_index)
    
    // 检查背离
    divergence = false
    if array.size(indPeaks) >= 2
        lastInd = array.get(indPeaks, array.size(indPeaks)-1)
        prevInd = array.get(indPeaks, array.size(indPeaks)-2)
        lastSrc = array.get(srcPeaks, array.size(indPeaks)-1)
        prevSrc = array.get(srcPeaks, array.size(indPeaks)-2)
        
        if isBearish
            divergence := lastInd < prevInd and lastSrc > prevSrc // 顶背离
        else
            divergence := lastInd > prevInd and lastSrc < prevSrc // 底背离
        
        if divergence
            clear()
    
    divergence

// ———— 检测各类背离 ————
rsiBearDiv = f_findDivergence(rsiVal, src, lookback, true) and rsiVal > rsiOB
rsiBullDiv = f_findDivergence(rsiVal, src, lookback, false) and rsiVal < rsiOS

macdBearDiv = f_findDivergence(macdLine, src, lookback, true)
macdBullDiv = f_findDivergence(macdLine, src, lookback, false)

stochBearDiv = f_findDivergence(kVal, src, lookback, true) and kVal > 80
stochBullDiv = f_findDivergence(kVal, src, lookback, false) and kVal < 20

// ———— 绘制标记 ————
plotshape(rsiBearDiv, title="RSI Bearish Divergence", location=location.belowbar, color=color.red, style=shape.triangleup, size=size.small)
plotshape(rsiBullDiv, title="RSI Bullish Divergence", location=location.abovebar, color=color.green, style=shape.triangledown, size=size.small)

plotshape(macdBearDiv, title="MACD Bearish Divergence", location=location.belowbar, color=color.orange, style=shape.triangleup, size=size.small)
plotshape(macdBullDiv, title="MACD Bullish Divergence", location=location.abovebar, color=color.blue, style=shape.triangledown, size=size.small)

plotshape(stochBearDiv, title="Stochastic Bearish Divergence", location=location.belowbar, color=color.purple, style=shape.triangleup, size=size.small)
plotshape(stochBullDiv, title="Stochastic Bullish Divergence", location=location.abovebar, color=color.teal, style=shape.triangledown, size=size.small)

// ———— 指标窗口绘制 ————
plot(rsiVal, title="RSI", color=color.gray)
hline(rsiOB, "RSI OB", color=color.red)
hline(rsiOS, "RSI OS", color=color.green)

plot(macdLine, title="MACD Line", color=color.blue)
plot(signalLine, title="Signal Line", color=color.red)
barcolor(histLine > 0 ? color.green : color.red, title="MACD Histogram")

plot(kVal, title="%K", color=color.orange)
plot(dVal, title="%D", color=color.blue)
hline(80, "Stoch OB", color=color.red)
hline(20, "Stoch OS", color=color.green)

关键升级调整说明

  • 版本声明切换为//@version=5,study函数替换为v5标准的indicator函数,保留原有的叠加、短标题等配置
  • 输入参数采用v5强类型声明(input.int),提升代码规范性与类型安全
  • 适配v5指标函数返回值格式:ta.macd、ta.stoch的返回值直接解构赋值,无需额外变量转换
  • 数组操作语法升级为v5原生API(array.new_float、array.push等),核心的峰值识别与背离判断逻辑完全保留
  • 绘图函数参数适配v5要求,颜色、形状等配置与原指标一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 01:25:39