Adaptive Aroon, MACD, Schaff Trend Cycle, and Williams %R Indicators for ThinkOrSwim

Sesqui

New member
VIP
Screenshot from 2025-08-29 09-10-23.png


The indicators provided below use the dominant cycle period obtained from Dr Ehlers AutoCorrelationPeriodogram to make them adaptive to the market cycle. The screenshot above shows what they look like on the lower portion of the chart.

Here is the script code for each.

Adaptive Aroon Indicator:

CSS:
# Adaptive Aroon Indicator script by Sesqui 29AUG2025

declare lower;
input Beta = 1.0;
input showUpDownLines = 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 getOffsetToRecentMax {
    input CycleLength = 25;
    def Length = if IsNaN(Floor(CycleLength)) then Length[1] else Floor(CycleLength);
    def OffsetToRecentMax = fold i = 0 to Length with index = 0 do if GetValue(high, i) > GetValue(high, index) then index + 1 else index;
    plot BarsToMaxHigh = OffsetToRecentMax;
    BarsToMaxHigh.HideBubble();
}# end script getOffsetToRecentMax

#------------------------------------------------------

script getOffsetToRecentMin {
    input CycleLength = 25;
    def Length = if IsNaN(Floor(CycleLength)) then Length[1] else Floor(CycleLength);
    def OffsetToRecentMin = fold i = 0 to Length with index = 0 do if GetValue(low, i) < GetValue(low, index) then index + 1 else index;
    plot BarsToMaxLow = OffsetToRecentMin;
    BarsToMaxLow.HideBubble();
}# end script getOffsetToRecentMin

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

def DCLength= GetCycle().Cycle;
def CycleLength = if IsNaN(Floor(DCLength)) then CycleLength[1] else Floor(DCLength);

def Up_ = (Floor(Beta*(CycleLength/2)) - 1 - getOffsetToRecentMax(Floor(Beta*(CycleLength/2)))) * 100.0 / (Floor(Beta*0.5*CycleLength) - 1);
def Down_ = (Floor(Beta*(CycleLength/2)) - 1 - getOffsetToRecentMin(Floor(Beta*(CycleLength/2)))) * 100.0 / (Floor(Beta*0.5*CycleLength) - 1);
plot Trend = Up_ - Down_;

Trend.AssignvalueColor(Color.PLUM);
Trend.SetLineWeight(2);

plot Up = if showUpDownLines then Up_ else Double.NaN;
plot Down = if showUpDownLines then Down_ else Double.NaN;

plot OverBought = 50;
plot OverSold = -50;

Up.SetDefaultColor(GetColor(1));
Down.SetDefaultColor(GetColor(5));
OverBought.SetDefaultColor(GetColor(8));
OverSold.SetDefaultColor(GetColor(8));


Adaptive MACD indicator:

CSS:
# Adaptive MACD (AMACD) script by Sesqui
# 5AUG2025

declare lower;
declare real_size;

input ShowHIST = yes;
input ShowZeroLine = yes;

#===========================================================================================================================
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 AdaptiveEMA {
    input src = close;
    input Cycle = 10;
    input numCycles = 1;

    def length = if IsNaN(Floor(numCycles * Cycle)) then length[1] else Floor(numCycles*Cycle);
    def ExpMovAvg = src*(2/(1+length))+ExpMovAvg[1]*(1-(2/(1+length)));
    plot EMA = ExpMovAvg;
    EMA.HideBubble();

}# endScript AdaptiveEMA{}

#------------------------------------------------
script AdaptiveMACD {
    # Computes the Adaptive MACD
    input fastLength = 3;
    input Cycle = 10; # pass Cycle into this parameter for adaptive MACD
    input SmoothLength = 16;
    input averageType = AverageType.EXPONENTIAL;

    def CycleLength = if IsNaN(Floor(Cycle)) then CycleLength[1] else Floor(Cycle);
    plot Value = MovingAverage(averageType, close, fastLength) - AdaptiveEMA(close, CycleLength).EMA;
    
    plot Avg = MovingAverage(averageType, Value, SmoothLength);

}# end AdaptiveMACD{}
#==============================================================================================

def CycleLength = GetCycle().Cycle;

plot val = AdaptiveMACD(3, CycleLength).Value;
plot ave = AdaptiveMACD(3, CycleLength).Avg;
plot Diff = if ShowHIST == 1 then val - ave else Double. NaN;
plot ZeroLine = if ShowZeroLine == 1 then 0 else Double.NaN;
ZeroLine.SetDefaultColor(GetColor(0));

val.SetDefaultColor(GetColor(1));
ave.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.Color("Positive and Up") else Diff.Color("Positive and Down") else if Diff < Diff[1] then Diff.Color("Negative and Down") else Diff.Color("Negative and Up"));

