You need to enable JavaScript to run this app. Pine Script v2多时间框架SuperTrend转v4后14根K线信号滞后问题
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script v2多时间框架SuperTrend转v4后14根K线信号滞后问题

你判断的信号滞后原因正确,原V2版本中Trend30从30分钟周期取数,但是Tsl30从15分钟周期取数,你在V4版本中将二者绑定到同一个30分钟周期的security调用返回值,导致Tsl30的取数周期和原代码不一致,才出现信号滞后问题。

修复方式很简单:单独调用对应周期的security函数,分别获取Trend30和Tsl30的数值即可,不需要二者绑定同一个security返回结果,修改后的核心代码片段如下:

//@version=4
//Multi time frame supertrend - 6 timeframe supertrends in one chart
study("Multitimeframe Supertrend", overlay = true)

Factor=input(4, minval=1,maxval = 100)
Pd=input(10, minval=1,maxval = 100)

Up=hl2-(Factor*atr(Pd))
Dn=hl2+(Factor*atr(Pd))

st() =>
    int Trend = na, float TrendUp = na, float TrendDown = na
    TrendUp:=close[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up
    TrendDown:=close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn
    Trend := close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1)
    Tsl = Trend==1? TrendUp: TrendDown
    [Trend, Tsl]

[Trend15, Tsl15] = security(syminfo.tickerid,"15", st())
// 单独取30分钟周期的Trend值给Trend30,忽略不需要的Tsl返回值
[Trend30, _] = security(syminfo.tickerid,"30", st())
// 单独取15分钟周期的Tsl值给Tsl30,和原V2逻辑完全对齐,忽略不需要的Trend返回值
[_, Tsl30] = security(syminfo.tickerid,"15", st())
[Trend60, Tsl60] = security(syminfo.tickerid,"60", st())
[Trend120, Tsl120] = security(syminfo.tickerid,"120", st())
[Trend240, Tsl240] = security(syminfo.tickerid,"240", st())
[TrendD, TslD] = security(syminfo.tickerid,"D", st())
[TrendW, TslW] = security(syminfo.tickerid,"W", st())


// linecolor = Trend == 1 ? color.green : color.red
linecolor15 = Trend15 == 1 ? color.green : color.red
linecolor30 = Trend30 == 1 ? color.green : color.red
linecolor60 = Trend60 == 1 ? color.green : color.red
linecolor120 = Trend120 == 1 ? color.green : color.red
linecolor240 = Trend240 == 1 ? color.green : color.red
linecolorD = TrendD == 1 ? color.green : color.red
linecolorW = TrendW == 1 ? color.green : color.red

plot(Tsl15, color = linecolor15 , style = plot.style_line , linewidth = 1,title = "SuperTrend15")
plot(Tsl30, color = linecolor30 , style = plot.style_linebr , linewidth = 1,title = "SuperTrend30")
plot(Tsl60, color = linecolor60 , style = plot.style_circles, linewidth = 1,title = "SuperTrend60")
plot(Tsl120, color = linecolor120 , style = plot.style_cross,linewidth = 1,title = "SuperTrend120")
plot(Tsl240, color = linecolor240 , style = plot.style_line , linewidth = 1,title = "SuperTrend240")
plot(TslD, color = linecolorD , style = plot.style_line , linewidth = 2,title = "SuperTrendD")
plot(TslW, color = linecolorW , style = plot.style_line , linewidth = 2,title = "SuperTrendW")
//plotshape(cross(close,Tsl) and close>Tsl , "Up Arrow", shape.triangleup,location.belowbar,color.green,0,0)
//plotshape(cross(Tsl,close) and close<Tsl , "Down Arrow", shape.triangledown , location.abovebar, color.red,0,0)
//plot(Trend==1 and Trend[1]==-1,color = linecolor, style = circles, linewidth = 3,title="Trend")

//plotarrow(Trend == 1 and Trend[1] == -1 ? Trend : na, title="Up Entry Arrow", colorup=lime, maxheight=60, minheight=50, transp=0)
//plotarrow(Trend == -1 and Trend[1] == 1 ? Trend : na, title="Down Entry Arrow", colordown=color.red, maxheight=60, minheight=50, transp=0)

上述代码中用下划线_作为占位符,忽略security调用返回的不需要的数值即可,修改后信号逻辑会和原V2版本完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 18:45:03