Here is a script that uses the EhlersAutoCorrelationPeriodogram to make the Commodity Channel Index (CCI) adaptive so that it uses the dominant market cycle, eliminating any doubt that its length is set right. Some traders recommend using 1/3*cycle length for the period, so an input has been included and is currently set to 1/3, however you can change it to any acceptable value.
The adaptive CCI is showin in the lower chart of this screenshot.
Here is the script:
The adaptive CCI is showin in the lower chart of this screenshot.
Here is the script:
CSS:
# Adaptive CCI using EhlersAutoCorrelationPeriodogram by Sesqui 31AUG2025
declare lower;
input CycleFraction = 0.333;
input over_sold = -100;
input over_bought = 100;
input showBreakoutSignals = no;
#===========================================================================================================================
script GetCycle {
# Returns the dominant market cycle for use in adaptive indicators
#------------------------------------------
# Charles Schwab & Co. (c) 2016-2025
#
def lag = 48;
def x = EhlersRoofingFilter("cutoff length" = 8, "roof cutoff length" = 48);
def cosinePart = fold i = 3 to 48 with cosPart do cosPart + (3 * (x * GetValue(x, i) + GetValue(x, 1) * GetValue(x, i + 1) + GetValue(x, 2) * GetValue(x, i + 2)) - (x + GetValue(x, 1) + GetValue(x, 2)) * (GetValue(x, i) + GetValue(x, i + 1) + GetValue(x, i + 2))) / Sqrt((3 * (x * x + GetValue(x, 1) * GetValue(x, 1) + GetValue(x, 2) * GetValue(x, 2)) - Sqr(x + GetValue(x, 1) + GetValue(x, 2))) * (3 * (GetValue(x, i) * GetValue(x, i) + GetValue(x, i + 1) * GetValue(x, i + 1) + GetValue(x, i + 2) * GetValue(x, i + 2)) - Sqr(GetValue(x, i) + GetValue(x, i + 1) + GetValue(x, i + 2)))) * Cos(2 * Double.Pi * i / lag);
def sinePart = fold j = 3 to 48 with sinPart do sinPart + (3 * (x * GetValue(x, j) + GetValue(x, 1) * GetValue(x, j + 1) + GetValue(x, 2) * GetValue(x, j + 2)) - (x + GetValue(x, 1) + GetValue(x, 2)) * (GetValue(x, j) + GetValue(x, j + 1) + GetValue(x, j + 2))) / Sqrt((3 * (x * x + GetValue(x, 1) * GetValue(x, 1) + GetValue(x, 2) * GetValue(x, 2)) - Sqr(x + GetValue(x, 1) + GetValue(x, 2))) * (3 * (GetValue(x, j) * GetValue(x, j) + GetValue(x, j + 1) * GetValue(x, j + 1) + GetValue(x, j + 2) * GetValue(x, j + 2)) - Sqr(GetValue(x, j) + GetValue(x, j + 1) + GetValue(x, j + 2)))) * Sin(2 * Double.Pi * j / lag);
def sqSum = Sqr(cosinePart) + Sqr(sinePart);
plot Cycle = ExpAverage(sqSum, 9);
#-------------------------------------------
}# end Script GetCycle{}
script getStdDeviation {
input Cycle = 10;
input source = close;
def CycleLength = if IsNaN(Floor(Cycle)) then CycleLength[1] else Floor(Cycle);
def sum = fold i = 0 to CycleLength with s = 0 do s + GetValue(source, i);
def x_bar = sum / CycleLength;
def SumDeviationSquared = fold j = 0 to CycleLength with sd = 0 do sd + Sqr(GetValue(source, j) - x_bar);
def avgDev = SumDeviationSquared / CycleLength;
def Sigma = Sqrt(avgDev);
plot StdDev = Sigma;
}#end Script getStdDeviation{}
script GetAvg {
input Cycle = 10;
input source = close;
def CycleLength = if IsNaN(Floor(Cycle)) then CycleLength[1] else Floor(Cycle);
def sum = fold i = 0 to CycleLength with s = 0 do s + GetValue(source, i);
plot AvgDev = sum / CycleLength;
}# End Script AgetAvg{}
#==============================================================================================
def CycleLength = GetCycle().Cycle;
def price = close + low + high;
def LinDev = GetStdDeviation(CycleLength*CycleFraction, price);
plot CCI = if linDev == 0 then 0 else (price - GetAvg(CycleLength*CycleFraction, price)) /(LinDev * 0.015);
CCI.AssignValueColor(Color.WHITE);
plot OverBought = over_bought;
plot ZeroLine = 0;
plot OverSold = over_sold;
plot UpSignal = if CCI crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if CCI crosses below ZeroLine then ZeroLine else Double.NaN;
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
OverBought.SetDefaultColor(GetColor(5));
ZeroLine.SetDefaultColor(GetColor(5));
OverSold.SetDefaultColor(GetColor(5));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);