Swing Data - ADR% / RVol / PVol / Float % / Avg $ Vol
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// Credit to  MikeC for the first version. Fred6724 for his contribution to this updated version
// TheScrutiniser and GlinckEastwoot for ADR% formula
// latest update with clean chart

//@version=5
indicator(title = "Swing Data - ADR% / RVol / PVol / Float % / Avg $ Vol", shorttitle="ADR% / RVol / PVol / Float % / Avg $ Vol", overlay=true)


show_mktCap = input(true, title="Show Market Cap")
show_floatpercent = input(true, title="Show Float%")
show_adrp = input(true, title="Show ADR%")
show_atr = input(false, title="Show ATR")
show_lod_dist = input(true, title="Show LoD dist.")
show_ADVS = input(true, title="Show Avg Daily $ Volume")
show_ADV = input(true, title="Show Avg Daily Volume")
show_projectedVol = input(true, title="Show Projected Volumes")
show_relVol = input(true, title="Show Relative Volume")
show_volBuzz = input(true, title="Show Vol. Buzz")
show_sector = input(true, title="Show Sector")
show_ind = input(true, title="Show Ind. Grp")
adrp_len = input(20, title="ADR% Length", tooltip="The number of daily bars used in the calculation of the ADR%")
atr_len = input(14, title="ATR Length",tooltip="The number of daily bars used in the calculation of the ATR")
len = input(50, "Volume MA Length",tooltip="The number of daily bars back for the MA calculation of the volume")
tbl_size  = input.string(size.normal, title='Table Size', options=[size.tiny,size.small,size.normal, size.large])
bg_col = input(#00000000, title="Background Color")
txt_col = input(color.orange, title="Text Color")
show_empty_row = input(true, title="Add an empty row above the displayed values", tooltip="Serves the purpose of not having the pane icons overlap with the " +"text in the table if multiple panes are shown and the chart pane is hovered")

arp = 100 * (ta.sma(high / low, adrp_len) - 1)
adrp = request.security(syminfo.tickerid, 'D', arp)
atr = request.security(syminfo.tickerid, 'D', ta.atr(atr_len))
lod_dist = request.security(syminfo.tickerid, 'D', 100 * (close - low) / atr)


// Volume calculation

tf = timeframe.period
s = syminfo.tickerid
vol = request.security(s, tf, volume)
ma = ta.sma(vol, len)
// 1) Relative Volume
relVol = vol/ma*100
// 2) Average Daily Vol
avgDaVol = request.security(s, tf, ma)
avgDaVolCalc = avgDaVol
uV = ''
if(avgDaVol >= 1000 and avgDaVol < 1000000)
    avgDaVol := avgDaVol/1000
    uV := 'K'
if(avgDaVol >= 1000000 and avgDaVol < 1000000000)
    avgDaVol := avgDaVol/1000000
    uV := 'M'
if(avgDaVol >= 1000000000)
    avgDaVol := avgDaVol/1000000000
    uV := 'B'
// 3) Average Daily $ Vol
closeDa = request.security(s, tf, close)
advdolDa = closeDa*avgDaVolCalc
uVDoll = ''
if(advdolDa >= 1000 and advdolDa < 1000000)
    advdolDa := advdolDa/1000
    uVDoll := 'K'
if(advdolDa >= 1000000 and advdolDa < 1000000000)
    advdolDa := advdolDa/1000000
    uVDoll := 'M'
if(advdolDa >= 1000000000)
    advdolDa := advdolDa/1000000000
    uVDoll := 'B'
// 4)  Market Cap
TSO = request.financial(s, 'TOTAL_SHARES_OUTSTANDING', 'FQ', ignore_invalid_symbol=true)
MCap = TSO*closeDa
MCapCalc = MCap
uM = ''
if(MCap >= 1000 and MCap < 1000000)
    MCap := MCap/1000
    uM := 'K'
if(MCap >= 1000000 and MCap < 1000000000)
    MCap := MCap/1000000
    uM := 'M'
if(MCap >= 1000000000)
    MCap := MCap/1000000000
    uM := 'B'
// 5) Float%
FSO = request.financial(s, 'FLOAT_SHARES_OUTSTANDING', 'FY', ignore_invalid_symbol=true)
FFMCap = FSO * closeDa
floatPer = FFMCap/MCapCalc*100
// 6) Projected Volume
// Code implented from    https://www.tradingview.com/script/tFe9aLsd-Projected-Volume/  credits to Bob-Lablaw
// Calculation of projected volume
time_period = ((time_close  - time)     / 1000)
time_passed = ((timenow     - time)     / 1000)
time_left   = ((time_close  - timenow)  / 1000)

volume_per_second = 0.0
volume_projected  = float(volume)
uP = ''
if time_left > 0
    volume_per_second := (volume / time_passed)
    volume_projected  := (volume + (volume_per_second * time_left))
if(volume_projected >= 1000 and volume_projected < 1000000)
    volume_projected := volume_projected/1000
    uP := 'K'
if(volume_projected >= 1000000 and volume_projected < 1000000000)
    volume_projected := volume_projected/1000000
    uP := 'M'
if(volume_projected >= 1000000000)
    volume_projected := volume_projected/1000000000
    uP := 'B'
// 7) Volume Buzz by monkaOMEGA ->  https://www.tradingview.com/script/vAxiK8VI-Volume-Buzz-2-0/
volBuzz = 100 * (volume / ta.sma(volume, len) - 1)
// 8) Sector
string sector = syminfo.sector
if(na(sector))
    sector := "N/A"
// 9) Inudstry group
string industryGrp = syminfo.industry
if(na(industryGrp))
    industryGrp := "N/A"

// Display Table
tfDaily = timeframe.isdaily
table t = table.new(position.top_right, 3, 13, bgcolor=bg_col)
if barstate.islast and timeframe.period != "W" and timeframe.period != "M"
    if show_empty_row
        table.cell(t, 1, 0, "")
        table.cell(t, 2, 0, "")
    if show_mktCap
        table.cell(t, 1, 1, "Market Cap", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 2, 1, str.tostring(math.round(MCap, 2), '0.00') + uM, text_color=txt_col, text_size = tbl_size)
    if show_floatpercent
        table.cell(t, 1, 2, "Float %", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 2, 2, str.tostring(math.round(floatPer, 1), '0.0') + '%', text_color=txt_col, text_size = tbl_size)
    if show_adrp
        table.cell(t, 1, 3, "ADR%", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 2, 3, str.tostring(math.round(adrp, 2)) + "%", text_color=txt_col, text_size = tbl_size)
    if show_atr
        table.cell(t, 1, 4, "ATR", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 2, 4, str.tostring(math.round(atr, 2)), text_color=txt_col, text_size = tbl_size)
    if show_lod_dist
        table.cell(t, 1, 5, "LoD dist.", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 2, 5, str.tostring(math.round(lod_dist)) + "%", text_color=txt_col, text_size = tbl_size)
    if show_ADVS
        table.cell(t, 1, 6, tfDaily ? "Avg Dly $ Vol":"Avg $ Vol", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 2, 6, '$' + str.tostring(math.round(advdolDa, 2), '0.00') + uVDoll, text_color=txt_col, text_size = tbl_size)
    if show_ADV
        table.cell(t, 1, 7, tfDaily ? "Avg Dly Vol":"Avg Vol", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 2, 7, str.tostring(math.round(avgDaVol, 2), '0.00') + uV, text_color=txt_col, text_size = tbl_size)
    if show_projectedVol
        table.cell(t, 1, 8, "Proj. Vol", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 2, 8, str.tostring(math.round(volume_projected, 2), '0.00') + uP, text_color=txt_col, text_size = tbl_size)
    if show_relVol
        table.cell(t, 1, 9, "Rel. Vol", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 2, 9, str.tostring(math.round(relVol)) + "%", text_color=txt_col, text_size = tbl_size)
    if show_volBuzz
        table.cell(t, 1, 10, "Vol. Buzz", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 2, 10, str.tostring(math.round(volBuzz)) + "%", text_color=txt_col, text_size = tbl_size)
    if show_sector
        table.cell(t, 0, 11, "Sector", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 1, 11, sector, text_color=txt_col, text_size = tbl_size)
    if show_ind
        table.cell(t, 0, 12, "Ind. Grp", text_color=txt_col, text_size = tbl_size)
        table.cell(t, 1, 12, industryGrp, text_color=txt_col, text_size = tbl_size)
    table.merge_cells(t, 1, 11, 2, 11)
    table.merge_cells(t, 1, 12, 2, 12)