TICK - Custom Tickers [Pt]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PtGambler

//@version=5
indicator("TICK - Custom Tickers [Pt]", shorttitle = 'TICK-Custom[Pt]', timeframe = '', timeframe_gaps = false)

group1 = 'Symbols'
group2 = 'Top 20'
group3 = 'Top 30'
group4 = 'Top 40'

t1 = time(timeframe.period, "0930-1600:23456", 'America/New_York')

mode = input.string("Bar", 'Display Mode', ['Bar', 'Line', 'Both', 'Cumulative'])
apply_ma = input.bool(true, '└ Apply SMA', inline = 'ma')
ma_len = input.int(3, '', inline = 'ma', tooltip = 'Only applies to Line or Cumulative plots')
sym_count = input.string('Top 10', 'Number of Tickers', ['Top 7','Top 10', 'Top 20', 'Top 30', 'Top 40'])

symbol1 = input.symbol(defval = "AAPL", group = group1)
symbol2 = input.symbol(defval = "MSFT", group = group1)
symbol3 = input.symbol(defval = "AMZN", group = group1)
symbol4 = input.symbol(defval = "TSLA", group = group1)
symbol5 = input.symbol(defval = "GOOGL", group = group1)
symbol6 = input.symbol(defval = "BRK.B", group = group1)
symbol7 = input.symbol(defval = "UNH", group = group1)
symbol8 = input.symbol(defval = "XOM", group = group1)
symbol9 = input.symbol(defval = "JNJ", group = group1)
symbol10 = input.symbol(defval = "NVDA", group = group1)

symbol11 = input.symbol(defval = "JNJ", group = group2)
symbol12 = input.symbol(defval = "HD", group = group2)
symbol13 = input.symbol(defval = "PG", group = group2)
symbol14 = input.symbol(defval = "V", group = group2)
symbol15 = input.symbol(defval = "PFE", group = group2)
symbol16 = input.symbol(defval = "BAC", group = group2)
symbol17 = input.symbol(defval = "MA", group = group2)
symbol18 = input.symbol(defval = "ADBE", group = group2)
symbol19 = input.symbol(defval = "DIS", group = group2)
symbol20 = input.symbol(defval = "NFLX", group = group2)

symbol21 = input.symbol(defval = "TMO", group = group3)
symbol22 = input.symbol(defval = "XOM", group = group3)
symbol23 = input.symbol(defval = "CRM", group = group3)
symbol24 = input.symbol(defval = "CSCO", group = group3)
symbol25 = input.symbol(defval = "COST", group = group3) 
symbol26 = input.symbol(defval = "ABT", group = group3)
symbol27 = input.symbol(defval = "PEP", group = group3)
symbol28 = input.symbol(defval = "ABBV", group = group3)
symbol29 = input.symbol(defval = "KO", group = group3)
symbol30 = input.symbol(defval = "PYPL", group = group3)

symbol31 = input.symbol(defval = "CVX", group = group4)
symbol32 = input.symbol(defval = "CMCSA", group = group4)
symbol33 = input.symbol(defval = "LLY", group = group4)
symbol34 = input.symbol(defval = "QCOM", group = group4)
symbol35 = input.symbol(defval = "NKE", group = group4)
symbol36 = input.symbol(defval = "VZ", group = group4)
symbol37 = input.symbol(defval = "WMT", group = group4)
symbol38 = input.symbol(defval = "INTC", group = group4)
symbol39 = input.symbol(defval = "ACN", group = group4)
symbol40 = input.symbol(defval = "AVGO", group = group4)

count = switch sym_count
    'Top 7'  => 7
    'Top 10' => 10
    'Top 20' => 20
    'Top 30' => 30
    'Top 40' => 40

ticks = array.new_int(count, 0)

f_getTick(_sym, _index) =>
    tick = request.security(_sym, timeframe.period, close > open ? 1 : close < open ? -1 : 0)
    array.set(ticks, _index, tick)

f_getTick(symbol1, 0)
f_getTick(symbol2, 1)
f_getTick(symbol3, 2)
f_getTick(symbol4, 3)
f_getTick(symbol5, 4)
f_getTick(symbol6, 5)
f_getTick(symbol7, 6)

if count > 7
    f_getTick(symbol8, 7)
    f_getTick(symbol9, 8)
    f_getTick(symbol10, 9)

if count > 10
    f_getTick(symbol11, 10)
    f_getTick(symbol12, 11)
    f_getTick(symbol13, 12)
    f_getTick(symbol14, 13)
    f_getTick(symbol15, 14)
    f_getTick(symbol16, 15)
    f_getTick(symbol17, 16)
    f_getTick(symbol18, 17)
    f_getTick(symbol19, 18)
    f_getTick(symbol20, 19)

if count > 20
    f_getTick(symbol21, 20)
    f_getTick(symbol22, 21)
    f_getTick(symbol23, 22)
    f_getTick(symbol24, 23)
    f_getTick(symbol25, 24)
    f_getTick(symbol26, 25)
    f_getTick(symbol27, 26)
    f_getTick(symbol28, 27)
    f_getTick(symbol29, 28)
    f_getTick(symbol30, 29)

if count > 30
    f_getTick(symbol31, 30)
    f_getTick(symbol32, 31)
    f_getTick(symbol33, 32)
    f_getTick(symbol34, 33)
    f_getTick(symbol35, 34)
    f_getTick(symbol36, 35)
    f_getTick(symbol37, 36)
    f_getTick(symbol38, 37)
    f_getTick(symbol39, 38)
    f_getTick(symbol40, 39)

tickC = array.sum(ticks)
tickO = nz(tickC[1])
tickH = math.max(tickO, tickC)
tickL = math.min(tickO, tickC)

tickC_line = tickC * 1.

// Cumulative TICK ---------------------------------- 

var ctick = 0.0
ctick_ma = 0.0

if t1 and not session.isfirstbar_regular
    ctick += tickC
else if session.isfirstbar_regular
    ctick := tickC

ma_len := apply_ma ? math.min(ta.barssince(session.isfirstbar_regular)+1, ma_len) : 1

ctick_ma := ta.sma(ctick, ma_len)
tickC_line := ta.sma(tickC, ma_len)


plotcandle(t1 and (mode == 'Bar' or mode == 'Both') ? tickO : na, tickH, tickL, tickC, "TICK", color = tickC > tickO ? color.lime : color.red, wickcolor = tickC > tickO ? color.lime : color.red, bordercolor = tickC > tickO ? color.lime : color.red)
plot(t1 and (mode == 'Line' or mode == 'Both') ? tickC_line : na, color = color.yellow, linewidth = 2, style = plot.style_linebr)
plot(t1 and mode == 'Cumulative' ? ctick_ma : na, "Cumulative TICK", ctick_ma > 0 and ctick_ma < ctick_ma[1] ? color.new(color.green,80) : ctick_ma > 0 and ctick_ma >= ctick_ma[1] ? color.new(color.green,50) : ctick_ma < 0 and ctick_ma < ctick_ma[1] ? color.new(color.red, 50) : color.new(color.red, 80), style = plot.style_columns)

hline(count, 'Top line', mode != 'Cumulative' ? color.gray : color.new(color.black, 100))
hline(0, 'Zero line', color.white)
hline(-count, 'Bottom line', mode != 'Cumulative' ? color.gray : color.new(color.black, 100))