Hi .... I am experiencing challenges trying to get this pine-script to work in Multi Time Frame (MTF).
I so hope someone here can help me with this...
Displayed below are two scripts of the same indicator output....the first is the attempt to modify the script to MTF using the security() function, and the second is the script without MTF that works perfectly using global scope resolution="" defined in the study header. The objective (once it is working) is to eventually have several indicator higher timeframes overlapped in the one script; hence why i am using the security function (only using 1 indicator timeframe at present for simplicity).
However, when defined in Functions, it returns numerous local scope errors....for eg: Line 29: The variable ‘SOema0’ is declared in local scope, which may not be executed at every update. So, obtaining its historical values may lead to unexpected results.
When I take these variables out of local scope and put into global scope; I then expectedly get the Cannot use a mutable variable as an argument of the security function
error.
I have tried numerous things, including executing the security() script in a function
I have read the user manual and numerous forums, particularly in respect to Executing Pine Functions and Historical Context Inside Function Blocks but still struggling to find a solution… hence as a last resort…I reach out the community if they can help me work out how to get this script to work in MTF.
please excuse me if some of my terminologies above are not 'coder' correct - i am new to coding and learning rapidly.
Thankyou so much for your help with this…it is greatly appreciated.
1/. Script with attempted MTF using security():
//@version=4
study(title=Normalized Osc
, shorttitle = NSO 3.1 Dev Trial
, overlay=false)
//Indicator Inputs{
inpPeriod = input(defval=21, title=Stochastic period
, minval=1, type=input.integer)
SOinpSmoothPeriod = input(defval=12, title=Stoch Smoothing period
, minval=1, type=input.integer)
//}
//Resolution Inputs {
MO_1_res = input(title= Momentum Osc 1
, type=input.resolution, defval="")
//}
//Normalised Oscillator Script{
minSO = lowest(low,inpPeriod)
maxSO = highest(high,inpPeriod)
f_NSMO() =>
if bar_index > inpPeriod
SOalpha = 2.0/(1.0+SOinpSmoothPeriod)
sto = 10.0*((close-minSO)/(maxSO-minSO)-0.5)
SOema0 = 0.0
SOema0 := SOema0[1]+SOalpha*(sto-SOema0[1])
SOema1 = 0.0
SOema1 := SOema1[1]+SOalpha*(SOema0-SOema1[1])
iexp = exp(SOema1)
iexp
SOval = 0.0
SOval := (iexp-1.0)/(iexp+1.0)
SOval
f_sec() => security(syminfo.tickerid, MO_1_res, f_NSMO())
NSMO_MTF = f_sec()
//}
//Plot Indicator{
plot(NSMO_MTF, color=color.green, title=Osc
, style=plot.style_line, linewidth=2, transp=0)
//}
2/. Original Working Script – Global Scope Resolution in Study Header:
//@version=4
study(title=Normalized Stoch Osc
, shorttitle = NSMO Dev Trial
, overlay=false, resolution = "")
// Inputs {
inpPeriod = input(defval=9, title=Stochastic period
, minval=1, type=input.integer)
SOinpSmoothPeriod = input(defval=5, title=Stoch Smoothing period
, minval=1, type=input.integer)
//}
//Normalised Oscillator Script {
SOema0 = 0.0
SOema1 = 0.0
SOval = 0.0
minSO = lowest(low,inpPeriod)
maxSO = highest(high,inpPeriod)
if bar_index > inpPeriod
SOalpha = 2.0/(1.0+SOinpSmoothPeriod)
sto = 10.0*((close-minSO)/(maxSO-minSO)-0.5)
SOema0 := SOema0[1]+SOalpha*(sto-SOema0[1])
SOema1 := SOema1[1]+SOalpha*(SOema0-SOema1[1])
iexp = exp(SOema1)
SOval := (iexp-1.0)/(iexp+1.0)
//}
//Plot{
SOcol = SOval>SOval[2]?color.green:color.orange
plot(SOval, color=SOcol, title=Osc
, style=plot.style_line, linewidth=2, transp=0)
//}