MACDTradeSignals
Download scriptThis script is a technical analysis tool designed for the MetaTrader platform. It automates the monitoring of market momentum by calculating specific Moving Average Convergence Divergence (MACD) values and identifying key trading moments based on how these values interact with each other.
Instead of requiring you to watch charts constantly, the script scans price data and generates visual signals (arrows) and audible alerts when specific conditions are met.
Core Logic Overview
The tool operates by comparing two different moving averages—a "Fast" one and a "Slow" one—to determine the overall trend of the market. By subtracting the slow average from the fast average, it creates a momentum line. The script then looks for three specific types of interactions:
1. Primary Trend Shifts (Zero-Line Crosses) The script monitors when the momentum line crosses above or below the zero level.
- Buy Signal: Triggered when the momentum moves from negative to positive.
- Sell Signal: Triggered when the momentum moves from positive to negative.
2. Momentum Strength Signals (Signal Line Crosses) The script also calculates a "Signal Line" (an average of the momentum itself). It looks for instances where the momentum line crosses this signal line while maintaining a specific position relative to zero.
- Enhanced Buy: Triggered when the momentum is positive and crosses above its signal line.
- Enhanced Sell: Triggered when the momentum is negative and crosses below its signal line.
3. Exit Opportunities The tool identifies points where the current trend might be losing steam, providing visual cues to consider closing an open position.
- Exit Long: Triggered when the momentum is positive but crosses under the signal line.
- Exit Short: Triggered when the momentum is negative but crosses over the signal line.
User Interface and Alerts
To make these signals actionable, the script provides the following features:
- Visual Indicators: It places different types of arrows on your chart to distinguish between primary entries, secondary momentum shifts, and exit points.
- Dynamic Alerting: You can enable "Alerts" (pop-up windows) and "Sounds" to notify you the moment a signal appears.
- Dual-State Monitoring: The script is designed to check for signals on two different states of a candle: the "Live" candle (currently forming) and the "Closed" candle (the most recently completed price action). You can choose to receive notifications for either or both.
- Customizable Smoothing: The logic allows for various types of moving average calculations (such as Simple, Exponential, Smoothed, or Linear Weighted), giving you the flexibility to tune the sensitivity of the signals to match different trading styles.
//+------------------------------------------------------------------+
//| MACDTradeSignals.mq4 |
//| * |
//| * |
//+------------------------------------------------------------------+
#property copyright "*"
#property link "*"
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Aqua
#property indicator_color2 DeepPink
#property indicator_color3 Aqua
#property indicator_color4 DeepPink
#property indicator_color5 LightSkyBlue
#property indicator_color6 Pink
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3
#property indicator_width5 1
#property indicator_width6 1
//---- input parameters
extern int FastMAPeriod = 5; // Ïåðèîä áûñòðîé ÌÀ
extern int FastMAMethod = 0; // Ìåòîä ñãëàæèâàíèÿ: 0-SMA, 1-EMA, 2-SMMA, 3-LWMA
extern int FastMAPrice = 0; // Öåíà: 0-Close, 1-Open, 2-High, 3-Low, 4-Median, 5-Typical, 6-Weighted
extern int SlowMAPeriod = 35; // Ïåðèîä ìåäëåííîé ÌÀ
extern int SlowMAMethod = 0; // Ìåòîä ñãëàæèâàíèÿ: 0-SMA, 1-EMA, 2-SMMA, 3-LWMA
extern int SlowMAPrice = 0; // Öåíà: 0-Close, 1-Open, 2-High, 3-Low, 4-Median, 5-Typical, 6-Weighted
extern int SmPeriod = 15; // Ïåðèîä ñãëàæèâàíèÿ ñèãíàëüíîé ëèíèè
extern int SmMethod = 0; // Ìåòîä ñãëàæèâàíèÿ: 0-SMA, 1-EMA, 2-SMMA, 3-LWMA
extern bool Alert_0 = false; // Îêíî ñîîáùåíèå î ñèãíàëå íà íóëåâîì (ôîðìèðóþùåìñÿ) áàðå
extern bool Sound_0 = true; // Çâóê î ñèãíàëå íà íóëåâîì áàðå, ïðè ýòîì äåëàåòñÿ âûâîä ñîîáùåíèÿ â æóðíàë - ìîæíî óâèäèòü êòî ñèãíàëèë
extern string File_0 = "alert"; // Çâóêîâîé ôàéë, èãðàåìûé ïðè ñèãíàëå íà íóëåâîì áàðå
extern bool Alert_1 = false; // Îêíî ñîîáùåíèå î ñèãíàëå íà ïåðâîì (ñôîðìèðîâàííîì) áàðå
extern bool Sound_1 = true; // Çâóê î ñèãíàëå íà ïåðâîì áàðå, ïðè ýòîì äåëàåòñÿ âûâîä ñîîáùåíèÿ â æóðíàë - ìîæíî óâèäèòü êòî ñèãíàëèë
extern string File_1 = "news"; // Çâóêîâîé ôàéë, èãðàåìûé ïðè ñèãíàëå íà ïåðâîì áàðå
//---- buffers
double Up1[];
double Dn1[];
double Up2[];
double Dn2[];
double ClUp[];
double ClDn[];
double macd[];
double sig[];
double pt;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
pt=Point*PTA(Period());
//---- indicators
IndicatorBuffers(8);
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,233);
SetIndexBuffer(0,Up1);
SetIndexEmptyValue(0,0.0);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,234);
SetIndexBuffer(1,Dn1);
SetIndexEmptyValue(1,0.0);
SetIndexStyle(2,DRAW_ARROW);
SetIndexArrow(2,159);
SetIndexBuffer(2,Up2);
SetIndexEmptyValue(2,0.0);
SetIndexStyle(3,DRAW_ARROW);
SetIndexArrow(3,159);
SetIndexBuffer(3,Dn2);
SetIndexEmptyValue(3,0.0);
SetIndexStyle(4,DRAW_ARROW);
SetIndexArrow(4,251);
SetIndexBuffer(4,ClUp);
SetIndexEmptyValue(4,0.0);
SetIndexStyle(5,DRAW_ARROW);
SetIndexArrow(5,251);
SetIndexBuffer(5,ClDn);
SetIndexEmptyValue(5,0.0);
SetIndexStyle(6,DRAW_LINE);
SetIndexBuffer(6,macd);
SetIndexStyle(7,DRAW_LINE);
SetIndexBuffer(7,sig);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start(){
/*
Value1 = Average(Close, Length1)-Average(Close, Length2);
Value2 = XAverage(value1,length3)[1];
Value3 = 0;
if value1 crosses over 0 then buy ("EL1") this bar at close;
if value1>0 and value1 crosses over value2 then buy ("EL2") this bar at close;
if value1>0 and value1 crosses under value2 then exitlong ("XL") this bar at close;
if value1 crosses under 0 then sell ("ES1") this bar at close;
if value1<0 and value1 crosses under value2 then sell ("ES2") this bar at close;
if value1<0 and value1 crosses over value2 then exitshort ("XS") this bar at close;
*/
int limit=Bars-IndicatorCounted()-1;
for(int i=limit;i>=0;i--){
double fast_ma=iMA(NULL,0,FastMAPeriod,0,FastMAMethod,FastMAPrice,i);
double slow_ma=iMA(NULL,0,SlowMAPeriod,0,SlowMAMethod,SlowMAPrice,i);
macd[i]=fast_ma-slow_ma;
}
for(i=limit;i>=0;i--){
Up1[i]=0;
Dn1[i]=0;
Up2[i]=0;
Dn2[i]=0;
ClUp[i]=0;
ClDn[i]=0;
sig[i]=iMAOnArray(macd,0,SmPeriod,0,SmMethod,i);
if(macd[i]>0){
if(macd[i+1]<=0){
Up1[i]=Low[i]-pt;
}
}
if(macd[i]<0){
if(macd[i+1]>=0){
Dn1[i]=High[i]+pt;
}
}
if(macd[i]>0){
if(macd[i]>sig[i]){
if(macd[i+1]<=sig[i+1]){
Up2[i]=Low[i]-pt;
}
}
if(macd[i]<sig[i]){
if(macd[i+1]>=sig[i+1]){
ClUp[i]=High[i]+pt;
}
}
}
if(macd[i]<0){
if(macd[i]<sig[i]){
if(macd[i+1]>=sig[i+1]){
Dn2[i]=High[i]+pt;
}
}
if(macd[i]>sig[i]){
if(macd[i+1]<=sig[i+1]){
ClDn[i]=Low[i]-pt;
}
}
}
}
if(Alert_0 || Sound_0){
i=0;
static datetime lt01=0;
static datetime lt02=0;
static datetime lt03=0;
static datetime lt04=0;
static datetime lt05=0;
static datetime lt06=0;
if(Up1[i]!=0){
if(Time[0]!=lt01){
lt01=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": EL1 (0)");
}
if(Sound_0){
PlaySound(File_0);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": EL1 (0)");
}
}
}
if(Dn1[i]!=0){
if(Time[0]!=lt02){
lt02=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": ES1 (0)");
}
if(Sound_0){
PlaySound(File_0);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": ES1 (0)");
}
}
}
if(Up2[i]!=0){
if(Time[0]!=lt03){
lt03=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": EL2 (0)");
}
if(Sound_0){
PlaySound(File_0);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": EL2 (0)");
}
}
}
if(Dn2[i]!=0){
if(Time[0]!=lt04){
lt04=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": ES1 (0)");
}
if(Sound_0){
PlaySound(File_0);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": ES1 (0)");
}
}
}
if(ClUp[i]!=0){
if(Time[0]!=lt05){
lt05=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": XL (0)");
}
if(Sound_0){
PlaySound(File_0);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": XL (0)");
}
}
}
if(ClDn[i]!=0){
if(Time[0]!=lt06){
lt06=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": XS (0)");
}
if(Sound_0){
PlaySound(File_0);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": XS (0)");
}
}
}
}
if(Alert_1 || Sound_1){
i=1;
static datetime lt11=0;
static datetime lt12=0;
static datetime lt13=0;
static datetime lt14=0;
static datetime lt15=0;
static datetime lt16=0;
if(Up1[i]!=0){
if(Time[0]!=lt11){
lt11=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": EL1 (1)");
}
if(Sound_0){
PlaySound(File_1);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": EL1 (1)");
}
}
}
if(Dn1[i]!=0){
if(Time[0]!=lt12){
lt12=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": ES1 (1)");
}
if(Sound_0){
PlaySound(File_1);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": ES1 (1)");
}
}
}
if(Up2[i]!=0){
if(Time[0]!=lt13){
lt13=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": EL2 (1)");
}
if(Sound_0){
PlaySound(File_1);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": EL2 (1)");
}
}
}
if(Dn2[i]!=0){
if(Time[0]!=lt14){
lt14=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": ES1 (1)");
}
if(Sound_0){
PlaySound(File_1);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": ES1 (1)");
}
}
}
if(ClUp[i]!=0){
if(Time[0]!=lt15){
lt15=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": XL (1)");
}
if(Sound_0){
PlaySound(File_1);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": XL (1)");
}
}
}
if(ClDn[i]!=0){
if(Time[0]!=lt16){
lt16=Time[0];
if(Alert_0){
Alert("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": XS (1)");
}
if(Sound_0){
PlaySound(File_1);
Print("MACDTS "+Symbol()+" "+fTimeFrameName(Period())+": XS (1)");
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+
string fTimeFrameName(int arg){
int v;
if(arg==0){
v=Period();
}
else{
v=arg;
}
switch(v){
case 0:
return("0");
case 1:
return("M1");
case 5:
return("M5");
case 15:
return("M15");
case 30:
return("M30");
case 60:
return("H1");
case 240:
return("H4");
case 1440:
return("D1");
case 10080:
return("W1");
case 43200:
return("MN1");
default:
return("M"+arg);
}
}
int PTA(int aPeriod){
switch(aPeriod){
case PERIOD_M1:
return(4);
case PERIOD_M5:
return(6);
case PERIOD_M15:
return(8);
case PERIOD_M30:
return(13);
case PERIOD_H1:
return(17);
case PERIOD_H4:
return(40);
case PERIOD_D1:
return(70);
case PERIOD_W1:
return(150);
case PERIOD_MN1:
return(300);
}
return(5);
}
Comments