I wrote a test/practice function to do the macd for the given time frame as follows and it giving me errors
//@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)
Above function gives me the following errors
line 38: Undeclared identifier `omf`;
line 39: Undeclared identifier `osf`;
line 40: Undeclared identifier `ohf`
I just can't seem to figure out how to get around it.
They have been declared in the function omsh(nt), no?
And the function omsh(nt) has been run/executed/processed, with the input of the "nt", no?
Has the input nt been declared ok? I think I have done that correctly, but I could be wrong.
Thanks for any help.
omsh(nt) =>
omf = security(syminfo.tickerid, nt, macd)
osf = security(syminfo.tickerid, nt, sig)
ohf = security(syminfo.tickerid, nt, hist)
You have declared those variables in local scope. That means, they are not visible outside of the function. That's why you are getting that error.
You don't need a function in your code. You can simply do:
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)
Edit: Due to OP's comment
A function can return multiple variables. So, you can work with local variables and just return them at the end.
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]
Then when you call this function, you should proivde three variables to get the return values:
[omf, osf, ohf] = omsh(nt)
Then simply plot those variables as you did before:
plot(omf, color=color.blue)
plot(osf, color=color.red)
plot(ohf, style=plot.style_histogram, linewidth=1, transp=55)