IsPullbackPivotRetested experiment
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © brettosm8

//@version=5
indicator("My script")

// Calculate keltner channel boundaries
length = input.int(20, minval=1)
mult = input(2.0, "Multiplier")
src = input(close, title="Source")
exp = input(true, "Use Exponential MA")
BandsStyle = input.string("Average True Range", options = ["Average True Range", "True Range", "Range"], title="Bands Style")
atrlength = input(14, "ATR Length")
esma(source, length)=>
	s = ta.sma(source, length)
	e = ta.ema(source, length)
	exp ? e : s
ma = esma(src, length)
rangema = BandsStyle == "True Range" ? ta.tr(true) : BandsStyle == "Average True Range" ? ta.atr(atrlength) : ta.rma(high - low, length)
upper = ma + rangema * mult
lower = ma - rangema * mult

var float pivot_high = 0
var float pivot_low = 0
var bool isPivotHighAssigned = false
var bool isPivotLowAssigned = false
var bool isLongPullback = false
var bool isShortPullback = false
var int pullback_resolved_count = 0
var int pullback_failed_count = 0
var int pullback_sum = 0

isPivotHighIdentified = high[1] > upper and high < high[1]

if isPivotHighIdentified and isPivotHighAssigned == false
    pivot_high := high[1]
    isPivotHighAssigned := true
    isLongPullback := true
    
if isLongPullback and high > pivot_high
    pullback_resolved_count += 1
    pullback_sum += 1
    isLongPullback := false
    
if isLongPullback and low < lower
    pullback_failed_count += 1
    pullback_sum -= 1
    isLongPullback := false

if isPivotHighIdentified == false
    isPivotHighAssigned := false
    
//------------------------------------------------------------------------------

isPivotLowIdentified = low[1] < lower and low > low[1]

if isPivotLowIdentified and isPivotLowAssigned == false
    pivot_low := low[1]
    isPivotLowAssigned := true
    isShortPullback := true
    
if isShortPullback and low < pivot_low
    pullback_resolved_count += 1
    pullback_sum += 1
    isShortPullback := false
    
if isShortPullback and high > upper
    pullback_failed_count += 1
    pullback_sum -= 1
    isShortPullback := false

if isPivotLowIdentified == false
    isPivotLowAssigned := false
    
plot(pullback_resolved_count, color = color.blue)
plot(pullback_failed_count, color = color.red)
plot(pullback_sum, color = color.black)
