xpMA_v1
Download scriptThis tool is a versatile technical analysis indicator designed to help traders identify trend directions and price momentum by visualizing various types of moving averages on a trading chart. Rather than providing just a single standard line, it processes complex mathematical calculations to smooth out market "noise," making it easier for users to see the underlying direction of the asset's price.
Core Functionality
The primary logic of this script revolves around calculating different types of Moving Averages (MA) based on user preference. It allows you to choose from several industry-standard calculation methods:
- Standard Varieties: Includes Simple, Exponential, Smoothed, and Linear Weighted averages.
- Advanced Smoothing: Provides high-level options like Double Exponential (DEMA), Triple Exponential (TEMA), and the T3 Moving Average.
The script is highly customizable, allowing you to define the specific look-back period (how many candles of data to consider), the specific price point to analyze (such as the opening, closing, or high/low prices), and unique volume factors for specialized averages.
Visual Logic and Trend Detection
Beyond simply drawing a line on the chart, the script performs a comparative analysis between the current price point and the previous one to determine momentum:
- Data Processing: The script first calculates the primary moving average based on your chosen settings. It then creates a "buffer" (a temporary memory space) to hold these values.
- Directional Analysis: Once the main average is calculated, the logic compares each data point to the one immediately preceding it:
- If the current value is higher than the previous value, the script identifies an upward movement.
- If the current value is lower than the previous value, it identifies a downward movement.
- Dynamic Coloring: The tool uses this logic to assign specific visual cues. It distinguishes between rising and falling trends, effectively highlighting price momentum so that traders can quickly see whether the market is gaining strength or losing ground without having to manually interpret the raw numbers.
In essence, this script acts as a sophisticated filter that takes raw market data, applies complex smoothing algorithms, and translates that information into a clear visual representation of current market trends.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| 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