MA_Cross-over_Alerts_v1_1

Download script
MA_Cross-over_Alerts_v1_1

This tool is designed to simplify trend analysis by automatically identifying specific "crossover" moments between two moving averages on a trading chart. Rather than requiring you to constantly watch the lines for subtle changes, the script handles the monitoring for you and provides immediate visual and audible notifications when a trend shift occurs.

Core Functionality

The script tracks two distinct Moving Averages (MA): a Faster average and a Slower average. In technical analysis, these averages represent the momentum of the market; when the faster line crosses over the slower one, it often signifies a change in market direction.

The logic follows these specific steps:

  • Trend Detection: The script continuously calculates the position of both moving averages. It looks for the exact point where the Faster MA moves from below to above the Slower MA (a potential upward trend) or from above to below the Slower MA (a potential downward trend).
  • Visual Signals: When a crossover is confirmed, the script places an arrow directly on your chart. These arrows appear at specific distances above or below the price candles to ensure they are clearly visible without cluttering the price action.
  • Real-Time Notifications: To ensure you don't miss a movement, the script can be configured to trigger multiple types of alerts simultaneously:
    • Pop-up Alerts: A visual message on your trading platform.
    • Audio Alerts: A sound file plays to grab your attention.
    • Email Notifications: An automated email sent to your registered address.

Key Features and Customization

The script is built to be highly flexible, allowing you to tailor the logic to your specific trading style:

  • Versatile Moving Averages: You can choose between different types of averages (Simple, Exponential, Smoothed, or Linear Weighted) depending on how much "weight" you want to give to recent price data.
  • Dynamic Price Source: You can decide whether the calculations are based on the closing price, opening price, highs, lows, or a median of these values.
  • Shift Parameters: It includes "Shift" settings, which allow you to offset the calculation point, providing extra control over how the data is processed relative to the current candle.
  • Smart Filtering: The logic includes a safeguard to prevent repetitive alerts for the same trend. It tracks the "Current Trend" state so that it only notifies you once when a crossover happens, rather than alerting you on every single tick while the lines remain crossed.

Summary of Use

Essentially, this script acts as an automated lookout. It monitors the relationship between two momentum indicators and "calls out" to you the moment they intersect, allowing you to react quickly to market changes without having to manually scan the charts at all times.

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_v1_1
//+------------------------------------------------------------------+
//|                                     EMA_Cross-over_Alerts_v1.mq4 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                         EMA-Crossover_Signal.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+
//+-------------------------------------------------------------------+
//|             v1_1 edited by tcl (http://sharingforex.blogspot.com  |
//|just make it as a universal MA crossing alert with Shift parameter |
//+-------------------------------------------------------------------+
/*
  +------------------------------------------------------------------+
  | 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 string MA_MODE = "0=Simple, 1=Exponential, 2=Smoothed, 3= Linear Weighted"; //add by tcl - just for user information
extern string MA_PRICE1 = "0=Close, 1=Open, 2=High, 3=Low, 4=Median (high+low)/2"; //add by tcl - just for user information
extern string MA_PRICE2 = "5=Typical (high+low+close)/3, 6=Weighted Close (high+low+close+close)/4"; //add by tcl - just for user information
extern string note1 = "--- Faster MA Parameter ---"; //add by tcl - just for user information
//extern int FasterEMA = 5;
extern int FasterMA = 5; //edited by tcl
extern int FasterMA_MODE = 3;
extern int FasterMA_PRICE = 0;
extern int FasterMA_Shift = 0; //add by tcl

extern string note2 = "--- Faster MA Parameter ---"; //add by tcl - just for user information
//extern int SlowerEMA = 13;
extern int SlowerMA = 13; //edited by tcl
extern int SlowerMA_MODE = 3;
extern int SlowerMA_PRICE = 0;
extern int SlowerMA_Shift = 0; //add by tcl

extern string note3 = "--- Crossing Symbol Parameter ---"; //add by tcl - just for user information
//extern int Arrow_Symbol_UP = 108;
extern int Arrow_Symbol_UP = 233; //edited by tcl - make it an arrow as default crossing symbol
//extern int Arrow_Symbol_DN = 108;
extern int Arrow_Symbol_DN = 234; //edited by tcl - make it an arrow as default crossing symbol
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 fasterMAnow, slowerMAnow, fasterMAprevious, slowerMAprevious, fasterMAafter, slowerMAafter;
   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;
       
      fasterMAnow = iMA(NULL, 0, FasterMA, FasterMA_Shift, FasterMA_MODE, FasterMA_PRICE, i);
      fasterMAprevious = iMA(NULL, 0, FasterMA, FasterMA_Shift, FasterMA_MODE, FasterMA_PRICE, i+1);
      fasterMAafter = iMA(NULL, 0, FasterMA, FasterMA_Shift, FasterMA_MODE, FasterMA_PRICE, i-1);

      slowerMAnow = iMA(NULL, 0, SlowerMA, SlowerMA_Shift, SlowerMA_MODE, SlowerMA_PRICE, i);
      slowerMAprevious = iMA(NULL, 0, SlowerMA, SlowerMA_Shift, SlowerMA_MODE, SlowerMA_PRICE, i+1);
      slowerMAafter = iMA(NULL, 0, SlowerMA, SlowerMA_Shift, SlowerMA_MODE, SlowerMA_PRICE, i-1);
      
      if ((fasterMAnow > slowerMAnow) && (fasterMAprevious < slowerMAprevious) && (fasterMAafter > slowerMAafter)) {
         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 " + FasterMA + "/" + SlowerMA + " EMA");
               }
            if (Play_Sound == 1)  {
               PlaySound(SoundFilename);
               }
            if (Send_Mail == 1)  {
               SendMail("Cross Up on " + Symbol() + " " + Period() + "min " + FasterMA + "/" + SlowerMA + " EMA", "");
               }
               
            CurrentTrend = 1;
            }

         if (loop == 0)  {
            loop = 1;
            }
      }
      else if ((fasterMAnow < slowerMAnow) && (fasterMAprevious > slowerMAprevious) && (fasterMAafter < slowerMAafter)) {
         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 " + FasterMA + "/" + SlowerMA + " EMA");
               }
            if (Play_Sound == 1)  {
               PlaySound(SoundFilename);
               }
            if (Send_Mail == 1)  {
               SendMail("Cross Down on " + Symbol() + " " + Period() + "min " + FasterMA + "/" + SlowerMA + " EMA", "");
               }
               
            CurrentTrend = -1;
            }

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

Comments