Five_MA_Crossover_Email_Alert_v1
Download scriptOverview of the Trading Tool Logic
This script is designed to assist traders by identifying specific momentum shifts in the market. It achieves this by monitoring five different Moving Averages (MAs) simultaneously and alerting the user when the price moves into a position of clear dominance relative to all five indicators.
Core Functionality
The tool calculates five independent moving averages based on user-defined periods (such as 5, 8, 14, 21, and 34). Because different traders use different "speeds" of averages, this script allows for complete customization regarding:
- Calculation Type: You can choose between various methods like Simple, Exponential, Smoothed, Linear Weighted, or Linear Scaled Moving Averages.
- Price Source: You can decide which price data the averages should track (e.g., just the closing price, the highest/lowest points of a candle, or various weighted averages of those values).
How the Signals are Generated
The script looks for "confluence"—a situation where multiple indicators agree on a trend. It evaluates the current market price against all five moving averages at once:
- Buy Signal: When the current price closes above all five moving averages simultaneously, the tool identifies this as bullish momentum.
- Sell Signal: When the current price closes below all five moving averages simultaneously, the tool identifies this as bearish momentum.
Visual and Audible Notifications
To make the data actionable for the user, the script provides three layers of feedback:
- Visual Arrows: The script places physical arrows on the chart (Green for upward moves, Red for downward moves) to highlight exactly where these crossovers occurred. It automatically adjusts the distance of these arrows based on the recent volatility of the market so they remain clearly visible.
- Sound Alerts: If enabled, the tool will trigger an audible "pop" or sound through your trading platform the moment a new signal is confirmed.
- Email Notifications: For traders who are away from their desks, the script can be configured to send an automated email containing the specific price, time, and symbol details of the signal.
Smart Logic Features
To prevent the tool from "spamming" the user with constant notifications during a long trend, it includes a state-tracking logic. It ensures that once a Buy or Sell signal is triggered, it will not alert you again until the trend has flipped in the opposite direction. This helps filter out noise and keeps the trader focused on significant market shifts.
//+------------------------------------------------------------------+
//| 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