// Description :
// Mean Reversion Channel objective, based on Mean Reversion theory (everything has a tendency to revert back to its mean), is to help visualizing:
// Inner Channel -> Dynamic Support and Resistance
// Outer Channel -> Overbought/Oversold Zone which may signal consolidation phase or potential reversal due to unsustainable move
// The concept of this indicator oriiginally derived from Keltner Channel (The Keltner Channel was first introduced by Chester Keltner in the 1960s. The original formula used simple moving averages (SMA) and the high-low price range to calculate the bands. In the 1980s, a new formula was introduced that used ATR.)
// Instead if using SMA/EMA, this indicator used SuperSmoother MA as it's mean with longer lookback period (default to 200) to get more stable channel line. i also added second level so the indicator will have inner and outer channel
// Details of each filtering type used for mean calculation can be read in Ehlers Technical Papers: "Swiss Army Knife Indicator" and/or his book "Cybernetics Analysis for Stock and Futures"
// Disclaimer:
// Past performance is not an indicator of future results.
// My opinions are my own and do not constitute financial advice in any way whatsoever.
// Nothing published by me constitutes an investment/trading recommendation, nor should any data or Content published by me be relied upon for any investment/trading activities.
// I strongly recommends that you perform your own independent research and/or speak with a qualified investment professional before making any financial decisions.
// Any ideas to further improve this indicator are welcome :)
//************************************************************************************************************
// Functions Start {
//************************************************************************************************************
var pi = 2 * asin(1)
var mult = pi * innermult
var mult2 = pi * outermult
var gradsize = 0.5
var gradtransp = zonetransp
get_tf(_lvl)=>
y = f_resInMinutes()
z = timeframe.period
if(mtf_typ=="Auto")
if y < 1
z := _lvl == 1 ? "1" : _lvl == 2 ? "5" : z
else if y <= 3
z := _lvl == 1 ? "5" : _lvl == 2 ? "15" : z
else if y <= 10
z := _lvl == 1 ? "15" : _lvl == 2 ? "60" : z
else if y <= 30
z := _lvl == 1 ? "60" : _lvl == 2 ? "240" : z
else if y <= 120
z := _lvl == 1 ? "240" : _lvl == 2 ? "D" : z
else if y <= 240
z := _lvl == 1 ? "D" : _lvl == 2 ? "W" : z
else if y <= 1440
z := _lvl == 1 ? "W" : _lvl == 2 ? "M" : z
else if y <= 10080
z := _lvl == 1 ? "M" : z
else
z := z
else
z := _lvl == 1 ? mtf_lvl1 : _lvl == 2 ? mtf_lvl2 : z
z
//-----------------------
// Mean Reversion Channel Function
//-----------------------
get_mrc()=>
v_condition = 0
v_meanline = source
v_meanrange = supersmoother(tr, length)
get_stat(_cond) =>
ret = "Price at Mean Line\n"
if (_cond == 1)
ret := "Overbought (Weak)\n"
else if (_cond == 2)
ret := "Overbought\n"
else if (_cond == 3)
ret := "Overbought (Strong)\n"
else if (_cond == 4)
ret := "Price Near Mean\n"
else if (_cond == 5)
ret := "Price Above Mean\n"
else if (_cond == -1)
ret := "Oversold (Weak)\n"
else if (_cond == -2)
ret := "Oversold\n"
else if (_cond == -3)
ret := "Oversold (Strong)\n"
else if (_cond == -4)
ret := "Price Near Mean\n"
else if (_cond == -5)
ret := "Price Below Mean\n"
ret
//-----------------------
// Chart Drawing Function
//-----------------------
format_price(x) =>
y = tostring(x, "0.00000")
if(x>10)
y := tostring(x, "0.000")
if(x>1000)
y := tostring(x, "0.00")
y
//-----------------------
// Plot Extension
//-----------------------
if(displayline and enable_mtf and mtf_disp_typ == "Always Display")
displayline := false
var line mean = na, line.delete(mean), mean := displayline ? f_PriceLine(meanline, #FFCD00) : na
var line res1 = na, line.delete(res1), res1 := displayline ? f_PriceLine(upband1, color.green) : na
var line sup1 = na, line.delete(sup1), sup1 := displayline ? f_PriceLine(loband1, color.green) : na
var line res2 = na, line.delete(res2), res2 := displayline ? f_PriceLine(upband2, color.red) : na
var line sup2 = na, line.delete(sup2), sup2 := displayline ? f_PriceLine(loband2, color.red) : na
tf2 = not timeframe.isweekly and not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_2 + "\n\nStatus: " + get_stat(mtf2_condition) + dist_2 + brl : ""
mtf_lbl = title+tf0+tf1+tf2
var label label_mtf = na, label.delete(label_mtf), label_mtf := enable_mtf ? f_MTFLabel(mtf_lbl, meanline) : na
//} Drawing End
Based on your shared code, it seems you're working with a Pine Script indicator written in version 4, and you're encountering issues due to changes in Pine Script version 5. Specifically, the use of the security function and passing multiple values from a function have changed in version 5.
In Pine Script version 5, the security function is replaced with request.security, and there are stricter rules about how functions can be used within it. The expression parameter of request.security must be a single value, and you cannot pass a function that returns multiple values directly.
Here's how you can update your code to be compatible with Pine Script version 5:
Update the Version and Study Declaration:
Change the version declaration from @version=4 to @version=5, and replace study with indicator.
Replace all instances of security with request.security. Also, since you cannot pass a function that returns multiple values, you'll need to compute your variables in the global scope and then use request.security to fetch higher timeframe values individually.
Compute Variables in Global Scope:
Move the code from your get_mrc() function into the main script body. Compute each variable (meanline, meanrange, upband1, etc.) directly.
// Compute Mean Reversion Channel variables
var pi = 2 * math.asin(1)
var mult = pi * innermult
var mult2 = pi * outermult
var gradsize = 0.5
var gradtransp = zonetransp
// Compute the mean line and mean range
meanline = source
meanrange = supersmoother(tr, length)
if (type == "SuperSmoother")
meanline := supersmoother(source, length)
else
meanline := SAK_smoothing(type, source, length)
// Compute bands
upband1 = meanline + (meanrange * mult)
loband1 = meanline - (meanrange * mult)
upband2 = meanline + (meanrange * mult2)
loband2 = meanline - (meanrange * mult2)
// Determine condition
condition = 0
// ... (rest of your condition logic)
Fetch Higher Timeframe Values Using request.security:
Use request.security to fetch each higher timeframe variable individually.
Ensure that all your functions are compatible with version 5. For example, replace deprecated functions like asin with math.asin, and update any other syntax as necessary.
Test the Script:
After making these changes, save and test your script in TradingView to ensure it works correctly. Resolve any errors that may arise during the process.
Here's the updated code snippet adjusted for Pine Script version 5:
//@version=5
indicator("Mean Reversion Channel - MRI Variant", shorttitle="MRC", overlay=true, format=format.inherit)
//************************************************************************************************************
// Parameter
//************************************************************************************************************
indiSet = input(false, "═════════ MRC Parameter ════════")
source = input(hlc3, title="Price Source", type=input.source)
type = input.string("SuperSmoother", title="Filter Type", options=["SuperSmoother", "Ehlers EMA", "Gaussian", "Butterworth", "BandStop", "SMA", "EMA", "RMA"])
length = input.int(200, title="Lookback Period",minval=1)
innermult = input.float(1.0, title="Inner Channel Size Multiplier", minval = 0.1)
outermult = input.float(2.415, title="Outer Channel Size Multiplier", minval = 0.1)
ChartSet = input(false, "═════════ Chart Setting ════════")
drawchannel = input(true, title="Draw Channel")
displayzone = input(true, title="Draw Zone (With Channel)")
zonetransp = input.int(60, title="Zone Transparency", minval=0, maxval=100)
displayline = input(true, title="Display Line Extension")
MTFSet = input(false, "═════════ MTF Setting ════════")
enable_mtf = input(true, title="Enable Multiple TimeFrame Analysis")
mtf_disp_typ= input.string("On Hover", title="MTF Display Type", options=["Always Display", "On Hover"])
mtf_typ = input.string("Auto", title="Multiple TimeFrame Type", options=["Auto", "Custom"])
mtf_lvl1 = input.timeframe("D", title="Custom MTF Level 1")
mtf_lvl2 = input.timeframe("W", title="Custom MTF Level 2")
//************************************************************************************************************
// Functions Start {
//************************************************************************************************************
var pi = 2 * math.asin(1)
var gradsize = 0.5
var gradtransp = zonetransp
//-----------------------
// Ehler SwissArmyKnife Function
//-----------------------
SAK_smoothing(_type, _src, _length) =>
var c0 = 1.0
var c1 = 0.0
var b0 = 1.0
var b1 = 0.0
var b2 = 0.0
var a1 = 0.0
var a2 = 0.0
var alpha = 0.0
var beta = 0.0
var gamma = 0.0
cycle = 2 * math.pi / _length
if _type == "Ehlers EMA"
alpha := (math.cos(cycle) + math.sin(cycle) - 1) / math.cos(cycle)
b0 := alpha
a1 := 1 - alpha
if _type == "Gaussian"
beta := 2.415 * (1 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2 * beta))
c0 := alpha * alpha
a1 := 2 * (1 - alpha)
a2 := -(1 - alpha) * (1 - alpha)
if _type == "Butterworth"
beta := 2.415 * (1 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2 * beta))
c0 := alpha * alpha / 4
b1 := 2
b2 := 1
a1 := 2 * (1 - alpha)
a2 := -(1 - alpha) * (1 - alpha)
if _type == "BandStop"
beta := math.cos(cycle)
gamma := 1 / math.cos(cycle*2*0.1) // delta default to 0.1. Acceptable delta -- 0.05<d<0.5
alpha := gamma - math.sqrt((gamma * gamma) - 1)
c0 := (1 + alpha) / 2
b1 := -2 * beta
b2 := 1
a1 := beta * (1 + alpha)
a2 := -alpha
if _type == "SMA"
c1 := 1 / _length
b0 := 1 / _length
a1 := 1
if _type == "EMA"
alpha := 2/(_length+1)
b0 := alpha
a1 := 1 - alpha
if _type == "RMA"
alpha := 1 / _length
b0 := alpha
a1 := 1 - alpha
_Input = _src
var _Output = 0.0
_Output := (c0 * ((b0 * _Input) + (b1 * nz(_Input[1])) + (b2 * nz(_Input[2])))) + (a1 * nz(_Output[1])) + (a2 * nz(_Output[2])) - (c1 * nz(_Input[_length]))
//-----------------------
// SuperSmoother Function
//-----------------------
supersmoother(_src, _length) =>
s_a1 = math.exp(-math.sqrt(2) * math.pi / _length)
s_b1 = 2 * s_a1 * math.cos(math.sqrt(2) * math.pi / _length)
s_c3 = -math.pow(s_a1, 2)
s_c2 = s_b1
s_c1 = 1 - s_c2 - s_c3
var ss = 0.0
ss := s_c1 * _src + s_c2 * nz(ss[1], _src[1]) + s_c3 * nz(ss[2], _src[2])
//-----------------------
// Auto TimeFrame Function
//-----------------------
// ————— Converts current chart resolution into a float minutes value.
f_resInMinutes() =>
_resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1. / 60 :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 60. * 24 :
timeframe.isweekly ? 60. * 24 * 7 :
timeframe.ismonthly ? 60. * 24 * 30.4375 : na)
get_tf(_lvl)=>
y = f_resInMinutes()
z = timeframe.period
if(mtf_typ=="Auto")
if y < 1
z := _lvl == 1 ? "1" : _lvl == 2 ? "5" : z
else if y <= 3
z := _lvl == 1 ? "5" : _lvl == 2 ? "15" : z
else if y <= 10
z := _lvl == 1 ? "15" : _lvl == 2 ? "60" : z
else if y <= 30
z := _lvl == 1 ? "60" : _lvl == 2 ? "240" : z
else if y <= 120
z := _lvl == 1 ? "240" : _lvl == 2 ? "D" : z
else if y <= 240
z := _lvl == 1 ? "D" : _lvl == 2 ? "W" : z
else if y <= 1440
z := _lvl == 1 ? "W" : _lvl == 2 ? "M" : z
else if y <= 10080
z := _lvl == 1 ? "M" : z
else
z := z
else
z := _lvl == 1 ? mtf_lvl1 : _lvl == 2 ? mtf_lvl2 : z
z
//-----------------------
// Chart Drawing Functions
//-----------------------
format_price(x) =>
y = tostring(x, "0.00000")
if(x>10)
y := tostring(x, "0.000")
if(x>1000)
y := tostring(x, "0.00")
y
f_PriceLine(_ref, linecol) =>
line.new(
x1 = bar_index, x2 = bar_index - 1,
y1 = _ref, y2 = _ref,
extend = extend.left,
color = linecol)
f_MTFLabel(_txt, _yloc) =>
label.new(
x = time + round(change(time)*20),
y = _yloc,
xloc = xloc.bar_time,
text = mtf_disp_typ == "Always Display" ? _txt : "Check MTF",
tooltip = mtf_disp_typ == "Always Display" ? "" : _txt,
color = color.black,
textcolor = color.white,
size = size.normal,
style = mtf_disp_typ == "On Hover" and displayline ? label.style_label_lower_left : label.style_label_left,
textalign = text.align_left)
//} Functions End
//************************************************************************************************************
// Calculate Channel
//************************************************************************************************************
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
// Compute Mean Reversion Channel variables
mult = pi * innermult
mult2 = pi * outermult
meanline = source
meanrange = supersmoother(tr, length)
// Get line value
if (type == "SuperSmoother")
meanline := supersmoother(source, length)
else
meanline := SAK_smoothing(type, source, length)
// Compute bands
upband1 = meanline + (meanrange * mult)
loband1 = meanline - (meanrange * mult)
upband2 = meanline + (meanrange * mult2)
loband2 = meanline - (meanrange * mult2)
// Determine condition
condition = 0
if (close > meanline)
upband2_1 = upband2 + (meanrange * gradsize * 4)
upband2_9 = upband2 + (meanrange * gradsize * -4)
if high >= upband2_9 and high < upband2
condition := 1
else if high >= upband2 and high < upband2_1
condition := 2
else if high >= upband2_1
condition := 3
else if close <= meanline + meanrange
condition := 4
else
condition := 5
else if (close < meanline)
loband2_1 = loband2 - (meanrange * gradsize * 4)
loband2_9 = loband2 - (meanrange * gradsize * -4)
if low <= loband2_9 and low > loband2
condition := -1
else if low <= loband2 and low > loband2_1
condition := -2
else if low <= loband2_1
condition := -3
else if close >= meanline + meanrange
condition := -4
else
condition := -5
// Fetch higher timeframe variables
mtf1_meanline = request.security(syminfo.tickerid, tf_1, meanline)
mtf1_meanrange = request.security(syminfo.tickerid, tf_1, meanrange)
mtf1_upband1 = request.security(syminfo.tickerid, tf_1, upband1)
mtf1_loband1 = request.security(syminfo.tickerid, tf_1, loband1)
mtf1_upband2 = request.security(syminfo.tickerid, tf_1, upband2)
mtf1_loband2 = request.security(syminfo.tickerid, tf_1, loband2)
mtf1_condition = request.security(syminfo.tickerid, tf_1, condition)
mtf2_meanline = request.security(syminfo.tickerid, tf_2, meanline)
mtf2_meanrange = request.security(syminfo.tickerid, tf_2, meanrange)
mtf2_upband1 = request.security(syminfo.tickerid, tf_2, upband1)
mtf2_loband1 = request.security(syminfo.tickerid, tf_2, loband1)
mtf2_upband2 = request.security(syminfo.tickerid, tf_2, upband2)
mtf2_loband2 = request.security(syminfo.tickerid, tf_2, loband2)
mtf2_condition = request.security(syminfo.tickerid, tf_2, condition)
//************************************************************************************************************
// Drawing Start {
//************************************************************************************************************
// ... (rest of your drawing code)
//} Drawing End
By making these adjustments, your script should now be compatible with Pine Script version 5. Remember to test the script thoroughly to ensure all features work as intended.
Note: If you encounter any specific errors during this process, please share the error messages, and we'll help you troubleshoot them further.