首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将研究脚本转换为策略时Pinescript中未声明的标识符错误

将研究脚本转换为策略时Pinescript中未声明的标识符错误
EN

Stack Overflow用户
提问于 2021-03-05 01:59:58
回答 2查看 221关注 0票数 0

我正在尝试将吊灯停止研究脚本转换为策略,但在version=4中遇到未声明的标识符错误

可在此处找到原始脚本- https://in.tradingview.com/script/mjBdRGXe-Chandelier-Stop/

代码语言:javascript
复制
//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)

这是错误日志

代码语言:javascript
复制
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'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-05 04:13:34

这解决了错误,并将绘制。

代码语言:javascript
复制
//@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)
票数 0
EN

Stack Overflow用户

发布于 2021-03-05 10:07:49

您在第19行的缩进中犯了一个错误。

Tradingview非常重视缩进。此外,我已经将其转化为一种策略

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66480550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档