Coppock Curve w/ Early Turns [QuantVue]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © QuantVue

//@version=5
indicator('Coppock Curve w/ Early Turns [QuantVue]', shorttitle='Coppock Curve w/ Early Turns', overlay=false)

//----------Inputs----------//
var g1                  = 'Calculation Settings'
RateOfChangeSlowPeriod  = input(14, title='Rate Of Change Slow Period', group = g1)
RateOfChangeFastPeriod  = input(11, title='Rate Of Change Fast Period', group = g1)
WeightedMAPeriod        = input(10, title='Weighted MA Period', group = g1)

var g2                  = 'Display Settings'
above0Col               = input.color(color.green, 'Above 0 Color', group = g2)
below0Col               = input.color(color.red, 'Below 0 Color', group = g2)
zeroCol                 = input.color(color.black, 'Zero Line Color', group = g2)

var g3                  = 'Early Turns'
earlyTurns              = input.bool(true, 'Show Early Turns', group = g3)
etBullColor             = input.color(color.blue, 'Bullish Early Turn Color ', group = g3)
etBearColor             = input.color(color.maroon, 'Bearish Early Turn Color', group = g3)

//----------ROC function----------//
roc(src, length) =>
    if src[length] != 0
        (src / src[length] - 1) * 100
    else
        0.0

//----------Calculations----------//
ROC1 = roc(close, RateOfChangeSlowPeriod)
ROC2 = roc(close, RateOfChangeFastPeriod)

Coppock = ta.wma(ROC1 + ROC2, WeightedMAPeriod)

//----------Early Turns---------//
earlyTurnBull = Coppock[1] < 0 and Coppock > Coppock[1] and Coppock[1] < Coppock[2]
earlyTurnBear = Coppock[1] > 0 and Coppock < Coppock[1] and Coppock[1] > Coppock[2]

//----------Plots----------//
plot(Coppock, title='Coppock', color=Coppock > 0 ? above0Col : below0Col, linewidth=2)
plot(0, title='Zero Line', color=color.black)

plotshape(earlyTurnBull and earlyTurns ? 0 : na, style=shape.diamond, location=location.absolute, color = etBullColor, size = size.small, display = display.pane)
plotshape(earlyTurnBear and earlyTurns ? 0 : na, style=shape.diamond, location=location.absolute, color = etBearColor, size = size.small, display = display.pane)

alertcondition(earlyTurnBull or earlyTurnBear, 'Early Turn', 'Early Coppock turn for {{ticker}}')