Ergodic_v1

This tool is designed to analyze and visualize market momentum by measuring the relationship between price movement and volatility. Instead of simply looking at where the price is, it focuses on the "strength" behind the move to help traders identify the underlying trend dynamics.

How the Logic Works

The script processes market data through a multi-stage calculation to filter out "noise" and highlight significant trends:

1. Price Deviation Calculation The process begins by calculating the average price of specific candles (combining the High, Low, and Close prices). The script then compares the current average price against the average price from a previous point in time. This creates a "Delta," which represents the raw distance the price has traveled over that period.

2. Volatility Measurement Simultaneously, the script calculates the absolute value of that movement. While the first calculation shows direction (up or down), this second calculation measures the pure magnitude (the size of the move regardless of direction). This allows the tool to distinguish between a steady, controlled trend and erratic, high-volatility price swings.

3. Multi-Layer Smoothing To prevent the data from being too jumpy or reactive to minor fluctuations, the script applies three consecutive layers of Exponential Moving Averages (EMA):

  • The first layer smooths the raw price deviations.
  • The second layer provides a secondary level of smoothing to refine the trend.
  • The third layer ensures that only significant, sustained movements are captured, removing "false signals" caused by temporary market spikes.

4. The Ergodic Strength Index The final calculation takes these smoothed values and creates a ratio. By dividing the directional movement by the volatility measurement and scaling it (multiplied by 100), the script produces the final "Ergodic" value.

Visual Output

The result is displayed in a separate window as two distinct visual elements:

  • A Histogram: This provides a raw look at the processed strength data.
  • A Smooth Line: This represents the final, polished trend line that reflects the most consistent momentum currently present in the market.

By observing these values, users can better understand whether a move in price is backed by strong momentum or if it is simply a volatile fluctuation without significant direction.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
18 Views
0 Downloads
0 Favorites
Ergodic_v1
//+------------------------------------------------------------------+
//| Danny Feng                                           Ergodic.mq4 |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Yellow
#property indicator_color2 Red
//---- input parameters
extern int r=2;
extern int s=5;
extern int u=8;
extern int smooth=2;

//---- buffers
double ErgBuffer[];
double ema_ErgBuffer[];
double Price_Delta1_Buffer[];
double Price_Delta2_Buffer[];
double s_ema1_Buffer[];
double s_ema2_Buffer[];
double u_ema1_Buffer[];
double u_ema2_Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- additional buffers are used for counting.
   IndicatorBuffers(8);
   SetIndexBuffer(0,ema_ErgBuffer);
   SetIndexBuffer(1,ErgBuffer);
   SetIndexBuffer(2,Price_Delta1_Buffer);
   SetIndexBuffer(3,Price_Delta2_Buffer);
   SetIndexBuffer(4,s_ema1_Buffer);
   SetIndexBuffer(5,s_ema2_Buffer);
   SetIndexBuffer(6,u_ema1_Buffer);
   SetIndexBuffer(7,u_ema2_Buffer);

//---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_HISTOGRAM);
//---- name for DataWindow and indicator subwindow label
   short_name="Ergodic";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,r+s+u+smooth);
   SetIndexDrawBegin(1,r+s+u+smooth);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| True Strength Index                                          |
//+------------------------------------------------------------------+
int start() {
   int i,limit;
   double mean1, mean2;
   
   int counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   for(i=0; i<limit; i++) {
      //Price_Delta1_Buffer[i] = Close[i] - Close[i+r];
      //Price_Delta2_Buffer[i] = MathAbs(Close[i] - Close[i+r]);
      mean1 = (Close[i] + High[i] + Low[i]) /3;
      mean2 = (Close[i+r] + High[i+r] + Low[i+r]) /3;      
      Price_Delta1_Buffer[i] = mean1 - mean2;
      Price_Delta2_Buffer[i] = MathAbs(mean1 - mean2);
   }
   
   for(i=0; i<limit; i++) {
      s_ema1_Buffer[i]=iMAOnArray(Price_Delta1_Buffer,Bars,s,0,MODE_EMA,i);
      s_ema2_Buffer[i]=iMAOnArray(Price_Delta2_Buffer,Bars,s,0,MODE_EMA,i);
   }
   
   for(i=0; i<limit; i++) {
      u_ema1_Buffer[i]=iMAOnArray(s_ema1_Buffer,Bars,u,0,MODE_EMA,i);
      u_ema2_Buffer[i]=iMAOnArray(s_ema2_Buffer,Bars,u,0,MODE_EMA,i);
   }

   for(i=0; i<limit; i++) {
      ErgBuffer[i] = 100 * u_ema1_Buffer[i] / u_ema2_Buffer[i] ;
   }   

   for(i=0; i<limit; i++) {
      ema_ErgBuffer[i] = iMAOnArray(ErgBuffer,Bars,smooth,0,MODE_EMA,i);
   }   
   return(0);
}
//+------------------------------------------------------------------+


Comments