Author: senlin ge eMail:806935610@qq.com
kdj_v1

This script is a specialized technical analysis tool designed for the MetaTrader trading platform. It functions as a momentum oscillator, which helps traders identify whether a market is overbought, oversold, or currently trending in a specific direction. Instead of looking at just the price, this tool calculates "relative strength" to help smooth out market noise and reveal underlying trends.

How the Logic Works

The tool operates through a multi-layered calculation process that converts raw price data into several distinct visual signals:

1. Identifying Relative Strength (RSV) The foundation of the script is the Relative Strength Value (RSV). The logic looks at a specific window of recent historical data (determined by the user) to find the highest peak and the lowest valley. It then compares the current closing price to this range. This produces a percentage value that tells you where the price sits relative to its recent boundaries—essentially showing if the price is "stretched" toward the top or bottom.

2. Smoothing the Data (K and D Lines) Because raw strength values can be jagged and jump around rapidly, the script applies two types of mathematical smoothing:

  • The K Line: This is a moving average of the RSV. It represents the primary trend of the momentum.
  • The D Line: This is a secondary moving average calculated based on the K line. It acts as a "signal" line, showing the broader direction of the momentum.

3. Generating Advanced Signals (J and KDC) Beyond the standard lines, the script calculates two additional metrics to provide more nuance:

  • The J Line: This is a weighted calculation that amplifies the difference between the K and D lines. It is designed to be more sensitive and reactive, often helping traders spot turning points faster than the smoother lines might allow. The script ensures this value stays within a fixed 0 to 100 range for easier reading.
  • The KDC Histogram: This calculates the direct difference between the K and D values. By displaying this as a histogram (vertical bars), it allows traders to visually see the "gap" or "spread" between the primary momentum and its signal, which can indicate the strength of a current move.

Visual Summary for Traders

When you view this tool on your chart, you are seeing a combination of smoothed trends (K and D), high-sensitivity triggers (J), and a visual representation of momentum pressure (KDC). By observing how these lines cross each other or how the histogram expands and contracts, you can get a clearer picture of market sentiment than by looking at price alone.

Miscellaneous
Implements a curve of type %1
7 Views
0 Downloads
0 Favorites
kdj_v1
//+------------------------------------------------------------------+
//|                                                          KDJ.mq4 |
//|                                                        senlin ge |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "senlin ge eMail:806935610@qq.com"
#property link      "http://new.qzone.qq.com/806935610"
#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