Paste the indicator into Pine Editor and add it to a standard candle chart. The Pane scale input displays CCI or Stochastic without visually mixing their scales; all values remain available in the Data Window. C and K mark entries into the corresponding extreme zones. The script uses chart OHLC, not the synthetic fixture, and places no orders. Its shared readiness gate compares CCI with K; D is displayed but does not gate the threshold tests.
Complete Pine v6 indicator. Default lookback 5, K smoothing 1 and D smoothing 3 match the calculation fixture; market-chart readings depend on the chart data.pine
//@version=6
indicator("CCI vs Stochastic: matched bars", overlay = false, max_bars_back = 500)
length = input.int(5, "Shared lookback", minval = 2, maxval = 200)
smoothK = input.int(1, "K smoothing", minval = 1, maxval = 200)
lengthD = input.int(3, "D smoothing", minval = 1, maxval = 200)
pane = input.string("CCI", "Pane scale", options = ["CCI", "Stochastic"])
// Require consecutive observations; an na input invalidates the average.
strictSma(float source, int count) =>
float total = 0.0
for i = 0 to count - 1
total += source[i]
total / count
typical = hlc3
mean = strictSma(typical, length)
float deviationSum = 0.0
bool fullWindow = bar_index >= length - 1
for i = 0 to length - 1
deviationSum += math.abs(typical[i] - mean)
fullWindow := fullWindow and not na(high[i]) and not na(low[i]) and not na(close[i])
meanDeviation = deviationSum / length
cci = fullWindow and meanDeviation > 0 ? (typical - mean) / (0.015 * meanDeviation) : na
windowHigh = ta.highest(high, length)
windowLow = ta.lowest(low, length)
priceRange = windowHigh - windowLow
rawK = fullWindow and priceRange > 0 ? 100 * (close - windowLow) / priceRange : na
k = strictSma(rawK, smoothK)
d = strictSma(k, lengthD)
ready = not na(cci) and not na(k)
confirmed = barstate.isconfirmed
cciUpper = ready and cci > 100
stochUpper = ready and k > 80
cciLower = ready and cci < -100
stochLower = ready and k < 20
cciEnterUpper = confirmed and ready and ready[1] and cciUpper and cci[1] <= 100
stochEnterUpper = confirmed and ready and ready[1] and stochUpper and k[1] <= 80
cciEnterLower = confirmed and ready and ready[1] and cciLower and cci[1] >= -100
stochEnterLower = confirmed and ready and ready[1] and stochLower and k[1] >= 20
plot(confirmed and pane == "CCI" ? cci : na, "CCI pane", color.blue)
plot(confirmed and pane == "Stochastic" ? k : na, "K pane", color.orange)
plot(confirmed and pane == "Stochastic" ? d : na, "D pane", color.teal)
plot(confirmed ? cci : na, "CCI value", display = display.data_window)
plot(confirmed ? rawK : na, "Raw K value", display = display.data_window)
plot(confirmed ? k : na, "K value", display = display.data_window)
plot(confirmed ? d : na, "D value", display = display.data_window)
plot(confirmed ? mean : na, "Typical price SMA", display = display.data_window)
plot(confirmed ? meanDeviation : na, "Mean absolute deviation", display = display.data_window)
plot(confirmed and ready ? (cciUpper ? 1 : 0) : na, "CCI above 100", display = display.data_window)
plot(confirmed and ready ? (stochUpper ? 1 : 0) : na, "K above 80", display = display.data_window)
plot(confirmed and ready ? (cciLower ? 1 : 0) : na, "CCI below -100", display = display.data_window)
plot(confirmed and ready ? (stochLower ? 1 : 0) : na, "K below 20", display = display.data_window)
plotchar(cciEnterUpper, "CCI enters upper zone", "C", location.top, color.blue)
plotchar(stochEnterUpper, "K enters upper zone", "K", location.top, color.orange)
plotchar(cciEnterLower, "CCI enters lower zone", "C", location.bottom, color.blue)
plotchar(stochEnterLower, "K enters lower zone", "K", location.bottom, color.orange)