In a previous post I provided a script for the Adaptive Schaff Trend Cycle of Price (close). Here are the scripts for applying the Adaptive Schaff Trend Cycle to the On Balance Volume (OBV) and then to the Relative Volatility Index (RVI). The STC is made adaptive using EhlersAutocorrelationPeriodogram.
With all three you have adaptive price, volume and volatility indicators if needed.
Script for Adaptive STC of OBV:
Script for adaptive STC of Relative Volatility Index (RVI):
With all three you have adaptive price, volume and volatility indicators if needed.
Script for Adaptive STC of OBV:
CSS:
#Adaptive Schaff Trend Cycle (STC) of On Balance Volume (OBV) by Sesqui (15AUG2025)
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 KPeriod = 5;
input DPeriod = 3;
input over_bought = 80;
input over_sold = 20;
input averageType = AverageType.EXPONENTIAL;
def STC_SOURCE = OnBalanceVolume();
def macd = AdaptiveMACD(STC_SOURCE, 3, 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);
Script for adaptive STC of Relative Volatility Index (RVI):
CSS:
# Script for Adaptive Schaff Trend Cycle of Relative Volatility Index (RVI)
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 slowLength = 5;
input KPeriod = 5;
input DPeriod = 3;
input over_bought = 80;
input over_sold = 20;
input averageType = AverageType.EXPONENTIAL;
def STC_SOURCE = RelativeVolatilityIndex( stDevLength = fastLength, averageLength = slowLength);
def macd = AdaptiveMACD(STC_SOURCE, 3, 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);