% Stocks Above MA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © marquardt1195

//@version=4

study(title="% Stocks Above MA", shorttitle="Marquardt Above MA")

market = input(defval="Overall", type=input.string, title="Select Market", options=["Overall", "SP500", "DJI", "Nasdaq", "Nasdaq100", "Russel2000", "Russel3000"])
dma = input(defval="20", type=input.string, title="Select DMA", options=["200", "150", "100", "50", "20", "5"])
  
ticker = ("Overall" == market)           ? ("200" == dma) ? "MMTH" : ("150" == dma) ? "MMOF" : ("100" == dma) ? "MMOH" : ("50" == dma) ? "MMFI" : ("5" == dma) ? "MMFD" : "MMTW" : 
     ("SP500" == market)                 ? ("200" == dma) ? "S5TH" : ("150" == dma) ? "S5OF" : ("100" == dma) ? "S5OH" : ("50" == dma) ? "S5FI" : ("5" == dma) ? "S5FD" : "S5TW" :
     ("DJI" == market)                   ? ("200" == dma) ? "DITH" : ("150" == dma) ? "DIOF" : ("100" == dma) ? "DIOH" : ("50" == dma) ? "DIFI" : ("5" == dma) ? "DIFD" : "DITW" :
     ("Nasdaq" == market)                ? ("200" == dma) ? "NCTH" : ("150" == dma) ? "NCOF" : ("100" == dma) ? "NCOH" : ("50" == dma) ? "NCFI" : ("5" == dma) ? "NCFD" : "NCTW" :
     ("Nasdaq100" == market)             ? ("200" == dma) ? "NDTH" : ("150" == dma) ? "NDOF" : ("100" == dma) ? "NDOH" : ("50" == dma) ? "NDFI" : ("5" == dma) ? "NDFD" : "NDTW" :
     ("Russel2000" == market)            ? ("200" == dma) ? "R2TH" : ("150" == dma) ? "R2OF" : ("100" == dma) ? "R2OH" : ("50" == dma) ? "R2FI" : ("5" == dma) ? "R2FD" : "R2TW" :
     ("Russel3000" == market)            ? ("200" == dma) ? "R3TH" : ("150" == dma) ? "R3OF" : ("100" == dma) ? "R3OH" : ("50" == dma) ? "R3FI" : ("5" == dma) ? "R3FD" : "R3TW" :
     na

percentage_above_ma = security(ticker, timeframe.period, close-50)

high = input(color.rgb(30, 107, 255), "Up")
low = input(color.red, "Down")

pam_line = plot(series = percentage_above_ma, style=plot.style_histogram, color=percentage_above_ma > 0 ? color.rgb(30,107,255) : color.rgb(255,30,32), linewidth = 4, title="Above MA")
