AllAverages_v1.1
Download scriptThis technical tool is designed to provide traders with a versatile and highly customizable method for calculating moving averages on financial charts. Rather than offering just one type of trend indicator, it acts as a comprehensive suite that allows users to choose from twelve different mathematical models to determine price trends.
Core Logic and Functionality
The script operates by first taking raw price data (such as the open, high, low, or close) and processing it through multiple layers of calculation. Instead of simply calculating a moving average directly from the price, the tool follows a sophisticated "double-layer" approach:
- Initial Smoothing: The script first creates an intermediate set of smoothed data points based on the user's chosen price source.
- Advanced Transformation: It then applies one of twelve distinct mathematical formulas to that smoothed data. This allows for various types of trend detection, ranging from standard averages to complex, "zero-lag" models that aim to reduce the delay typically found in traditional indicators.
Included Calculation Models
The tool includes a wide array of professional methodologies, allowing users to switch between different behaviors:
- Standard Smoothing: Includes traditional methods like Simple Moving Averages (SMA) and Exponential Moving Averages (EMA).
- Weighted & Momentum Methods: Options like Linear Weighted (LWMA), Hull Moving Average (HMA), and Volume Weighted variations that place different importance on recent versus older data.
- Complex Signal Smoothing: Highly specialized formulas such as the T3, Smoothed Moving Average (SMMA), and Sine Wave Moving Average (SineWMA), which are designed to filter out market "noise" while maintaining responsiveness to actual price movements.
User Experience Features
- Dynamic Labeling: The script automatically updates its visual name on the chart based on the specific mathematical method selected by the user, ensuring clarity during analysis.
- Customizable Parameters: Users can define the look-back period (how much historical data to consider), the shift (visual offset), and the specific price point used for the calculation, making it adaptable to different timeframes and asset types.
- Visual Output: The result is rendered as a clean, solid orange line on the chart, providing a clear visual guide for identifying trend direction and potential entry or exit points.
//+------------------------------------------------------------------+
//| AllAverages_v1.1.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=12;
//---- indicator buffers
double MA[];
double aPrice[];
double tmp[][6];
double tmp2[];
double tmp3[];
double tmp4[];
//----
int draw_begin;
string short_name;
//+------------------------------------------------------------------+
//| 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;
case 11: short_name="DEMA("; break;
case 12: short_name="T3("; 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
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int limit, i, shift;
int counted_bars=IndicatorCounted();
ArrayResize(tmp,Bars+1);
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-1;
for(shift=limit; shift>=0; shift--)
aPrice[shift] = iMA(NULL,0,1,0,0,Price,shift);
for(shift=limit; shift>=0; shift--)
{
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;
case 11: MA[shift] = DEMA(0,aPrice[shift],MA_Period,1,shift); break;
case 12: MA[shift] = T3(aPrice[shift],MA_Period,0.7,shift); break;
default: MA[shift] = SMA(aPrice,MA_Period,shift); break;
}
}
//---- done
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)
{
if(bar == Bars - 2) double ema = array1[bar];
else
if(bar < Bars - 2) ema = array2[bar+1] + 2.0/(1+per)*(array1[bar] - array2[bar+1]);
return(ema);
}
double Wilder(double array1[],double array2[],int per,int bar)
{
if(bar == Bars - per) double wilder = SMA(array1,per,bar);
else
if(bar < Bars - per) wilder = array2[bar+1] + (array1[bar] - array2[bar+1])/per;
return(wilder);
}
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)
{
if(bar == Bars - per) double smma = SMA(array1,per,bar);
else
if(bar < Bars - per)
{
double Sum = 0;
for(int i = 0;i < per;i++) Sum += array1[i+bar+1];
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);
if(bar == Bars - lag) double zema = array1[bar];
else
if(bar < Bars - lag) zema = alfa*(2*array1[bar] - array1[bar+lag]) + (1-alfa)*array2[bar+1];
return(zema);
}
double DEMA(int num,double price,int per,double v,int bar)
{
int b = Bars - bar;
if(bar == Bars - 2) {double dema = price; tmp[b][num] = dema; tmp[b][num+1] = dema;}
else
if(bar < Bars - 2)
{
tmp[b][num] = tmp[b-1][num] + 2.0/(1+per)*(price - tmp[b-1][num]);
tmp[b][num+1] = tmp[b-1][num+1] + 2.0/(1+per)*(tmp[b][num] - tmp[b-1][num+1]);
dema = (1+v)*tmp[b][num] - v*tmp[b][num+1];
//if(bar==0) Print("Bars=",Bars," bar=", bar," tmp0=",tmp[b][num]," tmp1=",tmp[b-1][num]," pr=",price);
}
return(dema);
}
double T3(double price,int per,double v,int bar)
{
int b = Bars - bar;
if(bar == Bars - 2)
{
double T3 = price;
for(int k=0;k<=5;k++) tmp[b][k] = T3;
}
else
if(bar < Bars - 2)
{
double dema1 = DEMA(0,price,per,v,bar);
double dema2 = DEMA(2,dema1,per,v,bar);
T3 = DEMA(4,dema2,per,v,bar);
}
return(T3);
}
Comments