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

IF condition in Pine - assigning values based on different conditions

我试图通过在内置安全功能中使用带有 period/time-frame 信息的变量来获取 CCI 值。下面 IF 条件中的 period/time-frame 计算似乎不起作用:当我将 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") 

问候 沙拉德

我们可以在这里做一个例子来查看问题。

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

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

current_tf = period
other_tf = '1'

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'