adxdon_onlyADX

Author: Copyright � 2004, MetaQuotes Software Corp.
adxdon_onlyADX

This script is a technical analysis tool designed for the MetaTrader trading platform. It calculates and visualizes the Average Directional Index (ADX), a popular momentum indicator used by traders to determine the overall strength of a market trend, regardless of its direction.

The logic behind this tool can be broken down into three primary stages:

1. Movement Tracking

The script begins by analyzing price action bar-by-bar to identify where the "movement" is coming from. It looks at three specific components:

  • Upward Movement (+DI): It measures how much the current high price exceeds the previous high.
  • Downward Movement (-DI): It measures how much the current low price drops below the previous low.
  • True Range (TR): This calculates the actual volatility of the price, considering the distance between the highs and lows of the current bar compared to the previous closing price.

The logic ensures that only "new" highs or "new" lows are counted toward the directional movement; if the price stays within the previous range, those movements are discarded to keep the data clean.

2. Smoothing and Normalization

Raw market data is often too erratic for practical trading, so the script applies a mathematical smoothing process (specifically an Exponential Moving Average).

  • It smooths the True Range data first.
  • It then smooths the Upward and Downward movement values separately.
  • Finally, it divides the smoothed movements by the smoothed range. This "normalizes" the data, turning raw price points into a relative percentage of strength for both buying and selling pressure.

3. Final Strength Calculation (DX & ADX)

In the final stage, the script calculates the DX (Directional Index). It takes the difference between the upward and downward strength values and divides it by their sum. This tells you how dominant one direction is over the other as a percentage.

The final ADX value—which is what appears on your chart—is produced by taking a moving average of this DX value and multiplying it by 100.

Summary for the Trader

  • Trend Strength: The resulting ADX line shows how strong the current trend is. A rising ADX indicates a strengthening trend (either up or down), while a falling ADX suggests the market is becoming range-bound or losing momentum.
  • Directional Components: Although primarily used for strength, the script also calculates +DI and -DI. These help identify which direction the "bulk" of the price movement is favoring (Positive for bulls, Negative for bears).
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
8 Views
0 Downloads
0 Favorites
adxdon_onlyADX
//+------------------------------------------------------------------+
//|                                                       ADXdon.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Aqua
#property indicator_color2 LimeGreen
#property indicator_color3 Red
#property indicator_level1 20
#property indicator_level2 30
#property indicator_level3 50
#property indicator_level4 10
#property indicator_level5 12.5
#property indicator_maximum 60
#property indicator_minimum 5
//---- input parameters
 int ADXPeriod=14, DiPlusPeriod = 34, DiMinusPeriod=34;
//---- buffers
double ADXBuffer[];
double PlusDiBuffer[];
double MinusDiBuffer[];
double PlusSdiBuffer[];
double MinusSdiBuffer[];
double TRBuffer[];
double TempBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 3 additional buffers are used for counting.
   IndicatorBuffers(7);
//---- indicator buffers
   SetIndexBuffer(0,ADXBuffer);
   SetIndexBuffer(1,PlusDiBuffer);
   SetIndexBuffer(2,MinusDiBuffer);
   SetIndexBuffer(3,PlusSdiBuffer);
   SetIndexBuffer(4,MinusSdiBuffer);
   SetIndexBuffer(5,TempBuffer);
   SetIndexBuffer(6, TRBuffer );
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("ADX("+ADXPeriod+", DI-: "+DiMinusPeriod+", DI+: "+DiPlusPeriod+")");
   SetIndexLabel(0,"ADX");
   SetIndexLabel(1,"+DI");
   SetIndexLabel(2,"-DI");
//----
   SetIndexDrawBegin(0,ADXPeriod);
   SetIndexDrawBegin(1,ADXPeriod);
   SetIndexDrawBegin(2,ADXPeriod);
//----
SetIndexStyle(0, DRAW_LINE,0,2);
SetIndexStyle(1, DRAW_NONE);
SetIndexStyle(2, DRAW_NONE);

   return(0);
  }
//+------------------------------------------------------------------+
//| Average Directional Movement Index                               |
//+------------------------------------------------------------------+
int start()
  {
   double pdm,mdm,tr;
   double price_high,price_low;
   int    starti,i,counted_bars=IndicatorCounted();
//----
   
    //int limit=Bars-2;
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
   i = limit;
   starti = i;
//----
   while(i>=0)
     {
      price_low=Low[i];
      price_high=High[i];
      //----
      pdm = 0.0;
      mdm = 0.0;
      if ( price_high > High[i+1] )
         pdm = price_high - High[i+1];
      if ( price_low < Low[i+1] )
         mdm = Low[i+1] - price_low;
      
      if ( pdm < mdm )
         pdm = 0.0;
      else if ( mdm < pdm )
         mdm = 0.0;
      //---- absolute true range
      double num1=MathAbs(price_high-price_low);
      double num2=MathAbs(price_high-Close[i+1]);
      double num3=MathAbs(price_low-Close[i+1]);
      tr=MathMax(num1,num2);
      tr=MathMax(tr,num3);
      
      //---- counting plus/minus direction
      PlusSdiBuffer[i]=pdm; 
      MinusSdiBuffer[i]=mdm;
      TRBuffer[i] = tr;
     //----
      i--;
     }
//---- apply EMA to +DI
   for(i=0; i<limit; i++) {
//----    apply EMA to TR
      TRBuffer[i]=iMAOnArray(TRBuffer,0,ADXPeriod,0,MODE_EMA,i);
      PlusDiBuffer[i] = PlusDiBuffer[i+1];
      MinusDiBuffer[i] = MinusDiBuffer[i+1];
      if ( TRBuffer[i] != 0 ) {
         PlusDiBuffer[i]=iMAOnArray(PlusSdiBuffer,Bars,DiPlusPeriod,0,MODE_EMA,i) / TRBuffer[i];
//----    apply EMA to -DI
         MinusDiBuffer[i]=iMAOnArray(MinusSdiBuffer,Bars,DiMinusPeriod,0,MODE_EMA,i) / TRBuffer[i];
      }     
   }
//---- Directional Movement (DX)
   i=Bars-2;
   //TempBuffer[i+1]=0;
   i=starti;
   while(i>=0)
     {
      double div=MathAbs(PlusDiBuffer[i]+MinusDiBuffer[i]);
      if(div==0.00) TempBuffer[i]=TempBuffer[i+1];
      else TempBuffer[i]=MathAbs(PlusDiBuffer[i]-MinusDiBuffer[i]) / div;
      i--;
     }
//---- ADX is exponential moving average on DX
   for(i=0; i<limit; i++) {
      ADXBuffer[i]=iMAOnArray(TempBuffer,0,ADXPeriod,0,MODE_EMA,i) * 100.0;
      PlusDiBuffer[i] = PlusDiBuffer[i] * 100.0;
      MinusDiBuffer[i] = MinusDiBuffer[i] * 100.0;
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments