请求将TradingView的Supertrend V1.0 Pine Script从v1转换为v5
Pine Script v1 转 v5:Supertrend 指标转换结果
以下是将你提供的v1版本Supertrend指标转换为Pine Script v5的完整代码,同时修正了v1与v5在plotshape参数顺序上的差异,确保信号箭头显示位置正确:
//@version=5 indicator("Supertrend V1.0 - Buy or Sell Signal", overlay = true) // 输入参数(v5推荐使用类型明确的input函数) Factor = input.int(3, minval=1, maxval=100, title="Factor") Pd = input.int(7, minval=1, maxval=100, title="ATR Period") // 计算上下轨 Up = hl2 - (Factor * atr(Pd)) Dn = hl2 + (Factor * atr(Pd)) // 递归计算趋势轨 TrendUp = close[1] > TrendUp[1] ? math.max(Up, TrendUp[1]) : Up TrendDown = close[1] < TrendDown[1] ? math.min(Dn, TrendDown[1]) : Dn // 确定趋势方向 Trend = close > TrendDown[1] ? 1 : close < TrendUp[1] ? -1 : nz(Trend[1], 1) Tsl = Trend == 1 ? TrendUp : TrendDown // 趋势线颜色 linecolor = Trend == 1 ? color.green : color.red // 绘制Supertrend线 plot(Tsl, color=linecolor, style=plot.style_line, linewidth=2, title="SuperTrend") // 绘制交叉信号箭头(修正v5中plotshape的参数顺序:location在前,shape在后) plotshape(cross(close, Tsl) and close > Tsl, title="Up Arrow", location=location.belowbar, shape=shape.triangleup, color=color.green, size=size.small) plotshape(cross(Tsl, close) and close < Tsl, title="Down Arrow", location=location.abovebar, shape=shape.triangledown, color=color.red, size=size.small) // 绘制趋势反转箭头 plotarrow(Trend == 1 and Trend[1] == -1 ? 1 : na, title="Up Entry Arrow", colorup=color.lime, maxheight=60, minheight=50, transp=0) plotarrow(Trend == -1 and Trend[1] == 1 ? -1 : na, title="Down Entry Arrow", colordown=color.red, maxheight=60, minheight=50, transp=0)
主要转换说明:
- 将v1的
study()替换为v5标准的indicator()函数,并添加//@version=5声明 - 改用v5推荐的
input.int()类型明确的输入函数,提升代码可读性 - 将
max()/min()替换为v5的math.max()/math.min()(原函数也兼容,但v5更推荐使用math命名空间下的函数) - 修正
plotshape的参数顺序:v1中是shape在前,v5中需将location放在shape之前,否则箭头显示位置会错误 - 保持原有指标的逻辑和信号完全一致,仅适配v5语法规范
内容的提问来源于stack exchange,提问作者Pinery
相关产品推荐
相关产品推荐

