如何将TradingView PineScript V2版EMA脚本转换为V5版本?
PineScript V2 转 V5:Supertrend Alerts 1.1 转换完成
转换后的完整V5代码:
//@version=5 //Original author: Rajandran R indicator("Supertrend Alerts 1.1", overlay = true) cand_col = input(title="Color Candles", defval=true) show_line = input(title="Show Supertrend Line", defval=true) Factor = input.float(3, title="Factor") PD = input.int(7, title="Period", minval = 1, maxval = 100) Up = hl2 - (Factor * ta.atr(PD)) Dn = hl2 + (Factor * ta.atr(PD)) var float TrendUp = na var float TrendDown = na TrendUp := close[1] > TrendUp[1] ? math.max(Up, TrendUp[1]) : Up TrendDown := close[1] < TrendDown[1] ? math.min(Dn, TrendDown[1]) : Dn var int Trend = 1 Trend := close > TrendDown[1] ? 1 : close < TrendUp[1] ? -1 : Trend[1] TSL = Trend == 1 ? TrendUp : TrendDown linecolor = Trend == 1 ? color.new(color.green, 0) : color.new(color.red, 0) barcolor(cand_col and Trend == 1 ? color.new(color.green, 0) : na) barcolor(cand_col and Trend == -1 ? color.new(color.red, 0) : na) plot(show_line ? TSL : na, color = linecolor, style = plot.style_line, linewidth = 2, title = "SuperTrend Line", transp=65) newup = ta.crossover(close, TSL) newdn = ta.crossunder(close, TSL) plotshape(newup, title="Long", shape=shape.triangleup, location=location.belowbar, color=color.new(color.green, 0), size=size.normal) plotshape(newdn, title="Short", shape=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), size=size.normal) alertcondition(newup, title='Supertrend Long', message='Supertrend: Long ') alertcondition(newdn, title='Supertrend Short', message='Supertrend: Short ') alertcondition(newup or newdn, title='Supertrend Signal', message='Supertrend: Long or Short ')
关键转换说明:
- 版本与入口函数:将
//@version=2改为//@version=5,study()替换为V5标准的indicator() - 输入系统升级:使用
input.float()/input.int()替代旧版输入声明,布尔型输入无需指定类型,自动推断 - 命名空间规范:技术指标函数(如
atr())归属ta命名空间,数学函数(如max())归属math命名空间 - 变量初始化:用
var关键字声明趋势变量,确保仅初始化一次,适配V5变量作用域规则 - 赋值语法:使用
:=进行变量重新赋值,区分初始化与更新操作 - 颜色与样式:用
color.new()处理颜色透明度,绘图样式常量添加plot.命名空间前缀 - 交叉逻辑简化:用
ta.crossover()/ta.crossunder()替代原交叉判断,语义更清晰
内容的提问来源于stack exchange,提问作者diazluise
相关产品推荐
相关产品推荐

