松树脚本版本4中的编译错误

时间:2019-12-11 21:47:20

标签: pine-script

这段代码像/// @ version = 2一样进行编译:

//@version=2
study("My Script")
Factor=input(3, minval=1,maxval = 100)
Pd=input(7, minval=1,maxval = 100)
Up=hl2-(Factor*atr(Pd))
Dn=hl2+(Factor*atr(Pd))
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
linecolor = Trend == 1 ? green : red
plot(Tsl)

与// @ version = 4相同

Add to Chart operation failed, reason: line 9: Undeclared identifier `TrendUp`;
line 10: Undeclared identifier `TrendDown`;
line 11: Undeclared identifier `TrendDown`;
line 11: Undeclared identifier `TrendUp`;
line 11: Undeclared identifier `Trend`;
line 12: Undeclared identifier `Trend`;
line 12: Undeclared identifier `TrendUp`;
line 12: Undeclared identifier `TrendDown`;
line 13: Undeclared identifier `Trend`;
line 13: Undeclared identifier `green`;
line 13: Undeclared identifier `red`;
line 14: Undeclared identifier `Tsl` 

如何将此代码转换为版本4? 谢谢

1 个答案:

答案 0 :(得分:2)

不能在声明语句中使用要声明的变量。

您应该首先声明该变量,然后使用:=运算符为其分配一些值。

//@version=4
study("My Script2")
Factor=input(3, minval=1,maxval = 100)
Pd=input(7, minval=1,maxval = 100)
Up=hl2-(Factor*atr(Pd))
Dn=hl2+(Factor*atr(Pd))
TrendUp=Up
TrendUp:=close[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up
TrendDown=Dn
TrendDown:=close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn
Trend=TrendUp
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
plot(Tsl)