Pearson's R Convergence Divergence
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Gentleman-Goat

//@version=5
indicator(title="Pearson's R Convergence Divergence",shorttitle="PRCD",overlay=false)

linear_regression_group = "Linear Regression"
i_source_linreg         = input.source(close,"Linear Regressions Source",group=linear_regression_group)
i_startPeriod_l1        = input.int(48,"Start Period L1 (Fast)",group=linear_regression_group)
i_startPeriod_l2        = input.int(96,"Start Period L2 (Slow)",group=linear_regression_group)
i_def_deviation_l1      = input.float(2, "Deviation Amount L1" , minval=0.1, step=0.1,group=linear_regression_group)
i_def_deviation_l2      = input.float(2, "Deviation Amount L2" , minval=0.1, step=0.1,group=linear_regression_group)

createLinReg(period,source,deviations,_byNum)=>
    periodMinusOne = period-1
    Ex = 0.0, Ey = 0.0, Ex2 = 0.0,Ey2 =0.0, Exy = 0.0, for i=0 to periodMinusOne by _byNum
        closeI = nz(source[i]), Ex := Ex + i, Ey := Ey + closeI, Ex2 := Ex2 + (i * i),Ey2 := Ey2 + (closeI * closeI), Exy := Exy + (closeI * i)
    ExT2 = math.pow(Ex,2.0) //Sum of X THEN Squared
    EyT2 = math.pow(Ey,2.0) //Sym of Y THEN Squared
    PearsonsR = (Exy - ((Ex*Ey)/period))/(math.sqrt(Ex2-(ExT2/period))*math.sqrt(Ey2-(EyT2/period)))
    ExEx = Ex * Ex, slope = Ex2==ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
    linearRegression = (Ey - slope * Ex) / period
    intercept = linearRegression + bar_index * slope
    deviation = 0.0, for i=0 to periodMinusOne by _byNum
        deviation := deviation + math.pow(nz(source[i]) - (intercept - slope * (bar_index[i])), 2.0)
    deviation := deviations * math.sqrt(deviation / periodMinusOne)
    startingPointY = linearRegression + slope * periodMinusOne
    [PearsonsR,linearRegression,deviation,startingPointY]

[pearsonsR_l1,linearRegression_l1,deviation_l1,startingPointY_l1] = createLinReg(i_startPeriod_l1,i_source_linreg,i_def_deviation_l1,1)
[pearsonsR_l2,linearRegression_l2,deviation_l2,startingPointY_l2] = createLinReg(i_startPeriod_l2,i_source_linreg,i_def_deviation_l2,1)

macd = pearsonsR_l1 - pearsonsR_l2

col_grow_above = input.color(#FF5252, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input.color(#FFCDD2, "Fall", group="Histogram", inline="Above")
col_grow_below = input.color(#B2DFDB, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input.color(#26A69A, "Fall", group="Histogram", inline="Below")

hist = pearsonsR_l1 > pearsonsR_l2 ? math.abs(pearsonsR_l1) - math.abs(pearsonsR_l2) : (math.abs(pearsonsR_l2) - math.abs(pearsonsR_l1)) * -1

upperChannelPrice_l1 = startingPointY_l1-(startingPointY_l1-(linearRegression_l1+deviation_l1))
lowerChannelPrice_l1 = startingPointY_l1-(startingPointY_l1-(linearRegression_l1-deviation_l1))

upperChannelPrice_l2 = startingPointY_l2-(startingPointY_l2-(linearRegression_l2+deviation_l2))
lowerChannelPrice_l2 = startingPointY_l2-(startingPointY_l2-(linearRegression_l2-deviation_l2))

plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))

hline(0, linestyle=hline.style_dashed, color=color.white)
hline(-0.8, linestyle=hline.style_dashed, color=color.lime)
hline(0.8, linestyle=hline.style_dashed, color=color.red)
hline(1, linestyle=hline.style_solid, color=color.red)
hline(-1, linestyle=hline.style_solid, color=color.green)

plot( pearsonsR_l1,color=#2962FF,style=plot.style_linebr)
plot( pearsonsR_l2,color=#FF6D00,style=plot.style_linebr)

plot(ta.crossover(close,upperChannelPrice_l1) ? pearsonsR_l1 : na, color=color.green, style=plot.style_cross,linewidth = 3)
plot(ta.crossunder(close,lowerChannelPrice_l1) ? pearsonsR_l1 : na, color=color.red, style=plot.style_cross,linewidth = 3)

plot(ta.crossover(close,upperChannelPrice_l2) ? pearsonsR_l2 : na, color=color.green, style=plot.style_cross,linewidth = 3)
plot(ta.crossunder(close,lowerChannelPrice_l2) ? pearsonsR_l2 : na, color=color.red, style=plot.style_cross,linewidth = 3)
