EMA_Cross-over_Alerts_v1

Download script
EMA_Cross-over_Alerts_v1

This script is designed as a specialized trading tool for MetaTrader 4 that automates the identification of "crossovers" between two different moving averages (EMAs). Instead of requiring you to manually squint at overlapping lines on a busy chart, this tool acts as an intelligent observer that flags specific moments when price trends shift.

Core Logic and Functionality

1. Trend Identification via Crossovers The script monitors two distinct moving averages: a "Faster" one (which reacts quickly to price changes) and a "Slower" one (which represents the broader trend). It looks for the exact point where these two lines intersect:

  • Bullish Signal: When the Faster EMA moves from below the Slower EMA to above it, the script identifies a potential upward move.
  • Bearish Signal: When the Faster EMA moves from above the Slower EMA to below it, the script identifies a potential downward move.

2. Visual Signaling To make these signals easy to see—especially when you have zoomed out or have many other indicators on your screen—the script places visual markers (arrows) directly on the candles at the moment of the crossover. It automatically calculates the "range" of the recent price action to ensure the arrows are placed at an appropriate distance from the bars, keeping the chart clean and readable.

3. Real-Time Notifications Beyond just drawing arrows, the script is designed to proactively notify you of trend shifts. You can configure it to:

  • Trigger a pop-up alert window on your platform.
  • Play an audio sound file to grab your attention.
  • Send an email notification (optional).

4. Intelligent Filtering The logic includes "checks" to ensure you aren't overwhelmed with notifications. It is designed to track the current trend direction; once a crossover happens and an alert is triggered, it recognizes that trend as active. It helps prevent repetitive alerts on the same signal, ensuring that only meaningful transitions between upward and downward momentum are highlighted.

5. Customizability The tool is highly flexible, allowing you to define:

  • The specific periods for both the Fast and Slow moving averages.
  • The type of moving average calculation (e.g., Simple, Exponential, Smoothed, or Linear Weighted).
  • The price data used for the calculation (such as Open, High, Low, or Close prices).
  • The visual style (color, size, and symbol) of the alert arrows.
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screenIt plays sound alertsIt sends emails
11 Views
0 Downloads
0 Favorites
EMA_Cross-over_Alerts_v1
//+------------------------------------------------------------------+
//|                                     EMA_Cross-over_Alerts_v1.mq4 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                         EMA-Crossover_Signal.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+

/*
  +------------------------------------------------------------------+
  | Allows you to enter two ema periods and it will then show you at |
  | Which point they crossed over. It is more usful on the shorter   |
  | periods that get obscured by the bars / candlesticks and when    |
  | the zoom level is out. Also allows you then to remove the emas   |
  | from the chart.              
  +------------------------------------------------------------------+
*/   
//MODE_SMA 0 Simple moving average, 
//MODE_EMA 1 Exponential moving average, 
//MODE_SMMA 2 Smoothed moving average, 
//MODE_LWMA 3 Linear weighted moving average. 

//PRICE_CLOSE 0 Close price. 
//PRICE_OPEN 1 Open price. 
//PRICE_HIGH 2 High price. 
//PRICE_LOW 3 Low price. 
//PRICE_MEDIAN 4 Median price, (high+low)/2. 
//PRICE_TYPICAL 5 Typical price, (high+low+close)/3. 
//PRICE_WEIGHTED 6 Weighted close price, (high+low+close+close)/4. 

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Yellow
#property indicator_color2 Red

double CrossUp[];
double CrossDown[];
int CurrentTrend = 0;
int starttime = 0;
extern int FasterEMA = 5;
extern int FasterMA_MODE = 3;
extern int FasterMA_PRICE = 0;
extern int SlowerEMA = 13;
extern int SlowerMA_MODE = 3;
extern int SlowerMA_PRICE = 0;
extern int Arrow_Symbol_UP = 108;
extern int Arrow_Symbol_DN = 108;
extern int Arrow_Size      = 2;
extern int Show_Alert = 1;
extern int Play_Sound = 1;
extern int Send_Mail = 0;
extern string SoundFilename = "alert.wav";


int init()
  {
   SetIndexStyle(0, DRAW_ARROW,0,Arrow_Size);
   SetIndexArrow(0, Arrow_Symbol_UP);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW,0,Arrow_Size);
   SetIndexArrow(1, Arrow_Symbol_DN);
   SetIndexBuffer(1, CrossDown);
   starttime = TimeLocal();
   SetIndexLabel(0,"UP Signal");
   SetIndexLabel(1,"DN Signal");
   IndicatorShortName("EMA X Alerts");
   
   return(0);
  }


int deinit()
  {
   return(0);
  }


int start() {
   int limit, i, counter, loop;
   double fasterEMAnow, slowerEMAnow, fasterEMAprevious, slowerEMAprevious, fasterEMAafter, slowerEMAafter;
   double Range, AvgRange;
   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;

   loop = 0;
   for(i = 0; i <= limit; i++) {
      counter=i;
      Range=0;
      AvgRange=0;
      for (counter=i ;counter<=i+9;counter++)
      {
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10;
       
      fasterEMAnow = iMA(NULL, 0, FasterEMA, 0, FasterMA_MODE, FasterMA_PRICE, i);
      fasterEMAprevious = iMA(NULL, 0, FasterEMA, 0, FasterMA_MODE, FasterMA_PRICE, i+1);
      fasterEMAafter = iMA(NULL, 0, FasterEMA, 0, FasterMA_MODE, FasterMA_PRICE, i-1);

      slowerEMAnow = iMA(NULL, 0, SlowerEMA, 0, SlowerMA_MODE, SlowerMA_PRICE, i);
      slowerEMAprevious = iMA(NULL, 0, SlowerEMA, 0, SlowerMA_MODE, SlowerMA_PRICE, i+1);
      slowerEMAafter = iMA(NULL, 0, SlowerEMA, 0, SlowerMA_MODE, SlowerMA_PRICE, i-1);
      
      if ((fasterEMAnow > slowerEMAnow) && (fasterEMAprevious < slowerEMAprevious) && (fasterEMAafter > slowerEMAafter)) {
         CrossUp[i] = Low[i] - Range*0.5;

         if ((loop == 0) && (CurrentTrend != 1) && (TimeLocal() - starttime >= 10))  {
            if (Show_Alert == 1)  {
               Alert("Cross Up on " + Symbol() + " " + Period() + "min " + FasterEMA + "/" + SlowerEMA + " EMA");
               }
            if (Play_Sound == 1)  {
               PlaySound(SoundFilename);
               }
            if (Send_Mail == 1)  {
               SendMail("Cross Up on " + Symbol() + " " + Period() + "min " + FasterEMA + "/" + SlowerEMA + " EMA", "");
               }
               
            CurrentTrend = 1;
            }

         if (loop == 0)  {
            loop = 1;
            }
      }
      else if ((fasterEMAnow < slowerEMAnow) && (fasterEMAprevious > slowerEMAprevious) && (fasterEMAafter < slowerEMAafter)) {
         CrossDown[i] = High[i] + Range*0.5;

         if ((loop == 0) && (CurrentTrend != -1) && (TimeLocal() - starttime >= 10))  {
            if (Show_Alert == 1)  {
               Alert("Cross Down on " + Symbol() + " " + Period() + "min " + FasterEMA + "/" + SlowerEMA + " EMA");
               }
            if (Play_Sound == 1)  {
               PlaySound(SoundFilename);
               }
            if (Send_Mail == 1)  {
               SendMail("Cross Down on " + Symbol() + " " + Period() + "min " + FasterEMA + "/" + SlowerEMA + " EMA", "");
               }
               
            CurrentTrend = -1;
            }

         if (loop == 0)  {
            loop = 1;
            }
       }
   }
   return(0);
}

Comments