如何将RDX Score指标Pine Script旧版本转为V5并修复绘图异常
修复Pine Script V5版本RDX Score指标分数标识不显示问题
问题根源
原V4代码转V5时,递归计算的平滑变量未使用var关键字声明,导致每次脚本重新计算时变量被重置,ADX等指标值计算错误,进而触发不了后续的分数条件判断。此外V5中study()需替换为indicator()。
修复后的完整V5代码
//@version=5 indicator("RDX Score", overlay=true) len = input.int(title="length_ADX", defval=14) th = input.int(title="threshold_ADX", defval=14) TrueRange = ta.tr // 直接使用内置TR函数,等价于原计算逻辑 DirectionalMovementPlus = ta.dm(true) // 内置+DM函数 DirectionalMovementMinus = ta.dm(false) // 内置-DM函数 // 使用var初始化递归平滑变量,避免每次重置 var float SmoothedTrueRange = 0.0 var float SmoothedDirectionalMovementPlus = 0.0 var float SmoothedDirectionalMovementMinus = 0.0 SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100 DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100 DX = math.abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100 ADX = ta.sma(DX, len) length_RSI = input.int(14) priceRSI = close vrsi = ta.rsi(priceRSI, length_RSI) // 分数条件与绘图逻辑保持原逻辑,仅确保变量计算正确 m5 = ADX[1]<ADX and vrsi<30 plotshape(m5, title="SCORE -5", text="-5", textcolor=color.white, style=shape.triangledown, location=location.abovebar, color=color.red, transp=50, size=size.tiny) m4 = (ADX[1]>ADX and vrsi<30) or (ADX[1]<ADX and vrsi<35 and vrsi>30) plotshape(m4, title="SCORE -4", text="-4", textcolor=color.white, style=shape.triangledown, location=location.abovebar, color=color.red, transp=50, size=size.tiny) m3 = (ADX[1]>ADX and vrsi<35 and vrsi>30) or (ADX[1]<ADX and vrsi<40 and vrsi>35) plotshape(m3, title="SCORE -3", text="-3", textcolor=color.white, style=shape.triangledown, location=location.abovebar, color=color.red, transp=50, size=size.tiny) m2 = (ADX[1]>ADX and vrsi<40 and vrsi>35) or (ADX[1]<ADX and vrsi<45 and vrsi>40) plotshape(m2, title="SCORE -2", text="-2", textcolor=color.white, style=shape.triangledown, location=location.abovebar, color=color.red, transp=50, size=size.tiny) m1 = ADX[1]>ADX and vrsi<45 and vrsi>40 plotshape(m1, title="SCORE -1", text="-1", textcolor=color.white, style=shape.triangledown, location=location.abovebar, color=color.red, transp=50, size=size.tiny) m0 = vrsi<55 and vrsi>45 // plotshape(m0, title="QQE short", text="0", textcolor=color.black, style=shape.labeldown, location=location.abovebar, color=color.yellow, transp=0, size=size.tiny) // plotshape(m0, title="QQE long", text="0", textcolor=color.black, style=shape.labelup, location=location.belowbar, color=color.yellow, transp=0, size=size.tiny) p1 = ADX[1]>ADX and vrsi>55 and vrsi<60 plotshape(p1, title="SCORE 1", text="1", textcolor=color.white, style=shape.triangleup, location=location.belowbar, color=color.green, transp=50, size=size.tiny) p2 = (ADX[1]>ADX and vrsi>60 and vrsi<65) or (ADX[1]<ADX and vrsi>55 and vrsi<60) plotshape(p2, title="SCORE 2", text="2", textcolor=color.white, style=shape.triangleup, location=location.belowbar, color=color.green, transp=50, size=size.tiny) p3 = (ADX[1]>ADX and vrsi>65 and vrsi<70) or (ADX[1]<ADX and vrsi>60 and vrsi<65) plotshape(p3, title="SCORE 3", text="3", textcolor=color.white, style=shape.triangleup, location=location.belowbar, color=color.green, transp=50, size=size.tiny) p4 = (ADX[1]>ADX and vrsi>70) or (ADX[1]<ADX and vrsi>65 and vrsi<70) plotshape(p4, title="SCORE 4", text="4", textcolor=color.white, style=shape.triangleup, location=location.belowbar, color=color.green, transp=50, size=size.tiny) p5 = ADX[1]<ADX and vrsi>70 plotshape(p5, title="SCORE 5", text="5", textcolor=color.white, style=shape.triangleup, location=location.belowbar, color=color.green, transp=50, size=size.tiny)
关键修改说明
- 替换
study()为indicator():这是Pine Script V5的强制要求,原V4的study在V5中已被indicator替代。 - 用
var声明递归平滑变量:SmoothedTrueRange、SmoothedDirectionalMovementPlus、SmoothedDirectionalMovementMinus这三个递归计算的变量必须用var初始化,确保脚本在运行时不会每次重置变量值,保证ADX等指标计算正确。 - 使用内置函数简化代码:用
ta.tr替代原TR手动计算,ta.dm(true/false)替代+DM/-DM的手动判断,逻辑完全等价但代码更简洁,减少出错概率。 - 输入类型调整:V5中
input需根据类型改为input.int、input.float等,这里整数输入用input.int更规范。
内容的提问来源于stack exchange,提问作者Munif Shaikh
相关产品推荐
相关产品推荐

