The scripts for the indicators are provided below and enable the user to make them adaptive using the TOS AutoCorrelationPeriodogram or to tune them to the market dominant cycle using the Band-Pass Filter method from CH 5 of Dr Ehlers book, "Cycle Analytics for Traders".
The screenshot above shows what they look like on the lower portion of the chart when using the TOS Periodogram to make them adaptive. The following screenshot shows what they look like when the dominant cycle is more precisely obtained using the Band-Pass Filter method per CH 5 of Dr Ehlers' book, "Cycle Analytics for Traders":
Here is the script code for each.
Adaptive Aroon Indicator:
CSS:
# Adaptive Aroon indicator script by Sesqui
# [29AUG2025]: TOS periodogram method used to make it adaptive. However, TOS does not support matrices
# as needed to precisely nail down the dominant cycle periods. Furthermore, it take a lot of computational
# power and can bog down some computers. As a result this method is not recommended
#
# [18OCT2025]: Sesqui added a more precise method of obtaining the dominant cycle periods, the Band-Pass
# filter method per ch5 of Dr Ehlers' book, "Cycle Analytics for Traders". When tested against sinewaves
# across a wide range of periods, this method obtains precise dominant cycle period values and does not
# appear to bog down the computer in TOS.
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
#---------------------------------------------------
#===================================================================================
#*** Script to measure dominant cycle length using band-pass
#*** filter zero crossings method per Dr Ehlers
#*** Chapter 5 of "Cycle Analytics for Traders".
Script GetCycleViaBandPassFilter{
input source = ohlc4;
input Period = 20;
input Bandwidth = 0.70;
def cosPart = Cos(0.25 * Bandwidth * 2.0 * Double.Pi / Period);
def sinPart = Sin(0.25 * Bandwidth * 2.0 * Double.Pi / Period);
def alpha2 = (cosPart + sinPart - 1) / cosPart;
def HP = (1 + alpha2 / 2) * (source - source[1]) + (1 - alpha2) * HP[1];
def beta1 = Cos(2.0 * Double.Pi / Period);
def gamma1 = 1 / Cos(2.0 * Double.Pi * Bandwidth / Period);
def alpha1 = gamma1 - Sqrt(gamma1 * gamma1 - 1);
def BP = If BarNumber() == 1 or BarNumber() == 2 then 0 else 0.5 * (1 - alpha1) * (HP - HP[2]) + beta1 * (1 + alpha1) * BP[1] - alpha1 * BP[2];
def Peak = if absValue(BP) > 0.991*Peak[1] then AbsValue(BP) else 0.991*Peak[1];
def Real = if Peak <> 0 then BP/Peak else Double.NaN;
def crossAbove = Real crosses above 0;
def crossBelow = Real crosses below 0;
def zeroCrossing = crossAbove or crossBelow;
# Uses a recursive variable to count the bars between zero-crossings
# If a zero-crossing happens, resets the count to 0.
# Otherwise, increments the count by 1.
def barsSinceCross = if zeroCrossing then 0 else barsSinceCross[1] + 1;
def barsBetweenCrossings = barsSinceCross + 1;
def val = if barsBetweenCrossings < barsBetweenCrossings[1] then 2*barsBetweenCrossings[1] else val[1];
# The following line is a port of Dr Ehlers EasyLanguage program, kept here for reference
#def DC = If barsBetweenCrossings < 3 then 3 else If 2*(barsBetweenCrossings) > 1.25*DC[1] then 1.25*DC[1] else if 2*(barsBetweenCrossings) < 0.8*DC[1] then 0.8*DC[1] else DC[1];
plot DC = val;
DC.HideBubble();
} # End script GetCycleViaBandPassFilter{}
#=============================================================================
# *** Computes the Adaptive Aroon indicator in this section ***
input BandPassSource = ohlc4;
input BandPassPeriod = 20;
input BandPassBandwidth = 0.70;
def Cycle = GetCycleViaBandPassFilter(BandPassSource, BandPassPeriod, BandPassBandwidth).DC;
def CycleLength = if IsNaN(Floor(Cycle)) then CycleLength[1] else Floor(Cycle);
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]: TOS Periodogram method applied. However, TOS does not suppotr matrices as needed to compute
# the precise dominant market cycles and this method bogs down the computer. As a result it is not recomended.
#
# [18OCT2025]: Sesqui added a precise method of obtaining the dominant cycle, the Band-Pass filter method per
# CH 5 of Dr Ehlers' book, "Cycle Analytics for Traders". This method when tested using a sinewave across a wide
# range of periods, obtained precise dominant cycle values and was shown to perform wwell in TOS
declare lower;
declare real_size;
input FastMACDLength = 3;
input MACDSmoothLength = 16;
input ShowHISTogram = 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{}
#------------------------------------------------
# Adaptive EMA used by Adaptive MACD (AMACD)
script AdaptiveEMA {
input src = close;
input Cycle = 10;
def length = if IsNaN(Floor(Cycle)) then length[1] else Floor(Cycle);
def ExpMovAvg = if !IsNaN(ExpMovAvg[1]) then src*(2/(1+length))+ExpMovAvg[1]*(1-(2/(1+length))) else src*(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);
Value.HideBubble();
Avg.HideBubble();
}# end AdaptiveMACD{}
#-----------------------------------------------
#===================================================================================
#*** Script to measure dominant cycle length using band-pass
#*** filter zero crossings method per Dr Ehlers
#*** Chapter 5 of "Cycle Analytics for Traders".
Script GetCycleViaBandPassFilter{
input source = ohlc4;
input Period = 20;
input Bandwidth = 0.70;
def cosPart = Cos(0.25 * Bandwidth * 2.0 * Double.Pi / Period);
def sinPart = Sin(0.25 * Bandwidth * 2.0 * Double.Pi / Period);
def alpha2 = (cosPart + sinPart - 1) / cosPart;
def HP = (1 + alpha2 / 2) * (source - source[1]) + (1 - alpha2) * HP[1];
def beta1 = Cos(2.0 * Double.Pi / Period);
def gamma1 = 1 / Cos(2.0 * Double.Pi * Bandwidth / Period);
def alpha1 = gamma1 - Sqrt(gamma1 * gamma1 - 1);
def BP = If BarNumber() == 1 or BarNumber() == 2 then 0 else 0.5 * (1 - alpha1) * (HP - HP[2]) + beta1 * (1 + alpha1) * BP[1] - alpha1 * BP[2];
def Peak = if absValue(BP) > 0.991*Peak[1] then AbsValue(BP) else 0.991*Peak[1];
def Real = if Peak <> 0 then BP/Peak else Double.NaN;
def crossAbove = Real crosses above 0;
def crossBelow = Real crosses below 0;
def zeroCrossing = crossAbove or crossBelow;
# Uses a recursive variable to count the bars between zero-crossings
# If a zero-crossing happens, resets the count to 0.
# Otherwise, increments the count by 1.
def barsSinceCross = if zeroCrossing then 0 else barsSinceCross[1] + 1;
def barsBetweenCrossings = barsSinceCross + 1;
def val = if barsBetweenCrossings < barsBetweenCrossings[1] then 2*barsBetweenCrossings[1] else val[1];
# The following line is a port of Dr Ehlers EasyLanguage program, kept here for reference
#def DC = If barsBetweenCrossings < 3 then 3 else If 2*(barsBetweenCrossings) > 1.25*DC[1] then 1.25*DC[1] else if 2*(barsBetweenCrossings) < 0.8*DC[1] then 0.8*DC[1] else DC[1];
plot DC = val;
DC.HideBubble();
} # End script GetCycleViaBandPassFilter{}
#=============================================================================
#=============================================================================
# *** Computes the Adaptive MACD (AMACD) indicator in this section ***
input BandPassSource = ohlc4;
input BandPassPeriod = 20;
input BandPassBandwidth = 0.70;
def Cycle = GetCycleViaBandPassFilter(BandPassSource, BandPassPeriod, BandPassBandwidth).DC;
def CycleLength = if IsNaN(Floor(Cycle)) then CycleLength[1] else Floor(Cycle);
plot val = AdaptiveMACD( FastMACDLength , CycleLength).Value;
plot ave = AdaptiveMACD(FastMACDLength , CycleLength).Avg;
plot Diff = if ShowHISTogram == 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 Williams Percent R Indicator script by Sesqui
# [29AUG2025]: Added the TOS Periodogram method. However, TOS does not support matrices as needed to compute
# the precise dominant cycle values as needed. Furthermore, since this method is also computatioanlly demanding
# on computers, it is not recommended.
#
# [18OCT2025]: Sesqui added a more precise method of obtaining dominant cycle values, the Band-Pass Filter method per
# CH 5 of Dr Ehlers' "Cycle analytics for Traders". This method is not as computatioanlly demanding and when tested
# against sinewaves across a wide range of periods, it obtained precise dominant cycle values.
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{}
#---------------------------------------------------------------
#===================================================================================
#*** Script to measure dominant cycle length using band-pass
#*** filter zero crossings method per Dr Ehlers
#*** Chapter 5 of "Cycle Analytics for Traders".
Script GetCycleViaBandPassFilter{
input source = ohlc4;
input Period = 20;
input Bandwidth = 0.70;
def cosPart = Cos(0.25 * Bandwidth * 2.0 * Double.Pi / Period);
def sinPart = Sin(0.25 * Bandwidth * 2.0 * Double.Pi / Period);
def alpha2 = (cosPart + sinPart - 1) / cosPart;
def HP = (1 + alpha2 / 2) * (source - source[1]) + (1 - alpha2) * HP[1];
def beta1 = Cos(2.0 * Double.Pi / Period);
def gamma1 = 1 / Cos(2.0 * Double.Pi * Bandwidth / Period);
def alpha1 = gamma1 - Sqrt(gamma1 * gamma1 - 1);
def BP = If BarNumber() == 1 or BarNumber() == 2 then 0 else 0.5 * (1 - alpha1) * (HP - HP[2]) + beta1 * (1 + alpha1) * BP[1] - alpha1 * BP[2];
def Peak = if absValue(BP) > 0.991*Peak[1] then AbsValue(BP) else 0.991*Peak[1];
def Real = if Peak <> 0 then BP/Peak else Double.NaN;
def crossAbove = Real crosses above 0;
def crossBelow = Real crosses below 0;
def zeroCrossing = crossAbove or crossBelow;
# Uses a recursive variable to count the bars between zero-crossings
# If a zero-crossing happens, resets the count to 0.
# Otherwise, increments the count by 1.
def barsSinceCross = if zeroCrossing then 0 else barsSinceCross[1] + 1;
def barsBetweenCrossings = barsSinceCross + 1;
def val = if barsBetweenCrossings < barsBetweenCrossings[1] then 2*barsBetweenCrossings[1] else val[1];
# The following line is a port of Dr Ehlers EasyLanguage program, kept here for reference
#def DC = If barsBetweenCrossings < 3 then 3 else If 2*(barsBetweenCrossings) > 1.25*DC[1] then 1.25*DC[1] else if 2*(barsBetweenCrossings) < 0.8*DC[1] then 0.8*DC[1] else DC[1];
plot DC = val;
DC.HideBubble();
} # End script GetCycleViaBandPassFilter{}
#=============================================================================
# *** Computes the adaptive Williams Percent R indicator in this section ***
input BandPassSource = ohlc4;
input BandPassPeriod = 20;
input BandPassBandwidth = 0.70;
def Cycle = GetCycleViaBandPassFilter(BandPassSource, BandPassPeriod, BandPassBandwidth).DC;
def CycleLength = if IsNaN(Floor(Cycle)) then CycleLength[1] else Floor(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));
plot midline = -50;
midline.AssignValueColor(Color.White);
midline.hidebubble();
midline.SetPaintingStrategy(PaintingStrategy.DASHES);
Adaptive Schaff Trend Cycle (ASTC) indicator:
CSS:
# Adaptive Schaff Trend Cycle script by Sesqui
#
# [25AUG2025]: TOS Periodogram method applied. However, TOS does not support matrices as needed to precisely
# obtain the dominant cycle periods. Furthermore, the method is computatioanlly demanding and can slow down
# the computer. As a result, it is not recommneded.
#
# [18OCT2025]: Sesqui added a more precise method of obtaining the Dominant Cycle lengths, the Band-Pass
# Filter method per Ch 5 of Dr Ehlers' book, "Cycle Analytics for Traders". When tested against sinewaves with
# a wide range of periods, this method obtians precise values for the dominant cycle and performs well in TOS.
#================================================================================================
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{}
#------------------------------------------------
# Adaptive EMA used by Adaptive MACD (AMACD)
script AdaptiveEMA {
input src = close;
input Cycle = 10;
def length = if IsNaN(Floor(Cycle)) then length[1] else Floor(Cycle);
def ExpMovAvg = if !IsNaN(ExpMovAvg[1]) then src * (2 / (1 + length)) + ExpMovAvg[1] * (1 - (2 / (1 + length))) else src * (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{}
#---------------------------------------------------
#===================================================================================
#*** Script to measure dominant cycle length using band-pass
#*** filter zero crossings method per Dr Ehlers
#*** Chapter 5 of "Cycle Analytics for Traders".
script GetCycleViaBandPassFilter {
input source = ohlc4;
input Period = 20;
input Bandwidth = 0.70;
def cosPart = Cos(0.25 * Bandwidth * 2.0 * Double.Pi / Period);
def sinPart = Sin(0.25 * Bandwidth * 2.0 * Double.Pi / Period);
def alpha2 = (cosPart + sinPart - 1) / cosPart;
def HP = (1 + alpha2 / 2) * (source - source[1]) + (1 - alpha2) * HP[1];
def beta1 = Cos(2.0 * Double.Pi / Period);
def gamma1 = 1 / Cos(2.0 * Double.Pi * Bandwidth / Period);
def alpha1 = gamma1 - Sqrt(gamma1 * gamma1 - 1);
def BP = if BarNumber() == 1 or BarNumber() == 2 then 0 else 0.5 * (1 - alpha1) * (HP - HP[2]) + beta1 * (1 + alpha1) * BP[1] - alpha1 * BP[2];
def Peak = if AbsValue(BP) > 0.991 * Peak[1] then AbsValue(BP) else 0.991 * Peak[1];
def Real = if Peak <> 0 then BP / Peak else Double.NaN;
def crossAbove = Real crosses above 0;
def crossBelow = Real crosses below 0;
def zeroCrossing = crossAbove or crossBelow;
# Uses a recursive variable to count the bars between zero-crossings
# If a zero-crossing happens, resets the count to 0.
# Otherwise, increments the count by 1.
def barsSinceCross = if zeroCrossing then 0 else barsSinceCross[1] + 1;
def barsBetweenCrossings = barsSinceCross + 1;
def val = if barsBetweenCrossings < barsBetweenCrossings[1] then 2 * barsBetweenCrossings[1] else val[1];
# The following line is a port of Dr Ehlers EasyLanguage program, kept here for reference
#def DC = If barsBetweenCrossings < 3 then 3 else If 2*(barsBetweenCrossings) > 1.25*DC[1] then 1.25*DC[1] else if 2*(barsBetweenCrossings) < 0.8*DC[1] then 0.8*DC[1] else DC[1];
plot DC = val;
DC.HideBubble();
} # End script GetCycleViaBandPassFilter{}
#----------------------------------------------------------
#--------------
script get_Highest {
input source = high;
input CycleLength = 10;
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 source = low;
input CycleLength = 10;
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{}
#------------------------------------------------------------------------------
script AdaptSMA {
input source = close;
input CycleLength = 10;
def length = if IsNaN(Floor(CycleLength)) then length[1] else Floor(CycleLength);
def sum = fold index = 0 to length with s = 0 do s + GetValue(source, index);
plot SMA = sum / CycleLength;
SMA.HideBubble();
}# End Script AdaptSMA{}
#======================================================================================
# *** Computes the Adaptive SchaffTrendCycle Trend Cycle (ASTC) in this section ***
input BandPassSource = ohlc4;
input BandPassPeriod = 20;
input BandPassBandwidth = 0.70;
#def Cycle = GetCycle().Cycle;
def Cycle = GetCycleViaBandPassFilter(BandPassSource, BandPassPeriod, BandPassBandwidth).DC;
def CycleLength = if IsNaN(Floor(Cycle)) then CycleLength[1] else Floor(Cycle);
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, 3, CycleLength).Value;
# Computes (Close - Low)/(High - Low) over a lookback of KPeriod:
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);
Note: Articles from Dr Ehlers use arrays to normalize the periodogram data and then a center of gravity method to pin point the dominant cycle. However, thinkscript does not support arrays as needed to do the drill down further from the periodogram to the precise dominant cycle, per se. However, the cycle value provided by the periodogram in TOS appears to be capable of finding more trading opportunities than found with static fixed length values. Be sure to test it out before using it.
Indeed, if your strategy requires a more precise identification of the market dominant cycle test drive the Band-Pass Filter method provided above. When tested using sinewaves across a wide range of periods, the Band-Pass filter method obtained precise dominant cycle values and did not seem to bog down the computer.
Last edited: