Breadth - Advance Decline Thrust
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © andr3w321

//@version=5
indicator('Breadth - Advance Decline Thrust')

// cumulative net up volume will show lower values than advance decline thrust on days where there were large buy/sell imbalances, but the total day's volume was low
// advance decline thrust will show values between 0 to 1

display_as_cum_net_up_volume = input.bool(title="Display as cumulative net up volume", defval=false)
lookbackInput = input.int(title="Cumulative lookback days including today", defval=1, minval = 1, maxval = 4999)
include_ny = input.bool(title="Include NYSE Up Volume?", defval=true)
include_nq = input.bool(title="Include NQ Up Volume?", defval=true)
include_us = input.bool(title="Include US Up Volume?", defval=true)
upper_band_value = input.float(title="Upper band value", defval=0.80)
middle_band_value = input.float(title="Middle band value", defval=0.50)
lower_band_value = input.float(title="Lower band value", defval=0.20)

ny_uvol = request.security('UPVOL.NY', "D", close)
nq_uvol = request.security('UPVOL.NQ', "D", close)
us_uvol = request.security('UPVOL.US', "D", close)

ny_dvol = request.security('DNVOL.NY', "D", close)
nq_dvol = request.security('DNVOL.NQ', "D", close)
us_dvol = request.security('DNVOL.US', "D", close)

cumulative_net_uvol = 0.0
cumulative_dvol = 0.0
cumulative_uvol = 0.0
for i = 0 to lookbackInput - 1
    ny_net_uvol = ny_uvol[i] - ny_dvol[i]
    nq_net_uvol = nq_uvol[i] - nq_dvol[i]
    us_net_uvol = us_uvol[i] - us_dvol[i]
    
    if include_ny
        cumulative_net_uvol += ny_net_uvol
        cumulative_uvol += ny_uvol[i]
        cumulative_dvol += ny_dvol[i]
    if include_nq
        cumulative_net_uvol += nq_net_uvol
        cumulative_uvol += nq_uvol[i]
        cumulative_dvol += nq_dvol[i]
    if include_us
        cumulative_net_uvol += us_net_uvol
        cumulative_uvol += us_uvol[i]
        cumulative_dvol += us_dvol[i]
        
cumulative_net_uvol := cumulative_net_uvol / 10000000 // make y axis more readable
advance_decline_thrust = cumulative_uvol / (cumulative_uvol + cumulative_dvol)

plot(display_as_cum_net_up_volume ? na : advance_decline_thrust, title="Advance Decline Thrust", color=color.blue)
plot(display_as_cum_net_up_volume ? cumulative_net_uvol : na, title="Cumulative Net Up Volume", color=color.purple)

upper_band = hline(upper_band_value, "Upper Band", color=#787B86)
hline(middle_band_value, "Middle Band", color=color.new(#787B86, 50))
lower_band = hline(lower_band_value, "Lower Band", color=#787B86)
fill(upper_band, lower_band, color=color.rgb(126, 87, 194, 90), title="Background Fill")