Five_MA_Crossover_Email_Alert

Download script
Author: Copyright � 2006, Robert Hill)
Five_MA_Crossover_Email_Alert

Overview

This script is a specialized technical analysis tool designed for the MetaTrader 4 platform. Its primary purpose is to identify specific trend directions by analyzing how the current market price interacts with five different Moving Averages (MA) simultaneously. Instead of relying on a single indicator, this tool looks for "confluence"—a situation where multiple mathematical averages agree on the market's direction.

Core Functionality

The script monitors five independent Moving Averages. Users can customize each of these five lines by choosing:

  • Calculation Type: Different mathematical methods (such as Simple, Exponential, Smoothed, Linear Weighted, or LSMA).
  • Time Period: How far back the average should look (e.g., short-term vs. long-term trends).
  • Price Source: Which price point to use for the calculation (such as the Open, High, Low, Close, or various weighted averages of these values).

Logic and Signals

The script continuously scans the chart to determine if the current price is trending strongly above or below all five moving averages at once.

1. Buy Signals (Bullish Trend) If the current closing price is higher than all five moving averages simultaneously, the script identifies this as a bullish trend.

  • Visual Aid: It places a green arrow below the candle to highlight the occurrence.
  • Notifications: If triggered on a fresh candle, the system can provide an immediate audible alert and/or send an email notification containing the current price, time, and currency symbol.

2. Sell Signals (Bearish Trend) If the current closing price is lower than all five moving averages simultaneously, the script identifies this as a bearish trend.

  • Visual Aid: It places a red arrow above the candle to highlight the occurrence.
  • Notifications: Similar to the buy signal, it can trigger an audible alert or email notification with the relevant trade data.

Smart Filtering Features

To ensure the signals are clean and readable, the script includes several logic-based refinements:

  • Dynamic Spacing: The arrows placed on the chart are not fixed in size; they calculate a "range" based on recent volatility to ensure they appear at an appropriate distance from the price candles.
  • Signal Suppression: The script uses internal "flags" to prevent repeated alerts. It ensures that you are only notified of a new trend change rather than being bombarded with notifications on every single candle while the price stays in the same zone.
  • Visual Clarity: While the tool calculates all five averages to determine the signal, it is designed to allow users to remove the moving average lines from the visual chart if they prefer a cleaner workspace, keeping only the informative arrows visible.
Price Data Components
Series array that contains open time of each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screenIt sends emails
17 Views
0 Downloads
0 Favorites
Five_MA_Crossover_Email_Alert
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                      Five_MA-Crossover_Alert.mq4 |
//|                                    Copyright © 2006, Robert Hill |
//| Modified by Robert Hill to add LSMA and alert or send email      |
//+------------------------------------------------------------------+

/*
  +------------------------------------------------------------------+
  | Allows you to enter five ma periods and it will then show you at |
  | which point price crossed over. It is more useful 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  mas   |
  | from the chart.                                                  |
  +------------------------------------------------------------------+
*/   
#property copyright "Copyright © 2006, Robert Hill)"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LawnGreen
#property indicator_width1 2
#property indicator_color2 Red
#property indicator_width2 2

extern bool DisplayArrows = true;
extern bool SoundON=true;
extern bool EmailON=false;

extern int MA_Mode1 = 1; //0=sma, 1=ema, 2=smma, 3=lwma, 4=lsma
extern int MA_Period1 =   5;
extern int PriceMode1 = 0;//0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4
extern int MA_Mode2 = 1; //0=sma, 1=ema, 2=smma, 3=lwma, 4=lsma
extern int MA_Period2 =   8;
extern int PriceMode2 = 0;//0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4
extern int MA_Mode3 = 1; //0=sma, 1=ema, 2=smma, 3=lwma, 4=lsma
extern int MA_Period3 =   14;
extern int PriceMode3 = 0;//0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4
extern int MA_Mode4 = 1; //0=sma, 1=ema, 2=smma, 3=lwma, 4=lsma
extern int MA_Period4 =   21;
extern int PriceMode4 = 0;//0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4
extern int MA_Mode5 = 1; //0=sma, 1=ema, 2=smma, 3=lwma, 4=lsma
extern int MA_Period5 =   34;
extern int PriceMode5 = 0;//0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4
double CrossUp[];
double CrossDown[];
int flagval1 = 0;
int flagval2 = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexArrow(1, 234);
   SetIndexBuffer(1, CrossDown);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }

