DEMA

This script is designed to provide a specialized visual trend line on your trading chart, known as the Double Exponential Moving Average (DEMA). Unlike standard moving averages that can sometimes lag behind actual market price movements, this tool is engineered to react more quickly to recent price changes while still smoothing out minor market "noise."

How the Logic Works

The script operates by performing a multi-layered calculation based on the closing prices of assets over a specific timeframe (defined by the user as the "Period"). Here is the step-by-step logic:

1. Establishing the Foundation The tool first looks at the current price and calculates a standard Exponential Moving Average (EMA). This basic average gives more weight to recent data, making it more responsive than a simple average.

2. The Double Layering Effect To achieve higher precision, the script takes that initial EMA and calculates a second moving average of that first average. By creating this "average of an average," the tool becomes more sensitive to sudden shifts in momentum.

3. Calculating the Final Value The final value displayed on your chart is derived by taking double the first average and subtracting the "average of the average." This mathematical interaction serves to reduce the inherent delay found in traditional indicators.

Summary for the Trader

In practical terms, this script draws a red line on your chart that attempts to stay as close to the "true" price action as possible. Because it uses double-layered smoothing, it aims to highlight the underlying trend of the market more aggressively than a standard moving average would, helping you identify shifts in direction with potentially less delay.

Miscellaneous
Implements a curve of type %1
16 Views
0 Downloads
0 Favorites
DEMA
//+------------------------------------------------------------------+
//|                                                         DEMA.mq4 |
//| DEMA = 2 * EMA - EMA of EMA													|
//+------------------------------------------------------------------+
#property link "http://www.forexfactory.com/showthread.php?t=29419"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1  Red
#property indicator_width1  1
//---- input parameters
extern int PERIOD  =12;
//---- indicator buffer
double Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorShortName("DEMA("+PERIOD+")");
   SetIndexBuffer(0,Buffer);
   SetIndexStyle(0,DRAW_LINE);
  }
//+------------------------------------------------------------------+
int start()
  {
   int limit=Bars-1-IndicatorCounted();
//----
   static double lastEMA, lastEMA_of_EMA;
   double weight=2.0/(1.0+PERIOD);
   if(IndicatorCounted()==0)
     {
      Buffer[limit]  =Close[limit];
      lastEMA        =Close[limit];
      lastEMA_of_EMA  =Close[limit];
      limit--;
     }
//----
   //	Calculate old bars (not the latest), if necessary
   for(int i=limit; i > 0; i--)
     {
      lastEMA        =weight*Close[i]   + (1.0-weight)*lastEMA;
      lastEMA_of_EMA  =weight*lastEMA   + (1.0-weight)*lastEMA_of_EMA;
      Buffer[i]=2.0*lastEMA - lastEMA_of_EMA;
     }
//----
   //	(Re)calculate current bar
   double EMA        =weight*Close[0]   + (1.0-weight)*lastEMA,
   EMA_of_EMA  =weight*EMA      + (1.0-weight)*lastEMA_of_EMA;
   Buffer[0]=2.0*EMA - EMA_of_EMA;
//----
   return(0);
  }

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

Comments