Cumulative TICK Trend[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 Trend[Pt]', shorttitle='CumTICK-trend[Pt]', overlay=true)

group_atr = 'ATR EMA Band'
group_display = 'Display'

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')

show_atr = input.bool(false, 'Show ATR Bands', group = group_atr)
atrPeriod = input.int(10, 'ATR Period', minval=1, group = group_atr)
emaPeriod = input.int(10, 'EMA Period', minval=1, group = group_atr)
atr_src = input(close, 'Source', group = group_atr)
atrMultiplier = input.float(3, 'ATR Multiplier', group = group_atr)

bull_col = input.color(color.green, 'Trend Colors', inline = 'col', group = group_display)
bear_col = input.color(color.red, '', inline = 'col', group = group_display)
smooth_line = input.bool(true, 'Smooth Line', inline = 'smooth', group = group_display)
smooth_ma_len = input.int(5, '| Length', inline = 'smooth', group = group_display)

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

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

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

// Calculation - 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

bull_trend = cumTICK_ma >= 0
bear_trend = cumTICK_ma < 0

// ATR Band ------------------------------------------------------

atr = ta.atr(atrPeriod)
up_ma = ta.ema(atr_src + atr * atrMultiplier,emaPeriod)
down_ma = ta.ema(atr_src - atr * atrMultiplier, emaPeriod)

if smooth_line
    up_ma := ta.sma(up_ma, smooth_ma_len)
    down_ma := ta.sma(down_ma, smooth_ma_len)

// Plotting
plot(up_ma,     'Upper ATR', color= show_atr ? color.new(bear_col, 70) : na, style = plot.style_linebr)
plot(down_ma,   'Lower ATR', color= show_atr ? color.new(bull_col, 70) : na, style = plot.style_linebr)

plot(up_ma,     'Bear Trend', color= t1 and bear_trend and cumTICK_ma <= cumTICK_ma[1] ? bear_col : na, style = plot.style_linebr, linewidth = 4)
plot(up_ma,     'Bear Trend', color= t1 and bear_trend and cumTICK_ma > cumTICK_ma[1] ? color.new(bear_col,50) : na, style = plot.style_linebr, linewidth = 1)
plot(down_ma,   'Bull Trend', color= t1 and bull_trend and cumTICK_ma >= cumTICK_ma[1] ? bull_col : na, style = plot.style_linebr, linewidth = 4)
plot(down_ma,   'Bull Trend', color= t1 and bull_trend and cumTICK_ma < cumTICK_ma[1] ? color.new(bull_col,50) : na, style = plot.style_linebr, linewidth = 1)
