Adaptive CCI for ThinkOrSwim

Sesqui

New member
VIP
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.
1756698962171.png


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);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
206 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top