Bar Percent Complete
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cmthoman

//@version=5
indicator("Bar Percent Complete")
percent = input.int(title="Percent", defval=50, minval=1, maxval=100, step=1)

//varip variables allow intrabar states to be saved
varip bool conditionMet = false
varip bool stop = false

//----Calculate Bar Completion Percentage
barPercentage(percent) =>
    int barLen = math.abs(time - time[1])
    int barPercentThreshold = math.round((percent / 100 * barLen) + time)
    timenow >= barPercentThreshold ? true : false

//Reset stop
if  barstate.isrealtime and barstate.isnew
    stop := false
    conditionMet := false

//On the realtime bar and if the stop condition is false, test the time percentage
if barstate.isrealtime and not stop
    conditionMet := barPercentage(percent)
    if conditionMet
        alert("Bar is at least " + str.tostring(percent) + "% complete!", alert.freq_all)
        stop := true
        
plot(close, display=display.none)
