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

//@version=5
indicator(title="TOMMAR_2.0.1", shorttitle="TOMMAR_2.0.1", overlay=true)

//input
source = input.source(close, 'Source')

MAtype = input.string('EMA', 'MA Type', options=['EMA', 'HMA', 'RMA', 'SMA', 'VWMA', 'WMA', 'DEMA', 'LSMA', 'PSAR', 'SWMA', 'TEMA', 'SMMA'])

Signal_ON = input.bool(true, 'Trend Light')

Proj_ON = input.bool(false, 'MA Projection')

MA1ON = input.bool(true, '1st MA', group='1st MA')
MA2ON = input.bool(true, '2nd MA', group='2nd MA')
MA3ON = input.bool(true, '3rd MA', group='3rd MA')
MA4ON = input.bool(true, '4th MA', group='4th MA')
MA5ON = input.bool(true, '5th MA', group='5th MA')
//MA6ON = input.bool(true, '6th MA', group='6th MA')
// MA7ON = input.bool(true, '7th MA', group='7th MA')
// MA8ON = input.bool(true, '8th MA', group='8th MA')

//MALON = MA8ON ? MA8ON : MA7ON ? MA7ON : MA6ON ? MA6ON : MA5ON ? MA5ON : MA4ON ? MA4ON : MA3ON ? MA3ON : MA2ON ? MA2ON : MA1ON ? MA1ON : na  

MA1len = input.int(25, 'length', group='1st MA') 
MA2len = input.int(50, 'length', group='2nd MA') 
MA3len = input.int(100, 'length', group='3rd MA') 
MA4len = input.int(200, 'length', group='4th MA')
MA5len = input.int(400, 'length', group='5th MA') 
//MA6len = input.int(150, 'length', group='6th MA') 
// MA7len = input.int(176, 'length', group='7th MA') 
// MA8len = input.int(200, 'length', group='8th MA')

MALlen = MA5ON ? MA5len : MA4ON ? MA4len : MA3ON ? MA3len : MA2ON ? MA2len : MA1ON ? MA1len : na  

getMA(float _source, simple int _length) =>
    switch MAtype
	    'EMA' => ta.ema(_source, _length)
        'HMA' => ta.hma(_source, _length)
        'RMA' => ta.rma(_source, _length)
        'SMA' => ta.sma(_source, _length)
        'VWMA' => ta.vwma(_source, _length)
        'WMA' => ta.wma(_source, _length)
        'DEMA' => 2 * ta.ema(_source, _length) - ta.ema(ta.ema(_source, _length), _length)
        'LSMA' => 3 * ta.wma(_source, _length) - 2 * ta.sma(_source, _length)
        'SWMA' => ta.swma(_source)
        'TEMA' => 3 * ta.ema(_source, _length) - 3 * ta.ema(ta.ema(_source, _length), _length) + ta.ema(ta.ema(ta.ema(_source, _length), _length), _length)
        'PSAR' => ta.sar(_length/10, _length/10, _length*10)
        'SMMA' => 
            smma = 0.0
            smma := na(smma[1]) ? ta.sma(_source, _length) : (smma[1] * (_length - 1) + _source) / _length
            smma

MMA1 = MA1ON ? getMA(source, MA1len) : na
MMA2 = MA2ON ? getMA(source, MA2len) : na
MMA3 = MA3ON ? getMA(source, MA3len) : na
MMA4 = MA4ON ? getMA(source, MA4len) : na
MMA5 = MA5ON ? getMA(source, MA5len) : na
//MMA6 = MA6ON ? getMA(source, MA6len) : na
// MMA7 = MA7ON ? getMA(source, MA7len) : na
// MMA8 = MA8ON ? getMA(source, MA8len) : na
MMAL = getMA(source, MALlen)

leadMAColor = ta.change(MMA1) >=0 and MMA1>MMAL ? #089981 : ta.change(MMA1) <0 and MMA1<MMAL ? #e57373 : ta.change(MMA1) <=0 and MMA1<MMAL ? #e57373 : ta.change(MMA1)>=0 and MMA1<MMAL ? #089981 : #e3bb38

maColor(ma, maRef) => ta.change(ma)>=0 and MMA1>maRef ? #089981 : ta.change(ma)<=0 and MMA1<maRef ? #e57373 : ta.change(ma)>=0 and MMA1<maRef ? #089981 : #e3bb38

