ADXm smoothed

Download script
Author: mladen

This script is a technical trading tool designed to identify and smooth out market trends by analyzing price movement. It combines several advanced mathematical concepts—specifically volatility tracking, directional strength, and complex smoothing filters—to provide a cleaner visual signal of the market's momentum.

Here is a breakdown of how the logic works:

Core Logic Flow

The script operates by processing price data (Highs, Lows, and Closes) to determine the "strength" and "direction" of the current market trend. It does this through three main stages:

1. Dynamic Volatility Tracking Instead of just looking at price changes, the script measures the intensity of those changes. It calculates a "volatility" metric by comparing recent price movements against historical data. This helps the indicator understand whether the current market action is a significant move or just minor "noise."

2. Adaptive Trend Calculation The script uses an ADX-style logic (Average Directional Index) but modifies it to be more responsive. It calculates:

  • Upward Movement: How much the price is pushing higher relative to its volatility.
  • Downward Movement: How much the price is pushing lower relative to its volatility. By dividing these movements by the volatility, the script creates a "normalized" view of strength that isn't easily skewed by sudden, erratic spikes in price.

3. Multi-Stage Smoothing (The "m" Factor) To ensure the final result isn't jumpy, the script applies a sophisticated smoothing process. It doesn't just average the numbers; it uses an internal filtering system that:

  • Adjusts its sensitivity based on user-defined periods.
  • Uses "Alpha" and "Beta" coefficients (common in high-end signal processing) to balance how much weight to give to new information versus old data.
  • Reduces "lag"—the delay between a price move and the indicator's reaction—while maintaining a smooth, readable line.

Visual Output

The script produces a visual representation on your chart with the following characteristics:

  • ADXm Line: A primary line that changes color to represent different states (e.g., trending up, trending down, or flat).
  • DI Line: A secondary reference line used for internal calculations to help determine the direction of the trend.

Summary for the User

In plain terms, this tool tells you "How strongly is the market moving in a specific direction?" By filtering out the "jitter" of the market and normalizing the moves against volatility, it aims to show you the underlying trend more clearly than a standard indicator would. It is designed to be smoother and more stable, making it easier to identify when a trend is gaining strength or starting to exhaust itself.

Price Data Components
13 Views
0 Downloads
0 Favorites
ADXm smoothed
ÿþ//+------------------------------------------------------------------

#property copyright   "mladen"

#property link        "mladenfx@gmail.com"

#property link        "https://www.mql5.com"

#property description "ADXm smoothed"

//+------------------------------------------------------------------

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_plots   2

#property indicator_label1  "DI"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDarkGray

#property indicator_label2  "ADXm"

#property indicator_type2   DRAW_COLOR_LINE

#property indicator_color2  clrDarkGray,clrDodgerBlue,clrSandyBrown

#property indicator_width2  2

//--- input parameters

input int    inpAdxmPeriod      = 14; // ADXm period

input int    inpAdxmSmooth      =  5; // Smoothing period

input double inpAdxmSmoothPhase =  0; // Smoothing phase

//--- buffers declarations

double adxm[],adxmc[],di[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,di,INDICATOR_CALCULATIONS);

   SetIndexBuffer(1,adxm,INDICATOR_DATA);

   SetIndexBuffer(2,adxmc,INDICATOR_COLOR_INDEX);

//---

   IndicatorSetString(INDICATOR_SHORTNAME,"ADXm smoothed ("+(string)inpAdxmPeriod+","+(string)inpAdxmSmooth+")");

   return (INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator de-initialization function                      |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

   if(Bars(_Symbol,_Period)<rates_total) return(prev_calculated);

   int i=(int)MathMax(prev_calculated-1,1); for(; i<rates_total && !_StopFlag; i++)

     {

      double _di;

      adxm[i]  = iSmooth(iAdxm(close[i],high[i],low[i],inpAdxmPeriod,_di,i,rates_total),inpAdxmSmooth,inpAdxmSmoothPhase,i,rates_total);

      di[i]    = _di;

      adxmc[i] = (i>0) ?(adxm[i]>adxm[i-1]) ? 1 :(adxm[i]<adxm[i-1]) ? 2 : adxmc[i-1]: 0;

     }

   return (i);

  }



//+------------------------------------------------------------------+

//| Custom functions                                                 |

//+------------------------------------------------------------------+

#define _adxmInstances     1

#define _adxmInstancesSize 8

double  _adxmWork[][_adxmInstances*_adxmInstancesSize];

#define _adxmSdh   0

#define _adxmSdl   1

#define _adxmSdx   2

#define _adxmClose 3

#define _adxmHigh  4

#define _adxmLow   5

#define _adxmDi    6

#define _adxmAdx   7

//

//---

//

