Volume as a Percent of Float by 3iau
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Nathan J. Hart (insideandup)
//
// Plot the difference between: 1) the current Chart Volume as a percent of Float or Common Outstanding and 2) the moving average of the same.
// Apply a multiplier to this difference.
// Plot the moving average of this difference with applied multiplier.
// Plot as culumns, line or area.
//
// Created: August 7, 2023
// Last edited: September 4, 2023
//
// @version=5

indicator(title = "Volume as a Percent of Float by 3iau", shorttitle = "Vol % of Float", overlay = false, precision = 2)

// MOVING AVERAGE TYPE

// Tillson's Moving Average

a = 0.618033989 // Tillson applied a = 0.7, here applying the Golden ratio conjugate = |(1-√5)/2| = 0.618033989
a1 = -1 * math.pow(a, 3)
a2 = 3 * math.pow(a, 2) + 3 * math.pow(a, 3)
a3 = -6 * math.pow(a, 2) - 3 * a - 3 * math.pow(a, 3)
a4 = 1 + 3 * a + math.pow(a, 3) + 3 * math.pow(a, 2)

// Moving averages

ma(source, length, type) =>
    type == "SMA" ? ta.sma(source, length) :
     type == "EMA" ? ta.ema(source, length) :
     type == "DEMA" ? 2 * ta.ema(source, length) - ta.ema(ta.ema(source, length), length) :
     type == "TEMA" ? 3 * ta.ema(source, length) - 3 * ta.ema(ta.ema(source, length), length) + ta.ema(ta.ema(ta.ema(source, length), length), length) :
     type == "QEMA" ? 5 * ta.ema(source, length) - 10 * ta.ema(ta.ema(source, length), length) + 10 * ta.ema(ta.ema(ta.ema(source, length), length), length) - 5 * ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length) + ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length) :
     type == "PEMA" ? 8 * ta.ema(source, length) - 28 * ta.ema(ta.ema(source, length), length) + 56 * ta.ema(ta.ema(ta.ema(source, length), length), length) - 70 * ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length) + 56 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length) - 28 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length), length) + 8 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length), length), length) - ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length), length), length), length) :
     type == "ZLEMA" ? ta.ema(source + (source - source[math.round((length - 1) / 2)]), length) :
     type == "T3" ? a1 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length), length) + a2 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length) + a3 * ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length) + a4 * ta.ema(ta.ema(ta.ema(source, length), length), length) :
     type == "HMA" ? ta.hma(source, length) :
     type == "SMMA" ? ta.rma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     type == "VWMA" ? ta.vwma(source, length) :
     type == "SWMA" ? ta.swma(source) :
     na

// SETTINGS GROUP NAMES

group1_name = "Plot the Volume traded as a percent of Float / Outstanding"
group2_name = "Plot the Volume traded as a percent of Float / Outstanding relative a Moving Average of the same values"
tooltip2_text = "This moving average will become the zero line about which Volume as a Percent of Float / Outstanding are plotted. The values below the moving average will plot negative, and values above the moving average will plot positive regardless closing up or down."
group3_name = "Apply a multiplier to the values plotted"
group4_name = "Plot values as columns, area, or continuous line"
group5_name = "Plot a Moving Average of the values plotted"
group6_name = "Plot Moving Averages of the High / Low values--those Above >= / Below < the Moving Average of the values plotted"
group7_name = "Display Volume / Price Percent Change at specified Lookback Periods"
group7b_name = "Display a comparison of Closing Volume / Price to the Moving Average of the same"
group8_name = "Display Exchange data Percent Upside / Downside / Unchanged and Breadth data as Tick or Trin"
tooltip8_text = "The Number of Names that are Advancing ADVN / Declining DECL (reported for NYSE or NASDAQ exchanges, the DJIA30, or all U.S. names). Up-Volume UPVOL / Down-Volume DNVOL of Shares in the group 'ADVN / DECL Number of Names'. ADV / DECL Number of Names Advancing or Declining (not applicabale for 'DJIA' or 'US' selections). UVOL/DVOL Up-Volume or Down-Volume of Shares in the group 'ADV/DECL Number of Names'"


// VARIABLES

show_chart_vol_percent = input.string(defval = "Float", title = "Volume as a percent of", options = ["Float", "Outstanding"], inline = "Chart Volume", group = group1_name)

chart_outstanding_FQ = request.financial(syminfo.tickerid, financial_id = "total_shares_outstanding", period = "FQ", ignore_invalid_symbol = true) // FQ or FY possible
chart_outstanding_FY = request.financial(syminfo.tickerid, financial_id = "total_shares_outstanding", period = "FY", ignore_invalid_symbol = true) // FQ or FY possible
chart_float_FY = request.financial(syminfo.tickerid, financial_id = "float_shares_outstanding", period = "FY", ignore_invalid_symbol = true) // Only FY possible

chart_float = chart_float_FY
chart_outstanding = chart_outstanding_FY
if (chart_outstanding_FY != chart_outstanding_FQ) 
    chart_float := chart_float_FY
    chart_outstanding := chart_outstanding_FY
else
    if (chart_outstanding_FY == chart_outstanding_FQ)
        chart_float := chart_float_FY
        chart_outstanding := chart_outstanding_FQ

total_shares = chart_outstanding
if (show_chart_vol_percent == "Float") 
    total_shares := chart_float
else
    if (show_chart_vol_percent == "Outstanding")
        total_shares := chart_outstanding

// CALCULATIONS

// Volume as a Percent of Float or Commmon Outstanding
// Calculate the Difference between: 1) Current Names's Volume as a Percent of Name's Float or Total Outstanding Shares and 2) the Moving Average of the Same.
// Multiply the difference by a specified "multiplier" value.

chart_vol = volume
chart_vol_percent = 100 * chart_vol / total_shares

show_smoothed_chart_vol_percent = input(defval = false, title = "", inline = "Smoothed Chart Volume", group = group2_name)
smoothed_chart_vol_percent_type = input.string(defval = "EMA", title = "", options = ["SMA", "EMA", "DEMA", "TEMA", "QEMA", "PEMA", "ZLEMA", "T3", "HMA", "SMMA", "WMA", "VWMA", "SWMA"], inline = "Smoothed Chart Volume", group = group2_name)
smoothed_chart_vol_percent_length = input.int(defval = 10, title = "", minval = 2, maxval = 2000, tooltip = tooltip2_text, inline = "Smoothed Chart Volume", group = group2_name)
smoothed_chart_vol_percent = ma(chart_vol_percent, smoothed_chart_vol_percent_length, smoothed_chart_vol_percent_type)

// Float to outstanding ratio 
// NOTES: low floating stock to outstanding shares implies higher number of shares held by insiders or controlling investors; whereas floating stock to outstanding shares implies a higher number shares available for immediate trading.

chart_float_to_outstanding = chart_float / chart_outstanding

// Multiplier 

value_multiplier = input.int(defval = 1, title = "Value multiplier 0 to 1000", minval = 0, maxval = 1000, inline = "Value Multiplier", group = group3_name)

display_chart_vol_percent = 0.00

if (show_smoothed_chart_vol_percent == true) 
    display_chart_vol_percent := value_multiplier * (chart_vol_percent - smoothed_chart_vol_percent)
else
    if (show_smoothed_chart_vol_percent == false)
        display_chart_vol_percent := value_multiplier * (chart_vol_percent)

// PLOTS

// Plot the Difference between: 1) Current Names's Volume as a Percent of Name's Float or Total Outstanding Shares and 2) the Moving Average of the Same.

show_columns_smoothed_chart_vol_percent = input(defval = false, title = "Columns", tooltip = "Color scheme based on current period close vs previous period close (close-to-close) or current period close vs current period open (open-to-close).", inline = "Columns", group = group4_name)
show_area_smoothed_chart_vol_percent = input(defval = false, title = "Area", inline = "Area", group = group4_name)
show_line_smoothed_chart_vol_percent = input(defval = true, title = "Line", inline = "Line", group = group4_name)

//// Plot colors

plotcolor_columns_none_smoothed_chart_vol_percent = display_chart_vol_percent >= 0 ? color.rgb(97, 175, 239, 40) : color.rgb(97, 175, 239, 40)
plotcolor_columns_up_smoothed_chart_vol_percent = display_chart_vol_percent >= 0 ? color.rgb(97, 175, 239, 40) : color.rgb(97, 175, 239, 40)
plotcolor_columns_down_smoothed_chart_vol_percent = display_chart_vol_percent >= 0 ? color.rgb(97, 175, 239, 80) : color.rgb(97, 175, 239, 80)

