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

//@version=5
indicator("Trend Analysis")

//#region - Trend Selectin -----------------------------------------------------------------------//

TREND_GROUP = "Trend Analysis"
PRICE_SCREENER = "PRICE"
MA_SCREENER = "MA Trend"
RSI_SCREENER = "RSI Trend"
MACD_SCREENER = "MACD Trend"
analysis_type = input.string(title = "Analysis", defval = RSI_SCREENER, options = [RSI_SCREENER, MA_SCREENER, MACD_SCREENER, PRICE_SCREENER], group = TREND_GROUP)

//#endregion
//#region - RSI ----------------------------------------------------------------------------------//

RSI_GROUP = "RSI //////////////////////////////"
RSI_TREND = "Uptrend or Downtrend", RSI_VALUE = "RSI > 50", RSI_DIRECTION = "Advance or Decline"
rsi_selection = input.string(title = "RSI", defval = RSI_TREND, options = [RSI_TREND, RSI_VALUE, RSI_DIRECTION], group = RSI_GROUP)
rsi_period = input.int(title='period, ma, signal', defval = 14, group = RSI_GROUP, inline = "RSI")
rsima_period = input.int(title='', defval =9, group = RSI_GROUP, inline = "RSI")
rsisignal_period = input.int(title='', defval =14, group = RSI_GROUP, inline = "RSI")

//#endregion
//#region - MOVING AVERAGE -----------------------------------------------------------------------//

MA_GROUP = "Moving Average ////////////////////"
MA_CLOSE_TO_5D = "Close to Moving Average"
MA_CROSS_OVER = "MA Cross Over"

ma_selection = input.string(title = "Moving Average", defval = MA_CLOSE_TO_5D, options = [MA_CLOSE_TO_5D, MA_CROSS_OVER], group=MA_GROUP)
fast_ma_period = input.int(title = "Fast/Slow MA", defval = 5, group=MA_GROUP, inline = "MA")
slow_ma_period  = input.int(title = "", defval = 14, group=MA_GROUP, inline = "MA")

//#endregion
//#region - Calculation --------------------------------------------------------------------------//
MACDGROUP = "MACD "
MACDCROSSOVER = "CROSS OVER"
MACDZEROLINE = "MACD OVER ZERO LINE"
MACDDIRECTION = "DIRECTION"
macd_selection = input.string(title = "MACD", defval = MACDCROSSOVER, options = [MACDCROSSOVER, MACDZEROLINE,MACDDIRECTION], group=MACDGROUP)
fastmacdperiod = input.int(title = "Fast/slow/signal", defval = 12, group=MACDGROUP, inline = "macd")
slowmacdperiod = input.int(title = "", defval = 26, group=MACDGROUP, inline = "macd")
signalperiod = input.int(title = "", defval = 9, group=MACDGROUP, inline = "macd")


//#endregion 
//#region - Color --------------------------------------------------------------------------------//