//+------------------------------------------------------------------+
//| LSMA with PriceMode                                              |
//| PrMode  0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2,    |
//| 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4  |
//+------------------------------------------------------------------+

double LSMA(int Rperiod, int prMode, int shift)
{
   int i;
   double sum, pr;
   int length;
   double lengthvar;
   double tmp;
   double wt;

   length = Rperiod;
 
   sum = 0;
   for(i = length; i >= 1  ; i--)
   {
     lengthvar = length + 1;
     lengthvar /= 3;
     tmp = 0;
     switch (prMode)
     {
     case 0: pr = Close[length-i+shift];break;
     case 1: pr = Open[length-i+shift];break;
     case 2: pr = High[length-i+shift];break;
     case 3: pr = Low[length-i+shift];break;
     case 4: pr = (High[length-i+shift] + Low[length-i+shift])/2;break;
     case 5: pr = (High[length-i+shift] + Low[length-i+shift] + Close[length-i+shift])/3;break;
     case 6: pr = (High[length-i+shift] + Low[length-i+shift] + Close[length-i+shift] + Close[length-i+shift])/4;break;
     }
     tmp = ( i - lengthvar)*pr;
     sum+=tmp;
    }
    wt = sum*6/(length*(length+1));
    
    return(wt);
}


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit, i, counter;
   double tmp=0, price;
   double MA1, MA2, MA3, MA4, MA5;
   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;
   
   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;
       
      if (MA_Mode1 == 4)
      {
         MA1 = LSMA(MA_Period1, PriceMode1, i);
         
      }
      else
      {
         MA1 = iMA(NULL, 0, MA_Period1, 0, MA_Mode1, PriceMode1, i);
      }

      if (MA_Mode2 == 4)
      {
         MA2 = LSMA(MA_Period2, PriceMode2, i);
         
      }
      else
      {
         MA2 = iMA(NULL, 0, MA_Period2, 0, MA_Mode2, PriceMode2, i);
      }
      if (MA_Mode3 == 4)
      {
         MA3 = LSMA(MA_Period3, PriceMode3, i);
         
      }
      else
      {
         MA3 = iMA(NULL, 0, MA_Period3, 0, MA_Mode3, PriceMode3, i);
      }
      if (MA_Mode4 == 4)
      {
         MA4 = LSMA(MA_Period4, PriceMode4, i);
         
      }
      else
      {
         MA4 = iMA(NULL, 0, MA_Period4, 0, MA_Mode4, PriceMode4, i);
      }
      if (MA_Mode5 == 4)
      {
         MA5 = LSMA(MA_Period5, PriceMode5, i);
         
      }
      else
      {
         MA5 = iMA(NULL, 0, MA_Period5, 0, MA_Mode5, PriceMode5, i);
      }
      
      price = Close[i];
      if ((price > MA1) && (price > MA2) && (price > MA3) && (price > MA4) && (price > MA5))
      {
         if (DisplayArrows) CrossUp[i] = Low[i] - Range*0.75;
         if (i == 1 && flagval1==0)
         {
           flagval1=1;
           flagval2=0;
           if (SoundON) Alert("BUY signal at Ask=",Ask,"\n Bid=",Bid,"\n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
           if (EmailON) SendMail("BUY signal alert","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
         }
      }
      else if ((price < MA1) && (price < MA2) && (price < MA3) && (price < MA4) && (price < MA5))
      {
         if (DisplayArrows) CrossDown[i] = High[i] + Range*0.75;
         if (i == 1 && flagval2==0)
         {
           flagval2=1;
           flagval1=0;
           if (SoundON) Alert("SELL signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
           if (EmailON) SendMail("SELL signal alert","SELL signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
         }
      }
   }

   return(0);
}

Comments