No Climactic Bars
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kegesch

//@version=5
indicator("No Climactic Bars")
varianceThreshold = input.float(title = "Threshold", defval = 0.003, step = 0.0005)
windowSize = input.int(title = "Number of candles in window", defval = 3, minval = 1)

varToLast(i) =>
    math.pow(close[i+1] - close[i], 2)
    
lastNVars(cnt) =>
    window = array.new<float>(cnt)
    for i = 0 to cnt-1
        array.set(window, i, varToLast(i))
        
    array.max(window)
    
variance = lastNVars(windowSize)
plot(variance, color = variance < varianceThreshold ? color.gray : color.lime)
plateuReached = ta.barssince(variance >= varianceThreshold) > windowSize
plotshape(plateuReached)
        
        