Trend Master
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kent_RichProFit_93


//@version=5

indicator('Trend Master', overlay=false, precision=3)

//revision 1, removed Heikin Ashi candles
//revision 2, 22Nov21, convert to scripts version 5. Table size adjustable. Allow input on Trend Moving Average Length. 

/////////////////////////////////
//Input
/////////////////////////////////

gr1 = 'Table'
ind = input.bool(title='Show Table', defval=true, inline='00', group=gr1)
alert = input(title='Show alerts ', defval=true,inline='00', group=gr1)
text_size = input.string(title='Table Text Size:', options=['AUTO', 'TINY', 'SMALL', 'NORMAL', 'LARGE', 'HUGE'], defval='SMALL', inline='01', group=gr1)
w = input.int(title='/ Table Width:', defval=6, minval=4, maxval=20, inline='01', group=gr1)

gr2 = 'Trend Options'
tm_op = input.string(title='Select Trend Master Options', options=['EMA', 'HMA', 'SMA', 'WMA'], defval='HMA', inline='02', group = gr2)
tm_ma_len = input.int(13, title='Trend Master Moving Average Length', tooltip='Default Model : HMA, Default Trend Moving Average Lenght = 13',minval=1, inline='02', group = gr2)

gr3 = 'Rainbows'
rb = input(title='click to show rainbow', defval=false, group=gr3)


/////////////////////////////////
//Source
/////////////////////////////////

can_src = (3 * close + 2 * open + high + low) / 7
can_ohlc4 = ohlc4


/////////////////////////////////
//Trend Master
/////////////////////////////////

TM_src = can_src
TM_ohlc4 = can_ohlc4

TM_src_MAs = tm_op == 'EMA' ? ta.ema(TM_src, 7) : tm_op == 'SMA' ? ta.sma(TM_src, 7) : tm_op == 'WMA' ? ta.wma(TM_src, 7) : tm_op == 'HMA' ? ta.hma(TM_src, 7) : na
TM = ta.linreg(TM_src_MAs, 7, 0)

TM_color = TM > TM[1] ? color.new(#39FF14, 0) : color.new(#EB4C42, 0)  //color.new(#FFB7C5,0) //color.new(#EB4C42,10) 
plotcandle(TM, TM, TM[1], TM[1], title='Trend Master', color=TM_color)

/////////////////////////////////
//Moving Average of Trend Master
/////////////////////////////////

sma_tm = ta.sma(TM, tm_ma_len)
plot(sma_tm, title='Moving Average of Trend Master', color=color.new(color.purple, 0))


/////////////////////////////////
//Rainbows
/////////////////////////////////

tm1e = (ta.ema(TM_ohlc4, 3) + ta.ema(TM_ohlc4, 6) + ta.ema(TM_ohlc4, 9)) / 3
tm1s = (ta.sma(TM_ohlc4, 3) + ta.sma(TM_ohlc4, 6) + ta.sma(TM_ohlc4, 9)) / 3
tm1w = (ta.wma(TM_ohlc4, 3) + ta.wma(TM_ohlc4, 6) + ta.wma(TM_ohlc4, 9)) / 3
tm1h = (ta.hma(TM_ohlc4, 3) + ta.hma(TM_ohlc4, 6) + ta.hma(TM_ohlc4, 9)) / 3
tm1 = tm_op == 'EMA' ? tm1e : tm_op == 'SMA' ? tm1s : tm_op == 'WMA' ? tm1w : tm_op == 'HMA' ? tm1h : na
ftm1 = ta.linreg(tm1, 6, 0)
ftm1_c = ftm1 >= ftm1[1] ? color.new(#39FF14, 0) : color.new(#EB4C42, 50)
plot(rb ? ftm1 : na, title='Rainbow 1', style=plot.style_cross, color=ftm1_c)

tm2e = (ta.ema(TM_ohlc4, 5) + ta.ema(TM_ohlc4, 10) + ta.ema(TM_ohlc4, 20)) / 3
tm2s = (ta.sma(TM_ohlc4, 5) + ta.sma(TM_ohlc4, 10) + ta.sma(TM_ohlc4, 20)) / 3
tm2w = (ta.wma(TM_ohlc4, 5) + ta.wma(TM_ohlc4, 10) + ta.wma(TM_ohlc4, 20)) / 3
tm2h = (ta.hma(TM_ohlc4, 5) + ta.hma(TM_ohlc4, 10) + ta.hma(TM_ohlc4, 20)) / 3
tm2 = tm_op == 'EMA' ? tm2e : tm_op == 'SMA' ? tm2s : tm_op == 'WMA' ? tm2w : tm_op == 'HMA' ? tm2h : na
ftm2 = ta.linreg(tm2, 6, 0)
ftm2_c = ftm2 >= ftm2[1] ? color.new(#39FF14, 0) : color.new(#EB4C42, 50)
ftm2_t = ftm2 >= ftm2[1] ? ftm2 : na
plot(rb ? ftm2 : na, title='Rainbow 2', style=plot.style_cross, color=ftm2_c)

tm3e = (ta.ema(TM_ohlc4, 7) + ta.ema(TM_ohlc4, 14) + ta.ema(TM_ohlc4, 28)) / 3
tm3s = (ta.sma(TM_ohlc4, 7) + ta.sma(TM_ohlc4, 14) + ta.sma(TM_ohlc4, 28)) / 3
tm3w = (ta.wma(TM_ohlc4, 7) + ta.wma(TM_ohlc4, 14) + ta.wma(TM_ohlc4, 28)) / 3
tm3h = (ta.hma(TM_ohlc4, 7) + ta.hma(TM_ohlc4, 14) + ta.hma(TM_ohlc4, 28)) / 3
tm3 = tm_op == 'EMA' ? tm3e : tm_op == 'SMA' ? tm3s : tm_op == 'WMA' ? tm3w : tm_op == 'HMA' ? tm3h : na
ftm3 = ta.linreg(tm3, 6, 0)
ftm3_c = ftm3 >= ftm3[1] ? color.new(#39FF14, 0) : color.new(#EB4C42, 50)
plot(rb ? ftm3 : na, title='Rainbow 3', style=plot.style_cross, color=ftm3_c)

tm4e = (ta.ema(TM_ohlc4, 9) + ta.ema(TM_ohlc4, 18) + ta.ema(TM_ohlc4, 36)) / 3
tm4s = (ta.sma(TM_ohlc4, 9) + ta.sma(TM_ohlc4, 18) + ta.sma(TM_ohlc4, 36)) / 3
tm4w = (ta.wma(TM_ohlc4, 9) + ta.wma(TM_ohlc4, 18) + ta.wma(TM_ohlc4, 36)) / 3
tm4h = (ta.hma(TM_ohlc4, 9) + ta.hma(TM_ohlc4, 18) + ta.hma(TM_ohlc4, 36)) / 3
tm4 = tm_op == 'EMA' ? tm4e : tm_op == 'SMA' ? tm4s : tm_op == 'WMA' ? tm4w : tm_op == 'HMA' ? tm4h : na
ftm4 = ta.linreg(tm4, 6, 0)
ftm4_c = ftm4 >= ftm4[1] ? color.new(#39FF14, 0) : color.new(#EB4C42, 50)
plot(rb ? ftm4 : na, title='Rainbow 4', style=plot.style_cross, color=ftm4_c)

tm5e = (ta.ema(TM_ohlc4, 11) + ta.ema(TM_ohlc4, 22) + ta.ema(TM_ohlc4, 44)) / 3
tm5s = (ta.sma(TM_ohlc4, 11) + ta.sma(TM_ohlc4, 22) + ta.sma(TM_ohlc4, 44)) / 3
tm5w = (ta.wma(TM_ohlc4, 11) + ta.wma(TM_ohlc4, 22) + ta.wma(TM_ohlc4, 44)) / 3
tm5h = (ta.hma(TM_ohlc4, 11) + ta.hma(TM_ohlc4, 22) + ta.hma(TM_ohlc4, 44)) / 3
tm5 = tm_op == 'EMA' ? tm5e : tm_op == 'SMA' ? tm5s : tm_op == 'WMA' ? tm5w : tm_op == 'HMA' ? tm5h : na
ftm5 = ta.linreg(tm5, 6, 0)
ftm5_c = ftm5 >= ftm5[1] ? color.new(#39FF14, 0) : color.new(#EB4C42, 50)
plot(rb ? ftm5 : na, title='Rainbow 5', style=plot.style_cross, color=ftm5_c)

tm6e = (ta.ema(TM_ohlc4, 13) + ta.ema(TM_ohlc4, 26) + ta.ema(TM_ohlc4, 52)) / 3
tm6s = (ta.sma(TM_ohlc4, 13) + ta.sma(TM_ohlc4, 26) + ta.sma(TM_ohlc4, 52)) / 3
tm6w = (ta.wma(TM_ohlc4, 13) + ta.wma(TM_ohlc4, 26) + ta.wma(TM_ohlc4, 52)) / 3
tm6h = (ta.hma(TM_ohlc4, 13) + ta.hma(TM_ohlc4, 26) + ta.hma(TM_ohlc4, 52)) / 3
tm6 = tm_op == 'EMA' ? tm6e : tm_op == 'SMA' ? tm6s : tm_op == 'WMA' ? tm6w : tm_op == 'HMA' ? tm6h : na
ftm6 = ta.linreg(tm6, 6, 0)
ftm6_c = ftm6 >= ftm6[1] ? color.new(#39FF14, 0) : color.new(#EB4C42, 50)
plot(rb ? ftm6 : na, title='Rainbow 6', style=plot.style_cross, color=ftm6_c)

tm7e = (ta.ema(TM_ohlc4, 21) + ta.ema(TM_ohlc4, 34) + ta.ema(TM_ohlc4, 68)) / 3
tm7s = (ta.sma(TM_ohlc4, 21) + ta.sma(TM_ohlc4, 34) + ta.sma(TM_ohlc4, 68)) / 3
tm7w = (ta.wma(TM_ohlc4, 21) + ta.wma(TM_ohlc4, 34) + ta.wma(TM_ohlc4, 68)) / 3
tm7h = (ta.hma(TM_ohlc4, 21) + ta.hma(TM_ohlc4, 34) + ta.hma(TM_ohlc4, 68)) / 3
tm7 = tm_op == 'EMA' ? tm7e : tm_op == 'SMA' ? tm7s : tm_op == 'WMA' ? tm7w : tm_op == 'HMA' ? tm7h : na
ftm7 = ta.linreg(tm7, 6, 0)
ftm7_c = ftm7 >= ftm7[1] ? color.new(#39FF14, 0) : color.new(#EB4C42, 50)
plot(rb ? ftm7 : na, title='Rainbow 7', style=plot.style_cross, color=ftm7_c)




/////////////////////////////////
//Alerts
/////////////////////////////////

//Turn Green or Turn Red Labels
tm_turn_t = TM[2] > TM[1] and TM > TM[1] ? 'G' : TM[2] < TM and TM < TM[1] ? 'R' : na
tm_label_c = TM[2] > TM[1] and TM > TM[1] ? color.new(color.green, 0) : TM[2] < TM and TM < TM[1] ? color.new(color.red, 0) : na
tm_yloc = TM[2] > TM[1] and TM > TM[1] ? 0.9 * TM : TM[2] < TM and TM < TM[1] ? 1.05 * TM : na
tm_turn_label = alert?label.new(bar_index, tm_yloc, style=label.style_none, text=tm_turn_t, textcolor=tm_label_c):na


/////////////////////////////////
//indicator table
var table QTable = table.new(position.middle_right, 5, 5, border_width=1)

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


tm_type = tm_op == 'EMA' ? 'EMA' : tm_op == 'SMA' ? 'SMA Model' : tm_op == 'WMA Modle' ? 'WMA Model' : tm_op == 'HMA' ? 'HMA Model' : na

f_fillCell_00(_table, _column, _row, _label) =>
    _cellText_00 = ind ? _label : na
    _cellColor_00 = ind ? color.new(color.yellow, 0) : na
    table.cell(_table, _column, _row, _cellText_00, bgcolor=_cellColor_00, text_color=color.black, width=w)
    table.cell_set_text_size(QTable, 0, 0, text_1)
    table.cell_set_text_size(QTable, 0, 1, text_1)

if barstate.islast
    f_fillCell_00(QTable, 0, 0, 'Trend Master')
    f_fillCell_00(QTable, 0, 1, tm_type)

tm_t = TM[2] > TM[1] and TM > TM[1] ? 'Turn Green' : TM > TM[1] ? 'Green' : TM[2] < TM and TM < TM[1] ? 'Turn Red' : TM < TM[1] ? 'Red' : na
f_fillCell_11(_table, _column, _row, _label) =>
    _cellText_11 = ind ? _label : na
    _cellColor_11 = ind ? TM_color : na
    table.cell(_table, _column, _row, _cellText_11, bgcolor=_cellColor_11, text_color=color.black, width=w)
    table.cell_set_text_size(QTable, 0, 2, text_1)

if barstate.islast
    f_fillCell_11(QTable, 0, 2, tm_t)


