MarketSmith Daily Market Indicators
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Amphibiantrading

//@version=5
indicator("Marketsmith Daily Market Indicators", shorttitle = 'MSDI')

//----------inputs----------//
index       = input.string('Nasdaq', 'Index for Data', options = ['Nasdaq', 'NYSE'])
dataPlots   = input.string('Advance/Decline Line', 'Data to Plot', options = ['Advance/Decline Line', 'Overbought / Oversold Oscillator', '10 Day Average Up / Down Volume', '10 Day Average New Highs / Lows'])
water       = input.bool(false, 'Show Watermark', inline = '1')
waterCol    = input.color(color.gray, ' ', inline = '1')

var g1      = 'Data Table'
showTable   = input.bool(true, 'Show Data Table', group = g1)
yPos        = input.string('Top', 'Table Position ', options = ['Top', 'Middle', 'Bottom'], inline = '1', group = g1)
xPos        = input.string('Left', ' ', options = ['Right','Center', 'Left'], inline = '1', group = g1)
bgCol       = input.color(color.gray, 'Cell Color', inline = '2', group = g1)
txtCol      = input.color(color.white,'Text Color', inline = '2', group = g1)

var g2      = 'Other Options'
showNHL     = input.bool(false, 'Show Daily Net New Highs / Lows Histogram', group = g2)
showMa      = input.bool(false, 'Show Advance Decline Line Moving Average', group = g2)
maLen       = input.int(10, 'Moving Average', inline = '1', group = g2)
maType      = input.string('SMA', ' ', ['SMA', 'EMA'], inline = '1', group = g2)
maOffset    = input.int(0, 'Offset', inline = '2', group = g2)
maCol       = input.color(color.red, ' ', inline = '2', group = g2)

//----------functions----------//
getInfo(sym)=>
    request.security(sym, 'D', close)

adlCalc(difference)=> 
    ta.cum(difference > 0 ? math.sqrt(difference) : -math.sqrt(-difference))

//----------requests----------//
//----------nasdaq data----------//
advNas  = getInfo('ADVN.NQ')
decNas  = getInfo('DECL.NQ')
unchNas = getInfo('UNCH.NQ')
uVolNas = getInfo('UVOLQ')
dVolNas = getInfo('DVOLQ')
tVolNas = getInfo('TVOLQ')
hq      = getInfo('HIGQ')
lq      = getInfo('LOWQ')

//----------nyse data----------//
advNyse  = getInfo('ADVN.NY')
decNyse  = getInfo('DECL.NY')
unchNyse = getInfo('UNCH.NY')
uVolNyse = getInfo('UVOL')
dVolNyse = getInfo('DVOL')
tVolNyse = getInfo('TVOL')
hn       = getInfo('HIGN')
ln       = getInfo('LOWN')

//----------advance decline lines----------//
adlNY   = request.security("(USI:ADVN.NY - USI:DECL.NY) / (USI:UNCH.NY + 1)", 'D', adlCalc(close))
adlNAS  = request.security("(USI:ADVN.NQ - USI:DECL.NQ) / (USI:UNCH.NQ + 1)", 'D', adlCalc(close))
ma      = index == 'Nasdaq' ? maType == 'SMA' ? ta.sma(adlNAS,maLen) : ta.ema(adlNAS, maLen) : maType == 'SMA' ? ta.sma(adlNY,maLen) : ta.ema(adlNY,maLen)

//----------overbough oversold oscillator----------//
advDecNas   = advNas - decNas
advDecNyse  = advNyse - decNyse
obos        = index == 'Nasdaq' ? ta.sma(advDecNas,10) : ta.sma(advDecNyse,10)

//----------up and down volume----------//
upMa = index == 'Nasdaq' ? ta.sma(uVolNas,10) : ta.sma(uVolNyse,10)
dnMa = index == 'Nasdaq' ? ta.sma(dVolNas,10) : ta.sma(dVolNyse,10)

