Trend System Multiple Moving Averages Rating
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © exlux99

//@version=4

study("Multiple MA Rating", overlay = true)


////////////////////////////GENERAL INPUTS//////////////////////////////////////

line10= input(defval=20, title="Length 1", minval=1)


//==========DEMA
getDEMA(src, len) =>
    dema  = 2 * ema(src,len) - ema(ema(src,len),len)
    dema
//==========HMA
getHULLMA(src, len) =>
    hullma = wma(2*wma(src, len/2)-wma(src, len), round(sqrt(len)))
    hullma
//==========KAMA
getKAMA(src, len, k1, k2) =>
    change = abs(change(src, len))
    volatility = sum(abs(change(src)), len)
    efficiency_ratio = volatility != 0 ? change / volatility : 0
    kama = 0.0
    fast = 2 / (k1 + 1)
    slow = 2 / (k2 + 1)
    smooth_const = pow(efficiency_ratio * (fast - slow) + slow, 2)
    kama := nz(kama[1]) + smooth_const * (src - nz(kama[1]))
    kama
//==========TEMA
getTEMA(src, len) =>
    e = ema(src, len)
    tema = 3 * (e - ema(e, len)) + ema(ema(e, len), len)
    tema
//==========ZLEMA
getZLEMA(src, len) =>
    zlemalag_1 = (len - 1) / 2
    zlemadata_1 = src + (src - src[zlemalag_1])
    zlema = ema(zlemadata_1, len)
    zlema
//==========FRAMA
getFRAMA(src, len) =>
    Price = src
    N = len
    if N % 2 != 0
        N := N + 1
        N
    N1 = 0.0
    N2 = 0.0
    N3 = 0.0
    HH = 0.0
    LL = 0.0
    Dimen = 0.0
    alpha = 0.0
    Filt = 0.0
    N3 := (highest(N) - lowest(N)) / N
    HH := highest(N / 2 - 1)
    LL := lowest(N / 2 - 1)
    N1 := (HH - LL) / (N / 2)
    HH := high[N / 2]
    LL := low[N / 2]
    for i = N / 2 to N - 1 by 1
        if high[i] > HH
            HH := high[i]
            HH
        if low[i] < LL
            LL := low[i]
            LL
    N2 := (HH - LL) / (N / 2)
    if N1 > 0 and N2 > 0 and N3 > 0
        Dimen := (log(N1 + N2) - log(N3)) / log(2)
        Dimen
    alpha := exp(-4.6 * (Dimen - 1))
    if alpha < .01
        alpha := .01
        alpha
    if alpha > 1
        alpha := 1
        alpha
    Filt := alpha * Price + (1 - alpha) * nz(Filt[1], 1)
    if bar_index < N + 1
        Filt := Price
        Filt
    Filt
//==========VIDYA
getVIDYA(src, len) =>
    mom = change(src)
    upSum = sum(max(mom, 0), len)
    downSum = sum(-min(mom, 0), len)
    out = (upSum - downSum) / (upSum + downSum)
    cmo = abs(out)
    alpha = 2 / (len + 1)
    vidya = 0.0
    vidya := src * alpha * cmo + nz(vidya[1]) * (1 - alpha * cmo)
    vidya
//==========JMA
getJMA(src, len, power, phase) =>
    phase_ratio = phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5
    beta = 0.45 * (len - 1) / ( 0.45 * (len - 1) + 2)
    alpha = pow(beta, power)
    MA1=0.0, Det0=0.0, MA2=0.0, Det1=0.0, JMA=0.0
    MA1 := (1 - alpha) * src + alpha * nz(MA1[1])
    Det0 := (src - MA1) * (1 - beta) + beta * nz(Det0[1])
    MA2 := MA1 + phase_ratio * Det0
    Det1 := (MA2 - nz(JMA[1])) * pow(1 - alpha,2) + pow(alpha,2) * nz(Det1[1])
    JMA := nz(JMA[1]) + Det1
	JMA