double iAdxm(double _close,double _high,double _low,double _period,double &_di,int i,int bars,int instanceNo=0)

  {

   if(ArrayRange(_adxmWork,0)!=bars) ArrayResize(_adxmWork,bars); instanceNo*=_adxmInstancesSize; #ifdef __MQL4__ i=bars-i-1; #endif



   _adxmWork[i][instanceNo+_adxmClose]=_close;

   _adxmWork[i][instanceNo+_adxmHigh] =_high;

   _adxmWork[i][instanceNo+_adxmLow]  =_low;



//

//---

//



   double hc = _adxmWork[i][instanceNo+_adxmHigh];

   double lc = _adxmWork[i][instanceNo+_adxmLow ];

   double cp = (i>0) ? _adxmWork[i-1][instanceNo+_adxmClose] : _adxmWork[i][instanceNo+_adxmClose];

   double hp = (i>0) ? _adxmWork[i-1][instanceNo+_adxmHigh ] : _adxmWork[i][instanceNo+_adxmHigh ];

   double lp = (i>0) ? _adxmWork[i-1][instanceNo+_adxmLow  ] : _adxmWork[i][instanceNo+_adxmLow  ];

   double dh = MathMax(hc-hp,0);

   double dl = MathMax(lp-lc,0);



   if(dh==dl) {dh=0; dl=0;} else if(dh<dl) dh=0; else if(dl<dh) dl=0;



   double tr    = MathMax(hc,cp)-MathMin(lc,cp);

   double dhk   = (tr!=0) ? 100.0*dh/tr : 0;

   double dlk   = (tr!=0) ? 100.0*dl/tr : 0;

   double alpha = 2.0/(_period+1.0);



   _adxmWork[i][instanceNo+_adxmSdh] = (i>0) ? _adxmWork[i-1][instanceNo+_adxmSdh] + alpha*(dhk-_adxmWork[i-1][instanceNo+_adxmSdh]) : dhk;

   _adxmWork[i][instanceNo+_adxmSdl] = (i>0) ? _adxmWork[i-1][instanceNo+_adxmSdl] + alpha*(dlk-_adxmWork[i-1][instanceNo+_adxmSdl]) : dlk;

   _adxmWork[i][instanceNo+_adxmDi]  = _adxmWork[i][instanceNo+_adxmSdh] - _adxmWork[i][instanceNo+_adxmSdl];



   double div  = MathAbs(_adxmWork[i][instanceNo+_adxmSdh] + _adxmWork[i][instanceNo+_adxmSdl]);

   double temp = (div!=0.0) ? 100*_adxmWork[i][instanceNo+_adxmDi]/div : 0;



   _adxmWork[i][instanceNo+_adxmAdx]=(i>0) ? _adxmWork[i-1][instanceNo+_adxmAdx]+alpha*(temp-_adxmWork[i-1][instanceNo+_adxmAdx]) : 0;

   _di=_adxmWork[i][instanceNo+_adxmDi];

   return(_adxmWork[i][instanceNo+_adxmAdx]);

  }

//

//---

//

double m_wrk[][10];

int    m_size=0;

//

//---

//

double iSmooth(double price,double length,double phase,int r,int bars)

  {

#define bsmax  5

#define bsmin  6

#define volty  7

#define vsum   8

#define avolty 9



   if(m_size!=bars) ArrayResize(m_wrk,bars); if(ArrayRange(m_wrk,0)!=bars) return(price); m_size=bars;

   if(r==0 || length<=1) { int k=0; for(; k<7; k++) m_wrk[r][k]=price; for(; k<10; k++) m_wrk[r][k]=0; return(price); }



//

//---

//



   double len1   = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0);

   double pow1   = MathMax(len1-2.0,0.5);

   double del1   = price - m_wrk[r-1][bsmax];

   double del2   = price - m_wrk[r-1][bsmin];

   int    forBar = MathMin(r,10);



   m_wrk[r][volty]=0;

   if(MathAbs(del1) > MathAbs(del2)) m_wrk[r][volty] = MathAbs(del1);

   if(MathAbs(del1) < MathAbs(del2)) m_wrk[r][volty] = MathAbs(del2);

   m_wrk[r][vsum]=m_wrk[r-1][vsum]+(m_wrk[r][volty]-m_wrk[r-forBar][volty])*0.1;



//

//---

//



   m_wrk[r][avolty]=m_wrk[r-1][avolty]+(2.0/(MathMax(4.0*length,30)+1.0))*(m_wrk[r][vsum]-m_wrk[r-1][avolty]);

   double dVolty=(m_wrk[r][avolty]>0) ? m_wrk[r][volty]/m_wrk[r][avolty]: 0;

   if(dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);

   if(dVolty < 1)                      dVolty = 1.0;



//

//---

//



   double pow2 = MathPow(dVolty, pow1);

   double len2 = MathSqrt(0.5*(length-1))*len1;

   double Kv   = MathPow(len2/(len2+1), MathSqrt(pow2));



   if(del1 > 0) m_wrk[r][bsmax] = price; else m_wrk[r][bsmax] = price - Kv*del1;

   if(del2 < 0) m_wrk[r][bsmin] = price; else m_wrk[r][bsmin] = price - Kv*del2;



//

//---

//



   double corr  = MathMax(MathMin(phase,100),-100)/100.0 + 1.5;

   double beta  = 0.45*(length-1)/(0.45*(length-1)+2);

   double alpha = MathPow(beta,pow2);



   m_wrk[r][0] = price + alpha*(m_wrk[r-1][0]-price);

   m_wrk[r][1] = (price - m_wrk[r][0])*(1-beta) + beta*m_wrk[r-1][1];

   m_wrk[r][2] = (m_wrk[r][0] + corr*m_wrk[r][1]);

   m_wrk[r][3] = (m_wrk[r][2] - m_wrk[r-1][4])*MathPow((1-alpha),2) + MathPow(alpha,2)*m_wrk[r-1][3];

   m_wrk[r][4] = (m_wrk[r-1][4] + m_wrk[r][3]);



//

//---

//



   return(m_wrk[r][4]);



   #undef bsmax

   #undef bsmin

   #undef volty

   #undef vsum

   #undef avolty

  }

//+------------------------------------------------------------------+

Comments