Function 需要一些帮助使以下功能正常工作吗

Function 需要一些帮助使以下功能正常工作吗,function,pine-script,undeclared-identifier,Function,Pine Script,Undeclared Identifier,我编写了一个测试/实践函数,在给定的时间范围内执行macd,如下所示,它给了我错误 //@version=4 study(title="function test") src = close //---macd, signal, histogram definitions fastl = 12 slowl = 26 sigl = 9 fastMA = ema(src, fastl) slowMA = ema(src, slowl) macd = fastMA - slowMA sig

我编写了一个测试/实践函数,在给定的时间范围内执行macd,如下所示,它给了我错误

//@version=4
study(title="function test") 

src = close

//---macd, signal, histogram definitions

fastl = 12
slowl = 26
sigl = 9

fastMA = ema(src, fastl)
slowMA = ema(src, slowl)
macd = fastMA - slowMA
sig = ema(macd, sigl)
hist = macd - sig


//---function for macd calculations


//current time frame 

nt=timeframe.period


//function to automate calculating macd, signal and histogram for th nt or the current time frame.

omsh(nt) => 
    omf = security(syminfo.tickerid, nt, macd)
    osf = security(syminfo.tickerid, nt, sig)
    ohf = security(syminfo.tickerid, nt, hist)

// MACD plot

hline(0, '0 Line', linestyle=hline.style_solid, linewidth=1, color=color.gray)

plot(omf, color=color.blue)
plot(osf, color=color.red)
plot(ohf, style=plot.style_histogram, linewidth=1, transp=55)
上面的函数给出了以下错误

line 38: Undeclared identifier `omf`;
line 39: Undeclared identifier `osf`;
line 40: Undeclared identifier `ohf`
我就是想不出怎么避开它

它们已在函数omsh(nt)中声明,是否

并且omsh(nt)功能已经运行/执行/处理,输入“nt”,否

输入nt是否已声明为ok?我想我做得对,但我可能错了

谢谢你的帮助

omsh(nt) => 
    omf = security(syminfo.tickerid, nt, macd)
    osf = security(syminfo.tickerid, nt, sig)
    ohf = security(syminfo.tickerid, nt, hist)
您已经在局部范围中声明了这些变量。这意味着,它们在函数外部不可见。这就是为什么你会犯这样的错误

代码中不需要函数。您可以简单地执行以下操作:

omf = security(syminfo.tickerid, nt, macd)
osf = security(syminfo.tickerid, nt, sig)
ohf = security(syminfo.tickerid, nt, hist)
// MACD plot

hline(0, '0 Line', linestyle=hline.style_solid, linewidth=1, color=color.gray)

plot(omf, color=color.blue)
plot(osf, color=color.red)
plot(ohf, style=plot.style_histogram, linewidth=1, transp=55)

编辑:由于OP的评论

一个函数可以返回多个变量。因此,您可以使用局部变量,并在最后返回它们

omsh(nt) => 
    f_omf = security(syminfo.tickerid, nt, macd)
    f_osf = security(syminfo.tickerid, nt, sig)
    f_ohf = security(syminfo.tickerid, nt, hist)
    [f_omf, f_osf, f_ohf]
然后调用此函数时,应提供三个变量以获取返回值:

[omf, osf, ohf] = omsh(nt)
然后像以前一样简单地绘制这些变量:

plot(omf, color=color.blue)
plot(osf, color=color.red)
plot(ohf, style=plot.style_histogram, linewidth=1, transp=55)

我知道我不需要函数。但正如我所说,我正在努力学习如何编写函数,这是一次练习。您将如何修复它以使“函数”按预期运行?我想我必须在函数定义中声明它?我想做的是使用
omf
osf
ohf
作为函数的输出。通过这样做,我学会了如何编写函数,如何在另一个函数中使用函数的输出,等等。谢谢Baris。当我在这里得到答案后,我总是会遇到更多的问题。在使用提到的数组(?)tradingview中发现一些问题后,我编辑了原始帖子。通过将输出变量分别放在各自的行中,将其转换为列表(?),可以更改直方图,并且仅更改直方图。这怎么可能?嗨@boreddude,我建议你撤销编辑并问一个新问题。我们通常在每篇文章中问一个问题,因为如果你和我继续编辑,那么其他人很难跟上。见鬼,我现在甚至很难理解:)好的。我会的。顺便说一句,谢谢你们的努力。现在我正在为是否再次发表声明而斗争,我的大脑被炸了。我再也不能思考了。我明天做