//==========T3
getT3(src, len, vFactor) =>
    ema1 = ema(src, len)
    ema2 = ema(ema1,len)
    ema3 = ema(ema2,len)
    ema4 = ema(ema3,len)
    ema5 = ema(ema4,len)
    ema6 = ema(ema5,len)
    c1 = -1 * pow(vFactor,3)
    c2 = 3*pow(vFactor,2) + 3*pow(vFactor,3)
    c3 = -6*pow(vFactor,2) - 3*vFactor - 3*pow(vFactor,3)
    c4 = 1 + 3*vFactor + pow(vFactor,3) + 3*pow(vFactor,2)
    T3 = c1*ema6 + c2*ema5 + c3*ema4 + c4*ema3
    T3
//==========TRIMA
getTRIMA(src, len) =>
    N = len + 1
    Nm = round( N / 2 )
    TRIMA = sma(sma(src, Nm), Nm)
    TRIMA

sma_10   = sma(close, line10)


ema_10   = ema(close, line10)

wma_10   = wma(close, line10)


alma_10  = alma(close,line10, 0.85, 6)


smma_10 = rma(close,line10)


lsma_10 = linreg(close, line10, 0)


vwma_10 = vwma(close,line10)


dema_10 = getDEMA(close,line10)


hullma_10 = getHULLMA(close,line10)


kama_10= getKAMA(close,line10,2,30)


frama_10 = getFRAMA(close,line10)


vidya_10 = getVIDYA(close,line10)


jma_10= getJMA(close,line10,2,50)


tema_10= getTEMA(close,line10)


zlema_10= getZLEMA(close,line10)


t3_10 = getT3(close,line10,0.7)


trima_10 = getTRIMA (close,line10)




//-------------- MOVING AVERAGES CALCULATION
sma_10_index =0.0


ema_10_index =0.0


wma_10_index =0.0


alma_10_index =0.0


smma_10_index =0.0


lsma_10_index =0.0

vwma_10_index =0.0

dema_10_index =0.0

hullma_10_index =0.0

kama_10_index =0.0

frama_10_index =0.0

vidya_10_index =0.0

jma_10_index =0.0


tema_10_index =0.0


zlema_10_index =0.0


t3_10_index =0.0


trima_10_index =0.0





//sma
if sma_10 >= close
    sma_10_index := 1
    sma_10_index
if sma_10 < close
    sma_10_index := -1
    sma_10_index
    


//ema
if ema_10 >= close
    ema_10_index := 1
    ema_10_index
if ema_10 < close
    ema_10_index := -1
    ema_10_index


//wma
if wma_10 >= close
    wma_10_index := 1
    wma_10_index
if wma_10 < close
    wma_10_index := -1
    wma_10_index

    

//alma
if alma_10 >= close
    alma_10_index := 1
    alma_10_index
if alma_10 < close
    alma_10_index := -1
    alma_10_index



//smma
if smma_10 >= close
    smma_10_index := 1
    smma_10_index
if smma_10 < close
    smma_10_index := -1
    smma_10_index


    


//lsma
if lsma_10 >= close
    lsma_10_index := 1
    lsma_10_index
if lsma_10 < close
    lsma_10_index := -1
    lsma_10_index

    
    

//vwma
if vwma_10 >= close
    vwma_10_index := 1
    vwma_10_index
if vwma_10 < close
    vwma_10_index := -1
    vwma_10_index


    

//dema
if dema_10 >= close
    dema_10_index := 1
    dema_10_index
if dema_10 < close
    dema_10_index := -1
    dema_10_index
 
    

//hullma
if hullma_10 >= close
    hullma_10_index := 1
    hullma_10_index
if hullma_10 < close
    hullma_10_index := -1
    hullma_10_index


    


//kama
if kama_10 >= close
    kama_10_index := 1
    kama_10_index
if kama_10 < close
    kama_10_index := -1
    kama_10_index


    


//frama
if frama_10 >= close
    frama_10_index := 1
    frama_10_index