plotcolor_columns_color_scheme = input.string(defval = "Close-to-Close", title = "Color scheme", options = ["None", "Close-to-Close", "Open-to-Close"], inline = "Color scheme", group = group4_name)

plotcolor_columns_smoothed_chart_vol_percent = plotcolor_columns_up_smoothed_chart_vol_percent
if (plotcolor_columns_color_scheme == "None")
    plotcolor_columns_smoothed_chart_vol_percent := plotcolor_columns_none_smoothed_chart_vol_percent
else
    if (plotcolor_columns_color_scheme == "Close-to-Close" and close >= close[1]) 
        plotcolor_columns_smoothed_chart_vol_percent := plotcolor_columns_up_smoothed_chart_vol_percent
    else
        if (plotcolor_columns_color_scheme == "Close-to-Close" and close < close[1])
            plotcolor_columns_smoothed_chart_vol_percent := plotcolor_columns_down_smoothed_chart_vol_percent
        else
            if (plotcolor_columns_color_scheme == "Open-to-Close" and close >= open)
                plotcolor_columns_smoothed_chart_vol_percent := plotcolor_columns_up_smoothed_chart_vol_percent
            else
                if (plotcolor_columns_color_scheme == "Open-to-Close" and close < open)
                    plotcolor_columns_smoothed_chart_vol_percent := plotcolor_columns_down_smoothed_chart_vol_percent
                

plotcolor_line_smoothed_chart_vol_percent = display_chart_vol_percent >= 0 ? color.rgb(97, 175, 239, 10) : color.rgb(97, 175, 239, 10)
plotcolor_area_smoothed_chart_vol_percent = display_chart_vol_percent >= 0 ? color.rgb(97, 175, 239, 70) : color.rgb(97, 175, 239, 70)

//// Plot

plot_columns_smoothed_chart_vol_percent = plot(show_columns_smoothed_chart_vol_percent ? display_chart_vol_percent : na, "Chart Volume Moving Average", plotcolor_columns_smoothed_chart_vol_percent, style=plot.style_columns)
plot_line_smoothed_chart_vol_percent = plot(show_line_smoothed_chart_vol_percent ? display_chart_vol_percent : na, "Chart Volume Moving Average", plotcolor_line_smoothed_chart_vol_percent, style=plot.style_line)
plot_area_smoothed_chart_vol_percent = plot(show_area_smoothed_chart_vol_percent ? display_chart_vol_percent : na, "Chart Volume Moving Average", plotcolor_area_smoothed_chart_vol_percent, style=plot.style_area)

// Plot Moving Average of All Values

show_smoothed_display_chart_vol_percent = input(defval = true, title = "", inline = "MA Chart Volume", group = group5_name)

smoothed_display_chart_vol_percent_type = input.string(defval = "EMA", title = "", options = ["SMA", "EMA", "DEMA", "TEMA", "QEMA", "PEMA", "ZLEMA", "T3", "HMA", "SMMA", "WMA", "VWMA", "SWMA"], inline = "MA Chart Volume", group = group5_name)
smoothed_display_chart_vol_percent_length = input.int(defval = 10, title = "", minval = 2, maxval = 2000, inline = "MA Chart Volume", group = group5_name)
smoothed_display_chart_vol_percent = ma(display_chart_vol_percent, smoothed_display_chart_vol_percent_length, smoothed_display_chart_vol_percent_type)

plotcolor_smoothed_display_chart_vol_percent01 = display_chart_vol_percent >= 0 ? color.rgb(229, 192, 123, 0) : color.rgb(229, 192, 123, 0)
plot_smoothed_display_chart_vol_percent01 = plot(show_smoothed_display_chart_vol_percent ? smoothed_display_chart_vol_percent : na, "Display Moving Average", plotcolor_smoothed_display_chart_vol_percent01, linewidth = 1, style=plot.style_line)

// Plot Moving Averages of only High/Low Values

chart_vol_percent_high = 0.00
chart_vol_percent_low = 0.00

if (show_smoothed_chart_vol_percent == true)
    chart_vol_percent_high := display_chart_vol_percent >= smoothed_display_chart_vol_percent ? display_chart_vol_percent : na
    chart_vol_percent_low := display_chart_vol_percent <= smoothed_display_chart_vol_percent ? display_chart_vol_percent : na
else
    if (show_smoothed_chart_vol_percent == false)
        chart_vol_percent_high := display_chart_vol_percent >= smoothed_display_chart_vol_percent ? display_chart_vol_percent : na
        chart_vol_percent_low := display_chart_vol_percent <= smoothed_display_chart_vol_percent ? display_chart_vol_percent : na

show_chart_vol_percent_high_ma = input(defval = true, title = "", inline = "High MA", group = group6_name)
chart_vol_percent_high_ma_type = input.string(defval = "EMA", title = "", options = ["SMA", "EMA", "DEMA", "TEMA", "QEMA", "PEMA", "ZLEMA", "T3", "HMA", "SMMA", "WMA", "VWMA", "SWMA"], inline = "High MA", group = group6_name)
chart_vol_percent_high_ma_length = input.int(defval = 10, title = "", minval = 2, maxval = 2000, inline = "High MA", group = group6_name)

show_chart_vol_percent_low_ma = input(defval = true, title = "", inline = "Low MA", group = group6_name)
chart_vol_percent_low_ma_type = input.string(defval = "EMA", title = "", options = ["SMA", "EMA", "DEMA", "TEMA", "QEMA", "PEMA", "ZLEMA", "T3", "HMA", "SMMA", "WMA", "VWMA", "SWMA"], inline = "Low MA", group = group6_name)
chart_vol_percent_low_ma_length = input.int(defval = 10, title = "", minval = 2, maxval = 2000, inline = "Low MA", group = group6_name)

chart_vol_percent_high_ma = ma(chart_vol_percent_high, chart_vol_percent_high_ma_length, chart_vol_percent_high_ma_type)
chart_vol_percent_low_ma = ma(chart_vol_percent_low, chart_vol_percent_low_ma_length, chart_vol_percent_low_ma_type)

plot_chart_vol_percent_high_ma = plot(show_chart_vol_percent_high_ma ? chart_vol_percent_high_ma : na, "Moving Average", color.rgb(97, 175, 239, 80), style=plot.style_line)
plot_chart_vol_percent_low_ma = plot(show_chart_vol_percent_low_ma ? chart_vol_percent_low_ma : na, "Moving Average", color.rgb(97, 175, 239, 80), style=plot.style_line)

show_chart_vol_percent_ma_fill = input(defval = true, title = "High/Low MA Fill", inline = "", group = group6_name)
plot_chart_vol_percent_high_ma_fill = plot(show_chart_vol_percent_ma_fill ? chart_vol_percent_high_ma : na, "Moving Average", color.rgb(97, 175, 239, 90), style=plot.style_line)
plot_chart_vol_percent_low_ma_fill = plot(show_chart_vol_percent_ma_fill ? chart_vol_percent_low_ma : na, "Moving Average", color.rgb(97, 175, 239, 90), style=plot.style_line)
fill(plot1 = plot_chart_vol_percent_low_ma_fill, plot2 = plot_chart_vol_percent_high_ma_fill, title = "fill", editable = true, fillgaps = true, color = color.rgb(97, 175, 239, 90))

// TABLE

// Table display Float or Outstanding

// Table display value = to the actual volume as a precent of float/outstanding regardless the multiplier

table_text_vol_percent_lable = " %f"
if (show_chart_vol_percent == "Float") 
    table_text_vol_percent_lable := " %f"
else
    if (show_chart_vol_percent == "Outstanding")
        table_text_vol_percent_lable := " %o"

table_text_vol_percent_value = display_chart_vol_percent / value_multiplier

// Table display value = to the percent change of volume or price at a specified lookback period

percent_change = input.string(defval = "Price", title = "Percent change", options = ["Volume", "Price"], inline = "Percent Change", group = group7_name)

show_percent_change1 = input(defval = true, title = "", inline = "Volume Percent Change 1", group = group7_name)
lookback1 = input.int(defval = 1, title = "", minval = 1, maxval = 2000, tooltip = "A value of 1 measures change between the current period and the previous period", inline = "Volume Percent Change 1", group = group7_name)

