Index Breadth Percent of Stocks above Key Moving Averages
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Esti0000

//@version=5
indicator("Index Breadth Percent of Stocks above Key Moving Averages", shorttitle = '% Breadth')

//User Inputs
market = input.string(defval="SP500", title="Select Market", options=["Overall", "SP500", "DJI", "Nasdaq", "Nasdaq100", "Russel2000", "Russel3000",
     "SP500 Financials", "SP500 Materials", "SP500 Energy", "SP500 Staples", "SP500 Discretionary", "SP500 Industrials", "SP500 Real Estate", 
     "SP500 Utilities", "SP500 Technologies", "SP500 Heath Care"], group = 'Select Market')
dma = input.string(defval="20", title="Select DMA", options=["20", '50', "100", "150", "200"], group = 'Select Moving Average')
greed = input.int(defval = 80, title = 'Select Greed Level', group = 'Select Greed and Fear Zone')
fear = input.int(defval = 20, title = 'Select Fear Level', group = 'Select Greed and Fear Zone')

//Ticker Selection Criteria
ticker = ("Overall" == market)           ? ("200" == dma) ? "MMTH" : ("150" == dma) ? "MMOF" : ("100" == dma) ? "MMOH" : ("50" == dma) ? "MMFI" : "MMTW" : 
     ("SP500" == market)                 ? ("200" == dma) ? "S5TH" : ("150" == dma) ? "S5OF" : ("100" == dma) ? "S5OH" : ("50" == dma) ? "S5FI" : "S5TW" :
     ("DJI" == market)                   ? ("200" == dma) ? "DITH" : ("150" == dma) ? "DIOF" : ("100" == dma) ? "DIOH" : ("50" == dma) ? "DIFI" : "DITW" :
     ("Nasdaq" == market)                ? ("200" == dma) ? "NCTH" : ("150" == dma) ? "NCOF" : ("100" == dma) ? "NCOH" : ("50" == dma) ? "NCFI" : "NCTW" :
     ("Nasdaq100" == market)             ? ("200" == dma) ? "NDTH" : ("150" == dma) ? "NDOF" : ("100" == dma) ? "NDOH" : ("50" == dma) ? "NDFI" : "NDTW" :
     ("Russel2000" == market)            ? ("200" == dma) ? "R2TH" : ("150" == dma) ? "R2OF" : ("100" == dma) ? "R2OH" : ("50" == dma) ? "R2FI" : "R2TW" :
     ("Russel3000" == market)            ? ("200" == dma) ? "R3TH" : ("150" == dma) ? "R3OF" : ("100" == dma) ? "R3OH" : ("50" == dma) ? "R3FI" : "R3TW" :
     ("SP500 Financials" == market)      ? ("200" == dma) ? "SFTH" : ("150" == dma) ? "SFOF" : ("100" == dma) ? "SFOH" : ("50" == dma) ? "SFFI" : "SFTW" :
     ("SP500 Materials" == market)       ? ("200" == dma) ? "SBTH" : ("150" == dma) ? "SBOF" : ("100" == dma) ? "SBOH" : ("50" == dma) ? "SBFI" : "SBTW" :
     ("SP500 Energy" == market)          ? ("200" == dma) ? "SETH" : ("150" == dma) ? "SEOF" : ("100" == dma) ? "SEOH" : ("50" == dma) ? "SEFI" : "SETW" :
     ("SP500 Staples" == market)         ? ("200" == dma) ? "SPTH" : ("150" == dma) ? "SPOF" : ("100" == dma) ? "SPOH" : ("50" == dma) ? "SPFI" : "SPTW" :
     ("SP500 Discretionary" == market)   ? ("200" == dma) ? "SYTH" : ("150" == dma) ? "SYOF" : ("100" == dma) ? "SYOH" : ("50" == dma) ? "SYFI" : "SYTW" :
     ("SP500 Industrials" == market)     ? ("200" == dma) ? "SITH" : ("150" == dma) ? "SIOF" : ("100" == dma) ? "SIOH" : ("50" == dma) ? "SIFI" : "SITW" :
     ("SP500 Real Estate" == market)     ? ("200" == dma) ? "SSTH" : ("150" == dma) ? "SSOF" : ("100" == dma) ? "SSOH" : ("50" == dma) ? "SSFI" : "SSTW" :
     ("SP500 Utilities" == market)       ? ("200" == dma) ? "SUTH" : ("150" == dma) ? "SUOF" : ("100" == dma) ? "SUOH" : ("50" == dma) ? "SUFI" : "SUTW" :
     ("SP500 Technologies" == market)    ? ("200" == dma) ? "SKTH" : ("150" == dma) ? "SKOF" : ("100" == dma) ? "SKOH" : ("50" == dma) ? "SKFI" : "SKTW" :
     ("SP500 Heath Care" == market)      ? ("200" == dma) ? "SVTH" : ("150" == dma) ? "SVOF" : ("100" == dma) ? "SVOH" : ("50" == dma) ? "SVFI" : "SVTW" :
     na


percentage_above_ma = request.security(ticker, timeframe.period, close)

//Plots
p_above = plot(percentage_above_ma)
greed_p = plot(greed, color = color.white)
fear_p = plot(fear, color = color.white)

//logic for painting above/below Key Moving Averages
fear_plot = plot(percentage_above_ma < fear ? fear : na, display = display.none)
greed_plot = plot(percentage_above_ma > greed ? greed : na, display = display.none)
fill(fear_plot, p_above, color = color.green)
fill(greed_plot, p_above, color = color.red)