ADXmod
Download scriptThis script is a technical analysis tool designed to help traders identify the strength and direction of a market trend. Rather than simply showing whether a price is going up or down, this logic focuses on how much "momentum" is behind the current movement, filtering out minor fluctuations to reveal the underlying trend.
Core Logic Breakdown
The script operates by processing price data through three primary stages:
1. Directional Movement Analysis The tool first examines the high and low prices of the current candle relative to the previous one. It calculates two specific components:
- Positive Movement (+DM): Measures how much the price moved higher compared to the previous peak.
- Negative Movement (-DM): Measures how much the price dropped compared to the previous trough.
The logic applies a set of rules to ensure that only "true" movements are counted. For example, if the downward move is stronger than the upward move, the upward movement is disregarded (set to zero). This helps the script isolate which direction the market is actually leaning.
2. Relative Strength Calculation Once the directional movements are identified, the tool compares them against the "True Range" (the total volatility of the candle). By dividing the directional movements by this range, the script produces two percentage-based values:
- +DI: Represents the strength of the upward trend.
- -DI: Represents the strength of the downward trend.
To make these figures easier to read and less erratic, the script applies an Exponential Moving Average (EMA) to both values. This smoothing process removes "noise" from the data, providing a clearer picture of whether the bulls or the bears are currently in control.
3. Trend Strength (The ADX Value) The final step of the logic calculates the DX (Directional Index). This is derived by taking the difference between the smoothed +DI and -DI values and dividing it by their sum.
Finally, the script applies one more smoothing layer (another EMA) to this DX value to produce the final ADX line.
Summary for the User
- The +DI and -DI lines tell you the "tug-of-war" between buyers and sellers. If one is significantly higher than the other, it indicates which way the price is trending.
- The ADX line tells you how strong that trend is. It doesn't care if the price is going up or down; it simply measures the intensity of the movement. A rising ADX suggests a strengthening trend, while a falling ADX suggests the market is losing momentum and may be entering a consolidation phase.
//+------------------------------------------------------------------+
//| ADX.mq4 |
//| Copyright © 2004, 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 Yellow
#property indicator_color2 Blue
#property indicator_color3 Red
//---- input parameters
extern int ADXPeriod=14;
extern int SmoothPeriod=14;
extern int SmoothType=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+","+SmoothPeriod+")");
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=Low[i];
price_high=High[i];
//----
pdm=price_high-High[i+1];
mdm=Low[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-Close[i+1]);
double num3=MathAbs(price_low-Close[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,SmoothPeriod,0,SmoothType,i);
//----
return(0);
}
//+------------------------------------------------------------------+
Comments