Cumulative New Highs - New Lows
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © F30G20

//@version=5
indicator('Cumulative New Highs - New Lows', shorttitle='Cum. Hi-Lo', overlay=false, precision=0)
 
//Inputs
indexChoice = input.string(title='Index', defval='Total Stock Market', options=['Total Stock Market' , 'NYSE Composite' , 'Nasdaq Composite' , 'S&P 500', 'Nasdaq 100' , 'Russell 2000'])
maLength = input(20, title='Moving Average Length')

//Data & Calculation
indexData = indexChoice == 'Nasdaq 100'      ? request.security('NADC', 'D', close) :
           indexChoice == 'S&P 500'          ? request.security('MADP', 'D', close) :
           indexChoice == 'NYSE Composite'   ? request.security('MAHN', 'D', close) - request.security('MALN', 'D', close) :
           indexChoice == 'Nasdaq Composite' ? request.security('MAHQ', 'D', close) - request.security('MALQ', 'D', close) :
           indexChoice == 'Russell 2000'     ? request.security('MADC', 'D', close) : request.security('MAHX', 'D', close) - request.security('MALX', 'D', close)


cumulativeIndex = ta.cum(indexData)
lineColor = cumulativeIndex > cumulativeIndex[1] ? color.green : cumulativeIndex < cumulativeIndex[1] ? color.red : color.gray

movingAverage = ta.sma(cumulativeIndex, maLength)

//Plots
plot(cumulativeIndex, title='Cumulative Index Line', color=lineColor)
plot(movingAverage, title='Moving Average', color=color.rgb(33, 149, 243, 40))
