Schaff Trend Cycle_001
Download scriptThis script is designed to provide a visual representation of market momentum and cyclical trends by smoothing out price fluctuations. Rather than showing the raw price of an asset, it focuses on the "rhythm" of the market to help identify where a trend currently sits within its cycle.
Core Logic Overview
The tool operates by analyzing the relationship between two different moving averages: a short-term average (which reacts quickly to price changes) and a long-term average (which represents the broader trend). By calculating the difference between these two, the script identifies the current momentum of the market.
How the Calculation Works
To turn these raw numbers into a readable cycle, the logic follows a three-step process:
1. Momentum Detection The script first calculates the gap between the short-term and long-term averages. This gap tells us how much the price is "pulling away" from its historical average. If the short-term average is significantly higher than the long-term average, it indicates strong upward momentum; if it is lower, it suggests downward pressure.
2. Range Normalization (The Cycle) Instead of showing this momentum as a raw numerical value, the script looks back at a specific window of time (defined by the "Cycle" parameter). It identifies the highest and lowest momentum points within that period. It then maps the current momentum onto a percentage scale of 0 to 100.
- This allows the user to see where the current price action sits relative to its recent highs and lows, effectively "resetting" the view every time a new cycle is completed.
3. Smoothing and Stabilization To prevent the indicator from jumping erratically due to minor market noise, the script applies a secondary smoothing layer. It compares the current percentage value with the previous one and uses a weighted calculation to create a fluid, continuous line. This ensures that the resulting visual is easy to follow, highlighting the overarching trend rather than every tiny fluctuation in price.
Summary for the User
The end result is a smooth line that oscillates between levels. When the line moves toward the upper bounds, it suggests the market is reaching a peak in its current cycle of strength; when it moves toward the lower bounds, it indicates a period of relative weakness. By looking at the "waves" created by this indicator, traders can better visualize the cyclical nature of price movements.
//+------------------------------------------------------------------+
//| 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