Pine中的IF条件-根据不同的条件分配值

时间:2019-03-10 13:46:45

标签: pine-script

我试图通过在内置安全功能中使用带有周期/时间范围信息的变量来获取CCI值。在下面的IF条件下,周期/时间框架的计算似乎不起作用:当我将other_tf传递给安全功能时,我得到了未声明的标识符消息。

对于在多个时间范围内平滑CCI值的任何专家意见,也将不胜感激:)

//@version=2
//This indicator will draw the following
//CCI-8 on current time frame/5 minutes
//CCI-34 on current time frame/5 minutes
//CCI-34 on higher time frame/30 minutes

study("Multi Timeframe CCI", shorttitle="MTF_CCI",overlay=false)
ccia_len = input(8, title="CCI A Length", type=integer)
ccib_len = input(34, title="CCI B Length", type=integer)
src = input(close, title="Source", type=source)


current_tf = period

if current_tf == '5'
    other_tf = '30'
if current_tf == '15'
    other_tf = '60'
if current_tf == '30'
    other_tf = '120'
if current_tf == '120'
    other_tf = 'D'


current_tf_ccia = cci(src,ccia_len)
current_tf_ccib = cci(src,ccib_len)
other_tf_ccib = security(tickerid, other_tf, cci(src,ccib_len))  
//other_tf_ccib = security(tickerid,"30",cci(src,ccib_len))

x1 = ema(other_tf_ccib,3)
other_tf_smoothccib = ema(x1,3)

plot(current_tf_ccia, color=red, title="CCI8 CTF")
plot(current_tf_ccib, color=green, title="CCI34 CTF")
plot(other_tf_ccib, color=black, title="CCI34 HTF")
plot(other_tf_smoothccib, color=yellow, title="CCI34 SMOOTH HTF") 

问候 莎拉德

1 个答案:

答案 0 :(得分:1)

我们可以在此处处理示例以查看问题。

如果current_tf'1'怎么办?在那种情况下,您的if语句都不是真的,因此它将跳过所有这些ifs,因此other_tf将永远不会赋值给任何值,因此它不会被声明(因为您只声明了该变量,如果一个if语句为真)

您可以在进行检查之前声明它,以防止出现此错误。

current_tf = period
other_tf = '0'

if current_tf == '5'
    other_tf = '30'
if current_tf == '15'
    other_tf = '60'
if current_tf == '30'
    other_tf = '120'
if current_tf == '120'
    other_tf = 'D'