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.
Here is the script:
The market state is displayed in a simple label on the bottom right side of the price chart. Here is a screenshot.
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: