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

//@version=5
indicator("Market Snapshot", overlay=true)

//chose table position and colors
i_tableypos = input.string("bottom", "Y", inline = "i1", options = ["top", "middle", "bottom"], group="Table Settings")
i_tablexpos = input.string("left", "X", inline = "i1", options = ["right","center", "left"], group="Table Settings")
i_bgcolor = input(defval=color.new(color.orange,65), title='Background Color', inline = "k1")
i_textcolor = input(defval=color.black, title='Text Color', inline = "k1")
i_tablesize = input.string("normal", "Table Size", options = ["small","normal","large","huge"])
marketselect = input.string("Nasdaq", "Select Index", options = ["Nasdaq", "SPX"], group = "Market Data")
imatype = input.string("EMA", "Moving Average 1", options = ["EMA", "SMA"], group = "Market Data", inline = "n1")
imalength = input.int(21, "", minval = 1, step = 1, group = "Market Data", inline = "n1")
imatype1 = input.string("SMA", "Moving Average 2", options = ["EMA", "SMA"], group = "Market Data", inline = "n2")
imalength1 = input.int(50, "", minval = 1, step = 1, group = "Market Data", inline = "n2")
inas = input(true, "Nasdaq", inline = "a1", group = "Net High / Low Data")
inyse = input(false, "NYSE", inline = "a1", group = "Net High / Low Data")


//create var table
var table dashboard = table.new(i_tableypos + "_" +i_tablexpos, 2,7, bgcolor=i_bgcolor,frame_color=color.black, frame_width = 2, border_color = color.black, border_width = 1)

// function to switch index to proper symbols
indexselect = switch marketselect
    "Nasdaq" => "IXIC"
    "SPX" => "SPX"

// info for table cells 
comp = request.security(indexselect, "D", close)
putcall = request.security('PCP', 'D', close)
fiftydayspx = request.security("s5fi", "D", close)
twohundreddayspx = request.security("s5th", "D", close)
fiftydaycomp = request.security("ncfi", "D", close)
twohundreddaycomp = request.security("ncth", "D", close)
ema1 = imatype == "EMA" ? ta.ema(comp,imalength) : imatype == "SMA" ? ta.sma(comp, imalength) : na
sma1 = imatype1 == "EMA" ? ta.ema(comp,imalength1) : imatype1 == "SMA" ? ta.sma(comp, imalength1) : na


//get net highs / lows data
hq = request.security("HIGQ", "D", close)
lq = request.security("LOWQ", "D", close)
hn = request.security("HIGN", "D", close)
ln = request.security("LOWN", "D", close)

//decide which symbols to use in net h/l equation
higq = inas ? hq : 0
lowq = inas ? lq : 0
hign = inyse ? hn : 0
lown = inyse ? ln : 0

// calculate distance to ma's and net high / lows
distema1 = (comp / ema1) - 1
distema2 = (comp / sma1) - 1
nethighlow = hign + higq - lown - lowq

// row titles
Pcalltitle = "Put Call Ratio:"
Nethltitle = "Net Highs / Lows:"
fiftydaytitle = "% of Stocks > 50 day:"
twohundreddaytitle = "% of Stocks > 200 day:"
dist2title = "Distance to " + str.tostring(imalength, "0 ") + imatype + ":"
dist2title1 = "Distance to " + str.tostring(imalength1, "0 ") + imatype1 + ":"


// display table
if (barstate.islast and marketselect == "Nasdaq")
    table(dashboard)
    table.merge_cells(dashboard,0,0,1,0)
    table.cell(dashboard,0,0, "Nasdaq", text_color=i_textcolor, text_size = i_tablesize, text_valign = text.align_center)
    table.cell(dashboard,0,1, Pcalltitle, text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,0,2, Nethltitle, text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,0,3, fiftydaytitle, text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,0,4, twohundreddaytitle, text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,0,5, dist2title, text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,0,6, dist2title1, text_color=i_textcolor,text_size = i_tablesize)

    // table.cell(dashboard,1,0, str.tostring(comp, "##.##"), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,1, str.tostring(putcall), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,2, str.tostring(nethighlow), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,3, str.tostring(fiftydaycomp, "#.##"), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,4, str.tostring(twohundreddaycomp, "#.##"), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,5, str.tostring(distema1, "#.##%"), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,6, str.tostring(distema2, "#.##%"), text_color=i_textcolor,text_size = i_tablesize)

else if (barstate.islast and marketselect == "SPX")
    table(dashboard)
    table.merge_cells(dashboard,0,0,1,0)
    table.cell(dashboard,0,0, "SPX", text_color=i_textcolor, text_size = i_tablesize, text_valign = text.align_center)
    table.cell(dashboard,0,1, Pcalltitle, text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,0,2, Nethltitle, text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,0,3, fiftydaytitle, text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,0,4, twohundreddaytitle, text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,0,5, dist2title, text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,0,6, dist2title1, text_color=i_textcolor,text_size = i_tablesize)

    // table.cell(dashboard,1,0, str.tostring(comp, "##.##"), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,1, str.tostring(putcall), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,2, str.tostring(nethighlow), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,3, str.tostring(fiftydayspx, "#.##"), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,4, str.tostring(twohundreddayspx, "#.##"), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,5, str.tostring(distema1, "#.##%"), text_color=i_textcolor,text_size = i_tablesize)
    table.cell(dashboard,1,6, str.tostring(distema2, "#.##%"), text_color=i_textcolor,text_size = i_tablesize)