show_percent_change2 = input(defval = true, title = "", inline = "Volume Percent Change 2", group = group7_name)
lookback2 = input.int(defval = 2, title = "", minval = 1, maxval = 2000, inline = "Volume Percent Change 2", group = group7_name)

show_percent_change3 = input(defval = true, title = "", inline = "Volume Percent Change 3", group = group7_name)
lookback3 = input.int(defval = 3, title = "", minval = 1, maxval = 2000, inline = "Volume Percent Change 3", group = group7_name)

show_percent_change4 = input(defval = true, title = "", inline = "Volume Percent Change 4", group = group7_name)
lookback4 = input.int(defval = 4, title = "", minval = 1, maxval = 2000, inline = "Volume Percent Change 4", group = group7_name)

show_percent_change5 = input(defval = true, title = "", inline = "Volume Percent Change 5", group = group7_name)
lookback5 = input.int(defval = 5, title = "", minval = 1, maxval = 2000, inline = "Volume Percent Change 5", group = group7_name)

percent_change_show_ma_comparison = input(defval = false, title = "", inline = "Percent Change MA Compare", group = group7b_name)
percent_change_show_ma_comparison_type = input.string(defval = "EMA", title = "", options = ["SMA", "EMA", "DEMA", "TEMA", "QEMA", "PEMA", "ZLEMA", "T3", "HMA", "SMMA", "WMA", "VWMA", "SWMA"], inline = "Percent Change MA Compare", group = group7b_name)
percent_change_show_ma_comparison_length = input.int(defval = 10, title = "", minval = 2, maxval = 2000, inline = "Percent Change MA Compare", group = group7b_name)

// Sort percent change inputs - Begin

show_percent_change1_new = false
show_percent_change2_new = false
show_percent_change3_new = false
show_percent_change4_new = false
show_percent_change5_new = false

lookback1_new = lookback1
lookback2_new = lookback2
lookback3_new = lookback3
lookback4_new = lookback4
lookback5_new = lookback5

// lookback5_new

if (show_percent_change1 == true and lookback1 > lookback2 and lookback1 > lookback3 and lookback1 > lookback4 and lookback1 > lookback5) 
    lookback5_new := lookback1
    show_percent_change5_new := true
else
    if (show_percent_change2 == true and lookback2 > lookback1 and lookback2 > lookback3 and lookback2 > lookback4 and lookback2 > lookback5)
        lookback5_new := lookback2
        show_percent_change5_new := true
    else
        if (show_percent_change3 == true and lookback3 > lookback1 and lookback3 > lookback2 and lookback3 > lookback4 and lookback3 > lookback5)
            lookback5_new := lookback3
            show_percent_change5_new := true
        else
            if (show_percent_change4 == true and lookback4 > lookback1 and lookback4 > lookback2 and lookback4 > lookback3 and lookback4 > lookback5)
                lookback5_new := lookback4
                show_percent_change5_new := true
            else
                if (show_percent_change5 == true and lookback5 > lookback1 and lookback5 > lookback2 and lookback5 > lookback3 and lookback5 > lookback4)
                    lookback5_new := lookback5
                    show_percent_change5_new := true

// lookback4_new

if (show_percent_change1 == true and ((lookback1 > lookback2 and lookback1 > lookback3 and lookback1 > lookback4 and lookback1 <= lookback5) or (lookback1 > lookback2 and lookback1 > lookback3 and lookback1 > lookback5 and lookback1 <= lookback4) or (lookback1 > lookback2 and lookback1 > lookback4 and lookback1 > lookback5 and lookback1 <= lookback3) or (lookback1 > lookback3 and lookback1 > lookback4 and lookback1 > lookback5 and lookback1 <= lookback2)))
    lookback4_new := lookback1
    show_percent_change4_new := true
else
    if (show_percent_change2 == true and ((lookback2 > lookback1 and lookback2 > lookback3 and lookback2 > lookback4 and lookback2 <= lookback5) or (lookback2 > lookback1 and lookback2 > lookback3 and lookback2 > lookback5 and lookback2 <= lookback4) or (lookback2 > lookback1 and lookback2 > lookback4 and lookback2 > lookback5 and lookback2 <= lookback3) or (lookback2 > lookback3 and lookback2 > lookback4 and lookback2 > lookback5 and lookback2 <= lookback1)))
        lookback4_new := lookback2
        show_percent_change4_new := true
    else
        if (show_percent_change3 == true and ((lookback3 > lookback1 and lookback3 > lookback2 and lookback3 > lookback4 and lookback3 <= lookback5) or (lookback3 > lookback1 and lookback3 > lookback2 and lookback3 > lookback5 and lookback3 <= lookback4) or (lookback3 > lookback1 and lookback3 > lookback4 and lookback3 > lookback5 and lookback3 <= lookback2) or (lookback3 > lookback2 and lookback3 > lookback4 and lookback3 > lookback5 and lookback3 <= lookback1)))
            lookback4_new := lookback3
            show_percent_change4_new := true
        else
            if (show_percent_change4 == true and ((lookback4 > lookback1 and lookback4 > lookback2 and lookback4 > lookback3 and lookback4 <= lookback5) or (lookback4 > lookback1 and lookback4 > lookback2 and lookback4 > lookback5 and lookback4 <= lookback3) or (lookback4 > lookback1 and lookback4 > lookback3 and lookback4 > lookback5 and lookback4 <= lookback2) or (lookback4 > lookback2 and lookback4 > lookback3 and lookback4 > lookback5 and lookback4 <= lookback1)))
                lookback4_new := lookback4
                show_percent_change4_new := true
            else
                if (show_percent_change5 == true and ((lookback5 > lookback1 and lookback5 > lookback2 and lookback5 > lookback3 and lookback5 <= lookback4) or (lookback5 > lookback1 and lookback5 > lookback2 and lookback5 > lookback4 and lookback5 <= lookback3) or (lookback5 > lookback1 and lookback5 > lookback3 and lookback5 > lookback4 and lookback5 <= lookback2) or (lookback5 > lookback2 and lookback5 > lookback3 and lookback5 > lookback4 and lookback5 <= lookback1)))
                    lookback4_new := lookback5
                    show_percent_change4_new := true

// lookback3_new

if (show_percent_change1 == true and ((lookback1 > lookback2 and lookback1 > lookback3 and lookback1 <= lookback4 and lookback1 <= lookback5) or (lookback1 > lookback2 and lookback1 > lookback4 and lookback1 <= lookback3 and lookback1 <= lookback5) or (lookback1 > lookback2 and lookback1 > lookback5 and lookback1 <= lookback3 and lookback1 <= lookback4) or (lookback1 > lookback3 and lookback1 > lookback4 and lookback1 <= lookback2 and lookback1 <= lookback5) or (lookback1 > lookback3 and lookback1 > lookback5 and lookback1 <= lookback2 and lookback1 <= lookback4) or (lookback1 > lookback4 and lookback1 > lookback5 and lookback1 <= lookback2 and lookback1 <= lookback3)))
    lookback3_new := lookback1
    show_percent_change3_new := true
