xpMA_v2
Download scriptThe XP Moving Average is a specialized technical analysis tool designed to provide traders with a clearer view of market trends by calculating and visualizing specific types of moving averages. Rather than just providing a single line, this script focuses on identifying the direction of price momentum through multi-layered calculations.
Core Functionality
The primary role of this indicator is to choose and calculate a specific type of Moving Average based on user preference. It supports several standard industry methods, including Simple, Exponential, Smoothed, Linear Weighted, Double Exponential (DEMA), Triple Exponential (TEMA), and the specialized T3 Moving Average.
Because different trading styles require different levels of sensitivity, the logic allows for:
- Customizable Periods: Defining how much historical data is considered.
- Price Source Selection: Choosing whether to base the average on closing prices, opening prices, highs, lows, or various weighted averages of these values.
Advanced Calculation Logic
For more complex moving averages (like DEMA and TEMA), the script performs "nested" calculations. Instead of calculating a single average of the price, it takes an average of an average. This mathematical layering is designed to reduce the "lag" typically found in standard moving averages, making the indicator react more quickly to recent price changes while still filtering out minor market noise.
Visual Trend Identification
The script goes beyond basic calculation by interpreting the relationship between consecutive data points to determine market direction:
- Trend Detection: The logic compares the current value of the moving average against the previous one.
- Directional Mapping:
- If the moving average is currently higher than it was in the previous period, the script identifies an upward trend.
- If the moving average is lower than the previous period, it identifies a downward trend.
- Dynamic Visualization: By identifying these shifts, the indicator can visually separate "Up" movements from "Down" movements. This helps traders quickly distinguish between bullish and bearish momentum at a glance, providing a cleaner visual representation of where the price is heading relative to its historical average.
//+------------------------------------------------------------------+
//| XP Moving Average |
//| xpMA.mq4 |
//| Developed by Coders Guru |
//| http://www.xpworx.com |
//+------------------------------------------------------------------+
#property link "http://www.xpworx.com"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 Green
#property indicator_color3 Red
#define MODE_DEMA 4
#define MODE_TEMA 5
#define MODE_T3MA 6
/* Moving average types constants:
------------------------------------
MODE_SMA 0 Simple moving average,
MODE_EMA 1 Exponential moving average,
MODE_SMMA 2 Smoothed moving average,
MODE_LWMA 3 Linear weighted moving average.
MODE_DEMA 4 Double Exponential Moving Average.
MODE_TEMA 5 Triple Exponential Moving Average.
MODE_T3MA 6 T3 Moving Average.
------------------------------------*/
/* Applied price constants:
-------------------------------
PRICE_CLOSE 0 Close price.
PRICE_OPEN 1 Open price.
PRICE_HIGH 2 High price.
PRICE_LOW 3 Low price.
PRICE_MEDIAN 4 Median price, (high+low)/2.
PRICE_TYPICAL 5 Typical price, (high+low+close)/3.
PRICE_WEIGHTED 6 Weighted close price, (high+low+close+close)/4.
--------------------------------- */
extern int MA_Period = 10;
extern int MA_Type = MODE_EMA;
extern int MA_Applied = PRICE_CLOSE;
extern double T3MA_VolumeFactor = 0.8;
double UpBuffer[];
double DownBuffer[];
double Buffer3[];
double buffer[];
double tempbuffer[];
double matriple[];
int init()
{
IndicatorBuffers(6);
SetIndexStyle(2,DRAW_LINE,STYLE_DOT,2);
SetIndexBuffer(2,UpBuffer);
SetIndexStyle(1,DRAW_LINE,STYLE_DOT,2);
SetIndexBuffer(1,DownBuffer);
SetIndexStyle(0,DRAW_LINE,STYLE_DOT,2);
SetIndexBuffer(0,Buffer3);
SetIndexBuffer(3,buffer);
SetIndexBuffer(4,tempbuffer);
SetIndexBuffer(5,matriple);
return(0);
}
int deinit()
{
return(0);
}
void start()
{
int counted_bars=IndicatorCounted();
int i = 0;
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
int limit=Bars-counted_bars;
switch (MA_Type)
{
case 0:
case 1:
case 2:
case 3:
{
for(i=0; i<limit; i++)
{
buffer[i] = iMA(NULL,0,MA_Period,0,MA_Type,MA_Applied,i);
}
}
break;
case 4:
{
for(i=0; i<limit; i++)
{
tempbuffer[i] = iMA(NULL,0,MA_Period,0,MODE_EMA,MA_Applied,i);
}
for(i=0; i<limit; i++)
{
matriple[i] = iMAOnArray(tempbuffer,0,MA_Period,0,MODE_EMA,i);
}
for(i=0; i<limit; i++)
{
buffer[i] = iMAOnArray(matriple,0,MA_Period,0,MODE_EMA,i);
}
}
break;
case 5:
{
for(i=0; i<limit; i++)
{
tempbuffer[i] = iMA(NULL,0,MA_Period,0,MODE_EMA,MA_Applied,i);
}
for(i=0; i<limit; i++)
{
buffer[i] = iMAOnArray(tempbuffer,0,MA_Period,0,MODE_EMA,i);
}
}
break;
case 6:
{
for(i=0; i<limit; i++)
{
buffer[i] = iCustom(NULL,0,"T3MA",MA_Period,T3MA_VolumeFactor,0,i);
}
}
break;
}
for(int shift=limit; shift>=0; shift--)
{
UpBuffer[shift] = buffer[shift];
DownBuffer[shift] = buffer[shift];
Buffer3[shift] = buffer[shift];
}
for(shift=limit; shift>=0; shift--)
{
if (buffer[shift+1]>buffer[shift])
{
UpBuffer[shift+1] = EMPTY_VALUE;
//Buffer3[i+1] = EMPTY_VALUE;
}
else if (buffer[shift+1]<buffer[shift] )
{
DownBuffer[shift+1] = EMPTY_VALUE;
//Buffer3[i+1] = EMPTY_VALUE;
}
else
{
UpBuffer[shift+1] = CLR_NONE;
DownBuffer[shift+1] = CLR_NONE;
}
}
return(0);
}
Comments