Countdown Interval Timer
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © a_dema
//@version=5

// Given the execution model of Pine Script is ticked-based, alerts based on this indicator will only trigger if there is a tick at the right time.
// Specifically, a tick would be required between the event target (close of bar time, end of minute/s interval), less the Trigger Threshold (default = 5 seconds before).
//
// Alert instructions:
// 1. Create new alert
// 2. Select Condition options as:
//    a) This indicator & desired plot (Ti_Cl, Ti_01, Ti_02, Ti_03, Ti_04 or Ti_05)
//    b) 'Crossing Down'
//    c) This indicator & 'Trigger'
// 3. Set Options to 'Once Per Minute'
//
// Note that if you change the input values of this indicator you will need to recreate the alert as it will not pick up the changes.
// For example if you change 'Time interval 01' from 1 to 3, the alert will remain at 1

indicator(title="Countdown Interval Timer", shorttitle="CIT", overlay=true, scale=scale.none, precision=0, max_bars_back=1)

inThreshold = input.int(title="Trigger Threshold (secs)", minval=1, maxval=60, step=1, defval=5)
inTimeTo_01 = input.int(title="Time Interval 01 (minute)", minval=1, maxval=60, step=1, defval=1)
inTimeTo_02 = input.int(title="Time Interval 02 (minute)", minval=1, maxval=60, step=1, defval=5)
inTimeTo_03 = input.int(title="Time Interval 03 (minute)", minval=1, maxval=60, step=1, defval=15)
inTimeTo_04 = input.int(title="Time Interval 04 (minute)", minval=1, maxval=60, step=1, defval=30)
inTimeTo_05 = input.int(title="Time Interval 05 (minute)", minval=1, maxval=60, step=1, defval=60)

// Extra symbol formula, to add possibility of having a tick each second.
// Can use formulas such as + or * to include multiple symbols
inExtraSymbolFormula = input.symbol(title="Extra Symbol Formula", defval="", tooltip="Specify an extra symbol formula to trigger time ticks, to supplement those of the chart symbol. Multiple symbol formula recommended, for example:\nFX:EURUSD*BINANCE:BTCUSD*OANDA:XAUUSD")

var string sExtraSymbolFormula = ""
if inExtraSymbolFormula == ""
    sExtraSymbolFormula := syminfo.tickerid
else
    sExtraSymbolFormula := inExtraSymbolFormula

s1 = request.security(sExtraSymbolFormula, timeframe.period, close)


TimeToBarClose_Secs() =>
    if (barstate.isrealtime)
        (time_close - timenow) / 1000
    else
        timeframe.in_seconds(timeframe.period)

TimeTo_01_Secs() =>
    math.round((inTimeTo_01 * 60) - ((timenow / 1000) % (inTimeTo_01 * 60)))

TimeTo_02_Secs() =>
    math.round((inTimeTo_02 * 60) - ((timenow / 1000) % (inTimeTo_02 * 60)))

TimeTo_03_Secs() =>
    math.round((inTimeTo_03 * 60) - ((timenow / 1000) % (inTimeTo_03 * 60)))

TimeTo_04_Secs() =>
    math.round((inTimeTo_04 * 60) - ((timenow / 1000) % (inTimeTo_04 * 60)))

TimeTo_05_Secs() =>
    math.round((inTimeTo_05 * 60) - ((timenow / 1000) % (inTimeTo_05 * 60)))


plot(series=TimeToBarClose_Secs(), title="Ti_Cl", color=color.new(color.blue, 100), show_last=1, display=display.status_line + display.data_window)
plot(series=TimeTo_01_Secs(), title="Ti_01", color=color.new(color.blue, 100), show_last=1, display=display.status_line + display.data_window)
plot(series=TimeTo_02_Secs(), title="Ti_02", color=color.new(color.blue, 100), show_last=1, display=display.status_line + display.data_window)
plot(series=TimeTo_03_Secs(), title="Ti_03", color=color.new(color.blue, 100), show_last=1, display=display.status_line + display.data_window)
plot(series=TimeTo_04_Secs(), title="Ti_04", color=color.new(color.blue, 100), show_last=1, display=display.status_line + display.data_window)
plot(series=TimeTo_05_Secs(), title="Ti_05", color=color.new(color.blue, 100), show_last=1, display=display.status_line + display.data_window)
plot(series=inThreshold, title="Trigger", color=color.new(color.blue, 100), show_last=1, display=display.none)