AddCloud(val, ave, Color.GREEN, Color.RED);



Adaptive Williams % R indicator:

CSS:
# Adpative Aroon Indicator script by Sesqui 29AUG2025
declare lower;

input length = 10;
input overBought = -20;
input overSold = -80;

#===========================================================================================================================
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 get_Highest {
    input CycleLength = 10;
    input source = high;
    def length = if IsNaN(Floor(CycleLength)) then length[1] else Floor(CycleLength);
    def cur = source[0];
    def val = fold i = 0 to Length with max = cur do if max >= GetValue(source, i + 1) then max else GetValue(source, i+1);
    plot Highest = val;
    Highest.HideBubble();
}# end Script getHighest{}

#--------------
script get_Lowest {
    input CycleLength = 10;
    input source = low;
    def length = if IsNaN(Floor(CycleLength)) then length[1] else Floor(CycleLength);
    def cur = source[0];
    def val = fold i = 0 to Length with min = cur do if min <= GetValue(source, i + 1) then min else GetValue(source, i+1);
    plot Lowest = val;
    Lowest.HideBubble();
}# end Script getLowest{}

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

def CycleLength = GetCycle().Cycle;

def hh = get_Highest(CycleLength, high);
def ll = get_Lowest(CycleLength, low);
def result = if hh == ll then -100 else (hh - close) / (hh - ll) * (-100);

plot WR = if result > 0 then 0 else result;
WR.SetDefaultColor(GetColor(1));

plot Over_Sold = overSold;
Over_Sold.SetDefaultColor(GetColor(8));

plot Over_Bought = overBought;
Over_Bought.SetDefaultColor(GetColor(8));


Adaptive Schaff Trend Cycle (ASTC) indicator:

CSS:
# Adaptive Schaff Trend Cycle script by Sesqui
# 25AUG2025

declare lower;

#===========================================================================================================================
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 AdaptiveEMA {
    input src = close;
    input Cycle = 10;
    input numCycles = 1;

    def length = if IsNaN(Floor(numCycles * Cycle)) then length[1] else Floor(numCycles*Cycle);
    def ExpMovAvg = src*(2/(1+length))+ExpMovAvg[1]*(1-(2/(1+length)));
    plot EMA = ExpMovAvg;
    EMA.HideBubble();

}# endScript AdaptiveEMA{}

#------------------------------------------------
script AdaptiveMACD {
    # Computes the Adaptive MACD
    input source = close;
    input fastLength = 3;
    input Cycle = 10; # pass Cycle into this parameter for adaptive MACD
    input SmoothLength = 16;
    input averageType = AverageType.EXPONENTIAL;

    def CycleLength = if IsNaN(Floor(Cycle)) then CycleLength[1] else Floor(Cycle);
    plot Value = MovingAverage(averageType, source, fastLength) - AdaptiveEMA(source, CycleLength).EMA;
    
    plot Avg = MovingAverage(averageType, Value, SmoothLength);

}# end AdaptiveMACD{}
#==============================================================================================

def CycleLength = GetCycle().Cycle;

input fastLength = 3;

input KPeriod = 5;
input DPeriod = 3;
input over_bought = 80;
input over_sold = 20;
input averageType = AverageType.EXPONENTIAL;

def STC_SOURCE = close;
def macd = AdaptiveMACD(STC_SOURCE, fastLength, CycleLength).Value;

def fastK1 = FastKCustom(macd, KPeriod);
def fastD1 = MovingAverage(averageType, fastK1, DPeriod);
def fastK2 = FastKCustom(fastD1, KPeriod);

plot STC = MovingAverage(averageType, fastK2, DPeriod);
STC.HideBubble();

plot CenterLine = 50;
CenterLine.HideBubble();
CenterLine.AssignValueColor(Color.GRAY);
CenterLine.SetPaintingStrategy(PaintingStrategy.DASHES);

plot OverBought = over_bought;
OverBought.HideBubble();

plot OverSold = over_sold;
OverSold.HideBubble();

STC.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(7));
OverSold.SetDefaultColor(GetColor(7));

def Diff = STC - STC[1];
STC.SetLineWeight(3);

STC.DefineColor("Positive and Up", Color.GREEN);
STC.DefineColor("Positive and Down", Color.DARK_GREEN);
STC.DefineColor("Negative and Down", Color.RED);
STC.DefineColor("Negative and Up", Color.DARK_RED);
STC.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then STC.color("Positive and Up") else STC.color("Positive and Down") else if Diff < Diff[1] then STC.color("Negative and Down") else STC.color("Negative and Up"));

AddCloud(OverSold,0,Color.LIGHT_GREEN);
AddCloud(100,OverBought,Color.LIGHT_RED);
 

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