TTP PNR filter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TheTradingParrot

//@version=5
indicator("TTP PNR filter v0.1.2",overlay=false)
length = input(150)
plow = input.float(0.0,"Percentile low",minval=0,maxval=100, inline = "a")
phigh = input.float(1.0,"Percentile high",minval=0,maxval=100, inline = "a")

src    = input(close)

enableTime = input.bool(false, "Enable", tooltip = "When enabled it will analise time passed since last time certain value was reached. Use it to measure time PNR of signals. Example: connect any signal that prints 1 whenever it triggers, then select low 98 and high 100, then enable time analysis. You will see how long it tends to pass between one signal and the next in average. You can then apply similar PNR analysis to time and frequency.", group = "time analysis")
valueTime = input.float(1.0, "Value")

s = ta.barssince(src == valueTime)

if enableTime
    src := s

valuelow = ta.percentile_nearest_rank(src, length , plow)
valuehigh = ta.percentile_nearest_rank(src, length , phigh)
a = plot(valuelow,"% low", color=na)
b = plot(valuehigh, "% high" , color=na)
fill(a, b, valuelow, valuehigh, top_color = color.new(color.green,50), bottom_color =color.new(color.red,30))
plot(src, "src", color=color.white)

signaldirection = input.string("X over", "Direction", options = ["X over", "X under", "Below", "Above"], group = "signal")
signalsource    = input.string("% low", options = ["% low", "% high"], group = "signal") == "% low" ? valuelow : valuehigh

bool signal = switch signaldirection
    "X over" => ta.crossover(src, signalsource)
    "X under" => ta.crossunder(src, signalsource)
    "Below" => src < signalsource
    "Above" => src > signalsource
    => false


plot(signal ? 1 : na, "signal", display = display.data_window)


long = signaldirection == "X over" or signaldirection == "Above" 
short = not long
indicatorCondition = signal
indicatorValue = src
plotIndicator = true
indicatorName = "PNR"
plotSignal = true
plotBackground = true
signalValue = src


color triggerColor   = long ? color.lime : color.red


// Plot Character
plotchar(            series    = signal and long ? src : na, 
                     title     = "Entry long", 
                     char      = ".", 
                     location  = location.absolute, 
                     color     = triggerColor,
                     textcolor = color.white, 
                     size      = size.large, 
                     display   = display.all - display.data_window)

plotchar(            series    = signal and short ? src : na, 
                     title     = "Entry short", 
                     char      = ".", 
                     location  = location.absolute, 
                     color     = triggerColor,
                     textcolor = color.white, 
                     size      = size.large, 
                     display   = display.all - display.data_window)


alertcondition(signal,"PNR Signal")