Schaff Trend Cycle 1
Download scriptThis script is a specialized technical analysis tool designed to identify trends and momentum in financial markets. It works by layering different types of mathematical filters on top of each other—essentially smoothing out market "noise" to reveal the underlying direction of price movement.
Here is a breakdown of the logic used to generate the final visual data:
1. Measuring Momentum (MACD)
The process begins by comparing two different moving averages (fast and slow). By calculating the difference between these two, the script identifies the current momentum of the price. If the fast average is significantly higher than the slow average, it suggests upward momentum; if lower, it suggests downward momentum.
2. Smoothing the Momentum (CD Calculation)
Raw momentum data can often be erratic and jumpy. To fix this, the script applies a smoothing filter to the momentum data. This ensures that the resulting line moves more fluidly, helping traders see a clearer picture of whether the trend is accelerating or decelerating without being distracted by minor, insignificant price fluctuations.
3. Normalizing the Data (Fast K and D)
Once the smoothed momentum is established, the script performs a "normalization" step. It looks at the highest and lowest values over a specific period to determine the relative position of the current price action. This converts the raw data into a percentage-like scale (typically 0 to 100). This makes it much easier to identify "overbought" or "oversold" conditions, as the data is now contained within a predictable range.
4. Final Trend Refinement (Stochastic Smoothing)
To reach the final output shown on your chart, the script applies one last layer of smoothing to the normalized data. This final step produces the "Schaff Trend Cycle." By repeatedly filtering and refining the data through these four stages—Momentum, Smoothing, Normalization, and Final Refinement—the tool provides a cleaner, more stable signal than standard indicators might offer on their own.
Summary for the User: Instead of reacting to every tiny tick in price, this script "distills" the market data. It takes raw price movement, removes the noise, determines the strength of the trend, and then smooths that trend out one last time to provide a clear visual guide on the current market cycle.
//+------------------------------------------------------------------+
//| Schaff Trend Cycle 1.mq4 |
//| mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link "mladenfx@gmail.com"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//
//
//
//
//
extern int STCPeriod = 10;
extern int CDPeriod = 25;
extern int FastMAPeriod = 23;
extern int SlowMAPeriod = 50;
//
//
//
//
//
double stcBuffer[];
double macdBuffer[];
double cdBuffer[];
double fastKBuffer[];
double fastDBuffer[];
double fastKKBuffer[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int init()
{
IndicatorBuffers(6);
SetIndexBuffer(0,stcBuffer);
SetIndexBuffer(1,macdBuffer);
SetIndexBuffer(2,cdBuffer);
SetIndexBuffer(3,fastKBuffer);
SetIndexBuffer(4,fastDBuffer);
SetIndexBuffer(5,fastKKBuffer);
IndicatorShortName("Schaff TC1 ("+STCPeriod+","+FastMAPeriod+","+SlowMAPeriod+","+CDPeriod+")");
return(0);
}
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int start()
{
double alphaCD = 2.0 / (1.0 + CDPeriod);
int counted_bars = IndicatorCounted();
int limit,i;
if(counted_bars < 0) return(-1);
if(counted_bars>0) counted_bars--;
limit = Bars-counted_bars;
//
//
//
//
//
for(i = limit; i >= 0; i--)
{
macdBuffer[i] = iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,i)-
iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,i);
cdBuffer[i] = cdBuffer[i+1]+alphaCD*(macdBuffer[i]-cdBuffer[i+1]);
//
//
//
//
//
double lowCd = minValue(cdBuffer,i);
double highCd = maxValue(cdBuffer,i)-lowCd;
if (highCd > 0)
fastKBuffer[i] = 100*((cdBuffer[i]-lowCd)/highCd);
else fastKBuffer[i] = fastKBuffer[i+1];
fastDBuffer[i] = fastDBuffer[i+1]+0.5*(fastKBuffer[i]-fastDBuffer[i+1]);
//
//
//
//
//
double lowStoch = minValue(fastDBuffer,i);
double highStoch = maxValue(fastDBuffer,i)-lowStoch;
if (highStoch > 0)
fastKKBuffer[i] = 100*((fastDBuffer[i]-lowStoch)/highStoch);
else fastKKBuffer[i] = fastKKBuffer[i+1];
stcBuffer[i] = stcBuffer[i+1]+0.5*(fastKKBuffer[i]-stcBuffer[i+1]);
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
double minValue(double& array[],int shift)
{
double minValue = array[shift];
for (int i=1; i<STCPeriod; i++) minValue = MathMin(minValue,array[shift+i]);
return(minValue);
}
double maxValue(double& array[],int shift)
{
double maxValue = array[shift];
for (int i=1; i<STCPeriod; i++) maxValue = MathMax(maxValue,array[shift+i]);
return(maxValue);
}
Comments