Moving Average Support and Resistance Channel
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Best_Solve

//@version=4
study(title="Moving Average Support and Resistance Channel", shorttitle="MA Channel", format=format.price, precision=8, overlay=true, resolution="") 
mode = input (defval='Auto', title="Mode", options=["Auto", "User Defined"])
smoothing = input(defval="RMA", title="Smoothing", options=["SMA", "EMA", "RMA", "WMA", "VWMA", "HMA"])
length = input(30, minval=1, title="Length")
x = input(1.618, "Factor", minval=0.001, maxval=5)
HTFp = timeframe.period == '1'? 60:
  timeframe.period == '3'? 20:
  timeframe.period == '5'? 48:
  timeframe.period == '15'? 96:
  timeframe.period == '30'? 48:
  timeframe.period == '45'? 32:
  timeframe.period == '60'? 24:
  timeframe.period == '120'? 84:
  timeframe.period == '180'? 56:
  timeframe.period == '240'? 42:
  timeframe.period == 'D'? 30:
  timeframe.period == 'W'? 52: 12

len = mode=='Auto'? HTFp : length

ma_function(source, length)=>
    if smoothing == "SMA"
        sma(source, len)
    else 
        if smoothing == "EMA"
            ema(source, len)
        else 
            if smoothing == "RMA"
                rma(source, len)
            else 
                if smoothing == "WMA"
                    wma(source, len)
                else 
                    if smoothing == "VWMA"
                        vwma(source, len)
                    else 
                        hma(source, len)

serie1 = close>open? close: close<open? open: high
serie2 = close<open? close: close>open? open: low

bottom = ma_function (serie1, len)-x*ma_function (tr(true), len)
top = ma_function (serie2, len)+x*ma_function (tr(true), len)
bottom1 = bottom-x*ma_function (tr(true), len) 
top1 = top+x*ma_function (tr(true), len) 
basis = ma_function (hlc3, len)
plot(top, "R1", color=color.red) 
plot(bottom, "S1", color=color.green)
plot(top1, "R2")
plot(bottom1, "S2")
plot(basis, "Middle Line", color=color.yellow)

