iMACross
Download scriptThis technical tool is designed to help traders visualize momentum and trend shifts by calculating the relationship between two different moving averages. Rather than looking at simple price action, this script identifies the "gap" between a fast-moving trend and a slower, more stable trend to provide a clearer picture of market dynamics.
Core Functionality
The logic operates by comparing two distinct types of averages:
- Fast Moving Average: This tracks short-term price changes rapidly.
- Slow Moving Average: This tracks long-term price trends more smoothly.
By subtracting the slow average from the fast average, the script generates a primary data line (the MACD Line). When this line is above zero, it suggests that the short-term momentum is stronger than the long-term trend; when below zero, it suggests the opposite.
Smoothing and Signal Generation
To make the data more readable and reduce "noise" (frequent, insignificant price flickers), the script applies a secondary calculation known as a smoothing function. This creates a Signal Line.
The Signal Line acts as an average of the primary data line itself. By observing the interaction between these two lines—specifically when they cross one another—traders can identify potential turning points in the market. For example, if the primary line crosses above the signal line, it often indicates a shift toward upward momentum.
Key Customization Options
The script is highly flexible and allows users to tailor the logic to different trading styles:
- Periods: You can adjust the speed of the "Fast" and "Slow" calculations to suit different timeframes (e.g., day trading vs. long-term investing).
- MA Types: Users can choose between different mathematical methods for calculating the averages, such as Simple (SMA), Exponential (EMA), or Linear Weighted (LWMA), depending on how much weight they want to give to recent price data.
Visual Representation
The script displays these results in a separate window at the bottom of the trading chart. It plots the main calculated difference as a solid line and provides a secondary dotted signal line, making it easy for users to see exactly where trends are strengthening or weakening in real-time.
//+------------------------------------------------------------------+
//| 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;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
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;
}
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