This custom trading indicator is designed to provide visual cues regarding market momentum and trend direction by analyzing the relationship between different price averages. It operates by calculating the difference between two moving averages and then smoothing that data to identify specific shifts in market behavior.
Core Logic Overview
The script follows a three-step mathematical process to generate the visual data you see on your chart:
1. Calculating Momentum (The MACD Baseline) The indicator first looks at two different "speeds" of price averages—a fast-moving average and a slow-moving average. By subtracting the slow average from the fast average, it determines the current momentum of the market. If the fast average is significantly higher than the slow one, it suggests upward momentum; if lower, it suggests downward momentum.
2. Creating the Signal Line To ensure the indicator doesn't react too impulsively to minor price "noise," the script takes the result from the first step and applies a third smoothing calculation (a signal line). This acts as a secondary filter, helping to confirm whether a trend is sustained or just a temporary fluctuation.
3. Visualizing Trend Direction The final logic determines what you actually see on the screen:
- Comparison: The script compares the current momentum value against the previous one to detect a change in direction.
- Dynamic Mapping: Based on this comparison, the script assigns values to two different visual "buffers."
- Visual Output: It produces a histogram effect where the colors represent the trend. When the momentum is moving in one direction, it highlights that strength; when the momentum flips, the visual representation switches. This allows traders to quickly identify whether the market is currently pushing upward or downward based on the intensity of the plotted bars.
Summary for the User
Instead of just showing you a raw line, this tool filters out market "jitter" and provides a clear, color-coded visual representation of momentum. It tells you not just where the price is, but how fast and in what direction the current trend is moving relative to its recent history.
//+------------------------------------------------------------------+
//| macd-2.mq4 |
//| Copyright ?2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 LimeGreen
#property indicator_color2 Red
#property indicator_color3 Black
#property indicator_color4 Black
//---- indicator parameters
extern int FastEMA=13;
extern int SlowEMA=17;
extern int SignalEMA=9;
//---- indicator buffers
double ind_buffer1[];
double ind_buffer2[];
double ind_buffer3[];
double ind_buffer4[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(5);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2 );
SetIndexDrawBegin(0,SignalEMA);
SetIndexDrawBegin(1,SignalEMA);
//---- 4 indicator buffers mapping
if(!SetIndexBuffer(0,ind_buffer1) &&
!SetIndexBuffer(1,ind_buffer2) &&
!SetIndexBuffer(2,ind_buffer3) &&
!SetIndexBuffer(3,ind_buffer4))
Print("cannot set indicators\' buffers!");
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("MACD-2 ("+FastEMA+","+SlowEMA+","+SignalEMA+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Oscillator |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
double prev,current;
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- macd counted in the 1-st additional buffer
for(int i=0; i<limit; i++)
ind_buffer3[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd additional buffer
for(i=0; i<limit; i++)
ind_buffer4[i]=iMAOnArray(ind_buffer3,Bars,SignalEMA,0,MODE_EMA, i);
//---- dispatch values between 2 buffers
bool up=true;
for(i=limit-1; i>=0; i--)
{
current=ind_buffer3[i]-ind_buffer4[i];
prev=ind_buffer3[i+1]-ind_buffer4[i+1];
if(current>prev) up=true;
if(current<prev) up=false;
if(!up)
{
ind_buffer2[i]=3*current;
ind_buffer1[i]=0.0;
}
else
{
ind_buffer1[i]=3*current;
ind_buffer2[i]=0.0;
}
}
return(0);
}
//+------------------------------------------------------------------+
Comments