Author: Copyright � 2004, MetaQuotes Software Corp. && Yuriy Tokman
ADX+Period

This script is a specialized technical analysis tool designed to measure the strength and direction of market trends. It works by breaking down price movements into individual components to determine whether the current market momentum is shifting upward, downward, or staying range-bound.

Core Logic Overview

The indicator operates through a multi-step mathematical process that transforms raw price data (highs, lows, and closes) into visual signals. Instead of simply looking at where the price ends up, it analyzes how the price "travels" from one candle to the next.

How It Processes Data

  1. Identifying Movement (+DM and -DM): The script first looks at the difference between the current price range and the previous one.

    • If the price moves higher than its previous peak, it registers as positive movement.
    • If the price drops lower than its previous trough, it registers as negative movement.
    • Crucially, if both movements are equal or if there is no clear directional move, the script ignores them to filter out "noise."
  2. Calculating Relative Strength: The tool then compares these upward and downward movements against the total volatility of the period (the distance covered by the price). This creates two raw scores: one representing the strength of the "buying" pressure (+DI) and one for "selling" pressure (-DI).

  3. Smoothing the Data: Raw market data is often jagged and erratic. To make this information useful for human observation, the script applies an Exponential Moving Average (EMA) to both the positive and negative scores. This smoothing helps filter out minor price flickers while highlighting the primary trend.

  4. Determining Trend Intensity (DX and ADX): Once the smooth buying and selling strengths are established, the script calculates the "Directional Index" (DX). This is essentially a percentage that shows the gap between buying and selling strength. Finally, it applies one last round of smoothing to this result to produce the ADX.

What the Results Mean

The final output provides three distinct visual layers:

  • +DI (Positive Directional Indicator): Shows the strength of upward momentum.
  • -DI (Negative Directional Indicator): Shows the strength of downward momentum.
  • ADX (Average Directional Index): This is the primary trend indicator. It doesn't tell you which way the price is going; rather, it tells you how strong the current trend is. A rising ADX suggests a strengthening trend (regardless of direction), while a falling ADX suggests that the market is becoming weak or moving sideways.
Price Data Components
Series array that contains the lowest prices of each barSeries array that contains the highest prices of each barSeries array that contains close prices for each bar
Indicators Used
Moving average indicator
12 Views
0 Downloads
0 Favorites
ADX+Period
//+------------------------------------------------------------------+
//|                                                   ADX+Period.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|ðèõòîâàí 1 äåêàáðÿ 2008 ã.             http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp. && Yuriy Tokman"
#property link      "http://www.metaquotes.net/  &&  yuriytokman@gmail.com"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 Aqua
#property indicator_color3 Red

//---- input parameters
extern int ADXPeriod=6;
extern int timeframe =1;
//---- buffers
double ADXBuffer[];
double PlusDiBuffer[];
double MinusDiBuffer[];
double PlusSdiBuffer[];
double MinusSdiBuffer[];
double TempBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 3 additional buffers are used for counting.
   IndicatorBuffers(6);
//---- indicator buffers
   SetIndexBuffer(0,ADXBuffer);
   SetIndexBuffer(1,PlusDiBuffer);
   SetIndexBuffer(2,MinusDiBuffer);
   SetIndexBuffer(3,PlusSdiBuffer);
   SetIndexBuffer(4,MinusSdiBuffer);
   SetIndexBuffer(5,TempBuffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("ADX ("+ADXPeriod+")  Per ("+timeframe+")  ");
   SetIndexLabel(0,"ADX");
   SetIndexLabel(1,"+DI");
   SetIndexLabel(2,"-DI");
//----
   SetIndexDrawBegin(0,ADXPeriod);
   SetIndexDrawBegin(1,ADXPeriod);
   SetIndexDrawBegin(2,ADXPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Average Directional Movement Index                               |
//+------------------------------------------------------------------+
int start()
  {
   double pdm,mdm,tr;
   double price_high,price_low;
   int    starti,i,counted_bars=IndicatorCounted();
//----
   i=Bars-2;
   PlusSdiBuffer[i+1]=0;
   MinusSdiBuffer[i+1]=0;
   if(counted_bars>=i) i=Bars-counted_bars-1;
   starti=i;
//----
   while(i>=0)
     {
      price_low=iLow(NULL,timeframe,i);
      price_high=iHigh(NULL,timeframe,i);
      //----
      pdm=price_high-iHigh(NULL,timeframe,i+1);
      mdm=iLow(NULL,timeframe,i+1)-price_low;
      if(pdm<0) pdm=0;  // +DM
      if(mdm<0) mdm=0;  // -DM
      if(pdm==mdm) { pdm=0; mdm=0; }
      else if(pdm<mdm) pdm=0;
           else if(mdm<pdm) mdm=0;
      //---- âû÷èñëÿåì èñòèííûé èíòåðâàë
      double num1=MathAbs(price_high-price_low);
      double num2=MathAbs(price_high-iClose(NULL,timeframe,i+1));
      double num3=MathAbs(price_low-iClose(NULL,timeframe,i+1));
      tr=MathMax(num1,num2);
      tr=MathMax(tr,num3);
      //---- counting plus/minus direction
      if(tr==0) { PlusSdiBuffer[i]=0; MinusSdiBuffer[i]=0; }
      else      { PlusSdiBuffer[i]=100.0*pdm/tr; MinusSdiBuffer[i]=100.0*mdm/tr; }
      //----
      i--;
     }
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
//---- apply EMA to +DI
   for(i=0; i<=limit; i++)
      PlusDiBuffer[i]=iMAOnArray(PlusSdiBuffer,Bars,ADXPeriod,0,MODE_EMA,i);
//---- apply EMA to -DI
   for(i=0; i<=limit; i++)
      MinusDiBuffer[i]=iMAOnArray(MinusSdiBuffer,Bars,ADXPeriod,0,MODE_EMA,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]=0;
      else TempBuffer[i]=100*(MathAbs(PlusDiBuffer[i]-MinusDiBuffer[i])/div);
      i--;
     }
//---- ADX is exponential moving average on DX
   for(i=0; i<limit; i++)
      ADXBuffer[i]=iMAOnArray(TempBuffer,Bars,ADXPeriod,0,MODE_EMA,i);
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments