Normalized Correlation Coefficient
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PlantFi

//@version=5

indicator(title='Normalized Correlation Coefficient', shorttitle='Norm. Co. Co.', format=format.price, precision=2, timeframe="", timeframe_gaps=true)
sym = input.symbol('Symbol', confirm=true)
src = input(ohlc4, title='Source')
len = input.int(40, minval=1, title="CoCo Length")
res=timeframe.period
ovr = request.security(sym, res, src)
coco = ta.correlation(src, ovr, len)
coco_h = ta.max(coco)
coco_l = ta.min(coco)
norm_coco = (coco - coco_l) / (coco_h - coco_l)
coco_Color = (norm_coco > 0.80) ? color.green : color.red
coco_plot = plot(norm_coco, color=coco_Color, style=plot.style_line, linewidth=3, title='CorCoef')
coco_th = plot(0.80, "CoCo Threshold", color=color.silver)
fill(coco_plot, coco_th, color=color.white, transp=40)