if frama_10 < close
    frama_10_index := -1
    frama_10_index



//vidya
if vidya_10 >= close
    vidya_10_index := 1
    vidya_10_index
if vidya_10 < close
    vidya_10_index := -1
    vidya_10_index


    
    
    
//jma
if jma_10 >= close
    jma_10_index := 1
    jma_10_index
if jma_10 < close
    jma_10_index := -1
    jma_10_index


    
    
    
//tema
if tema_10 >= close
    tema_10_index := 1
    tema_10_index
if tema_10 < close
    tema_10_index := -1
    tema_10_index


    
    
    
//zlema
if zlema_10 >= close
    zlema_10_index := 1
    zlema_10_index
if zlema_10 < close
    zlema_10_index := -1
    zlema_10_index


 
 
//trima
if trima_10 >= close
    trima_10_index := 1
    trima_10_index
if trima_10 < close
    trima_10_index := -1
    trima_10_index

    
    
    

//t3
if t3_10 >= close
    t3_10_index := 1
    t3_10_index
if t3_10 < close
    t3_10_index := -1
    t3_10_index

// ////////////////////////////PLOTTING////////////////////////////////////////////

avg_10 = (sma_10+ema_10+wma_10+dema_10+alma_10+hullma_10+smma_10+lsma_10+kama_10+tema_10+zlema_10+frama_10+vidya_10+jma_10+t3_10+vwma_10+trima_10) /17
avg_10_index = (sma_10_index+ema_10_index+wma_10_index+dema_10_index+alma_10_index+hullma_10_index+smma_10_index+lsma_10_index+kama_10_index+tema_10_index+zlema_10_index+frama_10_index+vidya_10_index+jma_10_index+t3_10_index+vwma_10_index+trima_10_index) /-17


plot(avg_10, color = avg_10>avg_10[1] ? color.lime : color.red, linewidth=4)

posInput = input(title="Position", defval="Top Right", options=["Bottom Left", "Bottom Right", "Top Left", "Top Right"])
var pos = posInput == "Bottom Left" ? position.bottom_left : posInput == "Bottom Right" ? position.bottom_right : posInput == "Top Left" ? position.top_left : posInput == "Top Right" ? position.top_right : na


var table perfTable = table.new(pos, 2, 4, border_width = 3)


text_size = input("AUTO", "Text Size:", options=["AUTO", "TINY","SMALL", "NORMAL", "LARGE", "HUGE"])
table_size = input(6, "Table Width:")

text = size.auto
if text_size == "TINY"
    text := size.tiny
if text_size == "SMALL"
    text := size.small
if text_size == "NORMAL"
    text := size.normal
if text_size == "LARGE"
    text := size.large
if text_size == "HUGE"
    text := size.huge
    

    
LIGHTTRANSP = 90
AVGTRANSP   = 80
HEAVYTRANSP = 70


f_fillCell(_table, _column, _row, _value, _timeframe, _c_color) =>
    //_c_color = color.blue//close > open ? i_posColor : i_negColor
    _transp = HEAVYTRANSP//abs(_value) > 10 ? HEAVYTRANSP : abs(_value) > 5 ? AVGTRANSP : LIGHTTRANSP
    _cellText = tostring(_value, "#.## %") 
    table.cell(_table, _column, _row, _cellText, bgcolor = color.new(_c_color, _transp), text_color = _c_color, width = table_size)
    table.cell_set_text_size(perfTable, 1, 0, text)
    table.cell_set_text_size(perfTable, 1, 1, text)    
    //table.cell_set_text_size(perfTable, 0, 2, text)
    
    
if barstate.islast

    table.cell(perfTable, 0, 0, "Rating confirmation 0-100 Long | -100-0 Short", text_color=color.white, text_size=text, bgcolor=color.blue)
    f_fillCell(perfTable, 0, 1, (avg_10_index*100)/100 , "% confirmation", (avg_10_index*100)/100 >0? color.lime : color.red )
