Schaff Trend Cycle_v1

Download script
Schaff Trend Cycle_v1

This script functions as a technical analysis tool designed to identify and visualize cyclical trends in financial markets. Rather than looking at raw price action alone, it focuses on the "momentum" of the market by analyzing the relationship between two different moving averages.

Here is a breakdown of the logic used to generate these visual results:

Core Data Foundation

The script begins by calculating two distinct types of Exponential Moving Averages (EMAs):

  1. A Short-Term Average: Designed to capture recent price movements quickly.
  2. A Long-Term Average: Designed to represent the broader, underlying trend of the market.

By subtracting the long-term average from the short-term average, the script determines a "Momentum Difference." This tells the user whether the current price is accelerating faster than the historical average or slowing down.

The Cyclical Analysis

The primary goal of this tool is to turn that raw momentum data into a readable "cycle" (a percentage-based value). To achieve this, the script follows these steps:

  • Grouping Data: It organizes the market's momentum into specific windows of time (defined by the "Cycle" parameter).
  • Identifying Extremes: Within each of these time windows, the logic identifies the highest point (High) and the lowest point (Low) reached by the momentum difference.
  • Normalization: It then calculates where the current momentum sits relative to those highs and lows. This converts volatile price data into a normalized percentage. Essentially, it tells you where the market is currently positioned within its recent swing—whether it is near its peak, its trough, or somewhere in between.

Smoothing and Final Visualization

To ensure the resulting line is easy for a human to read without being overly "choppy," the script applies a final smoothing layer:

  • Smoothing Constant: It uses a specific mathematical constant to blend the current cycle value with the previous one. This reduces sudden spikes, providing a fluid line that highlights the underlying rhythm of the trend.
  • The Output: The final result is a smooth curve displayed in a separate window below your main chart. This curve allows traders to visualize the "heartbeat" of the market, identifying when the price is reaching cyclical extremes or beginning a new phase of movement.
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
Schaff Trend Cycle_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                          Schaff Trend Cycle.mq4  |
//|                                       Ramdass - Conversion only  |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2004, FostarFX."
#property  link      "mail: fostar_fx@yahoo.com"

#property indicator_separate_window
#property indicator_minimum -10
#property indicator_maximum 110
#property indicator_buffers 1
#property indicator_color1 DarkOrchid

//---- input parameters
extern int MAShort=23;
extern int MALong=50;
extern double Cycle=10;
extern int CountBars=300;
//---- buffers
double MA[];
double ST[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//   string short_name;
//---- indicator line
   IndicatorBuffers(2);
   SetIndexBuffer(0, MA);
   SetIndexBuffer(1, ST);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,DarkOrchid);

//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Schaff Trend Cycle                                               |
//+------------------------------------------------------------------+
int start()
  {
   SetIndexDrawBegin(0,Bars-CountBars+MALong+MAShort+1);
   int shift,u,counted_bars=IndicatorCounted();
   double MCD, LLV, HHV, MA_Short, MA_Long, sum ,prev, smconst;
   int n, i, s;
   bool check_begin=false, check_begin_MA=false;
   double MCD_Arr[100];

   if(Bars<=MALong) return(0);
 if (CountBars==0) CountBars=Bars;
//---- initial zero
   if(counted_bars<MALong+MAShort)
   {
      for(i=1;i<=MALong;i++) MA[Bars-i]=0.0;
      for(i=1;i<=MALong;i++) ST[Bars-i]=0.0;
   }
//----
   shift=CountBars-MALong-1;
//   if(counted_bars>=MALong) shift=Bars-counted_bars-1;
   
   check_begin = false;
   check_begin_MA = false;
   n = 1;
   s = 1;
   smconst = 2 / (1 + Cycle/2);

   while(shift>=0)
     {
   MA_Short = iMA(NULL,0,MAShort,0, MODE_EMA, PRICE_TYPICAL, shift);
	MA_Long = iMA(NULL,0,MALong,0, MODE_EMA, PRICE_TYPICAL, shift);
	MCD_Arr[n] = MA_Short - MA_Long;
	MCD = MA_Short - MA_Long;

	if (n >= Cycle)  
	{	
		n = 1; check_begin = true;	} else {n = n + 1;}
	
	if (check_begin)  
	{
		for (i = 1; i<=Cycle; i++)
		{	
			if (i == 1) {LLV = MCD_Arr[i];}
			else {
				if (LLV > MCD_Arr[i]) LLV = MCD_Arr[i];
			}
			
			if (i == 1) {HHV = MCD_Arr[i];}
			else {
				if (HHV < MCD_Arr[i]) HHV = MCD_Arr[i];
			}					
		}
		ST[shift] = ((MCD - LLV)/(HHV - LLV))*100 + 0.01;
		s = s + 1;
		if (s >= (Cycle)/2)
		{ 
			s = 1;
			check_begin_MA = true;
		}
	}	else {ST[shift] = 0;}
	if (check_begin_MA) {	
		prev = MA[shift + 1];
		MA[shift] = smconst * (ST[shift] - prev) + prev;	}

      shift--;
     }

   return(0);
  }
//+------------------------------------------------------------------+

Comments