Cumulative TICK [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(title='Cumulative TICK [Pt]', shorttitle='CumTICK[Pt]', overlay=false)

tick_src = input.string("USI:TICK", "TICK Source", options = ["USI:TICK", "USI:TICKQ", "USI:TICKI", "USI:TICKA"])
candle_src = input.string('Close', 'TICK Data', options = ['Close', 'Open', 'hl2', 'ohlc4', 'hlc3'])
show_ma = input.bool(false, "Apply SMA | length", inline = 'ma')
ma_len = input.int(5, '', inline = 'ma')

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

//get TICK data
[tickO, tickH, tickL, tickC, tickHL2, tickOHLC4, tickHLC3] = request.security(tick_src, timeframe.period, [open, high, low, close, hl2, ohlc4, hlc3])

cumTICK_src = switch candle_src
    'Close' => tickC
    'Open'  => tickO
    'hl2'   => tickHL2
    'ohlc4' => tickOHLC4
    'hlc3'  => tickHLC3

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

var cumTICK = 0.0

if t1
    cumTICK += cumTICK_src
if not t1 or session.islastbar_regular
    cumTICK := 0

cumTICK_ma = show_ma ? ta.sma(cumTICK, ma_len) : cumTICK

plot(cumTICK_ma, "Cumulative TICK", cumTICK_ma > 0 and cumTICK_ma < cumTICK_ma[1] ? color.new(color.green,80) : cumTICK_ma > 0 and cumTICK_ma >= cumTICK_ma[1] ? color.new(color.green,50) : cumTICK_ma < 0 and cumTICK_ma < cumTICK_ma[1] ? color.new(color.red, 50) : color.new(color.red, 80), style = plot.style_columns)

bgcolor(not t1 ? color.new(color.white,95) : na, title = 'After-hour Background Color')