else
    if (show_percent_change2 == true and ((lookback2 > lookback1 and lookback2 > lookback3 and lookback2 <= lookback4 and lookback2 <= lookback5) or (lookback2 > lookback1 and lookback2 > lookback4 and lookback2 <= lookback3 and lookback2 <= lookback5) or (lookback2 > lookback1 and lookback2 > lookback5 and lookback2 <= lookback3 and lookback2 <= lookback4) or (lookback2 > lookback3 and lookback2 > lookback4 and lookback2 <= lookback1 and lookback2 <= lookback5) or (lookback2 > lookback3 and lookback2 > lookback5 and lookback2 <= lookback1 and lookback2 <= lookback4) or (lookback2 > lookback4 and lookback2 > lookback5 and lookback2 <= lookback1 and lookback2 <= lookback3)))
        lookback3_new := lookback2
        show_percent_change3_new := true
    else
        if (show_percent_change3 == true and ((lookback3 > lookback1 and lookback3 > lookback2 and lookback3 <= lookback4 and lookback3 <= lookback5) or (lookback3 > lookback1 and lookback3 > lookback4 and lookback3 <= lookback2 and lookback3 <= lookback5) or (lookback3 > lookback1 and lookback3 > lookback5 and lookback3 <= lookback2 and lookback3 <= lookback4) or (lookback3 > lookback2 and lookback3 > lookback4 and lookback3 <= lookback1 and lookback3 <= lookback5) or (lookback3 > lookback2 and lookback3 > lookback5 and lookback3 <= lookback1 and lookback3 <= lookback4) or (lookback3 > lookback4 and lookback3 > lookback5 and lookback3 <= lookback1 and lookback3 <= lookback2)))
            lookback3_new := lookback3
            show_percent_change3_new := true
        else
            if (show_percent_change4 == true and ((lookback4 > lookback1 and lookback4 > lookback2 and lookback4 <= lookback3 and lookback4 <= lookback5) or (lookback4 > lookback1 and lookback4 > lookback3 and lookback4 <= lookback2 and lookback4 <= lookback5) or (lookback4 > lookback1 and lookback4 > lookback5 and lookback4 <= lookback2 and lookback4 <= lookback3) or (lookback4 > lookback2 and lookback4 > lookback3 and lookback4 <= lookback1 and lookback4 <= lookback5) or (lookback4 > lookback2 and lookback4 > lookback5 and lookback4 <= lookback1 and lookback4 <= lookback3) or (lookback4 > lookback3 and lookback4 > lookback5 and lookback4 <= lookback1 and lookback4 <= lookback2)))
                lookback3_new := lookback4
                show_percent_change3_new := true
            else
                if (show_percent_change5 == true and ((lookback5 > lookback1 and lookback5 > lookback2 and lookback5 <= lookback3 and lookback5 <= lookback4) or (lookback5 > lookback1 and lookback5 > lookback3 and lookback5 <= lookback2 and lookback5 <= lookback4) or (lookback5 > lookback1 and lookback5 > lookback4 and lookback5 <= lookback2 and lookback5 <= lookback3) or (lookback5 > lookback2 and lookback5 > lookback3 and lookback5 <= lookback1 and lookback5 <= lookback4) or (lookback5 > lookback2 and lookback5 > lookback4 and lookback5 <= lookback1 and lookback5 <= lookback3) or (lookback5 > lookback3 and lookback5 > lookback4 and lookback5 <= lookback1 and lookback5 <= lookback2)))
                    lookback3_new := lookback5
                    show_percent_change3_new := true

// lookback2_new

if (show_percent_change1 == true and ((lookback1 > lookback2 and lookback1 <= lookback3 and lookback1 <= lookback4 and lookback1 <= lookback5) or (lookback1 > lookback3 and lookback1 <= lookback2 and lookback1 <= lookback4 and lookback1 <= lookback5) or (lookback1 > lookback4 and lookback1 <= lookback2 and lookback1 <= lookback3 and lookback1 <= lookback5) or (lookback1 > lookback5 and lookback1 <= lookback2 and lookback1 <= lookback3 and lookback1 <= lookback4)))
    lookback2_new := lookback1
    show_percent_change2_new := true
else
    if (show_percent_change2 == true and ((lookback2 > lookback1 and lookback2 <= lookback3 and lookback2 <= lookback4 and lookback2 <= lookback5) or (lookback2 > lookback3 and lookback2 <= lookback1 and lookback2 <= lookback4 and lookback2 <= lookback5) or (lookback2 > lookback4 and lookback2 <= lookback1 and lookback2 <= lookback3 and lookback1 <= lookback5) or (lookback2 > lookback5 and lookback2 <= lookback3 and lookback2 <= lookback3 and lookback2 <= lookback4)))
        lookback2_new := lookback2
        show_percent_change2_new := true
    else
        if (show_percent_change3 == true and ((lookback3 > lookback1 and lookback3 <= lookback2 and lookback3 <= lookback4 and lookback3 <= lookback5) or (lookback3 > lookback2 and lookback3 <= lookback1 and lookback3 <= lookback4 and lookback3 <= lookback5) or (lookback3 > lookback4 and lookback3 <= lookback1 and lookback3 <= lookback2 and lookback3 <= lookback5) or (lookback3 > lookback5 and lookback3 <= lookback1 and lookback3 <= lookback2 and lookback3 <= lookback4)))
            lookback2_new := lookback3
            show_percent_change2_new := true
        else
            if (show_percent_change4 == true and ((lookback4 > lookback1 and lookback4 <= lookback2 and lookback4 <= lookback3 and lookback4 <= lookback5) or (lookback4 > lookback2 and lookback4 <= lookback1 and lookback4 <= lookback3 and lookback4 <= lookback5) or (lookback4 > lookback3 and lookback4 <= lookback1 and lookback4 <= lookback2 and lookback4 <= lookback5) or (lookback4 > lookback5 and lookback4 <= lookback1 and lookback4 <= lookback2 and lookback4 <= lookback3)))
                lookback2_new := lookback4
                show_percent_change2_new := true
            else
                if (show_percent_change5 == true and ((lookback5 > lookback1 and lookback5 <= lookback2 and lookback5 <= lookback3 and lookback5 <= lookback4) or (lookback5 > lookback2 and lookback5 <= lookback1 and lookback5 <= lookback3 and lookback5 <= lookback4) or (lookback5 > lookback3 and lookback5 <= lookback1 and lookback5 <= lookback2 and lookback5 <= lookback4) or (lookback5 > lookback4 and lookback5 <= lookback1 and lookback5 <= lookback2 and lookback5 <= lookback3)))
                    lookback2_new := lookback5
                    show_percent_change2_new := true

// lookback1_new

if (show_percent_change1 == true and lookback1 <= lookback2 and lookback1 <= lookback3 and lookback1 <= lookback4 and lookback1 <= lookback5) 
    lookback1_new := lookback1
    show_percent_change1_new := true
else
    if (show_percent_change2 == true and lookback2 <= lookback1 and lookback2 <= lookback3 and lookback2 <= lookback4 and lookback2 <= lookback5)
        lookback1_new := lookback2
        show_percent_change1_new := true
    else
        if (show_percent_change3 == true and lookback3 <= lookback1 and lookback3 <= lookback2 and lookback3 <= lookback4 and lookback3 <= lookback5)
            lookback1_new := lookback3
            show_percent_change1_new := true
        else
            if (show_percent_change4 == true and lookback4 <= lookback1 and lookback4 <= lookback2 and lookback4 <= lookback3 and lookback4 <= lookback5)
                lookback1_new := lookback4
                show_percent_change1_new := true
            else
                if (show_percent_change5 == true and lookback5 <= lookback1 and lookback5 <= lookback2 and lookback5 <= lookback3 and lookback5 <= lookback4)
                    lookback1_new := lookback5
                    show_percent_change1_new := true

// Sort percent change inputs - End

// Percent change = 100% * (new - old) / old

