Adaptive Market State Indicator for ThinkOrSwim

Sesqui

New member
VIP
Have you ever noticed that extra zing to the price action when trading with the trend? To help better identify the market state the script below modified the Dr Ehlers CorrelationCycleMarketState indicator to make it adaptive to the dominant market cycle using the EhlersAutocorrelationPeriodogram. As a result the indicator tells you if the market is trending up, down or is ranging. And it uses the dominant market cycle to do that to ensure there is no question as to whether its length is set right.

The market state is displayed in a simple label on the bottom right side of the price chart. Here is a screenshot.

Screenshot from 2025-08-31 11-50-02.png


Here is the script:

CSS:
#Adaptive Market State Label for ThinkOrSwim by Sesqui (30AUG2025)

#-----AutocorelationPeriodogram----------------
#
# Charles Schwab & Co. (c) 2008-2025
#

input lag = 48;

def x = EhlersRoofingFilter("cutoff length" = 2, "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);

def Periodogram = ExpAverage(sqSum, 3);
#-----------------------------------------------
#================================================================================
# Makes the CorrelationCycle Indicator Adaptable
# Modified by implementing nested folds to enable adaptive indicator
def limit = if IsNaN(Periodogram) then limit[1] else Floor(Periodogram);

def sx = fold ii = 0 to limit with S do S + GetValue(close, ii); #Sum(close, length);
def sxx = fold jj = 0 to limit with SS do SS + GetValue(Sqr(close), jj); #Sum(Sqr(close), length);
def sy1 = fold i1 = 0 to limit with s1 do (s1 + Cos(2 * Double.Pi * i1 / limit));
def sxy1 = fold i2 = 0 to limit with s2 do (s2 + GetValue(close, i2) * Cos(2 * Double.Pi * i2 / limit));
def syy1 = fold i3 = 0 to limit with s3 do (s3 + Sqr(Cos(2 * Double.Pi * i3 / limit)));

def sxx_sx = limit * sxx - Sqr(sx);
def corrCosine = if sxx_sx == 0 then 0 else (limit * sxy1 - sx * sy1) / Sqrt(sxx_sx * (limit * syy1 - Sqr(sy1)));

def sy2 = fold j1 = 0 to limit with t1 do (t1 - Sin(2 * Double.Pi * t1 / limit));
def sxy2 = fold j2 = 0 to limit with t2 do (t2 - GetValue(close, j2) * Sin(2 * Double.Pi * j2 / limit));
def syy2 = fold j3 = 0 to limit with t3 do (t3 + Sqr(Sin(2 * Double.Pi * j3 / limit)));

def corrNegSine = if sxx_sx == 0 then 0 else (limit * sxy2 - sx * sy2) / Sqrt(sxx_sx * (limit * syy2 - Sqr(sy2)));

def CorrelationWithCosine = corrCosine;
def CorrelationWithNegativeSine = corrNegSine;

#============================================================
# CorrelationCycleAngle Indicator made adaptive

#
# Charles Schwab & Co. (c) 2008-2025
#

def real = CorrelationWithCosine;
def imag = CorrelationWithNegativeSine;

def angleRad = if imag != 0 then Double.Pi / 2 + ATan(real / imag) - (if imag > 0 then Double.Pi else 0) else 0;
def angle = 180 / Double.Pi * angleRad;
def positiveAngle = if IsNaN(positiveAngle[1]) then 0 else if positiveAngle[1] - angle < 270 and angle < positiveAngle[1] then positiveAngle[1] else angle;

def CorrelationAngle = positiveAngle;

#==============================================================

#----MarketState indicator modified to make it adpative----

#
# Charles Schwab & Co. (c) 2008-2025
#

input trendLength = 40;
def trendAngle = 360 / trendLength;

def MarketState;
if (AbsValue(angle - angle[1]) < trendAngle and angle < 0) {
    MarketState = -1;
} else if (AbsValue(angle - angle[1]) < trendAngle and angle >= 0) {
    MarketState = 1;
} else {
    MarketState = 0;
}

#================================================================
# Label for Market State----------------------------
AddLabel(yes, if MarketState == 1 then "Adaptive: Trending UP" else if MarketState == -1 then "Adaptive: Trending DOWN" else "Adaptive: RANGING", if MarketState == 1 then Color.GREEN else if MarketState == -1 then Color.RED else CreateColor(255, 255, 204), Location.BOTTOM_RIGHT, FontSize.SMALL);
 
Last edited:

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
310 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