MA_Cross-over__Alerts

Download script
MA_Cross-over__Alerts

This tool is designed to simplify the way traders identify momentum shifts in the market by pinpointing the exact moment two Moving Averages (EMAs) intersect. While many traders look at moving averages to determine the direction of a trend, these lines can sometimes become cluttered or difficult to read clearly on a zoomed-out chart. This script solves that problem by placing clear visual markers—arrows—on the chart exactly where these crossovers occur.

Core Functionality

The logic operates by tracking two specific types of Exponential Moving Averages: a "Fast" average and a "Slow" average. The script constantly monitors the relationship between these two lines to detect three specific states:

  • Bullish Crossover (Upward): When the Fast EMA climbs above the Slow EMA, the script identifies this as a potential upward momentum shift. It places an arrow below the price candle and triggers a notification.
  • Bearish Crossover (Downward): When the Fast EMA drops below the Slow EMA, the script recognizes a potential downward shift. It places an arrow above the price candle and triggers a notification.
  • Visual Precision: To ensure the arrows are easy to see regardless of your chart's scale, the script calculates the average height of recent price bars. It then uses this information to position the arrows at a consistent distance from the candles, preventing them from overlapping with the price action.

Key Features for Traders

  • Automated Notifications: To help traders who cannot watch their screens 24/7, the script can be configured to provide immediate feedback when a crossover happens. You can choose to receive:
    • Pop-up Alerts: On-screen notifications with details on the currency pair and timeframe.
    • Audio Signals: A specific sound file plays to grab your attention.
    • Email Notifications: Direct messages sent to your inbox (depending on your platform setup).
  • Smart Filtering: The script includes logic to prevent "alert fatigue." It is designed to identify the primary trend change and ensure that notifications are spaced out correctly, avoiding repeated alerts for the same continuous move.
  • Customizable Parameters: Users can adjust the periods of the moving averages (e.g., a 5-period vs. a 13-period) to match different trading styles, whether you prefer scalping quick moves or identifying long-term trends.

In essence, this script acts as an automated scout that watches the price action for you, highlighting the exact moment market sentiment flips and alerting you instantly so you can make informed decisions without having to manually squint at moving average lines.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screenIt plays sound alertsIt sends emails
13 Views
0 Downloads
0 Favorites
MA_Cross-over__Alerts
//+------------------------------------------------------------------+
//|                                         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.              
  +------------------------------------------------------------------+
*/   

#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 FasterMAperiod = 5;
extern int FasterMAtype =   3;
extern int SlowerMAperiod = 13;
extern int SlowerMAtype  =   3;
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, EMPTY,2);
   SetIndexArrow(0, 108);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY,2);
   SetIndexArrow(1, 108);
   SetIndexBuffer(1, CrossDown);
   starttime = TimeLocal();
   
   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, FasterMAperiod, 0, FasterMAtype, PRICE_CLOSE, i);
      fasterEMAprevious = iMA(NULL, 0, FasterMAperiod, 0, FasterMAtype, PRICE_CLOSE, i+1);
      fasterEMAafter = iMA(NULL, 0, FasterMAperiod, 0, FasterMAtype, PRICE_CLOSE, i-1);

      slowerEMAnow = iMA(NULL, 0, SlowerMAperiod, 0, SlowerMAtype, PRICE_CLOSE, i);
      slowerEMAprevious = iMA(NULL, 0, SlowerMAperiod, 0, SlowerMAtype, PRICE_CLOSE, i+1);
      slowerEMAafter = iMA(NULL, 0, SlowerMAperiod, 0, SlowerMAtype, PRICE_CLOSE, 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 " + FasterMAperiod + "/" + SlowerMAperiod + " EMA");
               }
            if (Play_Sound == 1)  {
               PlaySound(SoundFilename);
               }
            if (Send_Mail == 1)  {
               SendMail("Cross Up on " + Symbol() + " " + Period() + "min " + FasterMAperiod + "/" + SlowerMAperiod + " 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 " + FasterMAperiod + "/" + SlowerMAperiod + " EMA");
               }
            if (Play_Sound == 1)  {
               PlaySound(SoundFilename);
               }
            if (Send_Mail == 1)  {
               SendMail("Cross Down on " + Symbol() + " " + Period() + "min " + FasterMAperiod + "/" + SlowerMAperiod + " EMA", "");
               }
               
            CurrentTrend = -1;
            }

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

Comments