CCPD Candle Color Price Detector
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KingForexBob

//@version=4
study(title="CCPD Candle Color Price Detector", shorttitle="CCPD", overlay=true)

// Input to adjust the number of bars for highest high and lowest low calculation
barsForHighLow = input(10, title="Bars for High/Low", minval=1)

var color currentColor = na
var float highestHigh = na
var float lowestLow = na

// Calculate highest high and lowest low
if bar_index >= barsForHighLow
    highestHigh := highest(high, barsForHighLow)
    lowestLow := lowest(low, barsForHighLow)

if close > open
    currentColor := color.green
else
    currentColor := color.red

// Calculate the midpoint between highest high and lowest low
midpoint = (highestHigh + lowestLow) / 2

// Plot buy and sell signals with EMA filter
plotshape(currentColor == color.green and close > midpoint, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, title="Buy Signal")
plotshape(currentColor == color.red and close < midpoint, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, title="Sell Signal")

// Calculate the midpoint line
plot(highestHigh, title="Highest High", color=color.orange)
plot(lowestLow, title="Lowest Low", color=color.orange)

// Plot the midpoint line
plot(midpoint, title="Midpoint", color=color.purple, linewidth=2)

// Plot a line between midpoint and highest high
plot((midpoint + highestHigh) / 2, title="Midpoint to Highest High", color=color.green, linewidth=1)

// Plot a line between midpoint and lowest low
plot((midpoint + lowestLow) / 2, title="Midpoint to Lowest Low", color=color.green, linewidth=1)

// Plot a line between green line and highest high
plot((highestHigh + (midpoint + highestHigh) / 2) / 2, title="Midpoint of Midpoint of Highest High", color=color.white, linewidth=1)

// Plot a line between red line and lowest low
plot((lowestLow + (midpoint + lowestLow) / 2) / 2, title="Midpoint of Midpoint of Lowest Low", color=color.white, linewidth=1)


// Create alerts for buy and sell signals
alertcondition(currentColor == color.green and close > midpoint, title="Buy Signal Alert", message="Buy Signal Detected!")
alertcondition(currentColor == color.red and close < midpoint, title="Sell Signal Alert", message="Sell Signal Detected!")