ADX-StDev
Download scriptThis tool is designed to help traders analyze market trends by measuring the strength and direction of price movements. It works by combining two primary technical concepts: Directional Movement and Standard Deviation.
How the Logic Works
The script processes market data through a multi-step calculation to provide a clear picture of current trading conditions:
1. Tracking Price Movement (Direction) The tool first looks at the high and low prices of each candle compared to the previous one. It identifies which direction the price is moving most aggressively:
- Positive Movement: If the current high is significantly higher than the previous candle's high, it marks a bullish movement.
- Negative Movement: If the current low is significantly lower than the previous candle's low, it marks a bearish movement. The script converts these movements into two distinct components (Plus DI and Minus DI), which essentially represent the "upward pressure" and "downward pressure" in the market.
2. Smoothing the Data Raw price movements can be erratic and "noisy." To make the data readable, the script applies a moving average to the upward and downward pressures. This smooths out minor fluctuations, allowing you to see the underlying trend rather than every tiny tick of price movement.
3. Determining Trend Strength (DX) Once the tool has the smoothed upward and downward pressures, it calculates the difference between them. By comparing these two values, it determines the overall strength of the current trend. This calculation provides a percentage-based view of how dominant one direction is over the other.
4. Final Analysis (ADX & Standard Deviation) The final output is created by taking that trend strength and applying two additional layers of analysis:
- ADX (Average Directional Index): The script calculates a moving average of the trend strength. This helps traders identify if a trend is gaining momentum, losing steam, or if the market is moving sideways.
- Standard Deviation: The script also calculates the volatility of that trend strength. By measuring how much the trend deviates from its average, it helps identify whether the current movement is stable or becoming increasingly unpredictable.
Summary for Traders
In practical terms, this script provides a visual representation of:
- Direction: Which way is the market leaning (Up vs. Down)?
- Strength: How powerful is that move?
- Volatility: Is the trend moving in a steady, reliable way, or is it fluctuating wildly?
//+------------------------------------------------------------------+
//| ADX-StDev.mq4 |
//| Copyright © 2011 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011"
//-----
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Gold
#property indicator_color2 Green
#property indicator_color3 Red
#property indicator_color4 Aqua
//-----
extern int ADXPeriod = 5;// ADX
extern int ADXMode = 1;// 0=MODE_SMA; 1=MODE_EMA; 2=MODE_SMMA; 3=MODE_LWMA.
extern int StdPeriod = 3;// Standard Deviation
extern int StdMode = 0;// 0=MODE_SMA; 1=MODE_EMA; 2=MODE_SMMA; 3=MODE_LWMA.
//-----
double ADXBuffer[];
double PlusDiBuffer[];
double MinusDiBuffer[];
double PlusSdiBuffer[];
double MinusSdiBuffer[];
double TempBuffer[];
double subBuffer[];
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(7);
//----
SetIndexStyle(0, DRAW_HISTOGRAM);
SetIndexBuffer(0, ADXBuffer);
SetIndexBuffer(1, PlusDiBuffer);
SetIndexBuffer(2, MinusDiBuffer);
SetIndexStyle(3, DRAW_LINE, STYLE_DOT);
SetIndexBuffer(3, subBuffer);
SetIndexBuffer(4, PlusSdiBuffer);
SetIndexBuffer(5, MinusSdiBuffer);
SetIndexBuffer(6, TempBuffer);
//----
SetIndexLabel(0, "ADX");
SetIndexLabel(1, "+DI");
SetIndexLabel(2, "-DI");
SetIndexLabel(3, "StDev");
//----
SetIndexDrawBegin(0,ADXPeriod);
SetIndexDrawBegin(1,ADXPeriod);
SetIndexDrawBegin(2,ADXPeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
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, ADXMode, i);
}
//---- apply EMA to -DI
for(i=0; i<=limit; i++)
MinusDiBuffer[i] = iMAOnArray(MinusSdiBuffer, Bars, ADXPeriod, 0, ADXMode, 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 and Standard Deviation
for(i = 0; i < limit; i++)
{
ADXBuffer[i] = iMAOnArray(TempBuffer, Bars, ADXPeriod, 0, ADXMode, i);
subBuffer[i] = iStdDevOnArray(TempBuffer, Bars, StdPeriod, 0, StdMode, i);
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments