Author: Copyright � 2011, EarnForex
3rdgenma

This tool provides a sophisticated way to track market trends by utilizing a "3rd Generation Moving Average." In the world of trading, standard moving averages often suffer from a dilemma: if they are smooth enough to filter out market noise, they tend to lag behind the current price (meaning they react too slowly to new information). If they are fast enough to be responsive, they often become too "jittery" and produce false signals.

The logic behind this specific script is designed to solve that problem by providing the smoothest possible line with the minimum amount of delay. Here is a breakdown of how it works:

The Core Logic

Instead of calculating a single average directly from the raw price, this script uses a two-layered mathematical approach:

  1. The Primary Layer: First, the script calculates a standard moving average (such as a Simple, Exponential, or Weighted average) based on your chosen timeframe and price type. This creates an initial smoothed baseline of the market's movement.
  2. The Secondary Smoothing: The script then performs a second calculation on that first average using a "Sampling Period." By analyzing the averages of the averages, it filters out remaining irregularities.

Key Mathematical Adjustments

To achieve this balance between smoothness and speed, the script utilizes two specific variables:

  • Lambda: This determines the relationship between the main period and the sampling period. It ensures that the secondary calculation is mathematically proportional to the primary one.
  • Alpha: This is a weighting factor derived from the periods you select. It dictates how much "weight" the script gives to the most recent data versus the historical data.

How it Functions for the User

When you apply this to a chart, the script processes the historical price data and generates a smoothed line. Because of its unique "3rd Generation" logic:

  • Reduced Lag: It stays closer to the actual current price than traditional moving averages of the same length.
  • Noise Reduction: It ignores minor price fluctuations that would typically cause a standard indicator to "wiggle" unnecessarily.
  • Customizable Dynamics: Users can choose between different averaging methods (SMA, EMA, etc.) and different price inputs (like High, Low, or Close prices), allowing the script to be tuned to specific trading styles.

In summary, this script is designed for traders who need a reliable trend indicator that reacts quickly to market shifts without sacrificing the visual clarity of a smooth moving average.

Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
3rdgenma
//+------------------------------------------------------------------+
//|                                                3rd Generation MA |
//|                                      Copyright © 2011, EarnForex |
//|                                        http://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, EarnForex"
#property link      "http://www.earnforex.com"
/*

3rd Generation MA based on research paper by Dr. Mafred Durschner:
http://www.vtad.de/node/1441 (in German)
Offers least possible lag but still provides price smoothing

*/
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red 
#property indicator_color2 Blue 
//---- indicator parameters
extern int MA_Period=220;
extern int MA_Sampling_Period=50; // Sampling period should be less than 1/4 of MA_Period
extern int MA_Method=1; //0 - SMA, 1 - EMA, 2 - SMMA, 3 - LWMA
extern int MA_Applied_Price=5; // 0 - PRICE_CLOSE, 1 - PRICE_OPEN, 2 - PRICE_HIGH, 3 - PRICE_LOW, 4 - PRICE_MEDIAN, 5 - PRICE_TYPICAL, 6 - PRICE_WEIGHTED
//---- indicator buffers
double MA3G[];
double MA1[];
//----
double Lambda,Alpha;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   int    draw_begin;
   string short_name;
//---
   IndicatorBuffers(3);
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---
   draw_begin=MA_Period-1;
//---- indicator short name
   switch(MA_Method)
     {
      case 1:
         short_name="3GEMA(";
         draw_begin=0;
         break;
      case 2:
         short_name="3GSMMA(";
         break;
      case 3:
         short_name="3GLWMA(";
         break;
      default:
         MA_Method=0;
         short_name="3GSMA(";
     }
   IndicatorShortName(short_name+MA_Period+")");
//---
   SetIndexDrawBegin(0,draw_begin);
//---- indicator buffers mapping
   SetIndexBuffer(0,MA3G);
   SetIndexBuffer(1,MA1);
//---
   Lambda= 1.0 * MA_Period/(1.0 * MA_Sampling_Period);
   Alpha = Lambda *(MA_Period-1)/(MA_Period-Lambda);
//---
   Print("Lambda = ",Lambda,"; Alpha = ",Alpha);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| 3rd Generation Moving Average Custom Indicator                   |
//+------------------------------------------------------------------+
int start()
  {
   int i;
//---
   if(MA_Period*2<MA_Sampling_Period)
     {
      Print("MA_Period should be >= MA_Sampling_Period * 2.");
      return(-1);
     }
   if(Bars <= MA_Period) return(0);
   int ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if(ExtCountedBars < 0) return(-1);
//---- last counted bar will be recounted
   if(ExtCountedBars>0) ExtCountedBars--;
   if(ExtCountedBars<MA_Period) ExtCountedBars=MA_Period;
//----
   for(i=Bars-ExtCountedBars-1; i>=0; i--)
      MA1[i]=iMA(NULL,0,MA_Period,0,MA_Method,MA_Applied_Price,i);
//---
   for(i=Bars-ExtCountedBars-1; i>=0; i--)
     {
      double MA2=iMAOnArray(MA1,0,MA_Sampling_Period,0,MA_Method,i);
      MA3G[i]=(Alpha+1)*MA1[i]-Alpha*MA2;
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

Comments