_AllAverages_v1

Download script
Author: Copyright � 2007-08, TrendLaboratory
_AllAverages_v1

The AllAverages_v1 script is a versatile technical analysis tool designed for the MetaTrader platform. Its primary purpose is to allow traders to visualize various types of moving averages on a price chart, providing a comprehensive way to identify trends and momentum without needing to install multiple separate indicators.

Core Functionality

Instead of being limited to a single type of moving average, this script acts as an "all-in-one" engine. It allows the user to toggle between several different mathematical models to calculate price averages. By changing the settings, a trader can switch how the indicator reacts to price fluctuations—ranging from "smooth" and slow reactions to "fast" and highly responsive ones.

Key Mathematical Models Included

The script incorporates a wide array of calculation methods, including:

  • Standard Averages: Basic Simple Moving Averages (SMA).
  • Exponential & Smoothing Models: These include the Exponential Moving Average (EMA), Smoothed Moving Average (SMMA), and Wilder’s Moving Average, which place more importance on recent price data.
  • Weighted & Linear Models: Options like the Linear Weighted Moving Average (LWMA) and the Hull Moving Average (HMA) adjust the "weight" of prices to reduce lag.
  • Advanced Trigonometric & Geometric Models: More complex calculations such as the Sine Weighted Moving Average (SineWMA), Triple Moving Average (TriMA), and Zero Lag EMA are included for traders seeking more sophisticated trend detection.

How it Works Visually

  1. Price Tracking: The script first identifies a base price point (such as the closing price).
  2. Multi-Layer Calculation: It takes that price data and processes it through the user-selected mathematical formula based on a specific time period (e.g., a 14-day window).
  3. Dynamic Rendering: The results are plotted on the chart as a continuous orange line.
  4. Real-Time Labeling: The script automatically updates its display name to reflect exactly which calculation method and period are currently active, making it easy for the trader to keep track of their settings at a glance.
  5. Current Price Marker: It places a specific visual marker (an arrow) on the most recent price point to provide a clear reference for the current value relative to the moving average.

Summary for the User

In essence, this script simplifies a trader's workspace. Rather than having to choose between ten different indicators, you can use this single tool to experiment with various types of averages to see which one best aligns with your trading strategy, all while maintaining a clean and organized chart.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
_AllAverages_v1
//+------------------------------------------------------------------+
//|             __AllAverages_v1                                     |
//|                                                                  |
//|   MODIFIED BY Avery T. Horton, Jr. aka TheRumpledOne             |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpldone@gmail.com                                         |  
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                               AllAverages_v1.mq4 |
//|                             Copyright © 2007-08, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007-08, TrendLaboratory"
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1  Orange
#property indicator_width1  2  
//---- indicator parameters
extern int Price=0;
extern int MA_Period=14;
extern int MA_Shift=0;
extern int MA_Method=0;
//---- indicator buffers
double MA[];
double aPrice[];
//----
int    draw_begin;
string short_name;

string tLHHL0   ;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexShift(0,MA_Shift);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   draw_begin=MA_Period;
//---- indicator short name
   switch(MA_Method)
   {
      case 1 : short_name="EMA(";  break;
      case 2 : short_name="Wilder("; break;
      case 3 : short_name="LWMA("; break;
      case 4 : short_name="SineWMA("; break;
      case 5 : short_name="TriMA("; break;
      case 6 : short_name="LSMA("; break;
      case 7 : short_name="SMMA("; break;
      case 8 : short_name="MMA("; break;
      case 9 : short_name="HMA("; break;
      case 10: short_name="ZeroLagEMA("; break;
      default: MA_Method=0; short_name="SMA(";
   }
   IndicatorShortName(short_name+MA_Period+")");
   SetIndexDrawBegin(0,draw_begin);
   SetIndexLabel(0,short_name+MA_Period+")");
//---- indicator buffers mapping
   IndicatorBuffers(2);
   SetIndexBuffer(0,MA);
   SetIndexBuffer(1,aPrice);
//---- initialization done

