xpMA_v3_modified
Download scriptThe XP Moving Average is a sophisticated technical analysis tool designed to help traders identify price trends and momentum by providing a visually distinct representation of market averages. Unlike standard moving averages that might lag behind price movements, this script utilizes multi-layered calculations to offer a more refined view of market dynamics.
Core Functionality
The primary purpose of this indicator is to calculate various types of Moving Averages (MA) based on user preferences and then overlay them onto the trading chart. It provides several calculation methods, allowing you to choose between:
- Standard Methods: Simple, Exponential, Smoothed, and Linear Weighted averages.
- Advanced Smoothing: More complex formulas like Double Exponential (DEMA), Triple Exponential (TEMA), and the T3 Moving Average, which are designed to reduce "noise" and provide smoother data lines.
Intelligent Trend Visualization
One of the unique features of this script is how it handles the visual output of these averages. Instead of just showing a single continuous line, the logic performs a secondary calculation to determine market direction:
- Reference Comparison: The script calculates a "Delayed Average" by looking at a specific number of preceding data points (determined by the Step Period).
- Dynamic Highlighting: It compares the current moving average value against this reference average.
- Visual Filtering:
- If the current moving average is lower than the reference, it modifies the visual display to represent a specific trend state.
- If the current moving average is higher than the reference, it identifies the opposite trend.
- This creates a cleaner visual experience where the indicator effectively "filters" out minor fluctuations, making it easier for a trader to see the broader market direction at a glance.
Advanced Features
- Customizable Price Inputs: The tool can be configured to calculate averages based on different price points, such as the Close, Open, High, Low, or Median prices, depending on what best suits your specific trading strategy.
- Debug & Documentation Mode: For developers or advanced users, the script includes a built-in "Debug Mode." When active, this captures screenshots of the chart automatically and prints data values to the log, allowing for precise verification of how the indicator is behaving in real-time.
In summary, the XP Moving Average acts as a sophisticated trend-filtering tool that simplifies complex price actions into clear, readable visual cues, helping traders distinguish between temporary market "noise" and significant directional moves.
//+------------------------------------------------------------------+
//| 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 Blue
#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;
extern bool DebugMode = true;
extern int Step_Period = 4;
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 limit;
int i = 0;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
//if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars-1;
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=0; shift<limit; shift++)
{
UpBuffer[shift] = buffer[shift];
DownBuffer[shift] = buffer[shift];
Buffer3[shift] = buffer[shift];
}
for(shift=0; shift<limit; shift++)
{
//#####################
// Add for Test
double dMA = 0;
for(int k = shift+1; k <= shift+Step_Period; k++){
dMA += buffer[k];
}
dMA = dMA / Step_Period;
//######################
if (buffer[shift] < dMA)
{
UpBuffer[shift] = EMPTY_VALUE;
}
else if (buffer[shift]>dMA)
{
DownBuffer[shift] = EMPTY_VALUE;
}
else
{
UpBuffer[shift] = CLR_NONE;
DownBuffer[shift] = CLR_NONE;
}
}
if(DebugMode)
{
Print ("limit=",limit , " : Previous value=" , Buffer3[1] , " : Currnt value=" , Buffer3[0] );
TakeScreenShot(40);
}
return(0);
}
void TakeScreenShot(int exit_on = -1)
{
int count = 1;
if(!GlobalVariableCheck("ssc"))
{
GlobalVariableSet("ssc",1);
count = 1;
}
else
{
count = GlobalVariableGet("ssc") + 1;
GlobalVariableSet("ssc",count);
}
if ( exit_on > 0 && count > exit_on ) return(0);
string filename = "xpMA\\" + "xpMA_" + Symbol() + "_" + DoubleToStr(count,0) + ".jpg";
ScreenShot(filename,320,480);
}
Comments