Improved Lowry Up-Down Volume + Stocks Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jlb05013

//@version=5
indicator("Improved Lowry Up-Down Volume + Stocks Indicator")

nyse_upvol      = request.security('UPVOL.NY', timeframe.period, close)
nyse_dnvol      = request.security('DNVOL.NY', timeframe.period, close)
nq_upvol        = request.security('UPVOL.NQ', timeframe.period, close)
nq_dnvol        = request.security('DNVOL.NQ', timeframe.period, close)
dj_upvol        = request.security('UPVOL.DJ', timeframe.period, close)
dj_dnvol        = request.security('DNVOL.DJ', timeframe.period, close)

nyse_upstocks   = request.security('ADVN', timeframe.period, close)
nyse_dnstocks   = request.security('DECN', timeframe.period, close)
nq_upstocks     = request.security('ADVN.NQ', timeframe.period, close)
nq_dnstocks     = request.security('DECL.NQ', timeframe.period, close)
dj_upstocks     = request.security('ADVN.DJ', timeframe.period, close)
dj_dnstocks     = request.security('DECL.DJ', timeframe.period, close)

// get input from the user on whether they would like to plot volume or stocks, and give a choice on which exchange to pick from
input_exchanges = input.string(defval='NYSE', title='Which Exchanges?', options=['NYSE', 'NASDAQ', 'NYSE + NASDAQ', 'DJ', 'NYSE + NASDAQ + DJ'], tooltip='Default is NYSE')
input_unit      = input.string(defval='Volume', title='Volume or Stocks?', options=['Volume', 'Stocks'], tooltip="Default is Volume")

// return the values to use, either volume or stocks, based on user inputs
nyse_up_unit    = input_unit == 'Volume' ? nyse_upvol : nyse_upstocks
nyse_dn_unit    = input_unit == 'Volume' ? nyse_dnvol : nyse_dnstocks
nq_up_unit      = input_unit == 'Volume' ? nq_upvol : nq_upstocks
nq_dn_unit      = input_unit == 'Volume' ? nq_dnvol : nq_dnstocks
dj_up_unit      = input_unit == 'Volume' ? dj_upvol : dj_upstocks
dj_dn_unit      = input_unit == 'Volume' ? dj_dnvol : dj_dnstocks

// perform the calculations based on user input
hist_up         = input_exchanges == 'NYSE' ? nyse_up_unit / (nyse_up_unit + nyse_dn_unit) : 
                     input_exchanges == 'NASDAQ' ? nq_up_unit / (nq_up_unit + nq_dn_unit) : 
                     input_exchanges == 'DJ' ? dj_up_unit / (dj_up_unit + dj_dn_unit) : 
                     input_exchanges == 'NYSE + NASDAQ' ? (nyse_up_unit + nq_up_unit) / (nyse_up_unit + nyse_dn_unit + nq_up_unit + nq_dn_unit) :
                     (nyse_up_unit + nq_up_unit + dj_up_unit) / (nyse_up_unit + nyse_dn_unit + nq_up_unit + nq_dn_unit + dj_up_unit + dj_dn_unit)

hist_dn         = input_exchanges == 'NYSE' ?
                     -nyse_dn_unit / (nyse_up_unit + nyse_dn_unit) :
                     input_exchanges == 'NASDAQ' ?
                     -nq_dn_unit / (nq_up_unit + nq_dn_unit) :
                     input_exchanges == 'DJ' ?
                     -dj_dn_unit / (dj_up_unit + dj_dn_unit) :
                     input_exchanges == 'NYSE + NASDAQ' ? 
                     -(nyse_dn_unit + nq_dn_unit) / (nyse_up_unit + nyse_dn_unit + nq_up_unit + nq_dn_unit) :
                     -(nyse_dn_unit + nq_dn_unit + dj_dn_unit) / (nyse_up_unit + nyse_dn_unit + nq_up_unit + nq_dn_unit + dj_up_unit + dj_dn_unit)


// create logic that will present a down arrow when there are 90% down days, and an up arrow when there is either a 90% up day or two back-to-back 80% up days
dn_arrow      = hist_dn <= -0.9
up_arrow        = hist_up >= 0.9 or (hist_up[0] >= 0.8 and hist_up[1] >= 0.8)

// create plots
plot(hist_up, title='Up %', color=color.new(color.green, 40), style=plot.style_histogram, linewidth=3)
plot(hist_dn, title='Down %', color=color.new(color.red, 40), style=plot.style_histogram, linewidth=3)

// add horizontal reference lines for positive and negative 90%
hline(0.9, linestyle=hline.style_solid)
hline(-0.9, linestyle=hline.style_solid)

// create logic that will present a down arrow when there are 90% down days, and an up arrow when there is either a 90% up day or two back-to-back 80% up days
plotshape(up_arrow, title='Up Trigger', style=shape.triangleup, location=location.top, color=color.new(color.green, 0), size=size.small)
plotshape(dn_arrow, title='Down Trigger', style=shape.triangledown, location=location.bottom, color=color.new(color.red, 0), size=size.small)