COLOR_GROUP = "COLOR ///////"
POSITIVE_COLOR = input.color(defval = color.new(#00ff08, 30), title = "Positve/Negative Trend", group = COLOR_GROUP, inline="COLOR")
NEGATIVE_COLOR = input.color(defval = color.new(#ff0707, 70), title = "", group = COLOR_GROUP, inline="COLOR")

//#endregion
//#region - Function -----------------------------------------------------------------------------//

getprice(symbol)=>
    // get price of each security.
    request.security(symbol, timeframe.period, close)
//

changescreener(src)=>
    trend = (
         src > src[1] ? 1 :-1 )

rsiscreener(src, analysis)=>
    // RSI 
        // Uptrend or Downtrend : RSI MA greater than RSI Signal
        // > 50                 : RSI MA greater than 50%
        // Advance or Decline   : RSI MA gretter than RSI MA[1]
    rsi = ta.rsi(src, rsi_period)
    rsima= ta.sma(rsi, rsima_period)
    rsisig = ta.sma(rsima, rsisignal_period)
    rsi_trend = analysis == RSI_TREND ? rsima > rsisig  ?  1 : -1 :  analysis == RSI_VALUE ?  rsima > 50 ? 1 : -1 :  rsima > rsima[1] ?  1 : -1 //"advance" : "decline"
    rsi_trend
//
mascreener(src, analysis)=>
    // MOVING AVERAGE
        // Close vs 5D MA : Close higher than 5D MA
        // 5MA vs 14MA.    : 5D MA higher than 14D MA
        // 14MA vs 200MA.  : 14D MA higher than 200D MA
    
    fast_ma = ta.sma(src, fast_ma_period)
    slow_ma = ta.sma(src, slow_ma_period)
    matrend = analysis == MA_CLOSE_TO_5D ? src > fast_ma ? 1 : -1 : fast_ma > slow_ma ? 1 : -1
    matrend
//
macdscreener(src, analysis)=>
    f = ta.sma(src, fastmacdperiod)
    s = ta.sma(src, slowmacdperiod)
    macd = f-s
    sign = ta.sma(macd, signalperiod)

    macdtrend = (
         analysis == MACDCROSSOVER ? (macd > sign ? 1 : -1) :
         analysis == MACDZEROLINE ? (macd > 0 ? 1 : -1) : 
         (macd > macd[1] ? 1 : -1)
         )
//-----------------------------------------------------------------------------------------------------------------------//

trendanalysis(symbol, type_of_analysis)=>
    screener =  (
         type_of_analysis == RSI_SCREENER ? 
             rsiscreener(getprice(symbol), rsi_selection) : 
         type_of_analysis == MA_SCREENER ? 
             mascreener(getprice(symbol), ma_selection) :
         type_of_analysis == MACD_SCREENER ?
             macdscreener(getprice(symbol), macd_selection) :
             changescreener(getprice(symbol))

         )
//#endregion
//#region - Symbol Input -------------------------------------------------------------------------//

symbol01	=input.symbol(	 'SET:DELTA'	)
symbol02	=input.symbol(	 'SET:AOT'      )
symbol03	=input.symbol(	 'SET:PTT'      )
symbol04	=input.symbol(	 'SET:PTTEP'	)
symbol05	=input.symbol(	 'SET:GULF'     )
symbol06	=input.symbol(	 'SET:CPALL'	)
symbol07	=input.symbol(	 'SET:ADVANC'   )
symbol08	=input.symbol(	 'SET:BDMS'	    )
symbol09	=input.symbol(	 'SET:SCC'      )
symbol10	=input.symbol(	 'SET:SCB'      )
symbol11	=input.symbol(	 'SET:KBANK'    )
symbol12	=input.symbol(	 'SET:EA'       )
symbol13	=input.symbol(	 'SET:CPN'	    )
symbol14	=input.symbol(	 'SET:BBL'      )
symbol15	=input.symbol(	 'SET:OR'       )
symbol16	=input.symbol(	 'SET:CRC'      )
symbol17	=input.symbol(	 'SET:KTB'      )
symbol18	=input.symbol(	 'SET:INTUCH'   )
symbol19	=input.symbol(	 'SET:IVL'	    )
symbol20	=input.symbol(	 'SET:SCGP'     )
symbol21	=input.symbol(	 'SET:PTTGC'	)
symbol22	=input.symbol(	 'SET:CPF'  	)
symbol23	=input.symbol(	 'SET:GPSC'	    )
symbol24	=input.symbol(	 'SET:AWC'	    )
symbol25	=input.symbol(	 'SET:HMPRO'	)
symbol26	=input.symbol(	 'SET:MINT' 	)
symbol27	=input.symbol(	 'SET:BH'       )
symbol28	=input.symbol(	 'SET:TRUE' 	)
symbol29	=input.symbol(	 'SET:BEM'      )
symbol30	=input.symbol(	 'SET:KTC'  	)
symbol31	=input.symbol(	 'SET:TTB'      )
symbol32	=input.symbol(	 'SET:TOP'      )
symbol33	=input.symbol(	 'SET:DTAC'     )
symbol34	=input.symbol(	 'SET:LH'	    )
symbol35	=input.symbol(	 'SET:BTS'      )
symbol36	=input.symbol(	 'SET:BGRIM'    )
symbol37	=input.symbol(	 'SET:BANPU'    )
symbol38	=input.symbol(	 'SET:CBG'      )
symbol39	=input.symbol(	 'SET:GLOBAL'	)
symbol40	=input.symbol(	 'SET:RATCH'	)
// symbol41	=input.symbol(	 'SET:EGCO''	)
// symbol42	=input.symbol(	 'SET:OSP')
// symbol43	=input.symbol(	 'SET:TISCO')
// symbol44	=input.symbol(	 'SET:MTC'	)
// symbol45	=input.symbol(	 'SET:JMT'R'	)
// symbol46	=input.symbol(	 'SET:TU''	)
// symbol47	=input.symbol(	 'SET:COM7')
// symbol48	=input.symbol(	 'SET:TIDLOR'	)
// symbol49	=input.symbol(	 'SET:CENTEL')
// symbol50	=input.symbol(	 'SET:JMART')

//#endregion 
//#region - Calculation --------------------------------------------------------------------------//

// trendanalysis(symbol, type_of_analysis)
trend01	 =	 trendanalysis(	symbol01, analysis_type)
trend02	 =	 trendanalysis(	symbol02, analysis_type)
trend03	 =	 trendanalysis(	symbol03, analysis_type)
trend04	 =	 trendanalysis(	symbol04, analysis_type)
trend05	 =	 trendanalysis(	symbol05, analysis_type)
trend06	 =	 trendanalysis(	symbol06, analysis_type)
trend07	 =	 trendanalysis(	symbol07, analysis_type)
trend08	 =	 trendanalysis(	symbol08, analysis_type)
trend09	 =	 trendanalysis(	symbol09, analysis_type)
trend10	 =	 trendanalysis(	symbol10, analysis_type)
trend11	 =	 trendanalysis(	symbol11, analysis_type)
trend12	 =	 trendanalysis(	symbol12, analysis_type)
trend13	 =	 trendanalysis(	symbol13, analysis_type)
trend14	 =	 trendanalysis(	symbol14, analysis_type)
trend15	 =	 trendanalysis(	symbol15, analysis_type)
trend16	 =	 trendanalysis(	symbol16, analysis_type)
trend17	 =	 trendanalysis(	symbol17, analysis_type)
trend18	 =	 trendanalysis(	symbol18, analysis_type)
trend19	 =	 trendanalysis(	symbol19, analysis_type)
trend20	 =	 trendanalysis(	symbol20, analysis_type)
trend21	 =	 trendanalysis(	symbol21, analysis_type)
trend22	 =	 trendanalysis(	symbol22, analysis_type)
trend23	 =	 trendanalysis(	symbol23, analysis_type)
trend24	 =	 trendanalysis(	symbol24, analysis_type)
trend25	 =	 trendanalysis(	symbol25, analysis_type)
trend26	 =	 trendanalysis(	symbol26, analysis_type)
trend27	 =	 trendanalysis(	symbol27, analysis_type)
trend28	 =	 trendanalysis(	symbol28, analysis_type)
trend29	 =	 trendanalysis(	symbol29, analysis_type)
trend30	 =	 trendanalysis(	symbol30, analysis_type)
trend31	 =	 trendanalysis(	symbol31, analysis_type)
trend32	 =	 trendanalysis(	symbol32, analysis_type)
trend33	 =	 trendanalysis(	symbol33, analysis_type)
trend34	 =	 trendanalysis(	symbol34, analysis_type)
trend35	 =	 trendanalysis(	symbol35, analysis_type)
trend36	 =	 trendanalysis(	symbol36, analysis_type)
trend37	 =	 trendanalysis(	symbol37, analysis_type)
trend38	 =	 trendanalysis(	symbol38, analysis_type)
trend39	 =	 trendanalysis(	symbol39, analysis_type)
trend40	 =	 trendanalysis(	symbol40, analysis_type)

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

trend_count = (trend01	+trend02	+ trend03	+trend04	+ trend05	+
     trend06	+trend07	+trend08	+trend09	+trend10	+trend11	+trend12	+trend13	+
     trend14	+trend15	+trend16	+trend17	+trend18	+trend19	+trend20	+trend21	+
     trend22	+trend23	+trend24	+trend25	+trend26	+trend27	+trend28	+trend29	+
     trend30	+trend31	+trend32	+trend33	+trend34	+trend35	+trend36	+trend37	+
     trend38	+trend39	+trend40		)
trend_ma = ta.rma(trend_count, 9)

positive = (40 + trend_count)/2
negative = 40 - positive
//#endregion
//#region - Plot ---------------------------------------------------------------------------------//

pos_line = plot(positive, title = "positive", color = color.green, style = plot.style_columns)
neg_line = plot(negative, title = "ngetive",  color = color.red)

base_line = plot(0, title = "")
plot(ta.rma(trend_count,5), title = "Trend Cumulative", color= color.gray)
plot(trend_ma, title = "Trend MA", color = trend_count > trend_ma ? color.rgb(0, 255, 8) : color.rgb(255, 6, 6))
fill(pos_line, base_line, color = POSITIVE_COLOR)
fill(neg_line, base_line, color=NEGATIVE_COLOR   )
// // plot(positive)

hline(20)
hline(40)
hline(-40)
hline(-20)

//#endregion
//------------------------------------------------------------------------------------------------//


