This tool is designed as a visual aid for traders to identify specific shifts in price momentum by analyzing moving averages. Instead of showing a continuous line, it acts as a signaling system that highlights when the average price trend changes direction.
Core Logic and Functionality
The script operates by constantly calculating a Moving Average based on a user-defined period (the number of previous price points to consider). It tracks the "running sum" of these prices to determine where the market is heading.
Rather than simply plotting that average, the logic performs a specific comparison:
- Trend Comparison: For every new price point, the script compares the current average calculation against the previous one.
- Directional Signaling:
- If the math indicates a downward shift in the average value, the tool triggers a visual marker on the chart (represented by a specific character).
- If the math indicates an upward shift or stability, it suppresses that marker and focuses on the alternative state.
- Visual Output: By using two distinct "buffers," the script effectively acts as a toggle. It switches between two different visual states based on which direction the moving average is leaning at any given moment.
How it assists the user
For a trader, this means the tool filters out the "noise" of constant price fluctuations and only provides a visual cue when the underlying trend—as defined by the Moving Average—actually changes. It simplifies technical analysis by translating complex mathematical sums into clear, actionable markers on your trading screen, allowing you to see trend reversals or momentum shifts at a glance without having to manually calculate the averages yourself.
//+------------------------------------------------------------------+
//| MultiMovingAverage.mq4 |
//+------------------------------------------------------------------+
#property copyright "Ron T"
#property link "http://www.lightpatch.com"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 White
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//----
int ExtCountedBars=0;
//---- indicator parameters
extern int MA_Period=13;
extern int Plot_Character=159;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
int init()
{
//---- indicators
// SetIndexStyle(0,DRAW_ARROW, 0, 1, Red);
SetIndexStyle(0,DRAW_ARROW);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexArrow(0,Plot_Character);
// SetIndexStyle(1,DRAW_ARROW, 0, 1, White);
SetIndexStyle(1,DRAW_ARROW);
SetIndexBuffer(1, ExtMapBuffer2);
SetIndexArrow(1,Plot_Character);
//----
SetIndexDrawBegin(0,10);
SetIndexDrawBegin(1,10);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexBuffer(1,ExtMapBuffer2);
if(MA_Period<2) MA_Period=13;
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
double sum=0;
double oldsum=0;
int i;
int pos=Bars-ExtCountedBars-1;
//---- initial accumulation
if(pos<MA_Period) pos=MA_Period;
for(i=1;i<MA_Period;i++,pos--)
sum+=Close[pos];
//=-=-=-=-=-=-=-=-
if(Bars<=10) return(0);
ExtCountedBars=IndicatorCounted();
//---- check for possible errors
if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
if (ExtCountedBars>0) ExtCountedBars--;
//---- main calculation loop
while(pos>=0)
{
oldsum=sum;
// add in the leading value to make MA_Period samples
sum+=Close[pos];
if(oldsum/(MA_Period-1) > sum/MA_Period)
{
ExtMapBuffer1[pos]=sum/MA_Period;
ExtMapBuffer2[pos]=0;
}
else
{
ExtMapBuffer1[pos]=0;
ExtMapBuffer2[pos]=sum/MA_Period;
}
// remove the trailing period to make MA_Period-1 samples
sum-=Close[pos+MA_Period-1];
pos--;
}
//---- zero initial bars
if(ExtCountedBars<1)
for(i=1;i<MA_Period;i++) ExtMapBuffer1[Bars-i]=0;
for(i=1;i<MA_Period;i++) ExtMapBuffer2[Bars-i]=0;
return(0);
}
//+------------------------------------------------------------------+
Comments