Autocorrelative Power Oscillator (APO) [SpiritualHealer117]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © spiritualhealer117

//@version=5
indicator("My script")
co_len = input(300,"Correlation Calculation Length")
len = input(10, "Number of correlation offsets")
src = input(close,"Source")
smo = input(5, "Smoothing Length")

calc_correl (src, lag, len)=> 
    s1 = 0. 
    s2 = 0.
    s3 = 0.
    x_bar = ta.sma(src,len)
    y_bar = ta.sma(src[lag],len)
    
    for i = 0 to len - 1
        s1+=(src[i]-x_bar)*(src[i+lag]-y_bar)
        s2+=math.pow((src[i]-x_bar),2)
        s3+=math.pow((src[i+lag]-y_bar),2)
    
    s1/(math.sqrt(s2*s3))
    
sgn(x)=>x>=0?1:-1

signed_correls = array.new<float>(len,0.0)
for i=1 to len
    mult_adjustment = src[i]<=src?1:-1
    array.set(signed_correls,i-1,mult_adjustment*sgn(calc_correl(close,i,co_len)))

smo_line = ta.sma(array.sum(signed_correls),smo)
clr = smo_line>=0?color.new(color.green,math.min(30,100-array.avg(signed_correls)*100)):color.new(color.red,math.min(30,100+array.avg(signed_correls)*100))
plot(smo_line, style=plot.style_columns, color=clr)

hline(len)
hline(-len)