NQ 7 Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RaenonX

//@version=5
indicator("NQ 7 Index", overlay = true)

divisor = input.int(1000000000, title = "Divisor")

candle_up = #26a699
candle_down = #ef5350

[aapl_o, aapl_h, aapl_l, aapl_c] = request.security(ticker.new("NASDAQ", "AAPL", session.extended), timeframe.period, [open, high, low, close])
[aapl_h_d, aapl_l_d, aapl_c_d] = request.security(ticker.new("NASDAQ", "AAPL", session.extended), "D", [high, low, close])
aapl_shares = request.financial("NASDAQ:AAPL", "TOTAL_SHARES_OUTSTANDING", "FQ")

[msft_o, msft_h, msft_l, msft_c] = request.security(ticker.new("NASDAQ", "MSFT", session.extended), timeframe.period, [open, high, low, close])
[msft_h_d, msft_l_d, msft_c_d] = request.security(ticker.new("NASDAQ", "MSFT", session.extended), "D", [high, low, close])
msft_shares = request.financial("NASDAQ:MSFT", "TOTAL_SHARES_OUTSTANDING", "FQ")

[goog_o, goog_h, goog_l, goog_c] = request.security(ticker.new("NASDAQ", "GOOG", session.extended), timeframe.period, [open, high, low, close])
[goog_h_d, goog_l_d, goog_c_d] = request.security(ticker.new("NASDAQ", "GOOG", session.extended), "D", [high, low, close])
goog_shares = request.financial("NASDAQ:GOOG", "TOTAL_SHARES_OUTSTANDING", "FQ")

[nvda_o, nvda_h, nvda_l, nvda_c] = request.security(ticker.new("NASDAQ", "NVDA", session.extended), timeframe.period, [open, high, low, close])
[nvda_h_d, nvda_l_d, nvda_c_d] = request.security(ticker.new("NASDAQ", "NVDA", session.extended), "D", [high, low, close])
nvda_shares = request.financial("NASDAQ:NVDA", "TOTAL_SHARES_OUTSTANDING", "FQ")

[tsla_o, tsla_h, tsla_l, tsla_c] = request.security(ticker.new("NASDAQ", "TSLA", session.extended), timeframe.period, [open, high, low, close])
[tsla_h_d, tsla_l_d, tsla_c_d] = request.security(ticker.new("NASDAQ", "TSLA", session.extended), "D", [high, low, close])
tsla_shares = request.financial("NASDAQ:TSLA", "TOTAL_SHARES_OUTSTANDING", "FQ")

[meta_o, meta_h, meta_l, meta_c] = request.security(ticker.new("NASDAQ", "META", session.extended), timeframe.period, [open, high, low, close])
[meta_h_d, meta_l_d, meta_c_d] = request.security(ticker.new("NASDAQ", "META", session.extended), "D", [high, low, close])
meta_shares = request.financial("NASDAQ:META", "TOTAL_SHARES_OUTSTANDING", "FQ")

[amzn_o, amzn_h, amzn_l, amzn_c] = request.security(ticker.new("NASDAQ", "AMZN", session.extended), timeframe.period, [open, high, low, close])
[amzn_h_d, amzn_l_d, amzn_c_d] = request.security(ticker.new("NASDAQ", "AMZN", session.extended), "D", [high, low, close])
amzn_shares = request.financial("NASDAQ:AMZN", "TOTAL_SHARES_OUTSTANDING", "FQ")

bar_o = (aapl_o * aapl_shares + msft_o * msft_shares + goog_o * goog_shares + nvda_o * nvda_shares + tsla_o * tsla_shares + meta_o * meta_shares + amzn_o * amzn_shares) / divisor
bar_h = (aapl_h * aapl_shares + msft_h * msft_shares + goog_h * goog_shares + nvda_h * nvda_shares + tsla_h * tsla_shares + meta_h * meta_shares + amzn_h * amzn_shares) / divisor
bar_l = (aapl_l * aapl_shares + msft_l * msft_shares + goog_l * goog_shares + nvda_l * nvda_shares + tsla_l * tsla_shares + meta_l * meta_shares + amzn_l * amzn_shares) / divisor
bar_c = (aapl_c * aapl_shares + msft_c * msft_shares + goog_c * goog_shares + nvda_c * nvda_shares + tsla_c * tsla_shares + meta_c * meta_shares + amzn_c * amzn_shares) / divisor

bar_c_prev = (aapl_c_d[1] * aapl_shares + msft_c_d[1] * msft_shares + goog_c_d[1] * goog_shares + nvda_c_d[1] * nvda_shares + tsla_c_d[1] * tsla_shares + meta_c_d[1] * meta_shares + amzn_c_d[1] * amzn_shares) / divisor

bar_color = bar_c > bar_o ? candle_up : candle_down

plotcandle(
     bar_o, bar_h, bar_l, bar_c,
     title = "NQ 7 Index", color = bar_color, wickcolor = bar_color, bordercolor = bar_color)

// --------------

get_text_diff_pct(diff_pct) =>
    if (diff_pct > 0)
        str.tostring(diff_pct, "▲ 0.00%")
    else if (diff_pct < 0)
        str.tostring(math.abs(diff_pct), "▼ 0.00%")
    else
        "- 0.00%"

var hud = table.new(position = position.bottom_right, rows = 1, columns = 2)

is_extrema = bar_c >= bar_h or bar_c <= bar_l

hud_color = bar_c > bar_c_prev ? candle_up : candle_down
hud_text_color = is_extrema ? color.white : hud_color
hud_bg_color = is_extrema ? hud_color : color.new(#000000, transp = 100)

table.cell(table_id = hud, row = 0, column = 0,
 text_size = size.normal, text_color = hud_text_color, bgcolor = hud_bg_color,
 text = str.tostring(bar_c, "0.00"))
table.cell(table_id = hud, row = 0, column = 1,
 text_size = size.normal, text_color = hud_text_color, bgcolor = hud_bg_color,
 text = get_text_diff_pct(bar_c / bar_c_prev - 1))
