Cumulative NHNL (markets, sectors, lookbacks)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © StevenM85

//@version=5
indicator("Cumulative NHNL")


//@version=5

var gref = 'Cum NHNL'

sector_full = input.string(defval='NDXcomp', title='', options=['NYSE','SPX', 'NDXcomp', 'NDX100','IWM','DJI','DJT','SP Growth','SP Value', 'XLK', 'XLF', 'XLV', 'XLE', 'XLB', 'XLC', 'XLY', 'XLP', 'XLRE', 'XLI', 'XLU'], inline='1', group=gref)
timeframe_full = input.string(defval='1-month', title='NHNL', options=['5-day', '1-month', '3-month','6-month','12-month','YTD'], inline='1', group=gref)
NHNLfirst=sector_full=='NDX100'?'N':sector_full=='DJI'?'N':sector_full=='DJT'?'N':sector_full=='SP Growth'?'N':sector_full=='SP Value'?'N':'M'
NHNLsecond=timeframe_full=='5-day'?'5':timeframe_full=='1-month'?'1':timeframe_full=='3-month'?'3':timeframe_full=='6-month'?'6':timeframe_full=='12-month'?'A':'Y'
NHNLfourth=sector_full=='NYSE'?'N':sector_full=='SPX'?'P':sector_full == 'NDXcomp'?'Q':sector_full=='NDX100'?'C':sector_full=='IWM'?'C':sector_full=='DJI'?'D':sector_full=='DJT'?'F':sector_full=='SP Growth'?'Z':sector_full=='SP Value'?'Y':sector_full=='XLK'?'K':sector_full=='XLF'?'F':sector_full=='XLV'?'S':sector_full=='XLE'?'T':sector_full=='XLB'?'B':sector_full=='XLC'?'W':sector_full=='XLY'?'Y':sector_full=='XLP'?'M':sector_full=='XLRE'?'R':sector_full=='XLI'?'I':'U'


//ticker = input.string(title='Exchange', defval='NDX', options=['NDX', 'NYSE'])
smaSshow=input(title='', defval=true, inline='1')
smaS=input(title='', defval=9, inline='1')
smaStype=input.string(title='', defval='EMA', options=['SMA','EMA'], inline='1')
smaScolor=input.color(color.green, inline='1', title='')
smaSwidth=input.int(defval=2, inline='1', title='')
smaLshow=input(title='', defval=true, inline='2')
smaL=input(title='', defval=50, inline='2')
smaLtype=input.string(title='', defval='SMA', options=['SMA','EMA'], inline='2')
smaLcolor=input.color(color.orange, inline='2',title='')
smaLwidth=input.int(defval=2, inline='2',title='')

highs=request.security(NHNLfirst+NHNLsecond+'H'+NHNLfourth, 'D', close)
lows=request.security(NHNLfirst+NHNLsecond+'L'+NHNLfourth, 'D', close)



//highs=request.security(ticker=='NDX'? "HIGQ":"HIGN", 'D', close)
//lows=request.security(ticker=='NDX'? "LOWQ":"LOWN", 'D', close)
diff=highs-lows
cumdiff = ta.cum(diff)
ma10=smaStype=='SMA'?ta.sma(cumdiff, smaS):ta.ema(cumdiff, smaS)
ma50=smaLtype=='SMA'?ta.sma(cumdiff, smaL):ta.ema(cumdiff, smaL)
plot(cumdiff, color=color.black)
plot(ma10, color=color.new(smaScolor, transp=smaSshow==true?0:100), linewidth = smaSwidth)
plot(ma50, color=color.new(smaLcolor, transp=smaLshow==true?0:100),linewidth = smaLwidth)


// --- PLOT THE TABLE ---
var table newHighLowTable = table.new(position.middle_left, 2, 1, border_width=1, border_color=color.rgb(209, 212, 219, 0))
i_plotTable = true
//Function
f_fillCell(_table, _column, _row, _value) =>
    _c_color = color.rgb(54, 58, 69, 0)
    _transp = 0
    _cellText = str.tostring(_value, '0')
    table.cell(_table, _column, _row, _cellText, bgcolor=color.rgb(209, 212, 219, 0), text_color=color.rgb(54, 58, 69, 0), text_size=size.normal)

// Diplay
if i_plotTable

    table.cell(newHighLowTable, 0, 0, text=timeframe_full + ' ' + sector_full, bgcolor=color.rgb(209, 212, 219, 0), text_color=color.rgb(54, 58, 69, 0), text_size=size.normal)
    table.cell(newHighLowTable, 1, 0, text='', bgcolor=color.white, text_color=color.rgb(54, 58, 69, 0), text_size=size.normal)





