// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// REUSING THIS CODE: You are welcome to reuse this code without permission, including in closed-source publications, as long as proper credits are given :)
// Author: ©fareidzulkifli
// 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 :)
//@version=4
study("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("SuperSmoother", title="Filter Type", options=["SuperSmoother", "Ehlers EMA", "Gaussian", "Butterworth", "BandStop", "SMA", "EMA", "RMA"])
length = input(200, title="Lookback Period",minval=1)
innermult = input(1.0, title="Inner Channel Size Multiplier", minval = 0.1)
outermult = input(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(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("On Hover", title="MTF Display Type", options=["Always Display", "On Hover"])
mtf_typ = input("Auto", title="Multiple TimeFrame Type", options=["Auto", "Custom"])
mtf_lvl1 = input("D", title="Custom MTF Level 1", type=input.resolution)
mtf_lvl2 = input("W", title="Custom MTF Level 2", type=input.resolution)
//************************************************************************************************************
// Functions Start {
//************************************************************************************************************
var pi = 2 * asin(1)
var mult = pi * innermult
var mult2 = pi * outermult
var gradsize = 0.5
var gradtransp = zonetransp
//-----------------------
// Ehler SwissArmyKnife Function
//-----------------------
SAK_smoothing(_type, _src, _length) =>
c0 = 1.0
c1 = 0.0
b0 = 1.0
b1 = 0.0
b2 = 0.0
a1 = 0.0
a2 = 0.0
alpha = 0.0
beta = 0.0
gamma = 0.0
cycle = 2 * pi / _length
code Copy code
if _type == "Ehlers EMA"
alpha := (cos(cycle) + sin(cycle) - 1) / cos(cycle)
b0 := alpha
a1 := 1 - alpha
if _type == "Gaussian"
beta := 2.415 * (1 - cos(cycle))
alpha := -beta + sqrt((beta * beta) + (2 * beta))
c0 := alpha * alpha
a1 := 2 * (1 - alpha)
a2 := -(1 - alpha) * (1 - alpha)
if _type == "Butterworth"
beta := 2.415 * (1 - cos(cycle))
alpha := -beta + 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 := cos(cycle)
gamma := 1 / cos(cycle*2*0.1) // delta default to 0.1. Acceptable delta -- 0.05<d<0.5
alpha := gamma - 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
_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 = exp(-sqrt(2) * pi / _length)
s_b1 = 2 * s_a1 * cos(sqrt(2) * pi / _length)
s_c3 = -pow(s_a1, 2)
s_c2 = s_b1
s_c1 = 1 - s_c2 - s_c3
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
//-----------------------
// Mean Reversion Channel Function
//-----------------------
get_mrc()=>
v_condition = 0
v_meanline = source
v_meanrange = supersmoother(tr, length)
code Copy code
//-- Get Line value
if(type == "SuperSmoother")
v_meanline := supersmoother(source, length)
if(type != "SuperSmoother")
v_meanline := SAK_smoothing(type, source, length)
v_upband1 = v_meanline+(v_meanrange*mult)
v_loband1 = v_meanline-(v_meanrange*mult)
v_upband2 = v_meanline+(v_meanrange*mult2)
v_loband2 = v_meanline-(v_meanrange*mult2)
//-- Check Condition
if(close > v_meanline)
v_upband2_1 = v_upband2 + (v_meanrange * gradsize * 4)
v_upband2_9 = v_upband2 + (v_meanrange * gradsize * -4)
if(high >= v_upband2_9 and high < v_upband2)
v_condition := 1
else if(high >= v_upband2 and high < v_upband2_1)
v_condition := 2
else if(high >= v_upband2_1)
v_condition := 3
else if(close <= v_meanline+v_meanrange)
v_condition := 4
else
v_condition := 5
if(close < v_meanline)
v_loband2_1 = v_loband2 - (v_meanrange * gradsize * 4)
v_loband2_9 = v_loband2 - (v_meanrange * gradsize * -4)
if(low <= v_loband2_9 and low > v_loband2)
v_condition := -1
else if(low <= v_loband2 and low > v_loband2_1)
v_condition := -2
else if(low <= v_loband2_1)
v_condition := -3
else if(close >= v_meanline+v_meanrange)
v_condition := -4
else
v_condition := -5
[v_meanline, v_meanrange, v_upband1, v_loband1, v_upband2, v_loband2, v_condition]
//-----------------------
// MTF Analysis
//-----------------------
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
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)
//} Function End
//************************************************************************************************************
// Calculate Channel
//************************************************************************************************************
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
[meanline, meanrange, upband1, loband1, upband2, loband2, condition] = get_mrc()
[mtf1_meanline, mtf1_meanrange, mtf1_upband1, mtf1_loband1, mtf1_upband2, mtf1_loband2, mtf1_condition] = security(syminfo.tickerid, tf_1, get_mrc())
[mtf2_meanline, mtf2_meanrange, mtf2_upband1, mtf2_loband1, mtf2_upband2, mtf2_loband2, mtf2_condition] = security(syminfo.tickerid, tf_2, get_mrc())
//************************************************************************************************************
// Drawing Start {
//************************************************************************************************************
float p_meanline = drawchannel ? meanline : na
float p_upband1 = drawchannel ? upband1 : na
float p_loband1 = drawchannel ? loband1 : na
float p_upband2 = drawchannel ? upband2 : na
float p_loband2 = drawchannel ? loband2 : na
z = plot(p_meanline, color=#FFCD00, style=plot.style_line, title=" Mean", linewidth=2)
x1 = plot(p_upband1, color=color.green, style=plot.style_circles, title=" R1", linewidth=1, transp=50)
x2 = plot(p_loband1, color=color.green, style=plot.style_circles, title=" S1", linewidth=1, transp=50)
y1 = plot(p_upband2, color=color.red, style=plot.style_line, title=" R2", linewidth=1, transp=50)
y2 = plot(p_loband2, color=color.red, style=plot.style_line, title=" S2", linewidth=1, transp=50)
//-----------------------
// Draw zone
//-----------------------
//---
var color1 = #FF0000, var color2 = #FF4200, var color3 = #FF5D00, var color4 = #FF7400, var color5 = #FF9700, var color6 = #FFAE00, var color7 = #FFC500, var color8 = #FFCD00
//---
float upband2_1 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 4) : na, float loband2_1 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 4) : na
float upband2_2 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 3) : na, float loband2_2 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 3) : na
float upband2_3 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 2) : na, float loband2_3 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 2) : na
float upband2_4 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 1) : na, float loband2_4 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 1) : na
float upband2_5 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 0) : na, float loband2_5 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 0) : na
float upband2_6 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -1) : na, float loband2_6 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -1) : na
float upband2_7 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -2) : na, float loband2_7 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -2) : na
float upband2_8 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -3) : na, float loband2_8 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -3) : na
float upband2_9 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -4) : na, float loband2_9 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -4) : na
//---
plot_upband2_1 = plot(upband2_1, color=na, transp=100, display=display.none), plot_loband2_1 = plot(loband2_1, color=na, transp=100, display=display.none)
plot_upband2_2 = plot(upband2_2, color=na, transp=100, display=display.none), plot_loband2_2 = plot(loband2_2, color=na, transp=100, display=display.none)
plot_upband2_3 = plot(upband2_3, color=na, transp=100, display=display.none), plot_loband2_3 = plot(loband2_3, color=na, transp=100, display=display.none)
plot_upband2_4 = plot(upband2_4, color=na, transp=100, display=display.none), plot_loband2_4 = plot(loband2_4, color=na, transp=100, display=display.none)
plot_upband2_5 = plot(upband2_5, color=na, transp=100, display=display.none), plot_loband2_5 = plot(loband2_5, color=na, transp=100, display=display.none)
plot_upband2_6 = plot(upband2_6, color=na, transp=100, display=display.none), plot_loband2_6 = plot(loband2_6, color=na, transp=100, display=display.none)
plot_upband2_7 = plot(upband2_7, color=na, transp=100, display=display.none), plot_loband2_7 = plot(loband2_7, color=na, transp=100, display=display.none)
plot_upband2_8 = plot(upband2_8, color=na, transp=100, display=display.none), plot_loband2_8 = plot(loband2_8, color=na, transp=100, display=display.none)
plot_upband2_9 = plot(upband2_9, color=na, transp=100, display=display.none), plot_loband2_9 = plot(loband2_9, color=na, transp=100, display=display.none)
//---
fill(plot_upband2_1,plot_upband2_2, color=color1, transp=gradtransp), fill(plot_loband2_1,plot_loband2_2, color=color1, transp=gradtransp)
fill(plot_upband2_2,plot_upband2_3, color=color2, transp=gradtransp), fill(plot_loband2_2,plot_loband2_3, color=color2, transp=gradtransp)
fill(plot_upband2_3,plot_upband2_4, color=color3, transp=gradtransp), fill(plot_loband2_3,plot_loband2_4, color=color3, transp=gradtransp)
fill(plot_upband2_4,plot_upband2_5, color=color4, transp=gradtransp), fill(plot_loband2_4,plot_loband2_5, color=color4, transp=gradtransp)
fill(plot_upband2_5,plot_upband2_6, color=color5, transp=gradtransp), fill(plot_loband2_5,plot_loband2_6, color=color5, transp=gradtransp)
fill(plot_upband2_6,plot_upband2_7, color=color6, transp=gradtransp), fill(plot_loband2_6,plot_loband2_7, color=color6, transp=gradtransp)
fill(plot_upband2_7,plot_upband2_8, color=color7, transp=gradtransp), fill(plot_loband2_7,plot_loband2_8, color=color7, transp=gradtransp)
fill(plot_upband2_8,plot_upband2_9, color=color8, transp=gradtransp), fill(plot_loband2_8,plot_loband2_9, color=color8, transp=gradtransp)
//-----------------------
// 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
//--------------
// Prep MTF Label
//--------------
var brl = "\n--------------------------------------"
dist_0 = "Distance from Mean: "+ tostring(((close-meanline)/close)*100, "#.##") + " %"
dist_1 = "Distance from Mean: "+ tostring(((close-mtf1_meanline)/close)*100, "#.##") + " %"
dist_2 = "Distance from Mean: "+ tostring(((close-mtf2_meanline)/close)*100, "#.##") + " %"
var title = "Mean Reversion Channel\nMultiple TimeFrame Analysis" + brl
tf0 =
"\n\nTimeframe: "+ tf_0 + " (Current)\n\nStatus: " + get_stat(condition) + dist_0 + brl
tf1 = not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_1 + "\n\nStatus: " + get_stat(mtf1_condition) + dist_1 + brl : ""
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
改成 v5 版本
Show full message · 379 lines Assistant2024-10-01 07:49 UTC Copy response
To migrate the given TradingView Pine Script from version 4 to version 5, we need to perform several updates. In Pine Script, there are some syntax and function name differences between version 4 and version 5. Below is the migrated version of the script:
Show full message · 377 lines Assistant2024-10-01 07:49 UTC Copy response
中午通常指一天中的中间时间段,通常在午后12点左右。对于很多人来说,中午是午餐时间,在这段时间里,人们会休息、吃饭或者休息一下,为下午的工作或活动做好准备。在不同的文化中,中午也可能会有一些特定的习俗或活动,比如在一些国家,人们可能会有午休的习惯。需要更多关于中午的信息吗?
Assistant2024-10-01 07:50 UTC Copy response
我明白了,你希望将之前的代码说明翻译成中文。以下是该 Pine Script 指标的中文说明:
plaintext Copy code
// 此源代码遵循 Mozilla 公共许可证 2.0 的条款,详情请见 https://mozilla.org/MPL/2.0/
// 代码重用:您可以在无需许可的情况下重用此代码,包括在闭源发布中,只需给予适当的署名。
// 作者: ©fareidzulkifli
// 描述:
// 平均回归通道的目标是基于平均回归理论(万物都有回归到其平均值的趋势),帮助可视化:
// 内部通道 -> 动态支撑和阻力
// 外部通道 -> 超买/超卖区域,可能表示整合阶段或由于不可持续移动而导致的潜在反转
// 这个指标的概念最初来源于凯尔特纳通道(凯尔特纳通道在 1960 年代首次由 Chester Keltner 引入。原始公式使用简单移动平均线(SMA)和高低价范围来计算带状线。在 1980 年代,出现了新公式,使用 ATR。)
// 该指标并没有使用 SMA/EMA,而是使用超级平滑移动平均线作为其均线,使用更长的回溯周期(默认为 200)以获得更稳定的通道线。我还增加了第二层级,因此该指标将包含内外通道。
// 有关用于均值计算的各过滤器类型的详细信息,可阅读 Ehlers 的技术文献:“Swiss Army Knife Indicator”或他的书《股票和期货的控制论分析》
如果需要,我可以继续翻译具体代码段的注释或进一步细化某些部分的作用说明。
错误于 50:18 Syntax error at input 'end of line without line continuation'
Assistant2024-10-01 07:50 UTC Copy response
你在第50行的输入遇到错误,可能是因为Pine Script期望每一行都能够在语法上成立。如果你在某一行没有正确结束语句,或者有输入错误,可能会导致这个错误。
错误位置是input定义部分,可能是在定义结束前没有合适地加上分号,或者在行末有未关闭的引号。可以检查第50行及其周围的地方,确保每个输入参数正确关闭,并且没有多余的字符。
以下是一个普通的定义示例:
pinescript Copy code
//@version=5
indicator("简单移动平均线", overlay=true)
length = input.int(14, "周期")
source = input(close, "数据源")
smaValue = ta.sma(source, length)
plot(smaValue, "SMA", color=color.blue)
检查代码中 input 函数是否有类似的错误,比如:
确保没有遗漏双引号 "。
确保每行完结的地方没有误用逗号或其他不属于该行结尾的字符。
检查参数列表是否有多余的逗号导致语法不连贯。
如果问题还存在,欢迎提供更具体的相关代码段,以便进一步分析和解决该问题。
//@version=5
indicator("Mean Reversion Channel - MRI Variant", shorttitle="MRC", overlay=true, format=format.inherit)
//************************************************************************************************************
// Parameter
//************************************************************************************************************
indiSet = input(false, "═════════ MRC Parameter ════════")
source = input.source(hlc3, title="Price 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 float pi = 2.0 * math.pi
var float gradsize = 0.5
var int gradtransp = zonetransp
//-----------------------
// Ehler SwissArmyKnife Function
//-----------------------
SAK_smoothing(_type, _src, _length) =>
var float c0 = 1.0
var float c1 = 0.0
var float b0 = 1.0
var float b1 = 0.0
var float b2 = 0.0
var float a1 = 0.0
var float a2 = 0.0
var float alpha = 0.0
var float beta = 0.0
var float gamma = 0.0
var float cycle = 2.0 * pi / _length
float _Output
float _Input = _src
Show full message · 364 lines Assistant2024-10-01 07:51 UTC Copy response
在 Pine Script 中遇到这类错误通常是因为一行语法没有正确终止,或某个表达式需要继续未完成的定义。为了确保解决第 50 行(及周围)的问题,请检查以下几方面:
可能存在的问题:
行结束符号问题 :
某些行可能缺少必要的行结束符,导致 Pine Script 认为语句尚未完成。
缩进和代码块 :
检查 if-else 结构的缩进和代码块,确保每个 if 语句的主体和条件是明确的。
变量声明和类型推断 :
在版本更新中,某些时候对变量声明可能需要类型推断在某些特定字段内。
解决方法:
检查代码中的缩进和语法:
特别是 if 语句,应确保其后是正确的缩进。如果某个 if 语句后没有语句体,确保紧随其后的是 :。
声明终止问题:
pinescript Copy code
var float pi = 2.0 * math.pi
var float gradsize = 0.5
var int gradtransp = zonetransp
确保变量声明除行终止符都正确无误。上面这些声明应该是没问题的。
确保完整的参数服务:
某些逻辑陈述在不完整情况下可能报错:
对 plot 函数,确保所有参数,包括强制属性,均被正确定义。
语法的格式化 :
有些时候,代码的缩进问题也是造成此类错误的一大原因,确保每个代码块中的缩进和页面调整正确。
调整:
以上问题列举的内容是通用检查的内容。如依然不能解决问题,请提供具体出错行的上下文及完整代码,我们可以进一步调整。
请尝试看看是否需要检查到上面的解决方法。希望能够成功解决如上的具体错误问题。
Assistant2024-10-01 07:52 UTC Copy response
好的,我会检查和调整代码,确保它的正确性,并给出一个尽量完成的版本供你使用。在调整代码时,我会特别注意语法错误、缩进问题,以及可能的逻辑结构错误。以下是经过检查和调整后的完整代码:
Show full message · 343 lines 错误于 130:33 Undeclared identifier 'tr'
15:52:51 错误于 216:46 Could not find function or function reference 'change'
Assistant2024-10-01 07:53 UTC Copy response
在 Pine Script 中,tr 和 change 是常用的技术指标和函数,但在你的代码中似乎没有定义或引用它们。下面是如何解决这些问题的步骤。
1. 未声明的标识符 tr
tr 通常表示真实波动幅度(True Range),在技术分析中是一个用于计算波动性的指标。你需要先声明它。
以下是定义 tr 的简单方法:
pinescript Copy code
// 计算真实波动幅度
tr = ta.tr(true)
请确保在使用 tr 变量之前,包括在 get_mrc() 调用以及所有调用的地方声明它。
2. 找不到函数或函数引用 change
change 在 Pine Script 的早期版本中可能直接作为一个函数被调用,你可以用 ta.change() 替代它,在 Pine Script v5 中的技术指标库中提供过变化值。
以下是 change 函数的正确使用:
pinescript Copy code
change_value = ta.change(time)
请替换原始代码中所有调用 change 的地方,确保它们被正确替换。
调整后的代码片段
请查看以下调整后的关键代码片段:
pinescript Copy code
//@version=5
indicator("Mean Reversion Channel - MRI Variant", shorttitle="MRC", overlay=true, format=format.inherit)
// Parameter (省略之前定义过的参数部分)
// 计算真实波动幅度
tr = ta.tr(true) // 声明TR
// Functions Start
// 请在使用 tr 的函数或调用前部分
supersmoother(_src, _length) =>
s_a1 = math.exp(-math.sqrt(2.0) * pi / _length)
s_b1 = 2.0 * s_a1 * math.cos(math.sqrt(2.0) * pi / _length)
s_c3 = -math.pow(s_a1, 2.0)
s_c2 = s_b1
s_c1 = 1.0 - s_c2 - s_c3
ss = 0.0
ss := s_c1 * _src + s_c2 * nz(ss[1], _src[1]) + s_c3 * nz(ss[2], _src[2])
f_PriceLine(_ref, linecol) =>
...
f_MTFLabel(_txt, _yloc) =>
...
// Calculate Channel
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
// Drawing Start
...
// Correct usage of change
change_value = ta.change(time) // 更换为 ta.change
...
请确保将初始化的变量放在需要使用之前,并通过技术指标库的函数来替换掉旧有的直接函数调用。这会避免遇到相关的函数未声明或未定义的问题。调整后应能解决你遇到的具体编译问题。如仍有其他问题,欢迎随时交流。
Assistant2024-10-01 07:54 UTC Copy response
下面是经过调整和修正的完整代码,包括正确声明和使用 tr 及 change 函数。
Show full message · 345 lines 15:54:29 警告于 279:18 The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that transp has no effect in plots where the color is calculated at runtime
15:54:29 警告于 279:96 The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that transp has no effect in plots where the color is calculated at runtime
15:54:29 警告于 280:18 The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that transp has no effect in plots where the color is calculated at runtime
15:54:29 警告于 280:96 The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that transp has no effect in plots where the color is calculated at runtime
15:54:29 警告于 281:18 The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that transp has no effect in plots where the color is calculated at runtime
15:54:29 警告于 281:96 The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that transp has no effect in plots where the color is calculated at runtime
15:54:29 警告于 282:18 The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that transp has no effect in plots where the color is calculated at runtime
15:54:29 警告于 282:96 The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that transp has no effect in plots where the color is calculated at runtime
15:54:29 警告于 283:18 The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that transp has no effect in plots where the color is calculated at runtime
15:54:29 警告于 283:96 The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that transp has no effect in plots where the color is calculated at runtime
15:54:29 警告于 284:18 The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 284:96 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 285:18 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 285:96 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 286:18 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 286:96 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 287:18 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 287:96 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 290:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 291:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 292:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 293:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 294:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 295:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 296:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 297:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 298:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 299:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 300:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 301:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 302:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 303:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 304:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
15:54:29 警告于 305:1 The argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that has no effect in plots where the color is calculated at runtime
Show full message · 34 lines Assistant2024-10-01 07:55 UTC Copy response
在 Pine Script v5 中,transp 参数用于设置透明度的方式已经被弃用了,现在建议使用 color.new() 或 color.rgb() 来设置颜色的透明度。以下是如何更新代码以使用新的方式来控制透明度。
如何更新透明度设置
你需要用 color.new() 替代所有的 transp 参数。比如:
pinescript Copy code
// 旧的方法
color=color1, transp=zonetransp
// 新的方法
color=color.new(color1, zonetransp)
请在你的代码中,把出现 transp 参数的地方按如下方式修改:
pinescript Copy code
// Fills for gradient zone with updated transparency setting
fill(plot_upband2_1, plot_upband2_2, color=color.new(color1, zonetransp))
fill(plot_upband2_2, plot_upband2_3, color=color.new(color2, zonetransp))
fill(plot_upband2_3, plot_upband2_4, color=color.new(color3, zonetransp))
fill(plot_upband2_4, plot_upband2_5, color=color.new(color4, zonetransp))
fill(plot_upband2_5, plot_upband2_6, color=color.new(color5, zonetransp))
fill(plot_upband2_6, plot_upband2_7, color=color.new(color6, zonetransp))
fill(plot_upband2_7, plot_upband2_8, color=color.new(color7, zonetransp))
fill(plot_upband2_8, plot_upband2_9, color=color.new(color8, zonetransp))
fill(plot_loband2_1, plot_loband2_2, color=color.new(color1, zonetransp))
fill(plot_loband2_2, plot_loband2_3, color=color.new(color2, zonetransp))
fill(plot_loband2_3, plot_loband2_4, color=color.new(color3, zonetransp))
fill(plot_loband2_4, plot_loband2_5, color=color.new(color4, zonetransp))
fill(plot_loband2_5, plot_loband2_6, color=color.new(color5, zonetransp))
fill(plot_loband2_6, plot_loband2_7, color=color.new(color6, zonetransp))
fill(plot_loband2_7, plot_loband2_8, color=color.new(color7, zonetransp))
fill(plot_loband2_8, plot_loband2_9, color=color.new(color8, zonetransp))
Show full message · 377 lines Assistant2024-10-01 08:13 UTC Copy response
要确保迁移后的指标在视觉效果上与原始版本一致,需要仔细检查几个方面。通常,图表显示不一致的原因可能是透明度设置、绘图顺序或颜色选择的差异。以下是一些可能影响视觉结果的因素以及如何纠正它们:
透明度设置 :
确保透明度值与原指标一致。color.new(color, transparency) 中的 transparency 值大于 100 会导致完全透明,0 表示不透明。
如果视觉上与之前不一致,调整 zonetransp 值更接近之前用 transp 的效果。
绘图顺序 :
绘图顺序可能影响颜色叠加效果。确保 fill 和 plot 函数的调用次序保持一致。
颜色设置 :
确保使用与原版本相同的颜色代码。例如,某些颜色值可能需要微调以达到类似的视觉效果,特别是在用 color.new() 设置透明度之后。
代码逻辑检查 :
确保转换过程中没有改变计算或逻辑。任何细微的数学运算或条件判断的不同都可能导致图形的明显变化。
以下是调整透明度和颜色的一个例子,试图保持与之前一致:
pinescript Copy code
// 假设原来 zone 的透明度效果为 60%,可以尝试以下参数
zonetransp = 40 // 透明度值范围是 0 到 100,值越大越透明
// 绘制渐变区间
fill(plot_upband2_1, plot_upband2_2, color=color.new(color1, zonetransp))
fill(plot_upband2_2, plot_upband2_3, color=color.new(color2, zonetransp))
fill(plot_upband2_3, plot_upband2_4, color=color.new(color3, zonetransp))
fill(plot_upband2_4, plot_upband2_5, color=color.new(color4, zonetransp))
fill(plot_upband2_5, plot_upband2_6, color=color.new(color5, zonetransp))
fill(plot_upband2_6, plot_upband2_7, color=color.new(color6, zonetransp))
fill(plot_upband2_7, plot_upband2_8, color=color.new(color7, zonetransp))
fill(plot_upband2_8, plot_upband2_9, color=color.new(color8, zonetransp))
fill(plot_loband2_1, plot_loband2_2, color=color.new(color1, zonetransp))
fill(plot_loband2_2, plot_loband2_3, color=color.new(color2, zonetransp))
fill(plot_loband2_3, plot_loband2_4, color=color.new(color3, zonetransp))
fill(plot_loband2_4, plot_loband2_5, color=color.new(color4, zonetransp))
fill(plot_loband2_5, plot_loband2_6, color=color.new(color5, zonetransp))
fill(plot_loband2_6, plot_loband2_7, color=color.new(color6, zonetransp))
fill(plot_loband2_7, plot_loband2_8, color=color.new(color7, zonetransp))
fill(plot_loband2_8, plot_loband2_9, color=color.new(color8, zonetransp))
调整 zonetransp 值以获得最接近原始指标的透明度效果。确保每个填充区间和线条的颜色在使用 color.new() 更新时保持一致。
如果调整后的代码仍与原始版本偏差较大,您可以提供更详细的信息或具体的代码部分,以便更为针对性地进行调试。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// REUSING THIS CODE: You are welcome to reuse this code without permission, including in closed-source publications, as long as proper credits are given :)
// Author: ©fareidzulkifli
// 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 :)
//@version=4
study("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("SuperSmoother", title="Filter Type", options=["SuperSmoother", "Ehlers EMA", "Gaussian", "Butterworth", "BandStop", "SMA", "EMA", "RMA"])
length = input(200, title="Lookback Period",minval=1)
innermult = input(1.0, title="Inner Channel Size Multiplier", minval = 0.1)
outermult = input(2.415, title="Outer Channel Size Multiplier", minval = 0.1)
Show full message · 377 lines Assistant2024-10-01 08:15 UTC Copy response
在将 Pine Script 代码从版本 4 升级到版本 5 时,我们需要确保所有功能及参数的使用方法都符合新版本的要求。下面是升级后的代码,已考虑到透明度和其他 v5 的特性:
Show full message · 350 lines 错误于 254:5 Mismatched input 'color2' expecting 'end of line without line continuation'
Assistant2024-10-01 08:16 UTC Copy response
这个错误消息表明,在第 254 行附近可能存在语法问题,尤其是在声明多个变量时使用了错误的行结束符或格式。在 Pine Script 中,声明多个变量时应该在每个变量后加上一条;来表示语句的结束,而不是将变量放在同一行并用逗号分隔。
我们将在声明颜色变量时进行调整。每个变量应该在一行内结束,确保没有语法错误。下面是调整后的代码片段:
pinescript Copy code
// Draw Zone
var color1 = color.new(#FF0000, zonetransp)
var color2 = color.new(#FF4200, zonetransp)
var color3 = color.new(#FF5D00, zonetransp)
var color4 = color.new(#FF7400, zonetransp)
var color5 = color.new(#FF9700, zonetransp)
var color6 = color.new(#FFAE00, zonetransp)
var color7 = color.new(#FFC500, zonetransp)
var color8 = color.new(#FFCD00, zonetransp)
请确保填充颜色的声明部分遵循上述格式:逐行声明变量,使用分号或换行结束每一声明。这样可以避免语法不匹配的问题。
如果进行了该调整后依然有错误信息,请确保在所有使用变量的地方如 fill 函数中没有格式不当的信息。通常这些调整能解决常见的语法错误。
错误于 130:33 Undeclared identifier 'tr'
Assistant2024-10-01 08:17 UTC Copy response
出现“Undeclared identifier 'tr'”错误是因为代码中用到了未定义的tr变量。在技术分析中,tr通常指“真实波动幅度”(True Range),但它需要正确地计算或导入。以下是在 Pine Script 中如何计算tr的步骤:
定义True Range
tr邮寄的定义通常通过当前高、低、和收盘价格之间的关系计算。Pine Script 有一个内置函数ta.tr可以直接用于这种计算。
pinescript Copy code
// 计算真实波动幅度
tr = ta.tr
插入到代码中
确保把tr的定义放在你需要使用的地方之前,通常这会在定义指标开始时就可以设置。更新后的代码片段可能如下所示:
pinescript Copy code
//@version=5
indicator("Mean Reversion Channel - MRI Variant", shorttitle="MRC", overlay=true, format=format.inherit)
// Parameters (该部分为你的输入参数设置,已省略)
// Variables
var float pi = 2.0 * math.pi
var float gradsize = 0.5
// 计算 True Range
tr = ta.tr
// Functions Start (以下为你的函数实现逻辑,已省略)
// Calculate Channel
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
// Main Channel Calculation
[meanline, meanrange, upband1, loband1, upband2, loband2, condition] = get_mrc()
[mtf1_meanline, mtf1_meanrange, mtf1_upband1, mtf1_loband1, mtf1_upband2, mtf1_loband2, mtf1_condition] = request.security(syminfo.tickerid, tf_1, get_mrc())
[mtf2_meanline, mtf2_meanrange, mtf2_upband1, mtf2_loband1, mtf2_upband2, mtf2_loband2, mtf2_condition] = request.security(syminfo.tickerid, tf_2, get_mrc())
// Drawing Start (以下为绘图逻辑,已省略)
通过上述代码片段,tr在被使用之前被正式定义,使其能够在get_mrc()函数等需要计算真实波动的地方正确工作。
这种做法应该解决你的语法问题,并确保代码的逻辑完整且符合 Pine Script 规范。如果还有其他问题,或者需要进一步的调整,请随时分享具体的错误细节。
Assistant2024-10-01 08:19 UTC Copy response
好的,我将把整段代码重新整理,并包括对tr的定义及一些其他必要调整,以便利于升级到 Pine Script v5。以下是完整的代码:
Show full message · 346 lines Copy first response
Not yet included in a published release.
//@version=5
indicator("Mean Reversion Channel - MRI Variant", shorttitle="MRC", overlay=true, format=format.inherit)
//************************************************************************************************************
// Parameter
//************************************************************************************************************
indiSet = input(false, "═════════ MRC Parameter ════════")
source = input.source(hlc3, title="Price 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 float pi = 2.0 * math.pi
var float gradsize = 0.5
var int gradtransp = zonetransp
//-----------------------
// Ehler SwissArmyKnife Function
//-----------------------
SAK_smoothing(_type, _src, _length) =>
var float c0 = 1.0
var float c1 = 0.0
var float b0 = 1.0
var float b1 = 0.0
var float b2 = 0.0
var float a1 = 0.0
var float a2 = 0.0
var float alpha = 0.0
var float beta = 0.0
var float gamma = 0.0
var float cycle = 2.0 * pi / _length
float _Output
float _Input = _src
if _type == "Ehlers EMA"
alpha := (math.cos(cycle) + math.sin(cycle) - 1.0) / math.cos(cycle)
b0 := alpha
a1 := 1.0 - alpha
if _type == "Gaussian"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "Butterworth"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha / 4.0
b1 := 2.0
b2 := 1.0
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "BandStop"
beta := math.cos(cycle)
gamma := 1.0 / math.cos(cycle * 2.0 * 0.1) // delta default to 0.1. Acceptable delta -- 0.05<d<0.5
alpha := gamma - math.sqrt((gamma * gamma) - 1.0)
c0 := (1.0 + alpha) / 2.0
b1 := -2.0 * beta
b2 := 1.0
a1 := beta * (1.0 + alpha)
a2 := -alpha
if _type == "SMA"
c1 := 1.0 / _length
b0 := 1.0 / _length
a1 := 1.0
if _type == "EMA"
alpha := 2.0 / (_length + 1.0)
b0 := alpha
a1 := 1.0 - alpha
if _type == "RMA"
alpha := 1.0 / _length
b0 := alpha
a1 := 1.0 - alpha
_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.0) * pi / _length)
s_b1 = 2.0 * s_a1 * math.cos(math.sqrt(2.0) * pi / _length)
s_c3 = -math.pow(s_a1, 2.0)
s_c2 = s_b1
s_c1 = 1.0 - s_c2 - s_c3
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.0 / 60.0 :
timeframe.isminutes ? 1.0 :
timeframe.isdaily ? 60.0 * 24.0 :
timeframe.isweekly ? 60.0 * 24.0 * 7.0 :
timeframe.ismonthly ? 60.0 * 24.0 * 30.4375 : na)
get_tf(_lvl)=>
y = f_resInMinutes()
z = timeframe.period
if mtf_typ=="Auto"
if y < 1.0
z := _lvl == 1 ? "1" : _lvl == 2 ? "5" : z
else if y <= 3.0
z := _lvl == 1 ? "5" : _lvl == 2 ? "15" : z
else if y <= 10.0
z := _lvl == 1 ? "15" : _lvl == 2 ? "60" : z
else if y <= 30.0
z := _lvl == 1 ? "60" : _lvl == 2 ? "240" : z
else if y <= 120.0
z := _lvl == 1 ? "240" : _lvl == 2 ? "D" : z
else if y <= 240.0
z := _lvl == 1 ? "D" : _lvl == 2 ? "W" : z
else if y <= 1440.0
z := _lvl == 1 ? "W" : _lvl == 2 ? "M" : z
else if y <= 10080.0
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()=>
var float mult = pi * innermult
var float mult2 = pi * outermult
v_condition = 0
v_meanline = source
v_meanrange = supersmoother(tr, length)
// Get Line value
if type == "SuperSmoother"
v_meanline := supersmoother(source, length)
if type != "SuperSmoother"
v_meanline := SAK_smoothing(type, source, length)
v_upband1 = v_meanline + (v_meanrange * mult)
v_loband1 = v_meanline - (v_meanrange * mult)
v_upband2 = v_meanline + (v_meanrange * mult2)
v_loband2 = v_meanline - (v_meanrange * mult2)
// Check Condition
if close > v_meanline
v_upband2_1 = v_upband2 + (v_meanrange * gradsize * 4.0)
v_upband2_9 = v_upband2 + (v_meanrange * gradsize * -4.0)
if high >= v_upband2_9 and high < v_upband2
v_condition := 1
else if high >= v_upband2 and high < v_upband2_1
v_condition := 2
else if high >= v_upband2_1
v_condition := 3
else if close <= v_meanline + v_meanrange
v_condition := 4
else
v_condition := 5
if close < v_meanline
v_loband2_1 = v_loband2 - (v_meanrange * gradsize * 4.0)
v_loband2_9 = v_loband2 - (v_meanrange * gradsize * -4.0)
if low <= v_loband2_9 and low > v_loband2
v_condition := -1
else if low <= v_loband2 and low > v_loband2_1
v_condition := -2
else if low <= v_loband2_1
v_condition := -3
else if close >= v_meanline + v_meanrange
v_condition := -4
else
v_condition := -5
[v_meanline, v_meanrange, v_upband1, v_loband1, v_upband2, v_loband2, v_condition]
//-----------------------
// MTF Analysis
//-----------------------
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 = str.tostring(x, "0.00000")
if x > 10.0
y := str.tostring(x, "0.000")
if x > 1000.0
y := str.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 + math.round(change(time)*20.0),
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)
//} Function End
//************************************************************************************************************
// Calculate Channel
//************************************************************************************************************
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
[meanline, meanrange, upband1, loband1, upband2, loband2, condition] = get_mrc()
[mtf1_meanline, mtf1_meanrange, mtf1_upband1, mtf1_loband1, mtf1_upband2, mtf1_loband2, mtf1_condition] = request.security(syminfo.tickerid, tf_1, get_mrc())
[mtf2_meanline, mtf2_meanrange, mtf2_upband1, mtf2_loband1, mtf2_upband2, mtf2_loband2, mtf2_condition] = request.security(syminfo.tickerid, tf_2, get_mrc())
//************************************************************************************************************
// Drawing Start {
//************************************************************************************************************
float p_meanline = drawchannel ? meanline : na
float p_upband1 = drawchannel ? upband1 : na
float p_loband1 = drawchannel ? loband1 : na
float p_upband2 = drawchannel ? upband2 : na
float p_loband2 = drawchannel ? loband2 : na
z = plot(p_meanline, color=color.new(#FFCD00, 0), style=plot.style_line, title=" Mean", linewidth=2)
x1 = plot(p_upband1, color=color.new(color.green, 50), style=plot.style_circles, title=" R1", linewidth=1)
x2 = plot(p_loband1, color=color.new(color.green, 50), style=plot.style_circles, title=" S1", linewidth=1)
y1 = plot(p_upband2, color=color.new(color.red, 50), style=plot.style_line, title=" R2", linewidth=1)
y2 = plot(p_loband2, color=color.new(color.red, 50), style=plot.style_line, title=" S2", linewidth=1)
//-----------------------
// Draw zone
//-----------------------
//---
var color1 = #FF0000, var color2 = #FF4200, var color3 = #FF5D00, var color4 = #FF7400, var color5 = #FF9700, var color6 = #FFAE00, var color7 = #FFC500, var color8 = #FFCD00
//---
upband2_1 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 4.0) : na
loband2_1 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 4.0) : na
upband2_2 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 3.0) : na
loband2_2 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 3.0) : na
upband2_3 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 2.0) : na
loband2_3 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 2.0) : na
upband2_4 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 1.0) : na
loband2_4 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 1.0) : na
upband2_5 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 0.0) : na
loband2_5 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 0.0) : na
upband2_6 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -1.0) : na
loband2_6 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -1.0) : na
upband2_7 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -2.0) : na
loband2_7 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -2.0) : na
upband2_8 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -3.0) : na
loband2_8 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -3.0) : na
upband2_9 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -4.0) : na
loband2_9 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -4.0) : na
//---
plot_upband2_1 = plot(upband2_1, color=na, transp=100, display=display.none), plot_loband2_1 = plot(loband2_1, color=na, transp=100, display=display.none)
plot_upband2_2 = plot(upband2_2, color=na, transp=100, display=display.none), plot_loband2_2 = plot(loband2_2, color=na, transp=100, display=display.none)
plot_upband2_3 = plot(upband2_3, color=na, transp=100, display=display.none), plot_loband2_3 = plot(loband2_3, color=na, transp=100, display=display.none)
plot_upband2_4 = plot(upband2_4, color=na, transp=100, display=display.none), plot_loband2_4 = plot(loband2_4, color=na, transp=100, display=display.none)
plot_upband2_5 = plot(upband2_5, color=na, transp=100, display=display.none), plot_loband2_5 = plot(loband2_5, color=na, transp=100, display=display.none)
plot_upband2_6 = plot(upband2_6, color=na, transp=100, display=display.none), plot_loband2_6 = plot(loband2_6, color=na, transp=100, display=display.none)
plot_upband2_7 = plot(upband2_7, color=na, transp=100, display=display.none), plot_loband2_7 = plot(loband2_7, color=na, transp=100, display=display.none)
plot_upband2_8 = plot(upband2_8, color=na, transp=100, display=display.none), plot_loband2_8 = plot(loband2_8, color=na, transp=100, display=display.none)
plot_upband2_9 = plot(upband2_9, color=na, transp=100, display=display.none), plot_loband2_9 = plot(loband2_9, color=na, transp=100, display=display.none)
//---
fill(plot_upband2_1,plot_upband2_2, color=color1, transp=gradtransp), fill(plot_loband2_1,plot_loband2_2, color=color1, transp=gradtransp)
fill(plot_upband2_2,plot_upband2_3, color=color2, transp=gradtransp), fill(plot_loband2_2,plot_loband2_3, color=color2, transp=gradtransp)
fill(plot_upband2_3,plot_upband2_4, color=color3, transp=gradtransp), fill(plot_loband2_3,plot_loband2_4, color=color3, transp=gradtransp)
fill(plot_upband2_4,plot_upband2_5, color=color4, transp=gradtransp), fill(plot_loband2_4,plot_loband2_5, color=color4, transp=gradtransp)
fill(plot_upband2_5,plot_upband2_6, color=color5, transp=gradtransp), fill(plot_loband2_5,plot_loband2_6, color=color5, transp=gradtransp)
fill(plot_upband2_6,plot_upband2_7, color=color6, transp=gradtransp), fill(plot_loband2_6,plot_loband2_7, color=color6, transp=gradtransp)
fill(plot_upband2_7,plot_upband2_8, color=color7, transp=gradtransp), fill(plot_loband2_7,plot_loband2_8, color=color7, transp=gradtransp)
fill(plot_upband2_8,plot_upband2_9, color=color8, transp=gradtransp), fill(plot_loband2_8,plot_loband2_9, color=color8, transp=gradtransp)
//-----------------------
// Plot Extension
//-----------------------
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
//--------------
// Prep MTF Label
//--------------
var string brl = "\n--------------------------------------"
dist_0 = "Distance from Mean: "+ str.tostring(((close-meanline)/close)*100.0, "#.##") + " %"
dist_1 = "Distance from Mean: "+ str.tostring(((close-mtf1_meanline)/close)*100.0, "#.##") + " %"
dist_2 = "Distance from Mean: "+ str.tostring(((close-mtf2_meanline)/close)*100.0, "#.##") + " %"
var string title = "Mean Reversion Channel\nMultiple TimeFrame Analysis" + brl
tf0 =
"\n\nTimeframe: "+ tf_0 + " (Current)\n\nStatus: " + get_stat(condition) + dist_0 + brl
tf1 = not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_1 + "\n\nStatus: " + get_stat(mtf1_condition) + dist_1 + brl : ""
tf2 = not timeframe.isweekly and not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_2 + "\n\nStatus: " + get_stat(mtf2_condition) + dist_2 + brl : ""
var string 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
Changes made:
Changed //@version=4 to //@version=5.
Converted study function to indicator.
Replaced all input function calls with their version 5 signature, including input.source, input.string, input.int, input.timeframe, and input.float.
Replaced security with request.security.
Replaced math function calls according to version 5 API (e.g., math.pi, math.cos, etc.)
Used str.tostring from the version 5 API to format strings.
Adjusted variable declarations to conform with the structure of version 5.
Adjusted color settings using color.new for transparency effect.
This should make the script compatible with Pine Script version 5.
if _type == "Ehlers EMA"
alpha := (math.cos(cycle) + math.sin(cycle) - 1.0) / math.cos(cycle)
b0 := alpha
a1 := 1.0 - alpha
if _type == "Gaussian"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "Butterworth"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha / 4.0
b1 := 2.0
b2 := 1.0
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "BandStop"
beta := math.cos(cycle)
gamma := 1.0 / math.cos(cycle * 2.0 * 0.1) // delta default to 0.1. Acceptable delta -- 0.05<d<0.5
alpha := gamma - math.sqrt((gamma * gamma) - 1.0)
c0 := (1.0 + alpha) / 2.0
b1 := -2.0 * beta
b2 := 1.0
a1 := beta * (1.0 + alpha)
a2 := -alpha
if _type == "SMA"
c1 := 1.0 / _length
b0 := 1.0 / _length
a1 := 1.0
if _type == "EMA"
alpha := 2.0 / (_length + 1.0)
b0 := alpha
a1 := 1.0 - alpha
if _type == "RMA"
alpha := 1.0 / _length
b0 := alpha
a1 := 1.0 - alpha
_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.0) * pi / _length)
s_b1 = 2.0 * s_a1 * math.cos(math.sqrt(2.0) * pi / _length)
s_c3 = -math.pow(s_a1, 2.0)
s_c2 = s_b1
s_c1 = 1.0 - s_c2 - s_c3
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.0 / 60.0 :
timeframe.isminutes ? 1.0 :
timeframe.isdaily ? 60.0 * 24.0 :
timeframe.isweekly ? 60.0 * 24.0 * 7.0 :
timeframe.ismonthly ? 60.0 * 24.0 * 30.4375 : na)
get_tf(_lvl)=>
y = f_resInMinutes()
z = timeframe.period
if mtf_typ=="Auto"
if y < 1.0
z := _lvl == 1 ? "1" : _lvl == 2 ? "5" : z
else if y <= 3.0
z := _lvl == 1 ? "5" : _lvl == 2 ? "15" : z
else if y <= 10.0
z := _lvl == 1 ? "15" : _lvl == 2 ? "60" : z
else if y <= 30.0
z := _lvl == 1 ? "60" : _lvl == 2 ? "240" : z
else if y <= 120.0
z := _lvl == 1 ? "240" : _lvl == 2 ? "D" : z
else if y <= 240.0
z := _lvl == 1 ? "D" : _lvl == 2 ? "W" : z
else if y <= 1440.0
z := _lvl == 1 ? "W" : _lvl == 2 ? "M" : z
else if y <= 10080.0
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()=>
var float mult = pi * innermult
var float mult2 = pi * outermult
v_condition = 0
v_meanline = source
v_meanrange = supersmoother(tr, length)
code Copy code
// Get Line value
if type == "SuperSmoother"
v_meanline := supersmoother(source, length)
if type != "SuperSmoother"
v_meanline := SAK_smoothing(type, source, length)
v_upband1 = v_meanline + (v_meanrange * mult)
v_loband1 = v_meanline - (v_meanrange * mult)
v_upband2 = v_meanline + (v_meanrange * mult2)
v_loband2 = v_meanline - (v_meanrange * mult2)
// Check Condition
if close > v_meanline
v_upband2_1 = v_upband2 + (v_meanrange * gradsize * 4.0)
v_upband2_9 = v_upband2 + (v_meanrange * gradsize * -4.0)
if high >= v_upband2_9 and high < v_upband2
v_condition := 1
else if high >= v_upband2 and high < v_upband2_1
v_condition := 2
else if high >= v_upband2_1
v_condition := 3
else if close <= v_meanline + v_meanrange
v_condition := 4
else
v_condition := 5
if close < v_meanline
v_loband2_1 = v_loband2 - (v_meanrange * gradsize * 4.0)
v_loband2_9 = v_loband2 - (v_meanrange * gradsize * -4.0)
if low <= v_loband2_9 and low > v_loband2
v_condition := -1
else if low <= v_loband2 and low > v_loband2_1
v_condition := -2
else if low <= v_loband2_1
v_condition := -3
else if close >= v_meanline + v_meanrange
v_condition := -4
else
v_condition := -5
[v_meanline, v_meanrange, v_upband1, v_loband1, v_upband2, v_loband2, v_condition]
//-----------------------
// MTF Analysis
//-----------------------
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 = str.tostring(x, "0.00000")
if x > 10.0
y := str.tostring(x, "0.000")
if x > 1000.0
y := str.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 + math.round(change(time)*20.0),
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)
//************************************************************************************************************
// Calculate Channel
//************************************************************************************************************
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
[meanline, meanrange, upband1, loband1, upband2, loband2, condition] = get_mrc()
[mtf1_meanline, mtf1_meanrange, mtf1_upband1, mtf1_loband1, mtf1_upband2, mtf1_loband2, mtf1_condition] = request.security(syminfo.tickerid, tf_1, get_mrc())
[mtf2_meanline, mtf2_meanrange, mtf2_upband1, mtf2_loband1, mtf2_upband2, mtf2_loband2, mtf2_condition] = request.security(syminfo.tickerid, tf_2, get_mrc())
//************************************************************************************************************
// Drawing Start {
//************************************************************************************************************
float p_meanline = drawchannel ? meanline : na
float p_upband1 = drawchannel ? upband1 : na
float p_loband1 = drawchannel ? loband1 : na
float p_upband2 = drawchannel ? upband2 : na
float p_loband2 = drawchannel ? loband2 : na
z = plot(p_meanline, color=color.new(#FFCD00, 0), style=plot.style_line, title=" Mean", linewidth=2)
x1 = plot(p_upband1, color=color.new(color.green, 50), style=plot.style_circles, title=" R1", linewidth=1)
x2 = plot(p_loband1, color=color.new(color.green, 50), style=plot.style_circles, title=" S1", linewidth=1)
y1 = plot(p_upband2, color=color.new(color.red, 50), style=plot.style_line, title=" R2", linewidth=1)
y2 = plot(p_loband2, color=color.new(color.red, 50), style=plot.style_line, title=" S2", linewidth=1)
//-----------------------
// Draw zone
//-----------------------
//---
var color1 = #FF0000, var color2 = #FF4200, var color3 = #FF5D00, var color4 = #FF7400, var color5 = #FF9700, var color6 = #FFAE00, var color7 = #FFC500, var color8 = #FFCD00
//---
upband2_1 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 4.0) : na
loband2_1 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 4.0) : na
upband2_2 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 3.0) : na
loband2_2 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 3.0) : na
upband2_3 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 2.0) : na
loband2_3 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 2.0) : na
upband2_4 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 1.0) : na
loband2_4 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 1.0) : na
upband2_5 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 0.0) : na
loband2_5 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 0.0) : na
upband2_6 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -1.0) : na
loband2_6 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -1.0) : na
upband2_7 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -2.0) : na
loband2_7 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -2.0) : na
upband2_8 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -3.0) : na
loband2_8 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -3.0) : na
upband2_9 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -4.0) : na
loband2_9 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -4.0) : na
//---
plot_upband2_1 = plot(upband2_1, color=na, transp=100, display=display.none), plot_loband2_1 = plot(loband2_1, color=na, transp=100, display=display.none)
plot_upband2_2 = plot(upband2_2, color=na, transp=100, display=display.none), plot_loband2_2 = plot(loband2_2, color=na, transp=100, display=display.none)
plot_upband2_3 = plot(upband2_3, color=na, transp=100, display=display.none), plot_loband2_3 = plot(loband2_3, color=na, transp=100, display=display.none)
plot_upband2_4 = plot(upband2_4, color=na, transp=100, display=display.none), plot_loband2_4 = plot(loband2_4, color=na, transp=100, display=display.none)
plot_upband2_5 = plot(upband2_5, color=na, transp=100, display=display.none), plot_loband2_5 = plot(loband2_5, color=na, transp=100, display=display.none)
plot_upband2_6 = plot(upband2_6, color=na, transp=100, display=display.none), plot_loband2_6 = plot(loband2_6, color=na, transp=100, display=display.none)
plot_upband2_7 = plot(upband2_7, color=na, transp=100, display=display.none), plot_loband2_7 = plot(loband2_7, color=na, transp=100, display=display.none)
plot_upband2_8 = plot(upband2_8, color=na, transp=100, display=display.none), plot_loband2_8 = plot(loband2_8, color=na, transp=100, display=display.none)
plot_upband2_9 = plot(upband2_9, color=na, transp=100, display=display.none), plot_loband2_9 = plot(loband2_9, color=na, transp=100, display=display.none)
//---
fill(plot_upband2_1,plot_upband2_2, color=color1, transp=gradtransp), fill(plot_loband2_1,plot_loband2_2, color=color1, transp=gradtransp)
fill(plot_upband2_2,plot_upband2_3, color=color2, transp=gradtransp), fill(plot_loband2_2,plot_loband2_3, color=color2, transp=gradtransp)
fill(plot_upband2_3,plot_upband2_4, color=color3, transp=gradtransp), fill(plot_loband2_3,plot_loband2_4, color=color3, transp=gradtransp)
fill(plot_upband2_4,plot_upband2_5, color=color4, transp=gradtransp), fill(plot_loband2_4,plot_loband2_5, color=color4, transp=gradtransp)
fill(plot_upband2_5,plot_upband2_6, color=color5, transp=gradtransp), fill(plot_loband2_5,plot_loband2_6, color=color5, transp=gradtransp)
fill(plot_upband2_6,plot_upband2_7, color=color6, transp=gradtransp), fill(plot_loband2_6,plot_loband2_7, color=color6, transp=gradtransp)
fill(plot_upband2_7,plot_upband2_8, color=color7, transp=gradtransp), fill(plot_loband2_7,plot_loband2_8, color=color7, transp=gradtransp)
fill(plot_upband2_8,plot_upband2_9, color=color8, transp=gradtransp), fill(plot_loband2_8,plot_loband2_9, color=color8, transp=gradtransp)
//-----------------------
// Plot Extension
//-----------------------
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
//--------------
// Prep MTF Label
//--------------
var string brl = "\n--------------------------------------"
dist_0 = "Distance from Mean: "+ str.tostring(((close-meanline)/close)*100.0, "#.##") + " %"
dist_1 = "Distance from Mean: "+ str.tostring(((close-mtf1_meanline)/close)*100.0, "#.##") + " %"
dist_2 = "Distance from Mean: "+ str.tostring(((close-mtf2_meanline)/close)*100.0, "#.##") + " %"
var string title = "Mean Reversion Channel\nMultiple TimeFrame Analysis" + brl
tf0 =
"\n\nTimeframe: "+ tf_0 + " (Current)\n\nStatus: " + get_stat(condition) + dist_0 + brl
tf1 = not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_1 + "\n\nStatus: " + get_stat(mtf1_condition) + dist_1 + brl : ""
tf2 = not timeframe.isweekly and not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_2 + "\n\nStatus: " + get_stat(mtf2_condition) + dist_2 + brl : ""
var string 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
有错误提示,请修改
错误于 50:18 Syntax error at input 'end of line without line continuation'
//@version=5
indicator("Mean Reversion Channel - MRI Variant", shorttitle="MRC", overlay=true, format=format.inherit)
// Parameter
indiSet = input(false, "═════════ MRC Parameter ════════")
source = input.source(hlc3, title="Price 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")
// Variables
var float pi = 2.0 * math.pi
var float gradsize = 0.5
// Ehler's Swiss Army Knife Function
SAK_smoothing(_type, _src, _length) =>
var float c0 = 1.0, c1 = 0.0, b0 = 1.0, b1 = 0.0, b2 = 0.0, a1 = 0.0, a2 = 0.0
var float alpha = 0.0, beta = 0.0, gamma = 0.0
var float cycle = 2.0 * pi / _length
float _Output = na
float _Input = _src
if _type == "Ehlers EMA"
alpha := (math.cos(cycle) + math.sin(cycle) - 1.0) / math.cos(cycle)
b0 := alpha
a1 := 1.0 - alpha
if _type == "Gaussian"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "Butterworth"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha / 4.0
b1 := 2.0
b2 := 1.0
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "BandStop"
beta := math.cos(cycle)
gamma := 1.0 / math.cos(cycle * 2.0 * 0.1) // delta default to 0.1. Acceptable delta -- 0.05<d<0.5
alpha := gamma - math.sqrt((gamma * gamma) - 1.0)
c0 := (1.0 + alpha) / 2.0
b1 := -2.0 * beta
b2 := 1.0
a1 := beta * (1.0 + alpha)
a2 := -alpha
if _type == "SMA"
c1 := 1.0 / _length
b0 := 1.0 / _length
a1 := 1.0
if _type == "EMA"
alpha := 2.0 / (_length + 1.0)
b0 := alpha
a1 := 1.0 - alpha
if _type == "RMA"
alpha := 1.0 / _length
b0 := alpha
a1 := 1.0 - alpha
_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.0) * pi / _length)
s_b1 = 2.0 * s_a1 * math.cos(math.sqrt(2.0) * pi / _length)
s_c3 = -math.pow(s_a1, 2.0)
s_c2 = s_b1
s_c1 = 1.0 - s_c2 - s_c3
ss = 0.0
ss := s_c1 * _src + s_c2 * nz(ss[1], _src[1]) + s_c3 * nz(ss[2], _src[2])
// Auto TimeFrame Function
f_resInMinutes() =>
_resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1.0 / 60.0 :
timeframe.isminutes ? 1.0 :
timeframe.isdaily ? 60.0 * 24.0 :
timeframe.isweekly ? 60.0 * 24.0 * 7.0 :
timeframe.ismonthly ? 60.0 * 24.0 * 30.4375 : na)
get_tf(_lvl)=>
y = f_resInMinutes()
z = timeframe.period
if mtf_typ=="Auto"
if y < 1.0
z := _lvl == 1 ? "1" : _lvl == 2 ? "5" : z
else if y <= 3.0
z := _lvl == 1 ? "5" : _lvl == 2 ? "15" : z
else if y <= 10.0
z := _lvl == 1 ? "15" : _lvl == 2 ? "60" : z
else if y <= 30.0
z := _lvl == 1 ? "60" : _lvl == 2 ? "240" : z
else if y <= 120.0
z := _lvl == 1 ? "240" : _lvl == 2 ? "D" : z
else if y <= 240.0
z := _lvl == 1 ? "D" : _lvl == 2 ? "W" : z
else if y <= 1440.0
z := _lvl == 1 ? "W" : _lvl == 2 ? "M" : z
else if y <= 10080.0
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()=>
var float mult = pi * innermult
var float mult2 = pi * outermult
v_condition = 0
v_meanline = source
v_meanrange = supersmoother(tr, length)
// Get Line value
if type == "SuperSmoother"
v_meanline := supersmoother(source, length)
if type != "SuperSmoother"
v_meanline := SAK_smoothing(type, source, length)
v_upband1 = v_meanline + (v_meanrange * mult)
v_loband1 = v_meanline - (v_meanrange * mult)
v_upband2 = v_meanline + (v_meanrange * mult2)
v_loband2 = v_meanline - (v_meanrange * mult2)
// Check Condition
if close > v_meanline
v_upband2_1 = v_upband2 + (v_meanrange * gradsize * 4.0)
v_upband2_9 = v_upband2 + (v_meanrange * gradsize * -4.0)
if high >= v_upband2_9 and high < v_upband2
v_condition := 1
else if high >= v_upband2 and high < v_upband2_1
v_condition := 2
else if high >= v_upband2_1
v_condition := 3
else if close <= v_meanline + v_meanrange
v_condition := 4
else
v_condition := 5
if close < v_meanline
v_loband2_1 = v_loband2 - (v_meanrange * gradsize * 4.0)
v_loband2_9 = v_loband2 - (v_meanrange * gradsize * -4.0)
if low <= v_loband2_9 and low > v_loband2
v_condition := -1
else if low <= v_loband2 and low > v_loband2_1
v_condition := -2
else if low <= v_loband2_1
v_condition := -3
else if close >= v_meanline + v_meanrange
v_condition := -4
else
v_condition := -5
[v_meanline, v_meanrange, v_upband1, v_loband1, v_upband2, v_loband2, v_condition]
// MTF Analysis
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 = str.tostring(x, "0.00000")
if x > 10.0
y := str.tostring(x, "0.000")
if x > 1000.0
y := str.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 + math.round(change(time)*20.0),
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)
// Calculate Channel
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
// Main Channel Calculation
[meanline, meanrange, upband1, loband1, upband2, loband2, condition] = get_mrc()
[mtf1_meanline, mtf1_meanrange, mtf1_upband1, mtf1_loband1, mtf1_upband2, mtf1_loband2, mtf1_condition] = request.security(syminfo.tickerid, tf_1, get_mrc())
[mtf2_meanline, mtf2_meanrange, mtf2_upband1, mtf2_loband1, mtf2_upband2, mtf2_loband2, mtf2_condition] = request.security(syminfo.tickerid, tf_2, get_mrc())
// Drawing Start
float p_meanline = drawchannel ? meanline : na
float p_upband1 = drawchannel ? upband1 : na
float p_loband1 = drawchannel ? loband1 : na
float p_upband2 = drawchannel ? upband2 : na
float p_loband2 = drawchannel ? loband2 : na
// Plot Main Lines
plot(p_meanline, color=color.new(#FFCD00, 0), style=plot.style_line, title=" Mean", linewidth=2)
plot(p_upband1, color=color.new(color.green, 50), style=plot.style_circles, title=" R1", linewidth=1)
plot(p_loband1, color=color.new(color.green, 50), style=plot.style_circles, title=" S1", linewidth=1)
plot(p_upband2, color=color.new(color.red, 50), style=plot.style_line, title=" R2", linewidth=1)
plot(p_loband2, color=color.new(color.red, 50), style=plot.style_line, title=" S2", linewidth=1)
// Draw zone
var color1 = #FF0000, var color2 = #FF4200, var color3 = #FF5D00, var color4 = #FF7400, var color5 = #FF9700, var color6 = #FFAE00, var color7 = #FFC500, var color8 = #FFCD00
// Zone Calculation
upband2_1 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 4.0) : na
loband2_1 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 4.0) : na
upband2_2 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 3.0) : na
loband2_2 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 3.0) : na
upband2_3 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 2.0) : na
loband2_3 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 2.0) : na
upband2_4 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 1.0) : na
loband2_4 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 1.0) : na
upband2_5 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 0.0) : na
loband2_5 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 0.0) : na
upband2_6 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -1.0) : na
loband2_6 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -1.0) : na
upband2_7 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -2.0) : na
loband2_7 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -2.0) : na
upband2_8 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -3.0) : na
loband2_8 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -3.0) : na
upband2_9 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -4.0) : na
loband2_9 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -4.0) : na
// Plot hidden zones and fills
plot_upband2_1 = plot(upband2_1, color=na, transp=100, display=display.none), plot_loband2_1 = plot(loband2_1, color=na, transp=100, display=display.none)
plot_upband2_2 = plot(upband2_2, color=na, transp=100, display=display.none), plot_loband2_2 = plot(loband2_2, color=na, transp=100, display=display.none)
plot_upband2_3 = plot(upband2_3, color=na, transp=100, display=display.none), plot_loband2_3 = plot(loband2_3, color=na, transp=100, display=display.none)
plot_upband2_4 = plot(upband2_4, color=na, transp=100, display=display.none), plot_loband2_4 = plot(loband2_4, color=na, transp=100, display=display.none)
plot_upband2_5 = plot(upband2_5, color=na, transp=100, display=display.none), plot_loband2_5 = plot(loband2_5, color=na, transp=100, display=display.none)
plot_upband2_6 = plot(upband2_6, color=na, transp=100, display=display.none), plot_loband2_6 = plot(loband2_6, color=na, transp=100, display=display.none)
plot_upband2_7 = plot(upband2_7, color=na, transp=100, display=display.none), plot_loband2_7 = plot(loband2_7, color=na, transp=100, display=display.none)
plot_upband2_8 = plot(upband2_8, color=na, transp=100, display=display.none), plot_loband2_8 = plot(loband2_8, color=na, transp=100, display=display.none)
plot_upband2_9 = plot(upband2_9, color=na, transp=100, display=display.none), plot_loband2_9 = plot(loband2_9, color=na, transp=100, display=display.none)
// Fills for gradient zone
fill(plot_upband2_1, plot_upband2_2, color=color1, transp=zonetransp)
fill(plot_upband2_2, plot_upband2_3, color=color2, transp=zonetransp)
fill(plot_upband2_3, plot_upband2_4, color=color3, transp=zonetransp)
fill(plot_upband2_4, plot_upband2_5, color=color4, transp=zonetransp)
fill(plot_upband2_5, plot_upband2_6, color=color5, transp=zonetransp)
fill(plot_upband2_6, plot_upband2_7, color=color6, transp=zonetransp)
fill(plot_upband2_7, plot_upband2_8, color=color7, transp=zonetransp)
fill(plot_upband2_8, plot_upband2_9, color=color8, transp=zonetransp)
fill(plot_loband2_1, plot_loband2_2, color=color1, transp=zonetransp)
fill(plot_loband2_2, plot_loband2_3, color=color2, transp=zonetransp)
fill(plot_loband2_3, plot_loband2_4, color=color3, transp=zonetransp)
fill(plot_loband2_4, plot_loband2_5, color=color4, transp=zonetransp)
fill(plot_loband2_5, plot_loband2_6, color=color5, transp=zonetransp)
fill(plot_loband2_6, plot_loband2_7, color=color6, transp=zonetransp)
fill(plot_loband2_7, plot_loband2_8, color=color7, transp=zonetransp)
fill(plot_loband2_8, plot_loband2_9, color=color8, transp=zonetransp)
// Plot Extension Lines
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
// Prep MTF Label
var string brl = "\n--------------------------------------"
dist_0 = "Distance from Mean: "+ str.tostring(((close-meanline)/close)*100.0, "#.##") + " %"
dist_1 = "Distance from Mean: "+ str.tostring(((close-mtf1_meanline)/close)*100.0, "#.##") + " %"
dist_2 = "Distance from Mean: "+ str.tostring(((close-mtf2_meanline)/close)*100.0, "#.##") + " %"
// Manage title concatenation
var string title = "Mean Reversion Channel\nMultiple TimeFrame Analysis" + brl
tf0 = "\n\nTimeframe: "+ tf_0 + " (Current)\n\nStatus: " + get_stat(condition) + dist_0 + brl
tf1 = not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_1 + "\n\nStatus: " + get_stat(mtf1_condition) + dist_1 + brl : ""
tf2 = not timeframe.isweekly and not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_2 + "\n\nStatus: " + get_stat(mtf2_condition) + dist_2 + brl : ""
var string 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
说明:
变量声明与初始化 :使用 var 类型是可选的,但推荐在需要长生命周期时使用。在参数类型未定义时确保类型推导。
函数与条件 :仔细检查每个函数内的返回值与条件语句,以确保变量在正确顺序下进行初始化与赋值。
图形化绘制 :保证每个 plot 和 line 在适当条件下定义,且各绘图函数具备充足的参数支持。
字符串与文本格式 :合并使用 str.tostring 提供数值格式化与文本展示需求。
//@version=5
indicator("Mean Reversion Channel - MRI Variant", shorttitle="MRC", overlay=true, format=format.inherit)
// Parameters
indiSet = input(false, "═════════ MRC Parameter ════════")
source = input.source(hlc3, title="Price 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")
// Variables
var float pi = 2.0 * math.pi
var float gradsize = 0.5
// Calculate True Range
tr = ta.tr(true) // True Range
// Ehler's Swiss Army Knife Smoothing
SAK_smoothing(_type, _src, _length) =>
var float c0 = 1.0, c1 = 0.0, b0 = 1.0, b1 = 0.0, b2 = 0.0, a1 = 0.0, a2 = 0.0
var float alpha = 0.0, beta = 0.0, gamma = 0.0
var float cycle = 2.0 * pi / _length
float _Output = na
float _Input = _src
if _type == "Ehlers EMA"
alpha := (math.cos(cycle) + math.sin(cycle) - 1.0) / math.cos(cycle)
b0 := alpha
a1 := 1.0 - alpha
if _type == "Gaussian"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "Butterworth"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha / 4.0
b1 := 2.0
b2 := 1.0
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "BandStop"
beta := math.cos(cycle)
gamma := 1.0 / math.cos(cycle * 2.0 * 0.1)
alpha := gamma - math.sqrt((gamma * gamma) - 1.0)
c0 := (1.0 + alpha) / 2.0
b1 := -2.0 * beta
b2 := 1.0
a1 := beta * (1.0 + alpha)
a2 := -alpha
if _type == "SMA"
c1 := 1.0 / _length
b0 := 1.0 / _length
a1 := 1.0
if _type == "EMA"
alpha := 2.0 / (_length + 1.0)
b0 := alpha
a1 := 1.0 - alpha
if _type == "RMA"
alpha := 1.0 / _length
b0 := alpha
a1 := 1.0 - alpha
_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.0) * pi / _length)
s_b1 = 2.0 * s_a1 * math.cos(math.sqrt(2.0) * pi / _length)
s_c3 = -math.pow(s_a1, 2.0)
s_c2 = s_b1
s_c1 = 1.0 - s_c2 - s_c3
ss = 0.0
ss := s_c1 * _src + s_c2 * nz(ss[1], _src[1]) + s_c3 * nz(ss[2], _src[2])
// Auto TimeFrame Function
f_resInMinutes() =>
_resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1.0 / 60.0 :
timeframe.isminutes ? 1.0 :
timeframe.isdaily ? 60.0 * 24.0 :
timeframe.isweekly ? 60.0 * 24.0 * 7.0 :
timeframe.ismonthly ? 60.0 * 24.0 * 30.4375 : na)
get_tf(_lvl)=>
y = f_resInMinutes()
z = timeframe.period
if mtf_typ=="Auto"
if y < 1.0
z := _lvl == 1 ? "1" : _lvl == 2 ? "5" : z
else if y <= 3.0
z := _lvl == 1 ? "5" : _lvl == 2 ? "15" : z
else if y <= 10.0
z := _lvl == 1 ? "15" : _lvl == 2 ? "60" : z
else if y <= 30.0
z := _lvl == 1 ? "60" : _lvl == 2 ? "240" : z
else if y <= 120.0
z := _lvl == 1 ? "240" : _lvl == 2 ? "D" : z
else if y <= 240.0
z := _lvl == 1 ? "D" : _lvl == 2 ? "W" : z
else if y <= 1440.0
z := _lvl == 1 ? "W" : _lvl == 2 ? "M" : z
else if y <= 10080.0
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()=>
var float mult = pi * innermult
var float mult2 = pi * outermult
v_condition = 0
v_meanline = source
v_meanrange = supersmoother(tr, length)
// Get Line value
if type == "SuperSmoother"
v_meanline := supersmoother(source, length)
if type != "SuperSmoother"
v_meanline := SAK_smoothing(type, source, length)
v_upband1 = v_meanline + (v_meanrange * mult)
v_loband1 = v_meanline - (v_meanrange * mult)
v_upband2 = v_meanline + (v_meanrange * mult2)
v_loband2 = v_meanline - (v_meanrange * mult2)
// Check Condition
if close > v_meanline
v_upband2_1 = v_upband2 + (v_meanrange * gradsize * 4.0)
v_upband2_9 = v_upband2 + (v_meanrange * gradsize * -4.0)
if high >= v_upband2_9 and high < v_upband2
v_condition := 1
else if high >= v_upband2 and high < v_upband2_1
v_condition := 2
else if high >= v_upband2_1
v_condition := 3
else if close <= v_meanline + v_meanrange
v_condition := 4
else
v_condition := 5
if close < v_meanline
v_loband2_1 = v_loband2 - (v_meanrange * gradsize * 4.0)
v_loband2_9 = v_loband2 - (v_meanrange * gradsize * -4.0)
if low <= v_loband2_9 and low > v_loband2
v_condition := -1
else if low <= v_loband2 and low > v_loband2_1
v_condition := -2
else if low <= v_loband2_1
v_condition := -3
else if close >= v_meanline + v_meanrange
v_condition := -4
else
v_condition := -5
[v_meanline, v_meanrange, v_upband1, v_loband1, v_upband2, v_loband2, v_condition]
// MTF Analysis
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 = str.tostring(x, "0.00000")
if x > 10.0
y := str.tostring(x, "0.000")
if x > 1000.0
y := str.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 + math.round(ta.change(time) * 20.0),
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)
// Calculate Channel
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
// Main Channel Calculation
[meanline, meanrange, upband1, loband1, upband2, loband2, condition] = get_mrc()
[mtf1_meanline, mtf1_meanrange, mtf1_upband1, mtf1_loband1, mtf1_upband2, mtf1_loband2, mtf1_condition] = request.security(syminfo.tickerid, tf_1, get_mrc())
[mtf2_meanline, mtf2_meanrange, mtf2_upband1, mtf2_loband1, mtf2_upband2, mtf2_loband2, mtf2_condition] = request.security(syminfo.tickerid, tf_2, get_mrc())
// Drawing Start
float p_meanline = drawchannel ? meanline : na
float p_upband1 = drawchannel ? upband1 : na
float p_loband1 = drawchannel ? loband1 : na
float p_upband2 = drawchannel ? upband2 : na
float p_loband2 = drawchannel ? loband2 : na
// Plot Main Lines
plot(p_meanline, color=color.new(#FFCD00, 0), style=plot.style_line, title=" Mean", linewidth=2)
plot(p_upband1, color=color.new(color.green, 50), style=plot.style_circles, title=" R1", linewidth=1)
plot(p_loband1, color=color.new(color.green, 50), style=plot.style_circles, title=" S1", linewidth=1)
plot(p_upband2, color=color.new(color.red, 50), style=plot.style_line, title=" R2", linewidth=1)
plot(p_loband2, color=color.new(color.red, 50), style=plot.style_line, title=" S2", linewidth=1)
// Draw zone
var color1 = #FF0000, var color2 = #FF4200, var color3 = #FF5D00, var color4 = #FF7400, var color5 = #FF9700, var color6 = #FFAE00, var color7 = #FFC500, var color8 = #FFCD00
// Zone Calculation
upband2_1 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 4.0) : na
loband2_1 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 4.0) : na
upband2_2 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 3.0) : na
loband2_2 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 3.0) : na
upband2_3 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 2.0) : na
loband2_3 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 2.0) : na
upband2_4 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 1.0) : na
loband2_4 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 1.0) : na
upband2_5 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 0.0) : na
loband2_5 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 0.0) : na
upband2_6 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -1.0) : na
loband2_6 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -1.0) : na
upband2_7 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -2.0) : na
loband2_7 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -2.0) : na
upband2_8 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -3.0) : na
loband2_8 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -3.0) : na
upband2_9 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -4.0) : na
loband2_9 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -4.0) : na
// Plot hidden zones and fills
plot_upband2_1 = plot(upband2_1, color=na, transp=100, display=display.none), plot_loband2_1 = plot(loband2_1, color=na, transp=100, display=display.none)
plot_upband2_2 = plot(upband2_2, color=na, transp=100, display=display.none), plot_loband2_2 = plot(loband2_2, color=na, transp=100, display=display.none)
plot_upband2_3 = plot(upband2_3, color=na, transp=100, display=display.none), plot_loband2_3 = plot(loband2_3, color=na, transp=100, display=display.none)
plot_upband2_4 = plot(upband2_4, color=na, transp=100, display=display.none), plot_loband2_4 = plot(loband2_4, color=na, transp=100, display=display.none)
plot_upband2_5 = plot(upband2_5, color=na, transp=100, display=display.none), plot_loband2_5 = plot(loband2_5, color=na, transp=100, display=display.none)
plot_upband2_6 = plot(upband2_6, color=na, transp=100, display=display.none), plot_loband2_6 = plot(loband2_6, color=na, transp=100, display=display.none)
plot_upband2_7 = plot(upband2_7, color=na, transp=100, display=display.none), plot_loband2_7 = plot(loband2_7, color=na, transp=100, display=display.none)
plot_upband2_8 = plot(upband2_8, color=na, transp=100, display=display.none), plot_loband2_8 = plot(loband2_8, color=na, transp=100, display=display.none)
plot_upband2_9 = plot(upband2_9, color=na, transp=100, display=display.none), plot_loband2_9 = plot(loband2_9, color=na, transp=100, display=display.none)
// Fills for gradient zone
fill(plot_upband2_1, plot_upband2_2, color=color1, transp=zonetransp)
fill(plot_upband2_2, plot_upband2_3, color=color2, transp=zonetransp)
fill(plot_upband2_3, plot_upband2_4, color=color3, transp=zonetransp)
fill(plot_upband2_4, plot_upband2_5, color=color4, transp=zonetransp)
fill(plot_upband2_5, plot_upband2_6, color=color5, transp=zonetransp)
fill(plot_upband2_6, plot_upband2_7, color=color6, transp=zonetransp)
fill(plot_upband2_7, plot_upband2_8, color=color7, transp=zonetransp)
fill(plot_upband2_8, plot_upband2_9, color=color8, transp=zonetransp)
fill(plot_loband2_1, plot_loband2_2, color=color1, transp=zonetransp)
fill(plot_loband2_2, plot_loband2_3, color=color2, transp=zonetransp)
fill(plot_loband2_3, plot_loband2_4, color=color3, transp=zonetransp)
fill(plot_loband2_4, plot_loband2_5, color=color4, transp=zonetransp)
fill(plot_loband2_5, plot_loband2_6, color=color5, transp=zonetransp)
fill(plot_loband2_6, plot_loband2_7, color=color6, transp=zonetransp)
fill(plot_loband2_7, plot_loband2_8, color=color7, transp=zonetransp)
fill(plot_loband2_8, plot_loband2_9, color=color8, transp=zonetransp)
// Plot Extension Lines
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
// Prep MTF Label
var string brl = "\n--------------------------------------"
dist_0 = "Distance from Mean: "+ str.tostring(((close-meanline)/close)*100.0, "#.##") + " %"
dist_1 = "Distance from Mean: "+ str.tostring(((close-mtf1_meanline)/close)*100.0, "#.##") + " %"
dist_2 = "Distance from Mean: "+ str.tostring(((close-mtf2_meanline)/close)*100.0, "#.##") + " %"
// Manage title concatenation
var string title = "Mean Reversion Channel\nMultiple TimeFrame Analysis" + brl
tf0 = "\n\nTimeframe: "+ tf_0 + " (Current)\n\nStatus: " + get_stat(condition) + dist_0 + brl
tf1 = not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_1 + "\n\nStatus: " + get_stat(mtf1_condition) + dist_1 + brl : ""
tf2 = not timeframe.isweekly and not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_2 + "\n\nStatus: " + get_stat(mtf2_condition) + dist_2 + brl : ""
var string 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
调整点:
声明 tr(真实波动幅度) :使用 ta.tr(true) 计算真实波动范围。
使用 ta.change 函数 :将 change 函数替换为 ta.change 来计算时间的变化。
确保所有函数的变量和返回值在代码块之间正确定义和引用 。
这种调整应该会解决你在第 130 行和其他位置遇到的语法错误。如果还有其他不清楚的地方,请随时联系我!
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
transp
完整替换的代码示例 使用上面的方法替换所有相应的地方后,以下是完整代码示例:
pinescript Copy code
//@version=5
indicator("Mean Reversion Channel - MRI Variant", shorttitle="MRC", overlay=true, format=format.inherit)
// Parameters
indiSet = input(false, "═════════ MRC Parameter ════════")
source = input.source(hlc3, title="Price 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")
// Variables
var float pi = 2.0 * math.pi
var float gradsize = 0.5
// Calculate True Range
tr = ta.tr(true) // True Range
// Ehler's Swiss Army Knife Smoothing
SAK_smoothing(_type, _src, _length) =>
var float c0 = 1.0, c1 = 0.0, b0 = 1.0, b1 = 0.0, b2 = 0.0, a1 = 0.0, a2 = 0.0
var float alpha = 0.0, beta = 0.0, gamma = 0.0
var float cycle = 2.0 * pi / _length
float _Output = na
float _Input = _src
if _type == "Ehlers EMA"
alpha := (math.cos(cycle) + math.sin(cycle) - 1.0) / math.cos(cycle)
b0 := alpha
a1 := 1.0 - alpha
if _type == "Gaussian"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "Butterworth"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha / 4.0
b1 := 2.0
b2 := 1.0
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "BandStop"
beta := math.cos(cycle)
gamma := 1.0 / math.cos(cycle * 2.0 * 0.1)
alpha := gamma - math.sqrt((gamma * gamma) - 1.0)
c0 := (1.0 + alpha) / 2.0
b1 := -2.0 * beta
b2 := 1.0
a1 := beta * (1.0 + alpha)
a2 := -alpha
if _type == "SMA"
c1 := 1.0 / _length
b0 := 1.0 / _length
a1 := 1.0
if _type == "EMA"
alpha := 2.0 / (_length + 1.0)
b0 := alpha
a1 := 1.0 - alpha
if _type == "RMA"
alpha := 1.0 / _length
b0 := alpha
a1 := 1.0 - alpha
_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.0) * pi / _length)
s_b1 = 2.0 * s_a1 * math.cos(math.sqrt(2.0) * pi / _length)
s_c3 = -math.pow(s_a1, 2.0)
s_c2 = s_b1
s_c1 = 1.0 - s_c2 - s_c3
ss = 0.0
ss := s_c1 * _src + s_c2 * nz(ss[1], _src[1]) + s_c3 * nz(ss[2], _src[2])
// Auto TimeFrame Function
f_resInMinutes() =>
_resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1.0 / 60.0 :
timeframe.isminutes ? 1.0 :
timeframe.isdaily ? 60.0 * 24.0 :
timeframe.isweekly ? 60.0 * 24.0 * 7.0 :
timeframe.ismonthly ? 60.0 * 24.0 * 30.4375 : na)
get_tf(_lvl)=>
y = f_resInMinutes()
z = timeframe.period
if mtf_typ=="Auto"
if y < 1.0
z := _lvl == 1 ? "1" : _lvl == 2 ? "5" : z
else if y <= 3.0
z := _lvl == 1 ? "5" : _lvl == 2 ? "15" : z
else if y <= 10.0
z := _lvl == 1 ? "15" : _lvl == 2 ? "60" : z
else if y <= 30.0
z := _lvl == 1 ? "60" : _lvl == 2 ? "240" : z
else if y <= 120.0
z := _lvl == 1 ? "240" : _lvl == 2 ? "D" : z
else if y <= 240.0
z := _lvl == 1 ? "D" : _lvl == 2 ? "W" : z
else if y <= 1440.0
z := _lvl == 1 ? "W" : _lvl == 2 ? "M" : z
else if y <= 10080.0
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()=>
var float mult = pi * innermult
var float mult2 = pi * outermult
v_condition = 0
v_meanline = source
v_meanrange = supersmoother(tr, length)
// Get Line value
if type == "SuperSmoother"
v_meanline := supersmoother(source, length)
if type != "SuperSmoother"
v_meanline := SAK_smoothing(type, source, length)
v_upband1 = v_meanline + (v_meanrange * mult)
v_loband1 = v_meanline - (v_meanrange * mult)
v_upband2 = v_meanline + (v_meanrange * mult2)
v_loband2 = v_meanline - (v_meanrange * mult2)
// Check Condition
if close > v_meanline
v_upband2_1 = v_upband2 + (v_meanrange * gradsize * 4.0)
v_upband2_9 = v_upband2 + (v_meanrange * gradsize * -4.0)
if high >= v_upband2_9 and high < v_upband2
v_condition := 1
else if high >= v_upband2 and high < v_upband2_1
v_condition := 2
else if high >= v_upband2_1
v_condition := 3
else if close <= v_meanline + v_meanrange
v_condition := 4
else
v_condition := 5
if close < v_meanline
v_loband2_1 = v_loband2 - (v_meanrange * gradsize * 4.0)
v_loband2_9 = v_loband2 - (v_meanrange * gradsize * -4.0)
if low <= v_loband2_9 and low > v_loband2
v_condition := -1
else if low <= v_loband2 and low > v_loband2_1
v_condition := -2
else if low <= v_loband2_1
v_condition := -3
else if close >= v_meanline + v_meanrange
v_condition := -4
else
v_condition := -5
[v_meanline, v_meanrange, v_upband1, v_loband1, v_upband2, v_loband2, v_condition]
// MTF Analysis
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 = str.tostring(x, "0.00000")
if x > 10.0
y := str.tostring(x, "0.000")
if x > 1000.0
y := str.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 + math.round(ta.change(time) * 20.0),
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)
// Calculate Channel
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
// Main Channel Calculation
[meanline, meanrange, upband1, loband1, upband2, loband2, condition] = get_mrc()
[mtf1_meanline, mtf1_meanrange, mtf1_upband1, mtf1_loband1, mtf1_upband2, mtf1_loband2, mtf1_condition] = request.security(syminfo.tickerid, tf_1, get_mrc())
[mtf2_meanline, mtf2_meanrange, mtf2_upband1, mtf2_loband1, mtf2_upband2, mtf2_loband2, mtf2_condition] = request.security(syminfo.tickerid, tf_2, get_mrc())
// Drawing Start
float p_meanline = drawchannel ? meanline : na
float p_upband1 = drawchannel ? upband1 : na
float p_loband1 = drawchannel ? loband1 : na
float p_upband2 = drawchannel ? upband2 : na
float p_loband2 = drawchannel ? loband2 : na
// Plot Main Lines
plot(p_meanline, color=color.new(#FFCD00, 0), style=plot.style_line, title=" Mean", linewidth=2)
plot(p_upband1, color=color.new(color.green, 50), style=plot.style_circles, title=" R1", linewidth=1)
plot(p_loband1, color=color.new(color.green, 50), style=plot.style_circles, title=" S1", linewidth=1)
plot(p_upband2, color=color.new(color.red, 50), style=plot.style_line, title=" R2", linewidth=1)
plot(p_loband2, color=color.new(color.red, 50), style=plot.style_line, title=" S2", linewidth=1)
// Draw zone
var color1 = #FF0000, var color2 = #FF4200, var color3 = #FF5D00, var color4 = #FF7400, var color5 = #FF9700, var color6 = #FFAE00, var color7 = #FFC500, var color8 = #FFCD00
// Zone Calculation
upband2_1 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 4.0) : na
loband2_1 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 4.0) : na
upband2_2 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 3.0) : na
loband2_2 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 3.0) : na
upband2_3 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 2.0) : na
loband2_3 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 2.0) : na
upband2_4 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 1.0) : na
loband2_4 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 1.0) : na
upband2_5 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 0.0) : na
loband2_5 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 0.0) : na
upband2_6 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -1.0) : na
loband2_6 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -1.0) : na
upband2_7 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -2.0) : na
loband2_7 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -2.0) : na
upband2_8 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -3.0) : na
loband2_8 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -3.0) : na
upband2_9 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -4.0) : na
loband2_9 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -4.0) : na
// Plot hidden zones and fills
plot_upband2_1 = plot(upband2_1, color=na, display=display.none), plot_loband2_1 = plot(loband2_1, color=na, display=display.none)
plot_upband2_2 = plot(upband2_2, color=na, display=display.none), plot_loband2_2 = plot(loband2_2, color=na, display=display.none)
plot_upband2_3 = plot(upband2_3, color=na, display=display.none), plot_loband2_3 = plot(loband2_3, color=na, display=display.none)
plot_upband2_4 = plot(upband2_4, color=na, display=display.none), plot_loband2_4 = plot(loband2_4, color=na, display=display.none)
plot_upband2_5 = plot(upband2_5, color=na, display=display.none), plot_loband2_5 = plot(loband2_5, color=na, display=display.none)
plot_upband2_6 = plot(upband2_6, color=na, display=display.none), plot_loband2_6 = plot(loband2_6, color=na, display=display.none)
plot_upband2_7 = plot(upband2_7, color=na, display=display.none), plot_loband2_7 = plot(loband2_7, color=na, display=display.none)
plot_upband2_8 = plot(upband2_8, color=na, display=display.none), plot_loband2_8 = plot(loband2_8, color=na, display=display.none)
plot_upband2_9 = plot(upband2_9, color=na, display=display.none), plot_loband2_9 = plot(loband2_9, color=na, display=display.none)
// Fills for gradient zone
fill(plot_upband2_1, plot_upband2_2, color=color.new(color1, zonetransp))
fill(plot_upband2_2, plot_upband2_3, color=color.new(color2, zonetransp))
fill(plot_upband2_3, plot_upband2_4, color=color.new(color3, zonetransp))
fill(plot_upband2_4, plot_upband2_5, color=color.new(color4, zonetransp))
fill(plot_upband2_5, plot_upband2_6, color=color.new(color5, zonetransp))
fill(plot_upband2_6, plot_upband2_7, color=color.new(color6, zonetransp))
fill(plot_upband2_7, plot_upband2_8, color=color.new(color7, zonetransp))
fill(plot_upband2_8, plot_upband2_9, color=color.new(color8, zonetransp))
fill(plot_loband2_1, plot_loband2_2, color=color.new(color1, zonetransp))
fill(plot_loband2_2, plot_loband2_3, color=color.new(color2, zonetransp))
fill(plot_loband2_3, plot_loband2_4, color=color.new(color3, zonetransp))
fill(plot_loband2_4, plot_loband2_5, color=color.new(color4, zonetransp))
fill(plot_loband2_5, plot_loband2_6, color=color.new(color5, zonetransp))
fill(plot_loband2_6, plot_loband2_7, color=color.new(color6, zonetransp))
fill(plot_loband2_7, plot_loband2_8, color=color.new(color7, zonetransp))
fill(plot_loband2_8, plot_loband2_9, color=color.new(color8, zonetransp))
// Plot Extension Lines
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
// Prep MTF Label
var string brl = "\n--------------------------------------"
dist_0 = "Distance from Mean: "+ str.tostring(((close-meanline)/close)*100.0, "#.##") + " %"
dist_1 = "Distance from Mean: "+ str.tostring(((close-mtf1_meanline)/close)*100.0, "#.##") + " %"
dist_2 = "Distance from Mean: "+ str.tostring(((close-mtf2_meanline)/close)*100.0, "#.##") + " %"
// Manage title concatenation
var string title = "Mean Reversion Channel\nMultiple TimeFrame Analysis" + brl
tf0 = "\n\nTimeframe: "+ tf_0 + " (Current)\n\nStatus: " + get_stat(condition) + dist_0 + brl
tf1 = not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_1 + "\n\nStatus: " + get_stat(mtf1_condition) + dist_1 + brl : ""
tf2 = not timeframe.isweekly and not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_2 + "\n\nStatus: " + get_stat(mtf2_condition) + dist_2 + brl : ""
var string 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
请确保代码中的每个 fill 和 plot 调用使用 color.new() 函数来正确管理透明度。这将避免在代码编译时看到这些警告。如果还有其他问题,请随时告知。
ChartSet = input(false, "═════════ Chart Setting ════════")
drawchannel = input(true, title="Draw Channel")
displayzone = input(true, title="Draw Zone (With Channel)")
zonetransp = input(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("On Hover", title="MTF Display Type", options=["Always Display", "On Hover"])
mtf_typ = input("Auto", title="Multiple TimeFrame Type", options=["Auto", "Custom"])
mtf_lvl1 = input("D", title="Custom MTF Level 1", type=input.resolution)
mtf_lvl2 = input("W", title="Custom MTF Level 2", type=input.resolution)
//************************************************************************************************************
// Functions Start {
//************************************************************************************************************
var pi = 2 * asin(1)
var mult = pi * innermult
var mult2 = pi * outermult
var gradsize = 0.5
var gradtransp = zonetransp
//-----------------------
// Ehler SwissArmyKnife Function
//-----------------------
SAK_smoothing(_type, _src, _length) =>
c0 = 1.0
c1 = 0.0
b0 = 1.0
b1 = 0.0
b2 = 0.0
a1 = 0.0
a2 = 0.0
alpha = 0.0
beta = 0.0
gamma = 0.0
cycle = 2 * pi / _length
code Copy code
if _type == "Ehlers EMA"
alpha := (cos(cycle) + sin(cycle) - 1) / cos(cycle)
b0 := alpha
a1 := 1 - alpha
if _type == "Gaussian"
beta := 2.415 * (1 - cos(cycle))
alpha := -beta + sqrt((beta * beta) + (2 * beta))
c0 := alpha * alpha
a1 := 2 * (1 - alpha)
a2 := -(1 - alpha) * (1 - alpha)
if _type == "Butterworth"
beta := 2.415 * (1 - cos(cycle))
alpha := -beta + 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 := cos(cycle)
gamma := 1 / cos(cycle*2*0.1) // delta default to 0.1. Acceptable delta -- 0.05<d<0.5
alpha := gamma - 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
_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 = exp(-sqrt(2) * pi / _length)
s_b1 = 2 * s_a1 * cos(sqrt(2) * pi / _length)
s_c3 = -pow(s_a1, 2)
s_c2 = s_b1
s_c1 = 1 - s_c2 - s_c3
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
//-----------------------
// Mean Reversion Channel Function
//-----------------------
get_mrc()=>
v_condition = 0
v_meanline = source
v_meanrange = supersmoother(tr, length)
code Copy code
//-- Get Line value
if(type == "SuperSmoother")
v_meanline := supersmoother(source, length)
if(type != "SuperSmoother")
v_meanline := SAK_smoothing(type, source, length)
v_upband1 = v_meanline+(v_meanrange*mult)
v_loband1 = v_meanline-(v_meanrange*mult)
v_upband2 = v_meanline+(v_meanrange*mult2)
v_loband2 = v_meanline-(v_meanrange*mult2)
//-- Check Condition
if(close > v_meanline)
v_upband2_1 = v_upband2 + (v_meanrange * gradsize * 4)
v_upband2_9 = v_upband2 + (v_meanrange * gradsize * -4)
if(high >= v_upband2_9 and high < v_upband2)
v_condition := 1
else if(high >= v_upband2 and high < v_upband2_1)
v_condition := 2
else if(high >= v_upband2_1)
v_condition := 3
else if(close <= v_meanline+v_meanrange)
v_condition := 4
else
v_condition := 5
if(close < v_meanline)
v_loband2_1 = v_loband2 - (v_meanrange * gradsize * 4)
v_loband2_9 = v_loband2 - (v_meanrange * gradsize * -4)
if(low <= v_loband2_9 and low > v_loband2)
v_condition := -1
else if(low <= v_loband2 and low > v_loband2_1)
v_condition := -2
else if(low <= v_loband2_1)
v_condition := -3
else if(close >= v_meanline+v_meanrange)
v_condition := -4
else
v_condition := -5
[v_meanline, v_meanrange, v_upband1, v_loband1, v_upband2, v_loband2, v_condition]
//-----------------------
// MTF Analysis
//-----------------------
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
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)
//************************************************************************************************************
// Calculate Channel
//************************************************************************************************************
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
[meanline, meanrange, upband1, loband1, upband2, loband2, condition] = get_mrc()
[mtf1_meanline, mtf1_meanrange, mtf1_upband1, mtf1_loband1, mtf1_upband2, mtf1_loband2, mtf1_condition] = security(syminfo.tickerid, tf_1, get_mrc())
[mtf2_meanline, mtf2_meanrange, mtf2_upband1, mtf2_loband1, mtf2_upband2, mtf2_loband2, mtf2_condition] = security(syminfo.tickerid, tf_2, get_mrc())
//************************************************************************************************************
// Drawing Start {
//************************************************************************************************************
float p_meanline = drawchannel ? meanline : na
float p_upband1 = drawchannel ? upband1 : na
float p_loband1 = drawchannel ? loband1 : na
float p_upband2 = drawchannel ? upband2 : na
float p_loband2 = drawchannel ? loband2 : na
z = plot(p_meanline, color=#FFCD00, style=plot.style_line, title=" Mean", linewidth=2)
x1 = plot(p_upband1, color=color.green, style=plot.style_circles, title=" R1", linewidth=1, transp=50)
x2 = plot(p_loband1, color=color.green, style=plot.style_circles, title=" S1", linewidth=1, transp=50)
y1 = plot(p_upband2, color=color.red, style=plot.style_line, title=" R2", linewidth=1, transp=50)
y2 = plot(p_loband2, color=color.red, style=plot.style_line, title=" S2", linewidth=1, transp=50)
//-----------------------
// Draw zone
//-----------------------
//---
var color1 = #FF0000, var color2 = #FF4200, var color3 = #FF5D00, var color4 = #FF7400, var color5 = #FF9700, var color6 = #FFAE00, var color7 = #FFC500, var color8 = #FFCD00
//---
float upband2_1 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 4) : na, float loband2_1 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 4) : na
float upband2_2 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 3) : na, float loband2_2 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 3) : na
float upband2_3 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 2) : na, float loband2_3 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 2) : na
float upband2_4 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 1) : na, float loband2_4 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 1) : na
float upband2_5 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 0) : na, float loband2_5 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 0) : na
float upband2_6 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -1) : na, float loband2_6 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -1) : na
float upband2_7 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -2) : na, float loband2_7 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -2) : na
float upband2_8 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -3) : na, float loband2_8 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -3) : na
float upband2_9 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -4) : na, float loband2_9 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -4) : na
//---
plot_upband2_1 = plot(upband2_1, color=na, transp=100, display=display.none), plot_loband2_1 = plot(loband2_1, color=na, transp=100, display=display.none)
plot_upband2_2 = plot(upband2_2, color=na, transp=100, display=display.none), plot_loband2_2 = plot(loband2_2, color=na, transp=100, display=display.none)
plot_upband2_3 = plot(upband2_3, color=na, transp=100, display=display.none), plot_loband2_3 = plot(loband2_3, color=na, transp=100, display=display.none)
plot_upband2_4 = plot(upband2_4, color=na, transp=100, display=display.none), plot_loband2_4 = plot(loband2_4, color=na, transp=100, display=display.none)
plot_upband2_5 = plot(upband2_5, color=na, transp=100, display=display.none), plot_loband2_5 = plot(loband2_5, color=na, transp=100, display=display.none)
plot_upband2_6 = plot(upband2_6, color=na, transp=100, display=display.none), plot_loband2_6 = plot(loband2_6, color=na, transp=100, display=display.none)
plot_upband2_7 = plot(upband2_7, color=na, transp=100, display=display.none), plot_loband2_7 = plot(loband2_7, color=na, transp=100, display=display.none)
plot_upband2_8 = plot(upband2_8, color=na, transp=100, display=display.none), plot_loband2_8 = plot(loband2_8, color=na, transp=100, display=display.none)
plot_upband2_9 = plot(upband2_9, color=na, transp=100, display=display.none), plot_loband2_9 = plot(loband2_9, color=na, transp=100, display=display.none)
//---
fill(plot_upband2_1,plot_upband2_2, color=color1, transp=gradtransp), fill(plot_loband2_1,plot_loband2_2, color=color1, transp=gradtransp)
fill(plot_upband2_2,plot_upband2_3, color=color2, transp=gradtransp), fill(plot_loband2_2,plot_loband2_3, color=color2, transp=gradtransp)
fill(plot_upband2_3,plot_upband2_4, color=color3, transp=gradtransp), fill(plot_loband2_3,plot_loband2_4, color=color3, transp=gradtransp)
fill(plot_upband2_4,plot_upband2_5, color=color4, transp=gradtransp), fill(plot_loband2_4,plot_loband2_5, color=color4, transp=gradtransp)
fill(plot_upband2_5,plot_upband2_6, color=color5, transp=gradtransp), fill(plot_loband2_5,plot_loband2_6, color=color5, transp=gradtransp)
fill(plot_upband2_6,plot_upband2_7, color=color6, transp=gradtransp), fill(plot_loband2_6,plot_loband2_7, color=color6, transp=gradtransp)
fill(plot_upband2_7,plot_upband2_8, color=color7, transp=gradtransp), fill(plot_loband2_7,plot_loband2_8, color=color7, transp=gradtransp)
fill(plot_upband2_8,plot_upband2_9, color=color8, transp=gradtransp), fill(plot_loband2_8,plot_loband2_9, color=color8, transp=gradtransp)
//-----------------------
// 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
//--------------
// Prep MTF Label
//--------------
var brl = "\n--------------------------------------"
dist_0 = "Distance from Mean: "+ tostring(((close-meanline)/close)*100, "#.##") + " %"
dist_1 = "Distance from Mean: "+ tostring(((close-mtf1_meanline)/close)*100, "#.##") + " %"
dist_2 = "Distance from Mean: "+ tostring(((close-mtf2_meanline)/close)*100, "#.##") + " %"
var title = "Mean Reversion Channel\nMultiple TimeFrame Analysis" + brl
tf0 =
"\n\nTimeframe: "+ tf_0 + " (Current)\n\nStatus: " + get_stat(condition) + dist_0 + brl
tf1 = not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_1 + "\n\nStatus: " + get_stat(mtf1_condition) + dist_1 + brl : ""
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
//@version=5
indicator("Mean Reversion Channel - MRI Variant", shorttitle="MRC", overlay=true, format=format.inherit)
// Parameters
indiSet = input(false, "═════════ MRC Parameter ════════")
source = input.source(hlc3, "Price Source")
type = input.string("SuperSmoother", "Filter Type", options=["SuperSmoother", "Ehlers EMA", "Gaussian", "Butterworth", "BandStop", "SMA", "EMA", "RMA"])
length = input.int(200, "Lookback Period", minval=1)
innermult = input.float(1.0, "Inner Channel Size Multiplier", minval=0.1)
outermult = input.float(2.415, "Outer Channel Size Multiplier", minval=0.1)
ChartSet = input(false, "═════════ Chart Setting ════════")
drawchannel = input(true, "Draw Channel")
displayzone = input(true, "Draw Zone (With Channel)")
zonetransp = input.int(60, "Zone Transparency", minval=0, maxval=100)
displayline = input(true, "Display Line Extension")
MTFSet = input(false, "═════════ MTF Setting ════════")
enable_mtf = input(true, "Enable Multiple TimeFrame Analysis")
mtf_disp_typ= input.string("On Hover", "MTF Display Type", options=["Always Display", "On Hover"])
mtf_typ = input.string("Auto", "Multiple TimeFrame Type", options=["Auto", "Custom"])
mtf_lvl1 = input.timeframe("D", "Custom MTF Level 1")
mtf_lvl2 = input.timeframe("W", "Custom MTF Level 2")
// Variables
var float pi = 2.0 * math.pi
var float gradsize = 0.5
// Ehler's Swiss Army Knife Smoothing
SAK_smoothing(_type, _src, _length) =>
var float c0 = 1.0, c1 = 0.0, b0 = 1.0, b1 = 0.0, b2 = 0.0, a1 = 0.0, a2 = 0.0
var float alpha = 0.0, beta = 0.0, gamma = 0.0
var float cycle = 2.0 * pi / _length
float _Output = na
float _Input = _src
if _type == "Ehlers EMA"
alpha := (math.cos(cycle) + math.sin(cycle) - 1.0) / math.cos(cycle)
b0 := alpha
a1 := 1.0 - alpha
if _type == "Gaussian"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "Butterworth"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha / 4.0
b1 := 2.0
b2 := 1.0
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "BandStop"
beta := math.cos(cycle)
gamma := 1.0 / math.cos(cycle * 2.0 * 0.1)
alpha := gamma - math.sqrt((gamma * gamma) - 1.0)
c0 := (1.0 + alpha) / 2.0
b1 := -2.0 * beta
b2 := 1.0
a1 := beta * (1.0 + alpha)
a2 := -alpha
if _type == "SMA"
c1 := 1.0 / _length
b0 := 1.0 / _length
a1 := 1.0
if _type == "EMA"
alpha := 2.0 / (_length + 1.0)
b0 := alpha
a1 := 1.0 - alpha
if _type == "RMA"
alpha := 1.0 / _length
b0 := alpha
a1 := 1.0 - alpha
_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.0) * pi / _length)
s_b1 = 2.0 * s_a1 * math.cos(math.sqrt(2.0) * pi / _length)
s_c3 = -math.pow(s_a1, 2.0)
s_c2 = s_b1
s_c1 = 1.0 - s_c2 - s_c3
ss = 0.0
ss := s_c1 * _src + s_c2 * nz(ss[1], _src[1]) + s_c3 * nz(ss[2], _src[2])
// Auto TimeFrame Function
f_resInMinutes() =>
_resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1.0 / 60.0 :
timeframe.isminutes ? 1.0 :
timeframe.isdaily ? 60.0 * 24.0 :
timeframe.isweekly ? 60.0 * 24.0 * 7.0 :
timeframe.ismonthly ? 60.0 * 24.0 * 30.4375 : na)
get_tf(_lvl)=>
y = f_resInMinutes()
z = timeframe.period
if mtf_typ=="Auto"
if y < 1.0
z := _lvl == 1 ? "1" : _lvl == 2 ? "5" : z
else if y <= 3.0
z := _lvl == 1 ? "5" : _lvl == 2 ? "15" : z
else if y <= 10.0
z := _lvl == 1 ? "15" : _lvl == 2 ? "60" : z
else if y <= 30.0
z := _lvl == 1 ? "60" : _lvl == 2 ? "240" : z
else if y <= 120.0
z := _lvl == 1 ? "240" : _lvl == 2 ? "D" : z
else if y <= 240.0
z := _lvl == 1 ? "D" : _lvl == 2 ? "W" : z
else if y <= 1440.0
z := _lvl == 1 ? "W" : _lvl == 2 ? "M" : z
else if y <= 10080.0
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()=>
var float mult = pi * innermult
var float mult2 = pi * outermult
v_condition = 0
v_meanline = source
v_meanrange = supersmoother(tr, length)
// Get Line value
if type == "SuperSmoother"
v_meanline := supersmoother(source, length)
if type != "SuperSmoother"
v_meanline := SAK_smoothing(type, source, length)
v_upband1 = v_meanline + (v_meanrange * mult)
v_loband1 = v_meanline - (v_meanrange * mult)
v_upband2 = v_meanline + (v_meanrange * mult2)
v_loband2 = v_meanline - (v_meanrange * mult2)
// Check Condition
if close > v_meanline
v_upband2_1 = v_upband2 + (v_meanrange * gradsize * 4.0)
v_upband2_9 = v_upband2 + (v_meanrange * gradsize * -4.0)
if high >= v_upband2_9 and high < v_upband2
v_condition := 1
else if high >= v_upband2 and high < v_upband2_1
v_condition := 2
else if high >= v_upband2_1
v_condition := 3
else if close <= v_meanline + v_meanrange
v_condition := 4
else
v_condition := 5
if close < v_meanline
v_loband2_1 = v_loband2 - (v_meanrange * gradsize * 4.0)
v_loband2_9 = v_loband2 - (v_meanrange * gradsize * -4.0)
if low <= v_loband2_9 and low > v_loband2
v_condition := -1
else if low <= v_loband2 and low > v_loband2_1
v_condition := -2
else if low <= v_loband2_1
v_condition := -3
else if close >= v_meanline + v_meanrange
v_condition := -4
else
v_condition := -5
[v_meanline, v_meanrange, v_upband1, v_loband1, v_upband2, v_loband2, v_condition]
// MTF Analysis
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 = str.tostring(x, "0.00000")
if x > 10.0
y := str.tostring(x, "0.000")
if x > 1000.0
y := str.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 + math.round(ta.change(time) * 20.0),
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)
// Calculate Channel
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
// Main Channel Calculation
[meanline, meanrange, upband1, loband1, upband2, loband2, condition] = get_mrc()
[mtf1_meanline, mtf1_meanrange, mtf1_upband1, mtf1_loband1, mtf1_upband2, mtf1_loband2, mtf1_condition] = request.security(syminfo.tickerid, tf_1, get_mrc())
[mtf2_meanline, mtf2_meanrange, mtf2_upband1, mtf2_loband1, mtf2_upband2, mtf2_loband2, mtf2_condition] = request.security(syminfo.tickerid, tf_2, get_mrc())
// Drawing Start
float p_meanline = drawchannel ? meanline : na
float p_upband1 = drawchannel ? upband1 : na
float p_loband1 = drawchannel ? loband1 : na
float p_upband2 = drawchannel ? upband2 : na
float p_loband2 = drawchannel ? loband2 : na
// Plot Main Lines
plot(p_meanline, color=color.new(#FFCD00, 0), style=plot.style_line, title=" Mean", linewidth=2)
plot(p_upband1, color=color.new(color.green, 50), style=plot.style_circles, title=" R1", linewidth=1)
plot(p_loband1, color=color.new(color.green, 50), style=plot.style_circles, title=" S1", linewidth=1)
plot(p_upband2, color=color.new(color.red, 50), style=plot.style_line, title=" R2", linewidth=1)
plot(p_loband2, color=color.new(color.red, 50), style=plot.style_line, title=" S2", linewidth=1)
// Draw Zone
var color1 = color.new(#FF0000, zonetransp),
color2 = color.new(#FF4200, zonetransp),
color3 = color.new(#FF5D00, zonetransp),
color4 = color.new(#FF7400, zonetransp),
color5 = color.new(#FF9700, zonetransp),
color6 = color.new(#FFAE00, zonetransp),
color7 = color.new(#FFC500, zonetransp),
color8 = color.new(#FFCD00, zonetransp)
// Zone Calculation
upband2_1 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 4.0) : na
loband2_1 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 4.0) : na
upband2_2 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 3.0) : na
loband2_2 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 3.0) : na
upband2_3 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 2.0) : na
loband2_3 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 2.0) : na
upband2_4 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 1.0) : na
loband2_4 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 1.0) : na
upband2_5 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 0.0) : na
loband2_5 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 0.0) : na
upband2_6 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -1.0) : na
loband2_6 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -1.0) : na
upband2_7 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -2.0) : na
loband2_7 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -2.0) : na
upband2_8 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -3.0) : na
loband2_8 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -3.0) : na
upband2_9 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -4.0) : na
loband2_9 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -4.0) : na
// Plot hidden zones and fills
plot_upband2_1 = plot(upband2_1, color=na, display=display.none), plot_loband2_1 = plot(loband2_1, color=na, display=display.none)
plot_upband2_2 = plot(upband2_2, color=na, display=display.none), plot_loband2_2 = plot(loband2_2, color=na, display=display.none)
plot_upband2_3 = plot(upband2_3, color=na, display=display.none), plot_loband2_3 = plot(loband2_3, color=na, display=display.none)
plot_upband2_4 = plot(upband2_4, color=na, display=display.none), plot_loband2_4 = plot(loband2_4, color=na, display=display.none)
plot_upband2_5 = plot(upband2_5, color=na, display=display.none), plot_loband2_5 = plot(loband2_5, color=na, display=display.none)
plot_upband2_6 = plot(upband2_6, color=na, display=display.none), plot_loband2_6 = plot(loband2_6, color=na, display=display.none)
plot_upband2_7 = plot(upband2_7, color=na, display=display.none), plot_loband2_7 = plot(loband2_7, color=na, display=display.none)
plot_upband2_8 = plot(upband2_8, color=na, display=display.none), plot_loband2_8 = plot(loband2_8, color=na, display=display.none)
plot_upband2_9 = plot(upband2_9, color=na, display=display.none), plot_loband2_9 = plot(loband2_9, color=na, display=display.none)
// Fills for gradient zone
fill(plot_upband2_1, plot_upband2_2, color=color1)
fill(plot_upband2_2, plot_upband2_3, color=color2)
fill(plot_upband2_3, plot_upband2_4, color=color3)
fill(plot_upband2_4, plot_upband2_5, color=color4)
fill(plot_upband2_5, plot_upband2_6, color=color5)
fill(plot_upband2_6, plot_upband2_7, color=color6)
fill(plot_upband2_7, plot_upband2_8, color=color7)
fill(plot_upband2_8, plot_upband2_9, color=color8)
fill(plot_loband2_1, plot_loband2_2, color=color1)
fill(plot_loband2_2, plot_loband2_3, color=color2)
fill(plot_loband2_3, plot_loband2_4, color=color3)
fill(plot_loband2_4, plot_loband2_5, color=color4)
fill(plot_loband2_5, plot_loband2_6, color=color5)
fill(plot_loband2_6, plot_loband2_7, color=color6)
fill(plot_loband2_7, plot_loband2_8, color=color7)
fill(plot_loband2_8, plot_loband2_9, color=color8)
// Plot Extension Lines
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
// Prep MTF Label
var string brl = "\n--------------------------------------"
dist_0 = "Distance from Mean: "+ str.tostring(((close-meanline)/close)*100.0, "#.##") + " %"
dist_1 = "Distance from Mean: "+ str.tostring(((close-mtf1_meanline)/close)*100.0, "#.##") + " %"
dist_2 = "Distance from Mean: "+ str.tostring(((close-mtf2_meanline)/close)*100.0, "#.##") + " %"
// Manage title concatenation
var string title = "Mean Reversion Channel\nMultiple TimeFrame Analysis" + brl
tf0 = "\n\nTimeframe: "+ tf_0 + " (Current)\n\nStatus: " + get_stat(condition) + dist_0 + brl
tf1 = not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_1 + "\n\nStatus: " + get_stat(mtf1_condition) + dist_1 + brl : ""
tf2 = not timeframe.isweekly and not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_2 + "\n\nStatus: " + get_stat(mtf2_condition) + dist_2 + brl : ""
var string 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
调整要点:
透明度 :使用 color.new() 方法替代 transp 参数来设置透明度。
函数更改 :将已废弃的 security 函数替换为 request.security。
变量和输入类型 :所有 input 和 var 均符合 v5 版本要求。
检查逻辑 :确保重要计算步骤未变更,最终展示效果应保持一致。
在完成这些调整后,输出图表应更符合预期效果,并且没有版本兼容性问题。如果还是不一致,可以调整 zonetransp 透明度值来更趋近之前的效果。
//@version=5
indicator("Mean Reversion Channel - MRI Variant", shorttitle="MRC", overlay=true, format=format.inherit)
// Parameters
indiSet = input(false, "═════════ MRC Parameter ════════")
source = input.source(hlc3, "Price Source")
type = input.string("SuperSmoother", "Filter Type", options=["SuperSmoother", "Ehlers EMA", "Gaussian", "Butterworth", "BandStop", "SMA", "EMA", "RMA"])
length = input.int(200, "Lookback Period", minval=1)
innermult = input.float(1.0, "Inner Channel Size Multiplier", minval=0.1)
outermult = input.float(2.415, "Outer Channel Size Multiplier", minval=0.1)
ChartSet = input(false, "═════════ Chart Setting ════════")
drawchannel = input(true, "Draw Channel")
displayzone = input(true, "Draw Zone (With Channel)")
zonetransp = input.int(60, "Zone Transparency", minval=0, maxval=100)
displayline = input(true, "Display Line Extension")
MTFSet = input(false, "═════════ MTF Setting ════════")
enable_mtf = input(true, "Enable Multiple TimeFrame Analysis")
mtf_disp_typ= input.string("On Hover", "MTF Display Type", options=["Always Display", "On Hover"])
mtf_typ = input.string("Auto", "Multiple TimeFrame Type", options=["Auto", "Custom"])
mtf_lvl1 = input.timeframe("D", "Custom MTF Level 1")
mtf_lvl2 = input.timeframe("W", "Custom MTF Level 2")
// Variables
var float pi = 2.0 * math.pi
var float gradsize = 0.5
// True Range Calculation
tr = ta.tr
// Functions Start
SAK_smoothing(_type, _src, _length) =>
var float c0 = 1.0, c1 = 0.0, b0 = 1.0, b1 = 0.0, b2 = 0.0, a1 = 0.0, a2 = 0.0
var float alpha = 0.0, beta = 0.0, gamma = 0.0
var float cycle = 2.0 * pi / _length
float _Output = na
float _Input = _src
if _type == "Ehlers EMA"
alpha := (math.cos(cycle) + math.sin(cycle) - 1.0) / math.cos(cycle)
b0 := alpha
a1 := 1.0 - alpha
if _type == "Gaussian"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "Butterworth"
beta := 2.415 * (1.0 - math.cos(cycle))
alpha := -beta + math.sqrt((beta * beta) + (2.0 * beta))
c0 := alpha * alpha / 4.0
b1 := 2.0
b2 := 1.0
a1 := 2.0 * (1.0 - alpha)
a2 := -(1.0 - alpha) * (1.0 - alpha)
if _type == "BandStop"
beta := math.cos(cycle)
gamma := 1.0 / math.cos(cycle * 2.0 * 0.1)
alpha := gamma - math.sqrt((gamma * gamma) - 1.0)
c0 := (1.0 + alpha) / 2.0
b1 := -2.0 * beta
b2 := 1.0
a1 := beta * (1.0 + alpha)
a2 := -alpha
if _type == "SMA"
c1 := 1.0 / _length
b0 := 1.0 / _length
a1 := 1.0
if _type == "EMA"
alpha := 2.0 / (_length + 1.0)
b0 := alpha
a1 := 1.0 - alpha
if _type == "RMA"
alpha := 1.0 / _length
b0 := alpha
a1 := 1.0 - alpha
_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(_src, _length) =>
s_a1 = math.exp(-math.sqrt(2.0) * pi / _length)
s_b1 = 2.0 * s_a1 * math.cos(math.sqrt(2.0) * pi / _length)
s_c3 = -math.pow(s_a1, 2.0)
s_c2 = s_b1
s_c1 = 1.0 - s_c2 - s_c3
ss = 0.0
ss := s_c1 * _src + s_c2 * nz(ss[1], _src[1]) + s_c3 * nz(ss[2], _src[2])
f_resInMinutes() =>
_resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1.0 / 60.0 :
timeframe.isminutes ? 1.0 :
timeframe.isdaily ? 60.0 * 24.0 :
timeframe.isweekly ? 60.0 * 24.0 * 7.0 :
timeframe.ismonthly ? 60.0 * 24.0 * 30.4375 : na)
get_tf(_lvl)=>
y = f_resInMinutes()
z = timeframe.period
if mtf_typ=="Auto"
if y < 1.0
z := _lvl == 1 ? "1" : _lvl == 2 ? "5" : z
else if y <= 3.0
z := _lvl == 1 ? "5" : _lvl == 2 ? "15" : z
else if y <= 10.0
z := _lvl == 1 ? "15" : _lvl == 2 ? "60" : z
else if y <= 30.0
z := _lvl == 1 ? "60" : _lvl == 2 ? "240" : z
else if y <= 120.0
z := _lvl == 1 ? "240" : _lvl == 2 ? "D" : z
else if y <= 240.0
z := _lvl == 1 ? "D" : _lvl == 2 ? "W" : z
else if y <= 1440.0
z := _lvl == 1 ? "W" : _lvl == 2 ? "M" : z
else if y <= 10080.0
z := _lvl == 1 ? "M" : z
else
z := z
else
z := _lvl == 1 ? mtf_lvl1 : _lvl == 2 ? mtf_lvl2 : z
z
get_mrc() =>
var float mult = pi * innermult
var float mult2 = pi * outermult
v_condition = 0
v_meanline = source
v_meanrange = supersmoother(tr, length)
if type == "SuperSmoother"
v_meanline := supersmoother(source, length)
else
v_meanline := SAK_smoothing(type, source, length)
v_upband1 = v_meanline + (v_meanrange * mult)
v_loband1 = v_meanline - (v_meanrange * mult)
v_upband2 = v_meanline + (v_meanrange * mult2)
v_loband2 = v_meanline - (v_meanrange * mult2)
if close > v_meanline
v_upband2_1 = v_upband2 + (v_meanrange * gradsize * 4.0)
v_upband2_9 = v_upband2 + (v_meanrange * gradsize * -4.0)
if high >= v_upband2_9 and high < v_upband2
v_condition := 1
else if high >= v_upband2 and high < v_upband2_1
v_condition := 2
else if high >= v_upband2_1
v_condition := 3
else if close <= v_meanline + v_meanrange
v_condition := 4
else
v_condition := 5
if close < v_meanline
v_loband2_1 = v_loband2 - (v_meanrange * gradsize * 4.0)
v_loband2_9 = v_loband2 - (v_meanrange * gradsize * -4.0)
if low <= v_loband2_9 and low > v_loband2
v_condition := -1
else if low <= v_loband2 and low > v_loband2_1
v_condition := -2
else if low <= v_loband2_1
v_condition := -3
else if close >= v_meanline + v_meanrange
v_condition := -4
else
v_condition := -5
[v_meanline, v_meanrange, v_upband1, v_loband1, v_upband2, v_loband2, v_condition]
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
format_price(x) =>
y = str.tostring(x, "0.00000")
if x > 10.0
y := str.tostring(x, "0.000")
if x > 1000.0
y := str.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 + math.round(ta.change(time) * 20.0),
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)
// Calculate Channel
var tf_0 = timeframe.period
var tf_1 = get_tf(1)
var tf_2 = get_tf(2)
// Main Channel Calculation
[meanline, meanrange, upband1, loband1, upband2, loband2, condition] = get_mrc()
[mtf1_meanline, mtf1_meanrange, mtf1_upband1, mtf1_loband1, mtf1_upband2, mtf1_loband2, mtf1_condition] = request.security(syminfo.tickerid, tf_1, get_mrc())
[mtf2_meanline, mtf2_meanrange, mtf2_upband1, mtf2_loband1, mtf2_upband2, mtf2_loband2, mtf2_condition] = request.security(syminfo.tickerid, tf_2, get_mrc())
// Plot Main Lines
plot(drawchannel ? meanline : na, color=color.new(#FFCD00, 0), style=plot.style_line, title="Mean", linewidth=2)
plot(drawchannel ? upband1 : na, color=color.new(color.green, 50), style=plot.style_circles, title="R1", linewidth=1)
plot(drawchannel ? loband1 : na, color=color.new(color.green, 50), style=plot.style_circles, title="S1", linewidth=1)
plot(drawchannel ? upband2 : na, color=color.new(color.red, 50), style=plot.style_line, title="R2", linewidth=1)
plot(drawchannel ? loband2 : na, color=color.new(color.red, 50), style=plot.style_line, title="S2", linewidth=1)
// Draw Zone
var color1 = color.new(#FF0000, zonetransp)
var color2 = color.new(#FF4200, zonetransp)
var color3 = color.new(#FF5D00, zonetransp)
var color4 = color.new(#FF7400, zonetransp)
var color5 = color.new(#FF9700, zonetransp)
var color6 = color.new(#FFAE00, zonetransp)
var color7 = color.new(#FFC500, zonetransp)
var color8 = color.new(#FFCD00, zonetransp)
upband2_1 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 4.0) : na
loband2_1 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 4.0) : na
upband2_2 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 3.0) : na
loband2_2 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 3.0) : na
upband2_3 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 2.0) : na
loband2_3 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 2.0) : na
upband2_4 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 1.0) : na
loband2_4 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 1.0) : na
upband2_5 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * 0.0) : na
loband2_5 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * 0.0) : na
upband2_6 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -1.0) : na
loband2_6 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -1.0) : na
upband2_7 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -2.0) : na
loband2_7 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -2.0) : na
upband2_8 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -3.0) : na
loband2_8 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -3.0) : na
upband2_9 = drawchannel and displayzone ? upband2 + (meanrange * gradsize * -4.0) : na
loband2_9 = drawchannel and displayzone ? loband2 - (meanrange * gradsize * -4.0) : na
plot_upband2_1 = plot(upband2_1, color=na, display=display.none)
plot_loband2_1 = plot(loband2_1, color=na, display=display.none)
plot_upband2_2 = plot(upband2_2, color=na, display=display.none)
plot_loband2_2 = plot(loband2_2, color=na, display=display.none)
plot_upband2_3 = plot(upband2_3, color=na, display=display.none)
plot_loband2_3 = plot(loband2_3, color=na, display=display.none)
plot_upband2_4 = plot(upband2_4, color=na, display=display.none)
plot_loband2_4 = plot(loband2_4, color=na, display=display.none)
plot_upband2_5 = plot(upband2_5, color=na, display=display.none)
plot_loband2_5 = plot(loband2_5, color=na, display=display.none)
plot_upband2_6 = plot(upband2_6, color=na, display=display.none)
plot_loband2_6 = plot(loband2_6, color=na, display=display.none)
plot_upband2_7 = plot(upband2_7, color=na, display=display.none)
plot_loband2_7 = plot(loband2_7, color=na, display=display.none)
plot_upband2_8 = plot(upband2_8, color=na, display=display.none)
plot_loband2_8 = plot(loband2_8, color=na, display=display.none)
plot_upband2_9 = plot(upband2_9, color=na, display=display.none)
plot_loband2_9 = plot(loband2_9, color=na, display=display.none)
fill(plot_upband2_1, plot_upband2_2, color=color1)
fill(plot_upband2_2, plot_upband2_3, color=color2)
fill(plot_upband2_3, plot_upband2_4, color=color3)
fill(plot_upband2_4, plot_upband2_5, color=color4)
fill(plot_upband2_5, plot_upband2_6, color=color5)
fill(plot_upband2_6, plot_upband2_7, color=color6)
fill(plot_upband2_7, plot_upband2_8, color=color7)
fill(plot_upband2_8, plot_upband2_9, color=color8)
fill(plot_loband2_1, plot_loband2_2, color=color1)
fill(plot_loband2_2, plot_loband2_3, color=color2)
fill(plot_loband2_3, plot_loband2_4, color=color3)
fill(plot_loband2_4, plot_loband2_5, color=color4)
fill(plot_loband2_5, plot_loband2_6, color=color5)
fill(plot_loband2_6, plot_loband2_7, color=color6)
fill(plot_loband2_7, plot_loband2_8, color=color7)
fill(plot_loband2_8, plot_loband2_9, color=color8)
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
var string brl = "\n--------------------------------------"
dist_0 = "Distance from Mean: "+ str.tostring(((close-meanline)/close)*100.0, "#.##") + " %"
dist_1 = "Distance from Mean: "+ str.tostring(((close-mtf1_meanline)/close)*100.0, "#.##") + " %"
dist_2 = "Distance from Mean: "+ str.tostring(((close-mtf2_meanline)/close)*100.0, "#.##") + " %"
var string title = "Mean Reversion Channel\nMultiple TimeFrame Analysis" + brl
tf0 = "\n\nTimeframe: "+ tf_0 + " (Current)\n\nStatus: " + get_stat(condition) + dist_0 + brl
tf1 = not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_1 + "\n\nStatus: " + get_stat(mtf1_condition) + dist_1 + brl : ""
tf2 = not timeframe.isweekly and not timeframe.ismonthly ?
"\n\nTimeframe: "+ tf_2 + "\n\nStatus: " + get_stat(mtf2_condition) + dist_2 + brl : ""
var string 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
调整点:
定义tr :使用ta.tr来定义真实波动幅度。
透明度设置 :使用color.new()方法替代transp参数设置透明度。
增强兼容性 :将所有函数和逻辑调整为 Pine Script v5 格式。
结构和逻辑 :保持与原有指标的逻辑一致,包括条件和绘图区域。
此代码应解决您在转换到 Pine Script v5 时遇到的所有语法和兼容性问题。如果仍有问题或需要进一步的调整,请与我联系!