plot(MMA1, title='MA1', color=leadMAColor, linewidth=1)
plot(MMA2, title='MA2', color=maColor(MMA1, MMAL), linewidth=1)
plot(MMA3, title='MA3', color=maColor(MMA2, MMAL), linewidth=1)
plot(MMA4, title='MA4', color=maColor(MMA3, MMAL), linewidth=1)
plot(MMA5, title='MA5', color=maColor(MMA4, MMAL), linewidth=1)
//plot(MMA6, title='MA6', color=maColor(MMA5, MMAL), linewidth=1)
// plot(MMA7, title='MA7', color=maColor(MMA6, MMAL), linewidth=1)
// plot(MMA8, title='MA8', color=maColor(MMA7, MMAL), linewidth=1)

Long = Signal_ON and MMA1 > MMA2 and MMA2 > MMA3 and MMA3 > MMA4 and MMA4 > MMA5 ? MMA5
     : Signal_ON and MMA1 > MMA2 and MMA2 > MMA3 and MMA3 > MMA4 and na(MMA5) ? MMA4
     : Signal_ON and MMA1 > MMA2 and MMA2 > MMA3 and na(MMA4) ? MMA3
     : Signal_ON and MMA1 > MMA2 and na(MMA3) ? MMA2
     : Signal_ON and na(MMA2) ? MMA1
     : na

Short = Signal_ON and MMA1 < MMA2 and MMA2 < MMA3 and MMA3 < MMA4 and MMA4 < MMA5 ? MMA5
      : Signal_ON and MMA1 < MMA2 and MMA2 < MMA3 and MMA3 < MMA4 and na(MMA5) ? MMA4
      : Signal_ON and MMA1 < MMA2 and MMA2 < MMA3 and na(MMA4) ? MMA3
      : Signal_ON and MMA1 < MMA2 and na(MMA3) ? MMA2
      : Signal_ON and na(MMA2) ? MMA1
      : na

// Long = Signal_ON and MMA1 > MMA2 and MMA2 > MMA3 and MMA3 > MMA4 and MMA4 > MMA5 and MMA5 > MMA6 and MMA6 > MMA7 and MMA7 > MMA8 ? MMAL : na
// Short = Signal_ON and MMA1 < MMA2 and MMA2 < MMA3 and MMA3 < MMA4 and MMA4 < MMA5 and MMA5 < MMA6 and MMA6 < MMA7 and MMA7 < MMA8 ? MMAL : na


