MTF EMA Ribbon & Bands + BB
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5

indicator("EMA • Yata", 
     overlay = true)

// ------------------------------
groupMA = "Moving Average Ribbon"
// ------------------------------

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
		"LSMA" => ta.linreg(source, length, offset=0)

// ------------------------------

maType		= input.string("EMA", title="Moving Average Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "LSMA"], inline="MA01", group=groupMA)
trend_flip	= input.int(defval=2, minval=0, title="Flip Trend Periods", inline="MA02", group=groupMA)
src			= input(defval=close, title="Source", inline="MA02", group=groupMA)

visible1 = input.bool(true	, title="MA 1:", inline="MA1", group=groupMA)
visible2 = input.bool(true	, title="MA 2:", inline="MA2", group=groupMA)
visible3 = input.bool(true	, title="MA 3:", inline="MA3", group=groupMA)
visible4 = input.bool(true	, title="MA 4:", inline="MA4", group=groupMA)
visible5 = input.bool(true	, title="MA 5:", inline="MA5", group=groupMA)
visible6 = input.bool(true	, title="MA 6:", inline="MA6", group=groupMA)
visible7 = input.bool(false	, title="MA 7:", inline="MA7", group=groupMA)
visible8 = input.bool(false	, title="MA 8:", inline="MA8", group=groupMA)
visible9 = input.bool(false	, title="MA 9:", inline="MA9", group=groupMA)

len1 = input.int(9		, minval=1, title="length", inline="MA1", group=groupMA)
len2 = input.int(21		, minval=1, title="length", inline="MA2", group=groupMA)
len3 = input.int(34		, minval=1, title="length", inline="MA3", group=groupMA)
len4 = input.int(55		, minval=1, title="length", inline="MA4", group=groupMA)
len5 = input.int(100	, minval=1, title="length", inline="MA5", group=groupMA)
len6 = input.int(200	, minval=1, title="length", inline="MA6", group=groupMA)
len7 = input.int(400	, minval=1, title="length", inline="MA7", group=groupMA)
len8 = input.int(800	, minval=1, title="length", inline="MA8", group=groupMA)
len9 = input.int(1600	, minval=1, title="length", inline="MA9", group=groupMA)

up_color1 = input.color(defval=color.new(#E12D7B, 25), title="up", inline="MA1", group=groupMA)
up_color2 = input.color(defval=color.new(#F67B52, 25), title="up", inline="MA2", group=groupMA)
up_color3 = input.color(defval=color.new(#EDCD3B, 25), title="up", inline="MA3", group=groupMA)
up_color4 = input.color(defval=color.new(#3BBC54, 25), title="up", inline="MA4", group=groupMA)
up_color5 = input.color(defval=color.new(#2665BD, 25), title="up", inline="MA5", group=groupMA)
up_color6 = input.color(defval=color.new(#481899, 25), title="up", inline="MA6", group=groupMA)
up_color7 = input.color(defval=color.new(#787b86, 75), title="up", inline="MA7", group=groupMA)
up_color8 = input.color(defval=color.new(#787b86, 75), title="up", inline="MA8", group=groupMA)
up_color9 = input.color(defval=color.new(#787b86, 75), title="up", inline="MA9", group=groupMA) // https://www.schemecolor.com/new-year-rainbow.php

down_color1 = input.color(defval=color.new(#F19A9C, 50), title="down", inline="MA1", group=groupMA)
down_color2 = input.color(defval=color.new(#FFC29F, 50), title="down", inline="MA2", group=groupMA)
down_color3 = input.color(defval=color.new(#FFFAAE, 50), title="down", inline="MA3", group=groupMA)
down_color4 = input.color(defval=color.new(#CDECAD, 50), title="down", inline="MA4", group=groupMA)
down_color5 = input.color(defval=color.new(#A0CDED, 50), title="down", inline="MA5", group=groupMA)
down_color6 = input.color(defval=color.new(#AF8FC1, 50), title="down", inline="MA6", group=groupMA)
down_color7 = input.color(defval=color.new(#787b86, 75), title="down", inline="MA7", group=groupMA)
down_color8 = input.color(defval=color.new(#787b86, 75), title="down", inline="MA8", group=groupMA)
down_color9 = input.color(defval=color.new(#787b86, 75), title="down", inline="MA9", group=groupMA) // https://www.schemecolor.com/thinnest-rainbow.php

// ------------------------------

ma1 = ma(src, len1, maType)
ma2 = ma(src, len2, maType)
ma3 = ma(src, len3, maType)
ma4 = ma(src, len4, maType)
ma5 = ma(src, len5, maType)
ma6 = ma(src, len6, maType)
ma7 = ma(src, len7, maType)
ma8 = ma(src, len8, maType)
ma9 = ma(src, len9, maType)

plot_color1 = visible1 ? ma1 >= ma1[trend_flip] ? up_color1 : down_color1 : na
plot_color2 = visible2 ? ma2 >= ma2[trend_flip] ? up_color2 : down_color2 : na
plot_color3 = visible3 ? ma3 >= ma3[trend_flip] ? up_color3 : down_color3 : na
plot_color4 = visible4 ? ma4 >= ma4[trend_flip] ? up_color4 : down_color4 : na
plot_color5 = visible5 ? ma5 >= ma5[trend_flip] ? up_color5 : down_color5 : na
plot_color6 = visible6 ? ma6 >= ma6[trend_flip] ? up_color6 : down_color6 : na
plot_color7 = visible7 ? ma7 >= ma7[trend_flip] ? up_color7 : down_color7 : na
plot_color8 = visible8 ? ma8 >= ma8[trend_flip] ? up_color8 : down_color8 : na
plot_color9 = visible9 ? ma9 >= ma9[trend_flip] ? up_color9 : down_color9 : na

map1 = plot(ma1, title="MA 1", style=plot.style_line, color=plot_color1, linewidth=1)
map2 = plot(ma2, title="MA 2", style=plot.style_line, color=plot_color2, linewidth=1)
map3 = plot(ma3, title="MA 3", style=plot.style_line, color=plot_color3, linewidth=1)
map4 = plot(ma4, title="MA 4", style=plot.style_line, color=plot_color4, linewidth=1)
map5 = plot(ma5, title="MA 5", style=plot.style_line, color=plot_color5, linewidth=1)
map6 = plot(ma6, title="MA 6", style=plot.style_line, color=plot_color6, linewidth=1)
map7 = plot(ma7, title="MA 7", style=plot.style_line, color=plot_color7, linewidth=1)
map8 = plot(ma8, title="MA 8", style=plot.style_line, color=plot_color8, linewidth=1)
map9 = plot(ma9, title="MA 9", style=plot.style_line, color=plot_color9, linewidth=1)

// ------------------------------

visiblefill = input.bool(false, title="Gradient Fill", inline="MA01", group=groupMA)
price		= plot(close, title="Price Line", color=color.silver, display=display.none)

ma1color = (ma1 > ma1[1] ? color.new(#00FEEF, 95) : color.new(#E21B22, 95))
ma2color = (ma2 > ma2[1] ? color.new(#00FEEF, 95) : color.new(#E21B22, 95))
ma3color = (ma3 > ma3[1] ? color.new(#09EBEE, 95) : color.new(#F71746, 95))
ma4color = (ma4 > ma4[1] ? color.new(#09EBEE, 95) : color.new(#F71746, 95))
ma5color = (ma5 > ma5[1] ? color.new(#28ACEA, 95) : color.new(#F23D92, 95))
ma6color = (ma6 > ma6[1] ? color.new(#28ACEA, 95) : color.new(#F23D92, 95))
ma7color = (ma7 > ma7[1] ? color.new(#28ACEA, 95) : color.new(#CA3AB0, 95)) // https://www.schemecolor.com/light-green-to-blue-gradient.php
ma8color = (ma8 > ma8[1] ? color.new(#388EE9, 95) : color.new(#9138A7, 95)) // https://www.schemecolor.com/turquoise-to-blue-gradient.php
ma9color = (ma9 > ma9[1] ? color.new(#3D76E0, 95) : color.new(#69208E, 95)) // https://www.schemecolor.com/purple-red-gradient.php

fill(price, map1, visible1 and visiblefill ? ma1color : na, title="MA Fill 1")
fill(price, map2, visible2 and visiblefill ? ma2color : na, title="MA Fill 2")
fill(price, map3, visible3 and visiblefill ? ma3color : na, title="MA Fill 3")
fill(price, map4, visible4 and visiblefill ? ma4color : na, title="MA Fill 4")
fill(price, map5, visible5 and visiblefill ? ma5color : na, title="MA Fill 5")
fill(price, map6, visible6 and visiblefill ? ma6color : na, title="MA Fill 6")
fill(price, map7, visible7 and visiblefill ? ma7color : na, title="MA Fill 7")
fill(price, map8, visible8 and visiblefill ? ma8color : na, title="MA Fill 8")
fill(price, map9, visible9 and visiblefill ? ma9color : na, title="MA Fill 9")

// ----------------------------------------
groupMTF = "Moving Average Multi Timeframe"
// ----------------------------------------

maTypeMTF = input.string("EMA", title="Moving Average Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "LSMA"], inline="MTF0", group=groupMTF)

visibleMTF1 = input.bool(false, title="MA 1:", inline="MTF1", group=groupMTF)
visibleMTF2 = input.bool(false, title="MA 2:", inline="MTF2", group=groupMTF)
visibleMTF3 = input.bool(false, title="MA 3:", inline="MTF3", group=groupMTF)
visibleMTF4 = input.bool(false, title="MA 4:", inline="MTF4", group=groupMTF)
visibleMTF5 = input.bool(false, title="MA 5:", inline="MTF5", group=groupMTF)
visibleMTF6 = input.bool(false, title="MA 6:", inline="MTF6", group=groupMTF)

lenMTF1 = input.int(9	, title="length", inline="MTF1", group=groupMTF)
lenMTF2 = input.int(21	, title="length", inline="MTF2", group=groupMTF)
lenMTF3 = input.int(34	, title="length", inline="MTF3", group=groupMTF)
lenMTF4 = input.int(55	, title="length", inline="MTF4", group=groupMTF)
lenMTF5 = input.int(100	, title="length", inline="MTF5", group=groupMTF)
lenMTF6 = input.int(200	, title="length", inline="MTF6", group=groupMTF)

srcMTF = input(defval=close, title="Source", inline="MTF2", group=groupMTF)

// ----------------------------------------

maMTF1 = ma(srcMTF, lenMTF1, maTypeMTF)
maMTF2 = ma(srcMTF, lenMTF2, maTypeMTF)
maMTF3 = ma(srcMTF, lenMTF3, maTypeMTF)
maMTF4 = ma(srcMTF, lenMTF4, maTypeMTF)
maMTF5 = ma(srcMTF, lenMTF5, maTypeMTF)
maMTF6 = ma(srcMTF, lenMTF6, maTypeMTF)

res = input.timeframe(defval="D", title="Timef.", inline="MTF1", group=groupMTF)

MTF1 = request.security(syminfo.tickerid, res, maMTF1, gaps=barmerge.gaps_off)
MTF2 = request.security(syminfo.tickerid, res, maMTF2, gaps=barmerge.gaps_off)
MTF3 = request.security(syminfo.tickerid, res, maMTF3, gaps=barmerge.gaps_off)
MTF4 = request.security(syminfo.tickerid, res, maMTF4, gaps=barmerge.gaps_off)
MTF5 = request.security(syminfo.tickerid, res, maMTF5, gaps=barmerge.gaps_off)
MTF6 = request.security(syminfo.tickerid, res, maMTF6, gaps=barmerge.gaps_off)

plot(visibleMTF1 ? MTF1 : na, title="MA MTF 1", style=plot.style_stepline, color=color.new(#E12D7B, 50), linewidth=1)
plot(visibleMTF2 ? MTF2 : na, title="MA MTF 2", style=plot.style_stepline, color=color.new(#F67B52, 50), linewidth=1)
plot(visibleMTF3 ? MTF3 : na, title="MA MTF 3", style=plot.style_stepline, color=color.new(#EDCD3B, 50), linewidth=1)
plot(visibleMTF4 ? MTF4 : na, title="MA MTF 4", style=plot.style_stepline, color=color.new(#3BBC54, 50), linewidth=1)
plot(visibleMTF5 ? MTF5 : na, title="MA MTF 5", style=plot.style_stepline, color=color.new(#2665BD, 50), linewidth=1)
plot(visibleMTF6 ? MTF6 : na, title="MA MTF 6", style=plot.style_stepline, color=color.new(#481899, 50), linewidth=1)

// ----------------------------
groupB = "Moving Average Bands"
// ----------------------------

maTypeB		= input.string("EMA", title="Moving Average Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "LSMA"], inline="MAB0", group=groupB)
visibleEB	= input.bool(false, title="Bands:", inline="MAB1", group=groupB)
lengthEB	= input.int(34, minval=1, title="length", inline="MAB1", group=groupB)
resEB		= input.timeframe(defval="", title="Timef.", inline="MAB1", group=groupB)

highShortEMA		= ma(high, lengthEB, maTypeB)
lowShortEMA			= ma(low, lengthEB, maTypeB)
EMA					= ma(close, lengthEB, maTypeB)
shortbandsHigh		= ((highShortEMA - EMA) * math.phi) * math.pi + EMA
shortbandsLow		= (-(EMA - lowShortEMA) * math.phi) * math.pi + EMA
shortbandsHighEMA	= ta.wma(shortbandsHigh, 8)
shortbandsLowEMA	= ta.wma(shortbandsLow, 8)
phiExtensionHigh	= ((highShortEMA - EMA) * math.phi) * (math.phi + 4) + EMA
phiExtensionLow		= (-(EMA - lowShortEMA) * math.phi) * (math.phi + 4) + EMA
phiExtensionHighEMA	= ta.wma(phiExtensionHigh, 8)
phiExtensionLowEMA	= ta.wma(phiExtensionLow, 8)

// ----------------------------

MTFhs	= request.security(syminfo.tickerid, resEB, highShortEMA)
MTFls	= request.security(syminfo.tickerid, resEB, lowShortEMA)
MTFsbh	= request.security(syminfo.tickerid, resEB, shortbandsHighEMA)
MTFsbl	= request.security(syminfo.tickerid, resEB, shortbandsLowEMA)
MTFhpeh = request.security(syminfo.tickerid, resEB, phiExtensionHighEMA)
MTFlpel = request.security(syminfo.tickerid, resEB, phiExtensionLowEMA)

highP1	= plot(visibleEB ? MTFhs : na		, color=color.new(color.blue	, 75), title = "B. Top Median Zone")
lowP1	= plot(visibleEB ? MTFls : na		, color=color.new(color.blue	, 75), title = "B. Bottom Median Zone")
highP3	= plot(visibleEB ? MTFsbh : na		, color=color.new(color.orange	, 75), title = "B. Lower Sell Zone")
lowP3	= plot(visibleEB ? MTFsbl : na		, color=color.new(color.green	, 75), title = "B. Higher Buy Zone")
phiPlotHigh = plot(visibleEB ? MTFhpeh : na	, color=color.new(color.red		, 75), title = "B. Top Sell Zone")
phiPlotLow = plot(visibleEB ? MTFlpel : na	, color=color.new(color.teal	, 75), title = "B. Bottom Buy Zone")

fill(phiPlotHigh, highP3, color.new(color.red	, 97), title = "B. Sell Zone")
fill(lowP3, phiPlotLow	, color.new(color.green	, 97), title = "B. Buy Zone")
fill(highP1, lowP1		, color.new(color.silver, 97), title = "B. Median Zone")

// -----------------------------
groupHMA = "Hull Moving Average"
// -----------------------------

resHMA			= input.timeframe(title="Time Frame", defval="", inline="HMA00", group=groupHMA)
trend_flipHMA	= input.int(defval=2, minval=0, title="Flip Trend Periods:", inline="HMA0", group=groupHMA)
srcHMA			= input(defval=close, title="Source", inline="HMA0", group=groupHMA)
visibleHMA1		= input.bool(false, title="HMA:", inline="HMA1", group=groupHMA)
lengthHMA1		= input.int(200, minval=1, title="length", inline="HMA1", group=groupHMA)

hullma1			= ta.wma(2*ta.wma(srcHMA, lengthHMA1/2)-ta.wma(srcHMA, lengthHMA1), math.floor(math.sqrt(lengthHMA1)))
hullmaS1		= request.security(syminfo.tickerid, resHMA, hullma1, gaps=barmerge.gaps_off)

up_colorHMA1	= input.color(defval=color.new(#00FEEF, 50), title="up", inline="HMA1", group=groupHMA)
down_colorHMA1	= input.color(defval=color.new(#E21B22, 50), title="down", inline="HMA1", group=groupHMA)

plot_colorHMA1	= visibleHMA1 ? hullmaS1 >= hullmaS1[trend_flipHMA] ? up_colorHMA1 : down_colorHMA1 : na
plot(visibleHMA1 ? hullmaS1 : na, title="HMA Line 1", style=plot.style_line, color=plot_colorHMA1, linewidth=2)

// ------------------------
groupBB = "Bollinger Bands"
// ------------------------

resBB		= input.timeframe(defval="", title="Time Frame", inline="BB0", group=groupBB)
visibleBB	= input.bool(false, title="BB:", inline="BB1", group=groupBB)
lengthBB	= input.int(20, minval=1, title="length", inline="BB1", group=groupBB)
srcBB		= input(defval=close, title="Source", inline="BB1", group=groupBB)
mult		= input.float(2.0, minval=0.001, maxval=50, title="Standard Deviation", inline="BB2", group=groupBB)

basis	= ta.sma(srcBB, lengthBB)
dev		= mult * ta.stdev(srcBB, lengthBB)
offset	= input.int(0, "Offset", minval=-500, maxval=500, inline="BB2", group=groupBB)

upper = basis + dev
lower = basis - dev

MTFbasis = request.security(syminfo.tickerid, resBB, basis)
MTFupper = request.security(syminfo.tickerid, resBB, upper)
MTFlower = request.security(syminfo.tickerid, resBB, lower)

plot(visibleBB ? MTFbasis : na, title="BB Basis", color=color.new(#787B86, 75), offset = offset)
p1 = plot(visibleBB ? MTFupper : na, title="BB Upper", color=color.new(#787B86, 75), offset = offset)
p2 = plot(visibleBB ? MTFlower : na, title="BB Lower", color=color.new(#787B86, 75), offset = offset)
fill(p1, p2, title = "BB Background", color=color.new(#787B86, 97))

// -------------------------
groupGC = "Gaussian Channel"
// -------------------------

f_filt9x(_a, _s, _i) =>
    int _m2 = 0
    int _m3 = 0
    int _m4 = 0
    int _m5 = 0
    int _m6 = 0
    int _m7 = 0
    int _m8 = 0
    int _m9 = 0
    float _f = .0
    _x = 1 - _a
    // Weights. 
    // Initial weight _m1 is a pole number and equal to _i
    _m2 := _i == 9 ? 36 : _i == 8 ? 28 : _i == 7 ? 21 : _i == 6 ? 15 : _i == 5 ? 10 : _i == 4 ? 6 : _i == 3 ? 3 : _i == 2 ? 1 : 0
    _m3 := _i == 9 ? 84 : _i == 8 ? 56 : _i == 7 ? 35 : _i == 6 ? 20 : _i == 5 ? 10 : _i == 4 ? 4 : _i == 3 ? 1 : 0
    _m4 := _i == 9 ? 126 : _i == 8 ? 70 : _i == 7 ? 35 : _i == 6 ? 15 : _i == 5 ? 5 : _i == 4 ? 1 : 0
    _m5 := _i == 9 ? 126 : _i == 8 ? 56 : _i == 7 ? 21 : _i == 6 ? 6 : _i == 5 ? 1 : 0
    _m6 := _i == 9 ? 84 : _i == 8 ? 28 : _i == 7 ? 7 : _i == 6 ? 1 : 0
    _m7 := _i == 9 ? 36 : _i == 8 ? 8 : _i == 7 ? 1 : 0
    _m8 := _i == 9 ? 9 : _i == 8 ? 1 : 0
    _m9 := _i == 9 ? 1 : 0
    // filter
    _f := math.pow(_a, _i) * nz(_s) + _i * _x * nz(_f[1]) - (_i >= 2 ? _m2 * math.pow(_x, 2) * nz(_f[2]) : 0) + (_i >= 3 ? _m3 * math.pow(_x, 3) * nz(_f[3]) : 0) - (_i >= 4 ? _m4 * math.pow(_x, 4) * nz(_f[4]) : 0) + (_i >= 5 ? _m5 * math.pow(_x, 5) * nz(_f[5]) : 0) - (_i >= 6 ? _m6 * math.pow(_x, 6) * nz(_f[6]) : 0) + (_i >= 7 ? _m7 * math.pow(_x, 7) * nz(_f[7]) : 0) - (_i >= 8 ? _m8 * math.pow(_x, 8) * nz(_f[8]) : 0) + (_i == 9 ? _m9 * math.pow(_x, 9) * nz(_f[9]) : 0)
    _f

f_pole(_a, _s, _i) =>
    _f1 = f_filt9x(_a, _s, 1)
    _f2 = _i >= 2 ? f_filt9x(_a, _s, 2) : 0
    _f3 = _i >= 3 ? f_filt9x(_a, _s, 3) : 0
    _f4 = _i >= 4 ? f_filt9x(_a, _s, 4) : 0
    _f5 = _i >= 5 ? f_filt9x(_a, _s, 5) : 0
    _f6 = _i >= 6 ? f_filt9x(_a, _s, 6) : 0
    _f7 = _i >= 2 ? f_filt9x(_a, _s, 7) : 0
    _f8 = _i >= 8 ? f_filt9x(_a, _s, 8) : 0
    _f9 = _i == 9 ? f_filt9x(_a, _s, 9) : 0
    _fn = _i == 1 ? _f1 : _i == 2 ? _f2 : _i == 3 ? _f3 : _i == 4 ? _f4 : _i == 5 ? _f5 : _i == 6 ? _f6 : _i == 7 ? _f7 : _i == 8 ? _f8 : _i == 9 ? _f9 : na
    [_fn, _f1]

// -------------------------

visibleg		= input(false, title="Show Gaussian Channel", inline="G1", group=groupGC)
int per			= input.int(defval=144, minval=2, title="Samp. Period", inline="G2", group=groupGC)
int N			= input.int(defval=4, minval=1, maxval=9, title="Poles", inline="G3", group=groupGC)
float multg 	= input.float(defval=1.414, minval=0, title="Filt. TR Mult.", inline="G3", group=groupGC)
srcg			= input(defval=hlc3, title="Source", inline="G2", group=groupGC)
bool modeLag	= input(defval=false, title="Reduced Lag Mode", inline="G4", group=groupGC)
bool modeFast	= input(defval=false, title="Fast Response Mode", inline="G4", group=groupGC)

beta = (1 - math.cos(4 * math.asin(1) / per)) / (math.pow(1.414, 2 / N) - 1)
alpha = -beta + math.sqrt(math.pow(beta, 2) + 2 * beta)

lag = (per - 1) / (2 * N)

srcgdata = modeLag ? srcg + srcg - srcg[lag] : srcg
trdata = modeLag ? ta.tr(true) + ta.tr(true) - ta.tr(true)[lag] : ta.tr(true)

[filtn, filt1]		= f_pole(alpha, srcgdata, N)
[filtntr, filt1tr]	= f_pole(alpha, trdata, N)

filt = modeFast ? (filtn + filt1) / 2 : filtn
filttr = modeFast ? (filtntr + filt1tr) / 2 : filtntr

hband = filt + filttr * multg
lband = filt - filttr * multg

// -------------------------

resGC		= input.timeframe(defval="", title="| Timef.", inline="G1", group=groupGC)
MTFfilt		= request.security(syminfo.tickerid, resGC, filt, gaps=barmerge.gaps_off)
MTFhband	= request.security(syminfo.tickerid, resGC, hband, gaps=barmerge.gaps_off)
MTFlband	= request.security(syminfo.tickerid, resGC, lband, gaps=barmerge.gaps_off)

fcolor = MTFfilt > MTFfilt[1] ? #00FEEF : MTFfilt < MTFfilt[1] ? #E21B22 : color.silver
//barcolor = srcg > srcg[1] and srcg > filt and srcg < hband ? #0aff68 : srcg > srcg[1] and srcg >= hband ? #0aff1b : srcg <= srcg[1] and srcg > filt ? #00752d : srcg < srcg[1] and srcg < filt and srcg > lband ? #ff0a5a : srcg < srcg[1] and srcg <= lband ? #ff0a11 : srcg >= srcg[1] and srcg < filt ? #990032 : #cccccc
//barcolor(barcolor)

filtplot	= plot(visibleg == true ? MTFfilt : na, title="GC Filter", color=fcolor, linewidth=2)
hbandplot	= plot(visibleg == true ? MTFhband : na, title="GC Filtered True Range High Band", color=fcolor, transp=50, linewidth=2, display=display.none)
lbandplot	= plot(visibleg == true ? MTFlband : na, title="GC Filtered True Range Low Band", color=fcolor, transp=50, linewidth=2, display=display.none)
fill(hbandplot, lbandplot, title="GC Channel Fill", color=fcolor, transp=95, display=display.all)

// ------------------
groupHT = "HalfTrend"
// ------------------

visibleh = input(false, title="Show HalfTrend", inline="H1", group=groupHT)
amplitude = input(defval=2, title="| Amplitude", inline="H1", group=groupHT)

var int trend = 0
var int nextTrend = 0
var float maxLowPrice = nz(low[1], low)
var float minHighPrice = nz(high[1], high)
var float up = 0.0
var float down = 0.0

atr2 = ta.atr(100) / 2

highPrice = high[math.abs(ta.highestbars(amplitude))]
lowPrice = low[math.abs(ta.lowestbars(amplitude))]
highma = ta.sma(high, amplitude)
lowma = ta.sma(low, amplitude)

// ------------------

if nextTrend == 1
    maxLowPrice := math.max(lowPrice, maxLowPrice)

    if highma < maxLowPrice and close < nz(low[1], low)
        trend := 1
        nextTrend := 0
        minHighPrice := highPrice
        minHighPrice
else
    minHighPrice := math.min(highPrice, minHighPrice)

    if lowma > minHighPrice and close > nz(high[1], high)
        trend := 0
        nextTrend := 1
        maxLowPrice := lowPrice
        maxLowPrice

if trend == 0
    if not na(trend[1]) and trend[1] != 0
        up := na(down[1]) ? down : down[1]
    else
        up := na(up[1]) ? maxLowPrice : math.max(maxLowPrice, up[1])
        up
else
    if not na(trend[1]) and trend[1] != 1
        down := na(up[1]) ? up : up[1]
    else
        down := na(down[1]) ? minHighPrice : math.min(minHighPrice, down[1])
        down

ht = trend == 0 ? up : down

//htColor = trend == 0 ? color.new(#2196F3, 0) : color.new(#FF5252, 0)
htColor = ht >= ht[1] ? color.new(#2196F3, 0) : ht <= ht[1] ? color.new(#FF5252, 0) : color.new(#CCCCCC, 0)

htPlot = plot(visibleh == true ? ht : na, title="HalfTrend", linewidth=2, color=htColor)

// -------------------------------------
groupRV = "Rolling VWAP and StDev Bands"
// -------------------------------------

rVWAP(length) =>
    float p = na
    float vol = na
    float sn = na
    p_ = hlc3 * volume
    p := nz(p[1]) + p_ - nz(p_[length])
    vol := nz(vol[1]) + volume - nz(volume[length])
    v = p / vol
    sn_ = volume * (hlc3 - nz(v[1])) * (hlc3 - v)
    sn := nz(sn[1]) + sn_ - nz(sn_[length])
    std = math.sqrt(sn / vol)
    [v, std]

// -------------------------------------

rolling_sv		= input(false, title="Show Rolling VWAP", inline="RV1", group=groupRV)
rolling_dv		= input(false, title="Show Rolling VWAP Bands |", inline="RV2", group=groupRV)
rolling_period	= input(200, title="", inline="RV1", group=groupRV)
vR_color		= input.color(color.new(color.silver, 50), title="", inline="RV1", group=groupRV)

showrL	= input(false, "Lines", inline="RV2", group=groupRV)
showrLC = showrL ? display.all : display.none

[vwap_r, std_r] = rVWAP(rolling_period)

plot(rolling_sv == true ? vwap_r : na, title = "VWAP - Rolling", color=vR_color, linewidth = 1)

// -------------------------------------

fill_col_up3	= color.new(color.red	, 85)
fill_col_up2	= color.new(color.red	, 90)
fill_col_up		= color.new(color.red	, 95)
fill_col_mid	= color.new(color.silver, 97)
fill_col_down	= color.new(color.blue	, 95)
fill_col_down2	= color.new(color.blue	, 90)
fill_col_down3	= color.new(color.blue	, 85)

b1_r_std_up_plot = plot(rolling_dv == true ? vwap_r + 0.618	* std_r : na, title="rVWAP - STDEV +1", color=color.new(color.silver, 75), style = plot.style_linebr, linewidth=1, display=showrLC)
b1_r_std_dn_plot = plot(rolling_dv == true ? vwap_r - 0.618	* std_r : na, title="rVWAP - STDEV -1", color=color.new(color.silver, 75), style = plot.style_linebr, linewidth=1, display=showrLC)
b2_r_std_up_plot = plot(rolling_dv == true ? vwap_r + 1		* std_r : na, title="rVWAP - STDEV +2", color=color.new(color.silver, 75), style = plot.style_linebr, linewidth=1, display=showrLC)
b2_r_std_dn_plot = plot(rolling_dv == true ? vwap_r - 1		* std_r : na, title="rVWAP - STDEV -2", color=color.new(color.silver, 75), style = plot.style_linebr, linewidth=1, display=showrLC)
b3_r_std_up_plot = plot(rolling_dv == true ? vwap_r + 1.618	* std_r : na, title="rVWAP - STDEV +3", color=color.new(color.silver, 75), style = plot.style_linebr, linewidth=1, display=showrLC)
b3_r_std_dn_plot = plot(rolling_dv == true ? vwap_r - 1.618	* std_r : na, title="rVWAP - STDEV -3", color=color.new(color.silver, 75), style = plot.style_linebr, linewidth=1, display=showrLC)
b4_r_std_up_plot = plot(rolling_dv == true ? vwap_r + 2		* std_r : na, title="rVWAP - STDEV +4", color=color.new(color.silver, 75), style = plot.style_linebr, linewidth=1, display=showrLC)
b4_r_std_dn_plot = plot(rolling_dv == true ? vwap_r - 2		* std_r : na, title="rVWAP - STDEV -4", color=color.new(color.silver, 75), style = plot.style_linebr, linewidth=1, display=showrLC)

fill(b1_r_std_up_plot, b1_r_std_dn_plot, title="rVWAP - STDEV +-1", color=fill_col_mid, display=display.none)
fill(b2_r_std_dn_plot, b1_r_std_dn_plot, title="rVWAP - STDEV -2", color=fill_col_down)
fill(b3_r_std_dn_plot, b2_r_std_dn_plot, title="rVWAP - STDEV -3", color=fill_col_down2)
fill(b4_r_std_dn_plot, b3_r_std_dn_plot, title="rVWAP - STDEV -4", color=fill_col_down3)
fill(b2_r_std_up_plot, b1_r_std_up_plot, title="rVWAP - STDEV +2", color=fill_col_up)
fill(b3_r_std_up_plot, b2_r_std_up_plot, title="rVWAP - STDEV +3", color=fill_col_up2)
fill(b4_r_std_up_plot, b3_r_std_up_plot, title="rVWAP - STDEV +4", color=fill_col_up3)