tLHHL0 = short_name+MA_Period+")";
   return(0);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
{
   int limit, i, shift;
   int counted_bars=IndicatorCounted();
     
   if(counted_bars<1)
      for(i=1;i<=draw_begin;i++) MA[Bars-i]=iMA(NULL,0,1,0,0,Price,Bars-i); 
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   for(shift=limit; shift>=0; shift--)
   aPrice[shift] = iMA(NULL,0,1,0,0,Price,shift);
   
   for(shift=limit; shift>=0; shift--)
   {
      if(shift == Bars - MA_Period)
      {
         switch(MA_Method)
         {
         case 1 : MA[shift] = SMA(aPrice,MA_Period,shift); break;
         case 2 : MA[shift] = SMA(aPrice,MA_Period,shift); break;
         case 7 : MA[shift] = SMA(aPrice,MA_Period,shift); break;
         case 10: MA[shift] = SMA(aPrice,1,shift); break;
         }
      }
      else
      if(shift < Bars - MA_Period)
      {
         switch(MA_Method)
         {
         case 1 : MA[shift] = EMA(aPrice,MA,MA_Period,shift); break;
         case 2 : MA[shift] = Wilder(aPrice,MA,MA_Period,shift); break;  
         case 3 : MA[shift] = LWMA(aPrice,MA_Period,shift); break;
         case 4 : MA[shift] = SineWMA(aPrice,MA_Period,shift); break;
         case 5 : MA[shift] = TriMA(aPrice,MA_Period,shift); break;
         case 6 : MA[shift] = LSMA(aPrice,MA_Period,shift); break;
         case 7 : MA[shift] = SMMA(aPrice,MA,MA_Period,shift); break;
         case 8 : MA[shift] = MMA(aPrice,MA_Period,shift); break;
         case 9 : MA[shift] = HMA(aPrice,MA_Period,shift); break;
         case 10: MA[shift] = ZeroLagEMA(aPrice,MA,MA_Period,shift); break;
         default: MA[shift] = SMA(aPrice,MA_Period,shift); break;
         }
      }
   }     
//---- done


  if (ObjectFind(tLHHL0) != 0)
      {
          ObjectCreate(tLHHL0,OBJ_ARROW,0,Time[0],MA[0]);
          ObjectSet(tLHHL0,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tLHHL0,OBJPROP_COLOR,Orange);  
      } 
      else
      {
         ObjectMove(tLHHL0,0,Time[0],MA[0]);
      }
   return(0);
}

double SMA(double array[],int per,int bar)
{
   double Sum = 0;
   for(int i = 0;i < per;i++) Sum += array[i+bar];
  
   return(Sum/per);
}                

double EMA(double array1[],double array2[],int per,int bar)
{
   return(array2[bar+1] + 2.0/(1+per)*(array1[bar] - array2[bar+1]));
}

double Wilder(double array1[],double array2[],int per,int bar)
{
   return(array2[bar+1] + 1.0/per*(array1[bar] - array2[bar+1]));
}

double LWMA(double array[],int per,int bar)
{
   double Sum = 0;
   double Weight = 0;
   
      for(int i = 0;i < per;i++)
      { 
      Weight+= (per - i);
      Sum += array[bar+i]*(per - i);
      }
   if(Weight>0) double lwma = Sum/Weight;
   else lwma = 0; 
   return(lwma);
} 

double SineWMA(double array[],int per,int bar)
{
   double pi = 3.1415926535;
   double Sum = 0;
   double Weight = 0;
   double del = 0.5*pi/per;
     
      for(int i = 0;i < per;i++)
      { 
      Weight+= MathSin(0.5*pi - del*i);
      Sum += array[bar+i]*MathSin(0.5*pi - del*i);
      }
   if(Weight>0) double swma = Sum/Weight;
   else swma = 0; 
   return(swma);
}

double TriMA(double array[],int per,int bar)
{
   double sma[];
   int len = MathCeil((per+1)*0.5);
   ArrayResize(sma,len);
   
   for(int i = 0;i < len;i++) sma[i] = SMA(array,len,bar+i);
   double trima = SMA(sma,len,0);
   
   return(trima);
}
 
double LSMA(double array[],int per,int bar)
{   
   double Sum=0;
   for(int i=per; i>=1; i--) Sum += (i-(per+1)/3.0)*array[per-i+bar];
   double lsma = Sum*6/(per*(per+1));
   return(lsma);
}

double SMMA(double array1[],double array2[],int per,int bar)
{
   double Sum = 0;
   for(int i = 0;i < per;i++) Sum += array1[i+bar+1];
         
   double smma = (Sum - array2[bar+1] + array1[bar])/per;
   return(smma);
}                

double MMA(double array[],int per,int bar)
{
   double Slope = 0;

   for (int i = 1;i <= per;i++)
   {
   double Factor = 1 + (2 * (i - 1));
   Slope += (array[bar + i - 1] * ((per - Factor)/2));
   }
   double sma = SMA(array,per,bar);
   double mma = sma + (6 * Slope) / ((per + 1) * per);
   return(mma);
}

double HMA(double array[],int per,int bar)
{
   double tmp[];
   int len =  MathSqrt(per);
   ArrayResize(tmp,len);
   
   for(int i = 0; i < len;i++) tmp[i] = 2*LWMA(array,per/2,bar+i) - LWMA(array,per,bar+i);  
   double hma = LWMA(tmp,len,0); 
     
   return(hma);
}

double ZeroLagEMA(double array1[],double array2[],int per,int bar)
{
   double alfa = 2.0/(1+per); 
   int lag = 0.5*(per - 1); 
   return(alfa*(2*array1[bar] - array1[bar+lag]) + (1-alfa)*array2[bar+1]);
}


Comments