MACrossIndicator
Download scriptThis tool is a technical analysis indicator designed to help traders identify momentum and potential trend reversals by calculating the relationship between two different moving averages. While it shares some foundational logic with the well-known MACD (Moving Average Convergence Divergence) indicator, it has been specialized to function as a "Moving Average Cross" system.
How the Logic Works
The core functionality of this script relies on comparing two distinct mathematical "averages" of price data:
- The Fast Component: This looks at short-term price action (determined by a fast period, such as 4 bars). It reacts quickly to recent changes in the market.
- The Slow Component: This looks at long-term price action (determined by a slower period, such as 14 bars). It represents the broader trend of the market.
The Calculation Process
The script performs three primary actions to generate the data you see on your screen:
- Difference Tracking: The tool constantly subtracts the value of the Slow Moving Average from the Fast Moving Average. By multiplying this difference by a scale factor (10,000), it creates a visible line that shows the "gap" between short-term and long-term trends.
- Signal Smoothing: To prevent the data from being too erratic or "noisy," the script takes that difference and applies a smoothing calculation. It blends the current difference with the previous value to create a secondary "Signal Line."
- Visual Representation: On your trading platform, these calculations are plotted in a separate window beneath your main price chart. This allows you to see at a glance whether the short-term momentum is pulling away from or returning to the long-term average.
Key Features for the User
- Customizable Averaging Methods: The logic allows you to choose between three different types of moving averages: Simple (SMA), Exponential (EMA), and Linear Weighted (LWMA). This gives you the flexibility to prioritize either recent price action or a balanced historical average.
- Dynamic Scaling: Because it calculates the difference between two lines, the indicator effectively highlights "crossovers." When the gap widens or narrows significantly, it signals changes in market velocity.
- Simplified Setup: The script is designed to automatically adjust its labels and display settings based on the periods and methods you choose, making it easy to switch between different trading styles without manual reconfiguration.
//+------------------------------------------------------------------+
//| iMACross.mq4 |
//| Copyright © 2005, David W. Thomas |
//| Copyright © 2005, firedave - MOD |
//| mailto:davidwt@usa.net |
//| mailto:firedave@fx-review.com |
//+------------------------------------------------------------------+
// This is the correct computation and display of MACD,
// that modified to a MA Cross Indicator
#property copyright "Copyright © 2005, Metaquotes"
#property link "mailto:metaquotes@metaquotes.net"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
#property indicator_color2 Blue
//---- input parameters
extern int FastMAPeriod=4;
extern int SlowMAPeriod=14;
extern int FastMAType=1;
extern int SlowMAType=1;
//---- buffers
double MACDLineBuffer[];
double SignalLineBuffer[];
int FastMAMode;
int SlowMAMode;
string strFastMAType;
string strSlowMAType;
//---- variables
double alpha = 0;
double alpha_1 = 0;
int SignalMAPeriod = 1;
switch (FastMAType)
{
case 1: strFastMAType="S"; FastMAMode=MODE_SMA; break;
case 2: strFastMAType="E"; FastMAMode=MODE_EMA; break;
case 3: strFastMAType="W"; FastMAMode=MODE_LWMA; break;
default: strFastMAType="S"; FastMAMode=MODE_SMA; break;
}
switch (SlowMAType)
{
case 1: strSlowMAType="S"; SlowMAMode=MODE_SMA; break;
case 2: strSlowMAType="E"; SlowMAMode=MODE_EMA; break;
case 3: strSlowMAType="W"; SlowMAMode=MODE_LWMA; break;
default: strSlowMAType="S"; SlowMAMode=MODE_SMA; break;
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,MACDLineBuffer);
SetIndexDrawBegin(0,SlowMAPeriod);
SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
SetIndexBuffer(1,SignalLineBuffer);
SetIndexDrawBegin(1,SlowMAPeriod+SignalMAPeriod);
SetIndexDrawBegin(2,SlowMAPeriod+SignalMAPeriod);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("MA Cross ("+FastMAPeriod+"-"+strFastMAType+","+SlowMAPeriod+"-"+strSlowMAType+") ");
SetIndexLabel(0,"DPO");
SetIndexLabel(1,"Signal");
SetLevelStyle(STYLE_DOT,1,Silver);
SetLevelValue(0,0);
//----
alpha = 2.0 / (SignalMAPeriod + 1.0);
alpha_1 = 1.0 - alpha;
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars = IndicatorCounted();
//---- 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;
for(int i=limit; i>=0; i--)
{
MACDLineBuffer[i] = MathRound((iMA(NULL,0,FastMAPeriod,0,FastMAMode,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod,0,SlowMAMode,PRICE_CLOSE,i))*10000);
SignalLineBuffer[i] = alpha*MACDLineBuffer[i] + alpha_1*SignalLineBuffer[i+1];
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments