This tool is a custom technical indicator designed for the MetaTrader platform to help traders identify potential momentum shifts and overbought or oversold conditions in the financial markets. It functions by analyzing price action over specific timeframes to generate several distinct visual signals.
Core Logic and Calculation Flow
The indicator operates through a multi-step mathematical process to transform raw price data into readable trend lines:
1. Relative Strength Analysis (RSV) The foundation of this tool is the calculation of the "RSV" value. The script looks back at a specific number of historical candles (defined by the user) to identify the highest peak and the lowest trough. It then calculates where the current closing price sits relative to that range. This provides a percentage-based look at whether the price is currently near its recent high or low.
2. Smoothing the Data (K and D Lines) Raw price fluctuations can be volatile and "noisy." To make the data more readable, the script applies moving averages to the RSV values:
- The K Line: This is a smoothed version of the RSV. It helps identify the primary direction of the current momentum.
- The D Line: This is a secondary smoothing of the K line. Because it is calculated based on the K line, it moves more slowly and acts as a baseline to show the "speed" or "strength" of the trend.
3. Advanced Momentum Indicators (J and KDC) Beyond the standard lines, the script calculates two additional specialized signals:
- The J Line: This is a highly sensitive momentum oscillator derived from the relationship between the K and D lines. It is designed to react quickly to sudden changes in price movement. The logic includes "clamping," which ensures the value stays within a 0 to 100 range, making it easier to spot extreme peaks and valleys.
- The KDC (Histogram): This measures the direct distance between the K and D lines. It is displayed as a histogram, providing a visual representation of how much "space" there is between the current trend and its moving average.
How to Interpret the Visuals
- Trend Following: By watching the interaction between the K and D lines, traders can look for "crosses." When one line moves over the other, it often suggests a potential change in the direction of the market momentum.
- Overextended Markets: The J Line and the high/low calculations help identify when a market might be "stretched" too far in one direction. When these values reach extreme ends of the scale, it may indicate that the current move is losing steam.
- Momentum Strength: The KDC Histogram allows for a quick glance at the intensity of the current move; a growing histogram suggests accelerating momentum, while a shrinking one suggests the trend is cooling off.
In summary, this script takes raw, messy price data and refines it into several smoothed layers, allowing users to see not just where the price is going, but how fast and how strongly it is moving in that direction.
//+------------------------------------------------------------------+
//| KDJ.mq4 |
//| senlin ge |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "senlin ge 20080219"
#property link "http://www.metaquotes.net"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 8
#property indicator_color1 LightSlateGray
#property indicator_color2 0x00ff00
#property indicator_color3 Red
#property indicator_color4 Yellow
#property indicator_color5 0Xffccff
#property indicator_color6 MediumSlateBlue
//---- input parameters
extern int M1=3;
extern int M2=6;
extern int KdjPeriod=30;
//---- buffers
double RsvBuffer[],RSV[],K[],D[],J[],KDC[];
double MaxHigh=0,MinLow=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- name for DataWindow and indicator subwindow label
string short_name;
short_name="KDJ("+KdjPeriod+","+M1+","+M2+") "+"Author:Senlin Ge";
IndicatorShortName(short_name);
SetIndexLabel(0,NULL);
SetIndexLabel(1,"RSV:");
SetIndexLabel(2,"K");
SetIndexLabel(3,"D:");
SetIndexLabel(4,"J");
SetIndexLabel(5,"KDC");
//----
//---- indicators
//---- drawing settings
IndicatorDigits(Digits-2); //setСÊý¾«¶ÈÁ½Î»
//SetIndexStyle(0,DRAW_NONE);
//SetIndexLabel(0,NULL);
SetIndexShift(0,100);
SetIndexStyle(0,DRAW_LINE,2,0);
SetIndexBuffer(0,RsvBuffer);
//----
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,RSV);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,K);
SetIndexStyle(3,DRAW_LINE);
SetIndexBuffer(3,D);
SetIndexStyle(4,DRAW_LINE);
SetIndexBuffer(4,J);
SetIndexStyle(5,DRAW_HISTOGRAM);
SetIndexBuffer(5,KDC);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
int i;
if(Bars<=KdjPeriod) return(0);
//----
i=Bars-counted_bars-1;
while(i>=0)
{
MaxHigh=High[iHighest(NULL,0,MODE_HIGH,KdjPeriod,i)];
MinLow=Low[iLowest(NULL,0,MODE_LOW,KdjPeriod,i)];
RSV[i]=(Close[i]-MinLow)/(MaxHigh-MinLow)*100;
if(i/2)
RsvBuffer[i]=50.0;
i--;
}
Ksma(RSV,M1,2); //get K value
Dsma(K,M2,3); //get D value
//---- //
if(counted_bars>0) counted_bars--;
for(i=Bars-counted_bars-1;i>=0;i--)
{
RsvBuffer[i]=50.0;
J[i]=3*K[i]-2*D[i];
if(J[i]<0) J[i]=0;
if(J[i]>100) J[i]=100;
KDC[i]=K[i]-D[i];
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| Simple Moving Average |
//+------------------------------------------------------------------+
double Dsma(double ArrPara[],int MA_Period,int flag)
{
double sum=0;
int ExtCountedBars=IndicatorCounted();
int i,pos=Bars-IndicatorCounted()-1;
//---- initial accumulation
if(pos<MA_Period) pos=MA_Period;
for(i=1;i<MA_Period;i++,pos--)
sum+=K[pos];
//---- main calculation loop
while(pos>=0)
{
sum+=K[pos]; //¼ÓÉÏ×îеÄKÖµ
switch(flag)
{
case 3: D[pos]=sum/MA_Period;break;
}
sum-=K[pos+MA_Period-1];
pos--;
}
}
//+------------------------------------------------------------------+
//| Simple Moving Average |
//+------------------------------------------------------------------+
double Ksma(double ArrPara[],int MA_Period,int flag)
{
double sum=0;
int ExtCountedBars=IndicatorCounted();
int i,pos=Bars-IndicatorCounted()-1;
//---- initial accumulation
if(pos<MA_Period) pos=MA_Period;
for(i=1;i<MA_Period;i++,pos--)
sum+=RSV[pos];
//---- main calculation loop
while(pos>=0)
{
sum+=RSV[pos]; //¼ÓÉÏ×îеÄRSV[]µÄÖµ
switch(flag)
{
case 2: K[pos]=sum/MA_Period;break;
}
sum-=RSV[pos+MA_Period-1];
pos--;
}
}
//+------------------------------------------------------------------+
Comments