//----------new highs/lows----------//
nhMa = index == 'Nasdaq' ? ta.sma(hq,10) : ta.sma(hn,10)
nlMa = index == 'Nasdaq' ? ta.sma(lq,10) : ta.sma(ln,10)
nhl  = index == 'Nasdaq' ? hq - lq : hn - ln

//----------data table----------//
var table data = table.new(str.lower(yPos) + '_' + str.lower(xPos), 2, 4, color.new(color.white,100), color.new(color.white,100), 1, color.new(color.white,100), 1)

if barstate.islast and showTable
    data.cell(0,0, '')
    data.merge_cells(0,0,1,0)
    data.cell(0,1, index == 'Nasdaq' ? str.tostring(advNas) + ' Advanced on ' + str.tostring(uVolNas) : str.tostring(advNyse) + ' Advanced on ' + str.tostring(uVolNyse), bgcolor = bgCol, text_color = txtCol)
    data.cell(0,2, index == 'Nasdaq' ? str.tostring(decNas) + ' Declined on ' + str.tostring(dVolNas) : str.tostring(decNyse) + ' Declined on ' + str.tostring(dVolNyse), bgcolor = bgCol, text_color = txtCol)
    data.cell(0,3, index == 'Nasdaq' ? str.tostring(unchNas) + ' Unchanged on ' + str.tostring(tVolNas - uVolNas - dVolNas) :  str.tostring(unchNyse) + ' Unchanged on ' + str.tostring(tVolNyse - uVolNyse - dVolNyse), bgcolor = bgCol, text_color = txtCol)
    data.cell(1,1, index == 'Nasdaq' ? str.tostring(hq) + ' Made New Highs' : str.tostring(hn) + ' Made New Highs', bgcolor = bgCol, text_color = txtCol)
    data.cell(1,2, index == 'Nasdaq' ? str.tostring(lq) + ' Made New Lows' : str.tostring(ln) + ' Made New Lows', bgcolor = bgCol, text_color = txtCol)

//----------plots----------//
plot(dataPlots == 'Advance/Decline Line' ? index == 'Nasdaq' ? adlNAS * (maOffset + 1)  : adlNY * (maOffset + 1) : na, 'Advance/Decline Line', color.blue, 2)
plot(showMa and dataPlots == 'Advance/Decline Line' ? ma * (maOffset + 1) : na, 'Advance/Decline Line Moving Average', maCol)
plot(dataPlots == 'Overbought / Oversold Oscillator' ? 0 : na, 'Zero Line', color = color.red, display = display.pane)
plot(dataPlots == 'Overbought / Oversold Oscillator' ? obos : na, 'Overbought / Oversold Oscillator', color.blue, 2)
plot(dataPlots == '10 Day Average Up / Down Volume' ? upMa : na, 'Up Volume', color.blue, 2)
plot(dataPlots == '10 Day Average Up / Down Volume' ? dnMa : na, 'Down Volume', color.red, 2)
plot(dataPlots == '10 Day Average New Highs / Lows' ? nhMa : na, 'New Highs', color.blue, 2)
plot(dataPlots == '10 Day Average New Highs / Lows' ? nlMa : na, 'New Lows', color.red, 2)
plot(dataPlots == '10 Day Average New Highs / Lows' and showNHL ? nhl : na, 'Daily Net Highs / Lows', nhl > 0 ? color.green : color.red, 2, style = plot.style_histogram)

//----------watermark----------//
var table waterM = table.new(position.top_center, 1, 1, color.new(color.white,100), color.new(color.white,100), 0, color.new(color.white,100), 0)
waterTxt  = dataPlots == 'Advance/Decline Line' ? 'Advance Decline Line' : dataPlots == 'Overbought / Oversold Oscillator' ? 'Overbought / Oversold Oscillator' : dataPlots == '10 Day Average Up / Down Volume' ? '10 Day Average Up / Down Volume' : '10 Day Average New Highs / Lows'

if water
    waterM.cell(0, 0, waterTxt, text_color = waterCol)