volume_percent_change1 = lookback1_new > 0 ? 100 * (volume[lookback1_new - 1] - volume[lookback1_new]) / volume[lookback1_new] : 0
price_percent_change1 = lookback1_new > 0 ? 100 * (close[lookback1_new - 1] - close[lookback1_new]) / close[lookback1_new] : 0
volume_percent1_above_ma = 100 * math.abs((volume[lookback1_new - 1] - ma(volume[lookback1_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))/ma(volume[lookback1_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))
volume_percent_change1_ma_comparison = volume[lookback1_new - 1] >= ma(volume[lookback1_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type) ? ", ≥" : ", <"
price_percent1_above_ma = 100 * math.abs((close[lookback1_new - 1] - ma(close[lookback1_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))/ma(close[lookback1_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))
price_percent_change1_ma_comparison = close[lookback1_new - 1] >= ma(close[lookback1_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type) ? ", ≥" : ", <"

volume_percent_change2 = lookback2_new > 0 ? 100 * (volume[lookback2_new - 1] - volume[lookback2_new])/volume[lookback2_new] : 0
price_percent_change2 = lookback2_new > 0 ? 100 * (close[lookback2_new - 1] - close[lookback2_new]) / close[lookback2_new] : 0
volume_percent2_above_ma = 100 * math.abs((volume[lookback2_new - 1] - ma(volume[lookback2_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))/ma(volume[lookback2_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))
volume_percent_change2_ma_comparison = volume[lookback2_new - 1] >= ma(volume[lookback2_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type) ? ", ≥" : ", <"
price_percent2_above_ma = 100 * math.abs((close[lookback2_new - 1] - ma(close[lookback2_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))/ma(close[lookback2_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))
price_percent_change2_ma_comparison = close[lookback2_new - 1] >= ma(close[lookback2_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type) ? ", ≥" : ", <"

volume_percent_change3 = lookback3_new > 0 ? 100 * (volume[lookback3_new - 1] - volume[lookback3_new])/volume[lookback3_new] : 0
price_percent_change3 = lookback3_new > 0 ? 100 * (close[lookback3_new - 1] - close[lookback3_new]) / close[lookback3_new] : 0
volume_percent3_above_ma = 100 * math.abs((volume[lookback3_new - 1] - ma(volume[lookback3_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))/ma(volume[lookback4_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))
volume_percent_change3_ma_comparison = volume[lookback3_new - 1] >= ma(volume[lookback3_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type) ? ", ≥" : ", <"
price_percent3_above_ma = 100 * math.abs((close[lookback3_new - 1] - ma(close[lookback3_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))/ma(close[lookback4_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))
price_percent_change3_ma_comparison = close[lookback3_new - 1] >= ma(close[lookback3_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type) ? ", ≥" : ", <"

volume_percent_change4 = lookback4_new > 0 ? 100 * (volume[lookback4_new - 1] - volume[lookback4_new])/volume[lookback4_new] : 0
price_percent_change4 = lookback4_new > 0 ? 100 * (close[lookback4_new - 1] - close[lookback4_new]) / close[lookback4_new] : 0
volume_percent4_above_ma = 100 * math.abs((volume[lookback4_new - 1] - ma(volume[lookback4_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))/ma(volume[lookback4_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))
volume_percent_change4_ma_comparison = volume[lookback4_new - 1] >= ma(volume[lookback4_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type) ? ", ≥" : ", <"
price_percent4_above_ma = 100 * math.abs((close[lookback4_new - 1] - ma(close[lookback4_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))/ma(close[lookback4_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))
price_percent_change4_ma_comparison = close[lookback4_new - 1] >= ma(close[lookback4_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type) ? ", ≥" : ", <"

volume_percent_change5 = lookback5_new > 0 ? 100 * (volume[lookback5_new - 1] - volume[lookback5_new])/volume[lookback5_new] : 0
price_percent_change5 = lookback5_new > 0 ? 100 * (close[lookback5_new - 1] - close[lookback5_new]) / close[lookback5_new] : 0
volume_percent5_above_ma = 100 * math.abs((volume[lookback5_new - 1] - ma(volume[lookback5_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))/ma(volume[lookback5_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))
volume_percent_change5_ma_comparison = volume[lookback5_new - 1] >= ma(volume[lookback5_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type) ? ", ≥" : ", <"
price_percent5_above_ma = 100 * math.abs((close[lookback5_new - 1] - ma(close[lookback5_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))/ma(close[lookback5_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type))
price_percent_change5_ma_comparison = close[lookback5_new - 1] >= ma(close[lookback5_new - 1], percent_change_show_ma_comparison_length, percent_change_show_ma_comparison_type) ? ", ≥" : ", <"

percent_change1 = price_percent_change1
percent_change2 = price_percent_change2
percent_change3 = price_percent_change3
percent_change4 = price_percent_change4
percent_change5 = price_percent_change5

percent_change1_ma_comparison = price_percent_change1_ma_comparison
percent_change2_ma_comparison = price_percent_change2_ma_comparison
percent_change3_ma_comparison = price_percent_change3_ma_comparison
percent_change4_ma_comparison = price_percent_change4_ma_comparison
percent_change5_ma_comparison = price_percent_change5_ma_comparison

table_text = " % ∆v"
if (percent_change == "Volume") 
    percent_change1 := volume_percent_change1
    percent_change2 := volume_percent_change2
    percent_change3 := volume_percent_change3
    percent_change4 := volume_percent_change4
    percent_change5 := volume_percent_change5
    table_text := " % ∆v"
    percent_change1_ma_comparison := volume_percent_change1_ma_comparison
    percent_change2_ma_comparison := volume_percent_change2_ma_comparison
    percent_change3_ma_comparison := volume_percent_change3_ma_comparison
    percent_change4_ma_comparison := volume_percent_change4_ma_comparison
    percent_change5_ma_comparison := volume_percent_change5_ma_comparison
else
    if (percent_change == "Price")
        percent_change1 := price_percent_change1
        percent_change2 := price_percent_change2
        percent_change3 := price_percent_change3
        percent_change4 := price_percent_change4
        percent_change5 := price_percent_change5
        table_text := " % ∆p"
        percent_change1_ma_comparison := price_percent_change1_ma_comparison
        percent_change2_ma_comparison := price_percent_change2_ma_comparison
        percent_change3_ma_comparison := price_percent_change3_ma_comparison
        percent_change4_ma_comparison := price_percent_change4_ma_comparison
        percent_change5_ma_comparison := price_percent_change5_ma_comparison

table_text1 = percent_change_show_ma_comparison ? " " + str.tostring(math.round(percent_change1, 2)) + table_text + str.tostring(math.round(lookback1_new,0)) + percent_change1_ma_comparison + str.tostring(percent_change_show_ma_comparison_length) + " ma" : " " + str.tostring(math.round(percent_change1, 2)) + table_text + str.tostring(lookback1_new)
table_text2 = percent_change_show_ma_comparison ? " " + str.tostring(math.round(percent_change2, 2)) + table_text + str.tostring(math.round(lookback2_new,0)) + percent_change2_ma_comparison + str.tostring(percent_change_show_ma_comparison_length) + " ma" : " " + str.tostring(math.round(percent_change2, 2)) + table_text + str.tostring(lookback2_new)
table_text3 = percent_change_show_ma_comparison ? " " + str.tostring(math.round(percent_change3, 2)) + table_text + str.tostring(math.round(lookback3_new,0)) + percent_change3_ma_comparison + str.tostring(percent_change_show_ma_comparison_length) + " ma" : " " + str.tostring(math.round(percent_change3, 2)) + table_text + str.tostring(lookback3_new)
table_text4 = percent_change_show_ma_comparison ? " " + str.tostring(math.round(percent_change4, 2)) + table_text + str.tostring(math.round(lookback4_new,0)) + percent_change4_ma_comparison + str.tostring(percent_change_show_ma_comparison_length) + " ma" : " " + str.tostring(math.round(percent_change4, 2)) + table_text + str.tostring(lookback4_new)
table_text5 = percent_change_show_ma_comparison ? " " + str.tostring(math.round(percent_change5, 2)) + table_text + str.tostring(math.round(lookback5_new,0)) + percent_change5_ma_comparison + str.tostring(percent_change_show_ma_comparison_length) + " ma" : " " + str.tostring(math.round(percent_change5, 2)) + table_text + str.tostring(lookback5_new)

// Table Display - Exchange Upside/Downside Data

display_exchange_data = input(defval = true, title = "Display exchage data", inline = "", group = group8_name)
volume_source = input.string(defval = "NYSE", title = "Exchange", options = ["NYSE", "NASDAQ", "DJIA", "US"], inline = "Source", group = group8_name)
volume_type = input.string(defval = "ADVN/DECL", title = "Variables", options = ["ADVN/DECL", "UPVOL/DNVOL", "ADV/DECL", "UVOL/DVOL"], tooltip = tooltip8_text, inline = "Type", group = group8_name)
display_exchange_data_upside = input(defval = true, title = "% Upside", inline = "", group = group8_name)
display_exchange_data_downside = input(defval = false, title = "% Downside", inline = "", group = group8_name)
display_exchange_data_unchanged = input(defval = false, title = "% Unchanged", inline = "", group = group8_name)
display_exchange_data_breadth = input(defval = true, title = "Breadth", inline = "Breadth", group = group8_name)
display_exchange_data_breadth_type = input.string(defval = "Tick", title = "", options = ["Tick", "Trin"], inline = "Breadth", group = group8_name)

// ADVN/DECL NUMBER OF NAMES
//          ADVANCING       DECLINING       UNCHANGED       TRADING
// NYSE     USI:ADVN.NY     USI:DECL.NY     USI:UNCH.NY     USI:ACTV.NY
// NASDAQ   USI:ADVN.NQ     USI:DECL.NQ     USI:UNCH.NQ     USI:ACTV.NQ
// DJIA     USI:ADVN.DJ     USI:DECL.DJ     USI:UNCH.DJ     USI:ACTV.DJ
// US       USI:ADVN.US     USI:DECL.US     USI:UNCH.US     USI:ACTV.US
//
// UPVOL/DNVOL VOLUME OF SHARES IN THE GROUP "NUMBER OF NAMES"
//          ADVANCING       DECLINING       UNCHANGED       TRADING
// NYSE     USI:UPVOL.NY    USI:DNVOL.NY    USI:UNCHVOL.NY  USI:TVOL.NY
// NASDAQ   USI:UPVOL.NQ    USI:DNVOL.NQ    USI:UNCHVOL.NQ  USI:TVOL.NQ
// DJIA     USI:UPVOL.DJ    USI:DNVOL.DJ    USI:UNCHVOL.DJ  USI:TVOL.DJ
// US       USI:UPVOL.US    USI:DNVOL.US    USI:UNCHVOL.US  USI:TVOL.US
//
// ADV/DECL NUMBER OF NAMES
//          ADVANCING       DECLINING       UNCHANGED       TRADING
// NYSE     USI:ADV         USI:DECL        USI:UCHG        USI:ADV+USI:DECL+USI:UCHG
// NASDAQ   USI:ADVQ        USI:DECLQ       USI:UCHGQ       USI:ADVQ+USI:DECLQ+USI:UCHGQ
// DJIA     n.a.
// US       n.a.
//
// UVOL/DVOL VOLUME OF SHARES IN THE GROUP "NUMBER OF NAMES"
//          ADVANCING       DECLINING       UNCHANGED       TRADING
// NYSE     USI:UVOL        USI:DVOL        USI:XVOL        USI:TVOL
// NASDAQ   USI:UVOLQ       USI:DVOLQ       USI:XVOLQ       USI:TVOLQ
// DJIA     n.a.
// US       n.a.

up = "USI:ADVN.NY"
down = "USI:DECL.NY"
unchanged = "USI:UNCH.NY"
total = "USI:ACTV.NY"
tick = "USI:TICK"
ticksource = " (nyse)"
trin_namesup =  "USI:ADV"
trin_namesdown = "USI:DECL"
trin_sharevolup = "USI:UVOL"
trin_sharevoldown = "USI:DVOL"
trinsource = " (nyse)"

if (volume_source == "NYSE") 
    up := volume_type == "ADVN/DECL" ? "USI:ADVN.NY" : volume_type == "UPVOL/DNVOL" ? "USI:UPVOL.NY" : volume_type == "ADV/DECL" ? "USI:ADV" : volume_type == "UVOL/DVOL" ? "USI:UVOL" : "USI:ADVN.NY"
    down := volume_type == "ADVN/DECL" ? "USI:DECL.NY" : volume_type == "UPVOL/DNVOL" ? "USI:UPVOL.NY" : volume_type == "ADV/DECL" ? "USI:DECL" : volume_type == "UVOL/DVOL" ? "USI:DVOL" : "USI:DECL.NY"
    unchanged := volume_type == "ADVN/DECL" ? "USI:UNCH.NY" : volume_type == "UPVOL/DNVOL" ? "USI:UNCHVOL.NY" : volume_type == "ADV/DECL" ? "USI:UCHG" : volume_type == "UVOL/DVOL" ? "USI:XVOL" : "USI:UNCH.NY"
    total := volume_type == "ADVN/DECL" ? "USI:ACTV.NY" : volume_type == "UPVOL/DNVOL" ? "USI:TVOL.NY" : volume_type == "ADV/DECL" ? "USI:ADV + USI:DECL + USI:UCHG" : volume_type == "UVOL/DVOL" ? "USI:TVOL" : "USI:TVOL.NY"
    tick := "USI:TICK"
    ticksource := na
    trin_namesup := "USI:ADV"
    trin_namesdown := "USI:DECL"
    trin_sharevolup := "USI:UVOL"
    trin_sharevoldown := "USI:DVOL"
    trinsource := na
else
    if (volume_source == "NASDAQ")
        up := volume_type == "UPVOL/DNVOL" ? "USI:UPVOL.NQ" : volume_type == "UVOL/DVOL" ? "USI:UVOLQ" : "USI:ADVN.NQ"
        down := volume_type == "UPVOL/DNVOL" ? "USI:DNVOL.NQ" : volume_type == "UVOL/DVOL" ? "USI:DVOLQ" : "USI:DECL.NQ"
        unchanged := volume_type == "UPVOL/DNVOL" ? "USI:UNCHVOL.NQ" : volume_type == "UVOL/DVOL" ? "USI:XVOLQ" : "USI:UNCH.NQ"
        total := volume_type == "UPVOL/DNVOL" ? "USI:TVOL.NQ" : volume_type == "UVOL/DVOL" ? "USI:TVOLQ" : "USI:ACTV.NQ"
        tick := "USI:TICKQ"
        ticksource := na
        trin_namesup := "USI:ADVQ"
        trin_namesdown := "USI:DECLQ"
        trin_sharevolup := "USI:UVOLQ"
        trin_sharevoldown := "USI:DVOLQ"
        trinsource := na
    else
        if (volume_source == "DJIA")
            up := volume_type == "UPVOL/DNVOL" ? "USI:UPVOL.DJ" : volume_type == "UVOL/DVOL" ? na : "USI:ADVN.DJ"
            down := volume_type == "UPVOL/DNVOL" ? "USI:DNVOL.DJ" : volume_type == "UVOL/DVOL" ? na : "USI:DECL.DJ"
            unchanged := volume_type == "UPVOL/DNVOL" ? "USI:UNCHVOL.DJ" : volume_type == "UVOL/DVOL" ? na : "USI:UNCH.DJ"
            total := volume_type == "UPVOL/DNVOL" ? "USI:TVOL.DJ" : volume_type == "UVOL/DVOL" ? na : "USI:ACTV.DJ"
            tick := "USI:TICKI"
            ticksource := na
            trin_namesup := "USI:ADV"
            trin_namesdown := "USI:DECL"
            trin_sharevolup := "USI:UVOL"
            trin_sharevoldown := "USI:DVOL"
            trinsource := " (nyse)"
        else
            if (volume_source == "US")
                up := volume_type == "UPVOL/DNVOL" ? "USI:UPVOL.US" : volume_type == "UVOL/DVOL" ? na : "USI:ADVN.US"
                down := volume_type == "UPVOL/DNVOL" ? "USI:DNVOL.US" : volume_type == "UVOL/DVOL" ? na : "USI:DECL.US"
                unchanged := volume_type == "UPVOL/DNVOL" ? "USI:UNCHVOL.US" : volume_type == "UVOL/DVOL" ? na : "USI:UNCH.US"
                total := volume_type == "UPVOL/DNVOL" ? "USI:TVOL.US" : volume_type == "UVOL/DVOL" ? na : "USI:ACTV.US"
                tick := "USI:TICK"
                ticksource := " (nyse)"
                trin_namesup := "USI:ADV"
                trin_namesdown := "USI:DECL"
                trin_sharevolup := "USI:UVOL"
                trin_sharevoldown := "USI:DVOL"
                trinsource := " (nyse)"

// Calculations
// request.security(symbol, timeframe, expression, gaps, lookahead, ignore_invalid_symbol, currency)
// volume(up) / volume(total) returns a value is 0 to 1, 100 * volume(up) / volume(total) returns a percent 0 to 100%

// Chart Time-Period Previous Period Close Up-volume Calculations

volumeperiodclose(x) => request.security(x, timeframe.period, close)
periodpercentup = 100 * volumeperiodclose(up) / volumeperiodclose(total)
periodpercentdown = 100 * volumeperiodclose(down) / volumeperiodclose(total)
periodpercentunchanged = 100 * volumeperiodclose(unchanged) / volumeperiodclose(total)

tickindex(y) => request.security(y, "390", close)
tickvalue = tickindex(tick)

// TRIN = (Number of names advaincing / number of names declining) / (volume of shares advancing / volume of shares declining)
// TRIN = (USI:ADV / USI:DECL) / ( USI:UVOL / USI:DVOL)

trinindex(z) => request.security(z, timeframe.period, close)
trinvalue = math.round((trinindex(trin_namesup)/trinindex(trin_namesdown)) / (trinindex(trin_sharevolup) / trinindex(trin_sharevoldown)), 3)

breadthvalue = tickvalue
tabletext_breadth = " tick" + ticksource
if (display_exchange_data_breadth_type == "Tick")
    breadthvalue := tickvalue
    tabletext_breadth := " tick" + ticksource
else
    if (display_exchange_data_breadth_type == "Trin")
        breadthvalue := trinvalue
        tabletext_breadth := " trin" + trinsource

// Table Display - Exchange Data Column

row_upside      = display_exchange_data_upside == true ? 1 : display_exchange_data_upside == false ? 1 : 1

row_downside    = display_exchange_data_downside == true and display_exchange_data_upside == true ? 2 : //
                  display_exchange_data_downside == false and display_exchange_data_upside == true ? 2 :
                  display_exchange_data_downside == true and display_exchange_data_upside == false ? 1 :
                  display_exchange_data_downside == false and display_exchange_data_upside == false ? 2 : 2

row_unchanged   = display_exchange_data_unchanged == true and display_exchange_data_downside == true and display_exchange_data_upside == true ? 3 : 
                  display_exchange_data_unchanged == false and display_exchange_data_downside == true and display_exchange_data_upside == true ? 3 : //
                  display_exchange_data_unchanged == true and display_exchange_data_downside == false and display_exchange_data_upside == true ? 2 : 
                  display_exchange_data_unchanged == true and display_exchange_data_downside == true and display_exchange_data_upside == false ? 2 : 
                  display_exchange_data_unchanged == false and display_exchange_data_downside == false and display_exchange_data_upside == true ? 2 :
                  display_exchange_data_unchanged == false and display_exchange_data_downside == true and display_exchange_data_upside == false ? 2 :
                  display_exchange_data_unchanged == true and display_exchange_data_downside == false and display_exchange_data_upside == false ? 1 :
                  display_exchange_data_unchanged == false and display_exchange_data_downside == false and display_exchange_data_upside == false ? 2 : 3

row_breadth     = display_exchange_data_breadth == true and display_exchange_data_unchanged == true and display_exchange_data_downside == true and display_exchange_data_upside == true ? 4 : 
                  display_exchange_data_breadth == false and display_exchange_data_unchanged == true and display_exchange_data_downside == true and display_exchange_data_upside == true ? 4 : //
                  display_exchange_data_breadth == true and display_exchange_data_unchanged == false and display_exchange_data_downside == true and display_exchange_data_upside == true ? 3 : 
                  display_exchange_data_breadth == true and display_exchange_data_unchanged == true and display_exchange_data_downside == false and display_exchange_data_upside == true ? 3 : 
                  display_exchange_data_breadth == true and display_exchange_data_unchanged == true and display_exchange_data_downside == true and display_exchange_data_upside == false ? 3 : 
                  display_exchange_data_breadth == false and display_exchange_data_unchanged == false and display_exchange_data_downside == true and display_exchange_data_upside == true ? 3 : //
                  display_exchange_data_breadth == false and display_exchange_data_unchanged == true and display_exchange_data_downside == false and display_exchange_data_upside == true ? 3 : //
                  display_exchange_data_breadth == false and display_exchange_data_unchanged == true and display_exchange_data_downside == true and display_exchange_data_upside == false ? 3 : //
                  display_exchange_data_breadth == true and display_exchange_data_unchanged == false and display_exchange_data_downside == false and display_exchange_data_upside == true ? 2 : 
                  display_exchange_data_breadth == true and display_exchange_data_unchanged == false and display_exchange_data_downside == true and display_exchange_data_upside == false ? 2 : 
                  display_exchange_data_breadth == true and display_exchange_data_unchanged == true and display_exchange_data_downside == false and display_exchange_data_upside == false ? 2 : 
                  display_exchange_data_breadth == false and display_exchange_data_unchanged == false and display_exchange_data_downside == false and display_exchange_data_upside == true ? 2 : 
                  display_exchange_data_breadth == false and display_exchange_data_unchanged == false and display_exchange_data_downside == true and display_exchange_data_upside == false ? 2 : 
                  display_exchange_data_breadth == false and display_exchange_data_unchanged == true and display_exchange_data_downside == false and display_exchange_data_upside == false ? 2 : 
                  display_exchange_data_breadth == true and display_exchange_data_unchanged == false and display_exchange_data_downside == false and display_exchange_data_upside == false ? 1 :
                  display_exchange_data_breadth == false and display_exchange_data_unchanged == false and display_exchange_data_downside == false and display_exchange_data_upside == false ? 2 : 4

// Table Display - Percent Change Column Text Content

tabletext_2_1 = show_percent_change1_new ? table_text1 : show_percent_change2_new ? table_text2 : show_percent_change3_new ? table_text3 : show_percent_change4_new ? table_text4 : show_percent_change5_new ? table_text5 : na
tabletext_2_2 = show_percent_change2_new and show_percent_change1_new ? table_text2 : (show_percent_change3_new and show_percent_change2_new) or (show_percent_change3_new and show_percent_change1_new) ? table_text3 : (show_percent_change4_new and show_percent_change3_new) or (show_percent_change4_new and show_percent_change2_new) or (show_percent_change4_new and show_percent_change1_new) ? table_text4 : (show_percent_change5_new and show_percent_change4_new) or (show_percent_change5_new and show_percent_change3_new) or (show_percent_change5_new and show_percent_change2_new) or (show_percent_change5_new and show_percent_change1_new) ? table_text5 : na
tabletext_2_3 = show_percent_change3_new and show_percent_change2_new and show_percent_change1_new ? table_text3 : (show_percent_change4_new and show_percent_change3_new and show_percent_change2_new) or (show_percent_change4_new and show_percent_change3_new and show_percent_change1_new) or (show_percent_change4_new and show_percent_change2_new and show_percent_change1_new) ? table_text4 : (show_percent_change5_new and show_percent_change4_new and show_percent_change3_new) or (show_percent_change5_new and show_percent_change4_new and show_percent_change2_new) or (show_percent_change5_new and show_percent_change4_new and show_percent_change1_new) or (show_percent_change5_new and show_percent_change3_new and show_percent_change2_new) or (show_percent_change5_new and show_percent_change3_new and show_percent_change1_new) or (show_percent_change5_new and show_percent_change2_new and show_percent_change1_new) ? table_text5 : na
tabletext_2_4 = show_percent_change4_new and show_percent_change3_new and show_percent_change2_new and show_percent_change1_new ? table_text4 : (show_percent_change5_new and show_percent_change4_new and show_percent_change3_new and show_percent_change2_new) or (show_percent_change5_new and show_percent_change4_new and show_percent_change3_new and show_percent_change1_new)  or (show_percent_change5_new and show_percent_change4_new and show_percent_change2_new and show_percent_change1_new)  or (show_percent_change5_new and show_percent_change3_new and show_percent_change2_new and show_percent_change1_new) ? table_text5 : na
tabletext_2_5 = show_percent_change5_new and show_percent_change4_new and show_percent_change3_new and show_percent_change2_new and show_percent_change1_new ? table_text5 : na

var table table_value = table.new(position.top_right, columns = 4, rows = 6)

table_column_name = str.tostring(ticker.standard(syminfo.ticker))
background_transparency = 95
backgroundcolor0 = display_exchange_data == true ? color.rgb(220, 223, 228, background_transparency) : na
backgroundcolor0a = display_exchange_data_upside == true ? display_exchange_data == true ? color.rgb(220, 223, 228, background_transparency) : na : na
backgroundcolor0b = display_exchange_data_downside == true ? display_exchange_data == true ? color.rgb(220, 223, 228, background_transparency) : na : na
backgroundcolor0c = display_exchange_data_unchanged == true ? display_exchange_data == true ? color.rgb(220, 223, 228, background_transparency) : na : na
backgroundcolor0d = display_exchange_data_breadth == true ? display_exchange_data == true ? color.rgb(220, 223, 228, background_transparency) : na : na
backgroundcolor1 = color.rgb(220, 223, 228, background_transparency)
backgroundcolor2 = show_percent_change1_new or show_percent_change2_new or show_percent_change3_new or show_percent_change4_new or show_percent_change5_new ? color.rgb(220, 223, 228, background_transparency) : na
backgroundcolor2a = str.tostring(tabletext_2_1) != "" ? color.rgb(220, 223, 228, background_transparency) : na
backgroundcolor2b = str.tostring(tabletext_2_2) != "" ? color.rgb(220, 223, 228, background_transparency) : na
backgroundcolor2c = str.tostring(tabletext_2_3) != "" ? color.rgb(220, 223, 228, background_transparency) : na
backgroundcolor2d = str.tostring(tabletext_2_4) != "" ? color.rgb(220, 223, 228, background_transparency) : na
backgroundcolor2e = str.tostring(tabletext_2_5) != "" ? color.rgb(220, 223, 228, background_transparency) : na

textcolor1 = color.rgb(220, 223, 228, 0)
if barstate.islast
    table.cell(table_value, column = 0, row = 0, text = (display_exchange_data_upside or display_exchange_data_downside or display_exchange_data_unchanged or display_exchange_data_breadth) ? display_exchange_data ? volume_source : na : na, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor0)
    table.cell(table_value, column = 0, row = row_upside, text = display_exchange_data and display_exchange_data_upside ? " " + str.tostring(math.round(periodpercentup, 2)) + " % up" : na, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor0a)
    table.cell(table_value, column = 0, row = row_downside, text = display_exchange_data and display_exchange_data_downside ? " " + str.tostring(math.round(periodpercentdown, 2)) + " % dn" : na, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor0b)
    table.cell(table_value, column = 0, row = row_unchanged, text = display_exchange_data and display_exchange_data_unchanged ? " " + str.tostring(math.round(periodpercentunchanged, 2)) + " % uc" : na, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor0c) 
    table.cell(table_value, column = 0, row = row_breadth, text = display_exchange_data and display_exchange_data_breadth ? " " + str.tostring(breadthvalue) + tabletext_breadth : na, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor0d) 
        
    table.cell(table_value, column = 1, row = 0, text = table_column_name, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor1)
    table.cell(table_value, column = 1, row = 1, text = " " + str.tostring(math.round(table_text_vol_percent_value, 3)) + table_text_vol_percent_lable, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor1)
    table.cell(table_value, column = 1, row = 2, text = " " + str.tostring(math.round(chart_float_to_outstanding, 3)) + " f/o", text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor1)

    table.cell(table_value, column = 2, row = 0, text = show_percent_change1_new or show_percent_change2_new or show_percent_change3_new or show_percent_change4_new or show_percent_change5_new ? table_column_name : na, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor2)
    table.cell(table_value, column = 2, row = 1, text = tabletext_2_1, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor2a)
    table.cell(table_value, column = 2, row = 2, text = tabletext_2_2, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor2b)
    table.cell(table_value, column = 2, row = 3, text = tabletext_2_3, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor2c)
    table.cell(table_value, column = 2, row = 4, text = tabletext_2_4, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor2d)
    table.cell(table_value, column = 2, row = 5, text = tabletext_2_5, text_color = textcolor1, text_halign = text.align_right, text_valign = text.align_center, text_size = size.small, bgcolor = backgroundcolor2e)

// Display Horizontal Lines 

obPlot = hline(show_smoothed_chart_vol_percent ? .51 : 1.1, title="Upper Band", linestyle=hline.style_solid, color=color.rgb(220, 223, 228, 95), linewidth=1) // white 220, 223, 228
hline(show_smoothed_chart_vol_percent ? na : na, title="100% Level", linestyle=hline.style_dotted, color=color.rgb(220, 223, 228, 40), linewidth=1) // white 220, 223, 228
hline(show_smoothed_chart_vol_percent ? na : na, title="90% Level", linestyle=hline.style_dotted, color=color.rgb(220, 223, 228, 50), linewidth=1) // white 220, 223, 228
hline(show_smoothed_chart_vol_percent ? na : na, title="70% Level", linestyle=hline.style_dotted, color=color.rgb(220, 223, 228, 40), linewidth=1) // white 220, 223, 228
hline(show_smoothed_chart_vol_percent ? na : na, title="55% Level", linestyle=hline.style_dotted, color=color.rgb(220, 223, 228, 20), linewidth=1) // white 220, 223, 228
hline(show_smoothed_chart_vol_percent ? 0 : na, title="50% Level", linestyle=hline.style_dotted, color=color.rgb(220, 223, 228, 40), linewidth=1) // white 220, 223, 228
hline(show_smoothed_chart_vol_percent ? na : na, title="45% Level", linestyle=hline.style_dotted, color=color.rgb(220, 223, 228, 20), linewidth=1) // white 220, 223, 228
hline(show_smoothed_chart_vol_percent ? na : na, title="30% Level", linestyle=hline.style_dotted, color=color.rgb(220, 223, 228, 40), linewidth=1) // white 220, 223, 228
hline(show_smoothed_chart_vol_percent ? na : na, title="10% Level", linestyle=hline.style_dotted, color=color.rgb(220, 223, 228, 50), linewidth=1) // white 220, 223, 228
hline(show_smoothed_chart_vol_percent ? na : 0, title="0% Level", linestyle=hline.style_dotted, color=color.rgb(220, 223, 228, 40), linewidth=1) // white 220, 223, 228
osPlot = hline(show_smoothed_chart_vol_percent ? -.51 : -.1, title="Lower Band", linestyle=hline.style_solid, color=color.rgb(220, 223, 228, 95), linewidth=1) // white 220, 223, 228
fill(obPlot, osPlot, title="Background", color=color.rgb(220, 223, 228, 99)) // white 220, 223, 228

// NOTES
//
// EXCHANGE % UPSIDE/DOWNSIDE VARIABLES
//
// ADVN/DECL NUMBER OF NAMES
//          ADVANCING       DECLINING       UNCHANGED       TRADING
// NYSE     USI:ADVN.NY     USI:DECL.NY     USI:UNCH.NY     USI:ACTV.NY
// NASDAQ   USI:ADVN.NQ     USI:DECL.NQ     USI:UNCH.NQ     USI:ACTV.NQ
// DJIA     USI:ADVN.DJ     USI:DECL.DJ     USI:UNCH.DJ     USI:ACTV.DJ
// US       USI:ADVN.US     USI:DECL.US     USI:UNCH.US     USI:ACTV.US
//
// UPVOL/DNVOL VOLUME OF SHARES IN THE GROUP "NUMBER OF NAMES"
//          ADVANCING       DECLINING       UNCHANGED       TRADING
// NYSE     USI:UPVOL.NY    USI:DNVOL.NY    USI:UNCHVOL.NY  USI:TVOL.NY
// NASDAQ   USI:UPVOL.NQ    USI:DNVOL.NQ    USI:UNCHVOL.NQ  USI:TVOL.NQ
// DJIA     USI:UPVOL.DJ    USI:DNVOL.DJ    USI:UNCHVOL.DJ  USI:TVOL.DJ
// US       USI:UPVOL.US    USI:DNVOL.US    USI:UNCHVOL.US  USI:TVOL.US
//
// ADV/DECL NUMBER OF NAMES
//          ADVANCING       DECLINING       UNCHANGED       TRADING
// NYSE     USI:ADV         USI:DECL        USI:UCHG        USI:ADV+USI:DECL+USI:UCHG
// NASDAQ   USI:ADVQ        USI:DECLQ       USI:UCHGQ       USI:ADVQ+USI:DECLQ+USI:UCHGQ
// DJIA     n.a.
// US       n.a.
//
// UVOL/DVOL VOLUME OF SHARES IN THE GROUP "NUMBER OF NAMES"
//          ADVANCING       DECLINING       UNCHANGED       TRADING
// NYSE     USI:UVOL        USI:DVOL        USI:XVOL        USI:TVOL
// NASDAQ   USI:UVOLQ       USI:DVOLQ       USI:XVOLQ       USI:TVOLQ
// DJIA     n.a.
// US       n.a.
//
// Compare data https://www.wsj.com/market-data/stocks/marketsdiary
//
// COLOR SCHEMES
//
// ONE HALF DARK by Son A. Pham at github.com/sonph/onehalf
//            HEX      RGB
// black      #282c34  40, 44, 52
// red        #e06c75  224, 108, 117
// green      #98c379  152, 195, 121
// yellow     #e5c07b  229, 192, 123
// blue       #61afef  97, 175, 239
// magenta    #c678dd  198, 120, 221
// cyan       #56b6c2  86, 182, 194
// white      #dcdfe4  220, 223, 228
// foreground #dcdfe4  220, 223, 228
// background #282c34  40, 44, 52
//
//
// END