将学习脚本转换为策略时 Pinescript 中出现未声明的标识符错误
Undeclared Identifier error in Pinescript while translating study script to strategy
我正在尝试将 Chandelier Stop 研究脚本转换为策略,但在版本=4 中出现未声明的标识符错误
原始脚本可以在这里找到 - https://in.tradingview.com/script/mjBdRGXe-Chandelier-Stop/
//input variables
Length2=input(title="Look Back Period", type=input.integer, defval=22)
ATRPeriod=input(title="ATR Period", type=input.integer, defval=22)
Mult=input(title="ATR Multiplier", type=input.integer, defval=3)
//calculate stop value
short_stop = lowest(Length2)+Mult*atr(ATRPeriod)
long_stop = highest(Length2)-Mult*atr(ATRPeriod)
shortvs=na(shortvs[1]) ? short_stop : iff(close>shortvs[1], short_stop , min(short_stop,shortvs[1]))
longvs=na(longvs[1]) ? long_stop : iff(close<longvs[1], long_stop, max(long_stop,longvs[1]))
longswitch=iff (close>=shortvs[1] and close[1]<shortvs[1] , 1 , 0)
shortswitch=iff (close<=longvs[1] and close[1]>longvs[1] , 1 , 0)
direction= iff(na(direction[1]), 0, iff (direction[1]<=0 and longswitch, 1, iff (direction[1]>=0 and shortswitch, -1, direction[1])))
pcup=direction>0?longvs : na
pcdn=direction<0?shortvs : na
plot(pcup, color=color.aqua, style=plot.style_circles, linewidth=2)
plot(pcup, color=color.aqua, style=plot.style_linebr, linewidth=2)
plot(pcdn, color=color.fuchsia, style=plot.style_circles, linewidth=2)
plot(pcdn, color=color.fuchsia, style=plot.style_linebr, linewidth=2)
这是错误日志
Processing script...
line 35: Undeclared identifier 'shortvs';
line 36: Undeclared identifier 'longvs';
line 38: Undeclared identifier 'shortvs';
line 39: Undeclared identifier 'longvs';
line 41: Undeclared identifier 'direction';
line 41: Undeclared identifier 'longswitch';
line 41: Undeclared identifier 'shortswitch';
line 43: Undeclared identifier 'direction';
line 43: Undeclared identifier 'longvs';
line 44: Undeclared identifier 'direction';
line 44: Undeclared identifier 'shortvs';
line 53: Undeclared identifier 'direction';
line 57: Undeclared identifier 'direction';
line 118: Undeclared identifier 'pcup';
line 119: Undeclared identifier 'pcup';
line 121: Undeclared identifier 'pcdn';
line 122: Undeclared identifier 'pcdn'
这解决了错误,并将绘制。
//@version=4
study("Chandelier Stop", overlay=true)
//input variables
Length2=input(title="Look Back Period", type=input.integer, defval=22)
ATRPeriod=input(title="ATR Period", type=input.integer, defval=22)
Mult=input(title="ATR Multiplier", type=input.integer, defval=3)
var float shortvs = na
var float longvs = na
var int direction = na
//calculate stop value
short_stop = lowest(Length2) + Mult * atr(ATRPeriod)
long_stop = highest(Length2) - Mult * atr(ATRPeriod)
shortvs := na(shortvs[1]) ? short_stop : iff(close > shortvs[1], short_stop , min(short_stop, shortvs[1]))
longvs := na(longvs[1]) ? long_stop : iff(close < longvs[1], long_stop, max(long_stop, longvs[1]))
longswitch = iff(close >= shortvs[1] and close[1] < shortvs[1], 1, 0)
shortswitch = iff(close <= longvs[1] and close[1] > longvs[1] , 1, 0)
direction := iff(na(direction[1]), 0, iff(direction[1] <= 0 and longswitch, 1, iff(direction[1] >= 0 and shortswitch, -1, direction[1])))
pcup = direction > 0 ? longvs : na
pcdn = direction < 0 ? shortvs : na
plot(pcup, color=color.aqua, style=plot.style_circles, linewidth=2)
plot(pcup, color=color.aqua, style=plot.style_linebr, linewidth=2)
plot(pcdn, color=color.fuchsia, style=plot.style_circles, linewidth=2)
plot(pcdn, color=color.fuchsia, style=plot.style_linebr, linewidth=2)
您在第 19 行的缩进有误。
Tradingview 非常重视缩进。
另外,我已经把它变成了一个策略
strategy("Chandelier Stop", overlay=true)
//input variables
Length=input(title="Look Back Period", type=integer, defval=22)
ATRPeriod=input(title="ATR Period", type=integer, defval=22)
Mult=input(title="ATR Multiplier", type=integer, defval=3)
//calculate stop value
short_stop = lowest(Length)+Mult*atr(ATRPeriod)
long_stop = highest(Length)-Mult*atr(ATRPeriod)
shortvs=na(shortvs[1]) ? short_stop : iff(close>shortvs[1], short_stop , min(short_stop,shortvs[1]))
longvs=na(longvs[1]) ? long_stop : iff(close<longvs[1], long_stop, max(long_stop,longvs[1]))
longswitch=iff (close>=shortvs[1] and close[1]<shortvs[1] , 1 , 0)
shortswitch=iff (close<=longvs[1] and close[1]>longvs[1] , 1 , 0)
direction=
iff(na(direction[1]), 0,
iff (direction[1]<=0 and longswitch, 1,
iff (direction[1]>=0 and shortswitch, -1, direction[1])))
pc=direction>0?longvs:shortvs
plot(pc, color=direction>0?aqua:fuchsia, style=circles, linewidth=2)
plot(pc, color=direction>0?aqua:fuchsia, style=line, linewidth=2)
strategy.entry("Buy",true,1,when = direction == 1)
strategy.entry("Sell",false,1, when = direction == -1)
我正在尝试将 Chandelier Stop 研究脚本转换为策略,但在版本=4 中出现未声明的标识符错误
原始脚本可以在这里找到 - https://in.tradingview.com/script/mjBdRGXe-Chandelier-Stop/
//input variables
Length2=input(title="Look Back Period", type=input.integer, defval=22)
ATRPeriod=input(title="ATR Period", type=input.integer, defval=22)
Mult=input(title="ATR Multiplier", type=input.integer, defval=3)
//calculate stop value
short_stop = lowest(Length2)+Mult*atr(ATRPeriod)
long_stop = highest(Length2)-Mult*atr(ATRPeriod)
shortvs=na(shortvs[1]) ? short_stop : iff(close>shortvs[1], short_stop , min(short_stop,shortvs[1]))
longvs=na(longvs[1]) ? long_stop : iff(close<longvs[1], long_stop, max(long_stop,longvs[1]))
longswitch=iff (close>=shortvs[1] and close[1]<shortvs[1] , 1 , 0)
shortswitch=iff (close<=longvs[1] and close[1]>longvs[1] , 1 , 0)
direction= iff(na(direction[1]), 0, iff (direction[1]<=0 and longswitch, 1, iff (direction[1]>=0 and shortswitch, -1, direction[1])))
pcup=direction>0?longvs : na
pcdn=direction<0?shortvs : na
plot(pcup, color=color.aqua, style=plot.style_circles, linewidth=2)
plot(pcup, color=color.aqua, style=plot.style_linebr, linewidth=2)
plot(pcdn, color=color.fuchsia, style=plot.style_circles, linewidth=2)
plot(pcdn, color=color.fuchsia, style=plot.style_linebr, linewidth=2)
这是错误日志
Processing script...
line 35: Undeclared identifier 'shortvs';
line 36: Undeclared identifier 'longvs';
line 38: Undeclared identifier 'shortvs';
line 39: Undeclared identifier 'longvs';
line 41: Undeclared identifier 'direction';
line 41: Undeclared identifier 'longswitch';
line 41: Undeclared identifier 'shortswitch';
line 43: Undeclared identifier 'direction';
line 43: Undeclared identifier 'longvs';
line 44: Undeclared identifier 'direction';
line 44: Undeclared identifier 'shortvs';
line 53: Undeclared identifier 'direction';
line 57: Undeclared identifier 'direction';
line 118: Undeclared identifier 'pcup';
line 119: Undeclared identifier 'pcup';
line 121: Undeclared identifier 'pcdn';
line 122: Undeclared identifier 'pcdn'
这解决了错误,并将绘制。
//@version=4
study("Chandelier Stop", overlay=true)
//input variables
Length2=input(title="Look Back Period", type=input.integer, defval=22)
ATRPeriod=input(title="ATR Period", type=input.integer, defval=22)
Mult=input(title="ATR Multiplier", type=input.integer, defval=3)
var float shortvs = na
var float longvs = na
var int direction = na
//calculate stop value
short_stop = lowest(Length2) + Mult * atr(ATRPeriod)
long_stop = highest(Length2) - Mult * atr(ATRPeriod)
shortvs := na(shortvs[1]) ? short_stop : iff(close > shortvs[1], short_stop , min(short_stop, shortvs[1]))
longvs := na(longvs[1]) ? long_stop : iff(close < longvs[1], long_stop, max(long_stop, longvs[1]))
longswitch = iff(close >= shortvs[1] and close[1] < shortvs[1], 1, 0)
shortswitch = iff(close <= longvs[1] and close[1] > longvs[1] , 1, 0)
direction := iff(na(direction[1]), 0, iff(direction[1] <= 0 and longswitch, 1, iff(direction[1] >= 0 and shortswitch, -1, direction[1])))
pcup = direction > 0 ? longvs : na
pcdn = direction < 0 ? shortvs : na
plot(pcup, color=color.aqua, style=plot.style_circles, linewidth=2)
plot(pcup, color=color.aqua, style=plot.style_linebr, linewidth=2)
plot(pcdn, color=color.fuchsia, style=plot.style_circles, linewidth=2)
plot(pcdn, color=color.fuchsia, style=plot.style_linebr, linewidth=2)
您在第 19 行的缩进有误。
Tradingview 非常重视缩进。 另外,我已经把它变成了一个策略
strategy("Chandelier Stop", overlay=true)
//input variables
Length=input(title="Look Back Period", type=integer, defval=22)
ATRPeriod=input(title="ATR Period", type=integer, defval=22)
Mult=input(title="ATR Multiplier", type=integer, defval=3)
//calculate stop value
short_stop = lowest(Length)+Mult*atr(ATRPeriod)
long_stop = highest(Length)-Mult*atr(ATRPeriod)
shortvs=na(shortvs[1]) ? short_stop : iff(close>shortvs[1], short_stop , min(short_stop,shortvs[1]))
longvs=na(longvs[1]) ? long_stop : iff(close<longvs[1], long_stop, max(long_stop,longvs[1]))
longswitch=iff (close>=shortvs[1] and close[1]<shortvs[1] , 1 , 0)
shortswitch=iff (close<=longvs[1] and close[1]>longvs[1] , 1 , 0)
direction=
iff(na(direction[1]), 0,
iff (direction[1]<=0 and longswitch, 1,
iff (direction[1]>=0 and shortswitch, -1, direction[1])))
pc=direction>0?longvs:shortvs
plot(pc, color=direction>0?aqua:fuchsia, style=circles, linewidth=2)
plot(pc, color=direction>0?aqua:fuchsia, style=line, linewidth=2)
strategy.entry("Buy",true,1,when = direction == 1)
strategy.entry("Sell",false,1, when = direction == -1)