plotshape(Long, title = 'Long', style=shape.circle, location=location.absolute, color=#089981, size=size.tiny) 
plotshape(Short, title = 'Short', style=shape.circle, location=location.absolute, color=#e57373, size=size.tiny)

// Projection //

ma_proj(_source_, _length_, offset) =>
    (getMA(_source_, _length_ - offset) * (_length_ - offset) + _source_ * offset) / _length_

MMA1_1 = MA1ON and Proj_ON ? ma_proj(source, MA1len, 2) : na
MMA1_2 = MA1ON and Proj_ON ? ma_proj(source, MA1len, 4) : na
MMA1_3 = MA1ON and Proj_ON ? ma_proj(source, MA1len, 6) : na
MMA1_4 = MA1ON and Proj_ON ? ma_proj(source, MA1len, 8) : na
MMA1_5 = MA1ON and Proj_ON ? ma_proj(source, MA1len, 10) : na

MMA2_1 = MA2ON and Proj_ON ? ma_proj(source, MA2len, 2) : na
MMA2_2 = MA2ON and Proj_ON ? ma_proj(source, MA2len, 4) : na
MMA2_3 = MA2ON and Proj_ON ? ma_proj(source, MA2len, 6) : na
MMA2_4 = MA2ON and Proj_ON ? ma_proj(source, MA2len, 8) : na
MMA2_5 = MA2ON and Proj_ON ? ma_proj(source, MA2len, 10) : na

MMA3_1 = MA3ON and Proj_ON ? ma_proj(source, MA3len, 2) : na
MMA3_2 = MA3ON and Proj_ON ? ma_proj(source, MA3len, 4) : na
MMA3_3 = MA3ON and Proj_ON ? ma_proj(source, MA3len, 6) : na
MMA3_4 = MA3ON and Proj_ON ? ma_proj(source, MA3len, 8) : na
MMA3_5 = MA3ON and Proj_ON ? ma_proj(source, MA3len, 10) : na

MMA4_1 = MA4ON and Proj_ON ? ma_proj(source, MA4len, 2) : na
MMA4_2 = MA4ON and Proj_ON ? ma_proj(source, MA4len, 4) : na
MMA4_3 = MA4ON and Proj_ON ? ma_proj(source, MA4len, 6) : na
MMA4_4 = MA4ON and Proj_ON ? ma_proj(source, MA4len, 8) : na
MMA4_5 = MA4ON and Proj_ON ? ma_proj(source, MA4len, 10) : na

MMA5_1 = MA5ON and Proj_ON ? ma_proj(source, MA5len, 2) : na
MMA5_2 = MA5ON and Proj_ON ? ma_proj(source, MA5len, 4) : na
MMA5_3 = MA5ON and Proj_ON ? ma_proj(source, MA5len, 6) : na
MMA5_4 = MA5ON and Proj_ON ? ma_proj(source, MA5len, 8) : na
MMA5_5 = MA5ON and Proj_ON ? ma_proj(source, MA5len, 10) : na


plot(MMA1_1, color=leadMAColor, linewidth=1, style=plot.style_circles, offset=2, show_last=1)
plot(MMA1_2, color=leadMAColor, linewidth=1, style=plot.style_circles, offset=4, show_last=1)
plot(MMA1_3, color=leadMAColor, linewidth=1, style=plot.style_circles, offset=6, show_last=1)
plot(MMA1_4, color=leadMAColor, linewidth=1, style=plot.style_circles, offset=8, show_last=1)
plot(MMA1_5, color=leadMAColor, linewidth=1, style=plot.style_circles, offset=10, show_last=1)

plot(MMA2_1, color=maColor(MMA1, MMAL), linewidth=1, style=plot.style_circles, offset=2, show_last=1)
plot(MMA2_2, color=maColor(MMA1, MMAL), linewidth=1, style=plot.style_circles, offset=4, show_last=1)
plot(MMA2_3, color=maColor(MMA1, MMAL), linewidth=1, style=plot.style_circles, offset=6, show_last=1)
plot(MMA2_4, color=maColor(MMA1, MMAL), linewidth=1, style=plot.style_circles, offset=8, show_last=1)
plot(MMA2_5, color=maColor(MMA1, MMAL), linewidth=1, style=plot.style_circles, offset=10, show_last=1)

plot(MMA3_1, color=maColor(MMA2, MMAL), linewidth=1, style=plot.style_circles, offset=2, show_last=1)
plot(MMA3_2, color=maColor(MMA2, MMAL), linewidth=1, style=plot.style_circles, offset=4, show_last=1)
plot(MMA3_3, color=maColor(MMA2, MMAL), linewidth=1, style=plot.style_circles, offset=6, show_last=1)
plot(MMA3_4, color=maColor(MMA2, MMAL), linewidth=1, style=plot.style_circles, offset=8, show_last=1)
plot(MMA3_5, color=maColor(MMA2, MMAL), linewidth=1, style=plot.style_circles, offset=10, show_last=1)

plot(MMA4_1, color=maColor(MMA3, MMAL), linewidth=1, style=plot.style_circles, offset=2, show_last=1)
plot(MMA4_2, color=maColor(MMA3, MMAL), linewidth=1, style=plot.style_circles, offset=4, show_last=1)
plot(MMA4_3, color=maColor(MMA3, MMAL), linewidth=1, style=plot.style_circles, offset=6, show_last=1)
plot(MMA4_4, color=maColor(MMA3, MMAL), linewidth=1, style=plot.style_circles, offset=8, show_last=1)
plot(MMA4_5, color=maColor(MMA3, MMAL), linewidth=1, style=plot.style_circles, offset=10, show_last=1)

plot(MMA5_1, color=maColor(MMA4, MMAL), linewidth=1, style=plot.style_circles, offset=2, show_last=1)
plot(MMA5_2, color=maColor(MMA4, MMAL), linewidth=1, style=plot.style_circles, offset=4, show_last=1)
plot(MMA5_3, color=maColor(MMA4, MMAL), linewidth=1, style=plot.style_circles, offset=6, show_last=1)
plot(MMA5_4, color=maColor(MMA4, MMAL), linewidth=1, style=plot.style_circles, offset=8, show_last=1)
plot(MMA5_5, color=maColor(MMA4, MMAL), linewidth=1, style=plot.style_circles, offset=10, show_last=1)



