Better_MA_M5
Download scriptThis script is designed as a visual trading tool for the MetaTrader platform that allows you to track and compare specific price trends across different sets of data simultaneously. Instead of showing traditional price action, it creates a dedicated sub-window on your chart where three distinct moving averages are plotted as individual lines.
Core Functionality
The primary purpose of this logic is to provide a synchronized view of three different Moving Average (MA) types at once. It calculates these values based on specific timeframes and historical data lengths, allowing you to see how three different price "velocities" behave in relation to each other over a set period of history (defined by the user as 500 bars).
How the Logic Works
The script performs several automated steps every time it runs:
- Data Synchronization: The tool looks at the current chart's timeframe and maps out the history. It ensures that even if you are looking at different data sets, the values remain aligned in time so that you can compare them accurately on a single horizontal axis.
- Moving Average Calculation: It calculates three separate Simple Moving Averages (SMA) using varying "look-back" periods:
- The first line uses a short-term average of 5 units.
- The second line uses a medium-term average of 10 units.
- The third line uses a longer-term average of 15 units.
- Visual Representation: Each of these three averages is assigned its own color (Lime, Aqua, and White) and displayed as a separate line in a dedicated window.
User Experience
For the end user, this means you can observe the relationship between short-term momentum and long-term trends at a glance. Because the script handles the complex math of aligning different time periods and "shifting" historical data automatically, it provides a clean, organized visual summary of market behavior without cluttering your main price chart.
//+------------------------------------------------------------------+
//| Better_MA.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Aqua
#property indicator_color3 White
extern int NrBars=500;
string Symbol1;
string Symbol2;
string Symbol3;
int Period1= PERIOD_M5;
//---- buffers
double VolBuffer1[];
double VolBuffer2[];
double VolBuffer3[];
double x1,x2,x3;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
int init()
{
/*
if(StringFind(Symbol(),"m",0)>0)
{
Symbol1 = Symbol1+"m";
Symbol2 = Symbol2+"m";
Symbol3 = Symbol3+"m";
}
*/
Symbol1 = Symbol();
Symbol2 = Symbol();
Symbol3 = Symbol();
IndicatorBuffers(3);
SetIndexStyle(0,DRAW_LINE,0,1);
SetIndexBuffer(0, VolBuffer1);
SetIndexLabel(0,Symbol1+" "+Period1);
SetIndexEmptyValue(0,EMPTY_VALUE);
SetIndexStyle(1,DRAW_LINE,0,1);
SetIndexBuffer(1, VolBuffer2);
SetIndexLabel(1,Symbol2+" "+Period1);
SetIndexEmptyValue(1,EMPTY_VALUE);
SetIndexStyle(2,DRAW_LINE,0,1);
SetIndexBuffer(2, VolBuffer3);
SetIndexLabel(2,Symbol3+" "+Period1);
SetIndexEmptyValue(2,EMPTY_VALUE);
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int pos=0;
pos=NrBars;
while(pos>=0)
//while(pos<=NrBars)
{
int pos1 = iBarShift(Symbol1, Period1, iTime(NULL,0,pos), false);
x1=iMA(Symbol1,Period1,5,0,MODE_SMA,PRICE_CLOSE ,pos1);
//if(x1==0)
// x1=EMPTY_VALUE;
if (iTime(Symbol1, Period1, pos1) == iTime(NULL,0,pos)) // have to be sure the times match.
{
VolBuffer1[pos]=x1;
}
else
{
VolBuffer1[pos]=x1;
}
int pos2 = iBarShift(Symbol2, Period1, iTime(NULL,0,pos), false);
x2=iMA(Symbol2,Period1,10,0,MODE_SMA,PRICE_CLOSE ,pos2);
//if(x2==0)
// x2=EMPTY_VALUE;
if (iTime(Symbol2, Period1, pos2) == iTime(NULL,0,pos)) // have to be sure the times match.
{
VolBuffer2[pos]=x2;
}
else
{
VolBuffer2[pos]=x2;
}
int pos3 = iBarShift(Symbol3, Period1, iTime(NULL,0,pos), false);
x3=iMA(Symbol3,Period1,15,0,MODE_SMA,PRICE_CLOSE,pos3);
//if(x3==0)
// x3=EMPTY_VALUE;
if (iTime(Symbol3, Period1, pos3) == iTime(NULL,0,pos)) // have to be sure the times match.
{
VolBuffer3[pos]=x3;
}
else
{
VolBuffer3[pos]=x3;
}
//pos++;
pos--;
}
return(0);
}
//+------------------------------------------------------------------+
Comments