ADR% / ATR / Market Cap
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ArmerSchlucker
// Credit to MikeC / TheScrutiniser and GlinckEastwoot for ADR% formula

//@version=4

study(title="ADR% / ATR / Market Cap", overlay=true)

show_adrp = input(true, title="Show ADR%")
show_atr = input(false, title="Show ATR")
show_market_cap = input(true, title="Show Market Cap")
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")
bg_col = input(#00000000, title="Background Color")
adrp_col = input(color.white, title="Text Color (ADR%)")
atr_col = input(color.white, title="Text Color (ATR)")
market_cap_col = input(color.orange, title="Text Color (Market Cap)")
show_empty_row = input(false, 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 * (sma(high / low, adrp_len) - 1)
adrp = security(syminfo.tickerid, 'D', arp)
atr = security(syminfo.tickerid, 'D', atr(atr_len))
market_cap = ((financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", "FQ")) * close)

table t = table.new(position.bottom_right, 2, 4, bgcolor=bg_col)
if barstate.islast and timeframe.period != "W" and timeframe.period != "M"
    if show_empty_row
        table.cell(t, 0, 0, "")
        table.cell(t, 1, 0, "")
    if show_adrp
        table.cell(t, 0, 1, "ADR%", text_color=adrp_col)
        table.cell(t, 1, 1, tostring(round(adrp, 2)) + "%", text_color=adrp_col)
    if show_atr
        table.cell(t, 0, 2, "ATR", text_color=atr_col)
        table.cell(t, 1, 2, tostring(round(atr, 2)), text_color=atr_col)
    if show_market_cap
        table.cell(t, 0, 3, "Market Cap", text_color=market_cap_col)
        table.cell(t, 1, 3, tostring(round(market_cap)), text_color=market_cap_col)