MA_Crossover_Email_Alert_v1
Download scriptThis script is designed as a technical analysis tool for the MetaTrader 4 platform, primarily intended to identify specific momentum shifts in financial markets. It works by tracking two different moving averages and identifying the exact moments they "cross" each other.
Core Functionality
The primary objective of this tool is to detect trend reversals or momentum changes. In trading terms, when a "fast" moving average (which reacts quickly to price changes) crosses over a "slow" moving average (which represents the broader trend), it often signals a potential change in market direction.
Key Features and Logic:
1. Versatile Moving Average Options The script allows users to customize how the averages are calculated. It supports several standard types, including:
- Simple Moving Average (SMA)
- Exponential Moving Average (EMA)
- Smoothed/Linear Weighted Averages
- Least Squares Moving Average (LSMA)
- Triangular Moving Average (TMA)
2. Customizable Price Data Instead of just looking at the closing price of a candle, the user can choose which price points the averages should base their calculations on. This includes Open, High, Low, Close, or specialized weighted averages (such as Median, Typical, or Weighted prices).
3. Visual Signals on Chart When the script detects a crossover, it places visual markers directly on your trading chart:
- Buy Signal: When the fast average crosses above the slow average, a marker appears below the candle.
- Sell Signal: When the fast average crosses below the slow average, a marker appears above the candle.
4. Real-Time Notifications The script is equipped with an alert system to notify the trader immediately when a crossover occurs. Depending on user settings, it can:
- Trigger an audible sound alert within the trading platform.
- Send an email notification containing the specific price (Ask/Bid), date, time, and symbol of the market where the signal occurred.
5. Dynamic Range Calculation To ensure the visual markers (arrows) are placed at a readable distance from the candles, the script calculates the "Range" of the recent price action. It does this by measuring the volatility over the last 10 bars, ensuring the icons aren't overlapping with the actual price bars regardless of whether you are zoomed in or out.
//+------------------------------------------------------------------+
//| MA-Crossover_Alert.mq4 |
//| Copyright © 2005, Jason Robinson (jnrtrading) |
//| http://www.jnrtading.co.uk |
//| Modified by Robert Hill to add LSMA and alert or send email |
//| Added Global LastAlert to try to have alert only on new cross |
//| but does not seem to work. So indicator does alert every bar |
//+------------------------------------------------------------------+
/*
+------------------------------------------------------------------+
| Allows you to enter two ma 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 mas |
| from the chart. (emas are initially set at 5 and 6) |
+------------------------------------------------------------------+
*/
#property copyright "Copyright © 2005, Jason Robinson (jnrtrading)"
#property link "http://www.jnrtrading.co.uk"
#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 SoundON=true;
extern bool EmailON=false;
extern int FastMA_Mode = 1; //0=sma, 1=ema, 2=smma, 3=lwma, 4=lsma
extern int FastMA_Period = 5;
extern int FastPriceMode = 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 SlowMA_Mode = 1; //0=sma, 1=ema, 2=smma, 3=lwma, 4=lsma
extern int SlowMA_Period = 6;
extern int SlowPriceMode = 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;
//int weights[]; // Array uses by TMA
//double divisor; // Used by TMA
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
// IndicatorBuffers(3);
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);
}
/*//====================================================================
// TMA coded as function from indicator code to save time on backtest
//====================================================================
double TMA(int Periods,int AppliedPrice,int shift)
{
int i,j;
double dPeriods = Periods;
int to=MathCeil(dPeriods/2.0);
//Calculate weigths
ArrayResize(weights,Periods);
for(i=0; i<to; i++)
{
weights[i] = i+1;
weights[Periods-1-i] = i+1;
}
divisor = 0.0;
for(j=0; j<Periods; j++)
divisor += weights[j];
double tma_val = 0.0;
for(j=0; j<Periods; j++)
{
double price = getPrice(AppliedPrice, Periods-j);
tma_val += price*weights[j];
}
return( tma_val/divisor);
}
//===================
// Used by TMA
//===================
double getPrice(int priceType, int index)
{
double price = 0.0;
switch(priceType)
{
case PRICE_OPEN : price = Open[index];
break;
case PRICE_HIGH : price = High[index];
break;
case PRICE_LOW : price = Low[index];
break;
case PRICE_MEDIAN : price = (High[index]+Low[index])/2.0;
break;
case PRICE_TYPICAL : price = (High[index]+Low[index]+Close[index])/3.0;
break;
case PRICE_WEIGHTED: price = (High[index]+Low[index]+2*Close[index])/4.0;
break;
case PRICE_CLOSE :
default : price = Close[index];
break;
}
return(price);
}
*/
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {
int limit, i, counter;
double tmp=0;
double fastMAnow, slowMAnow, fastMAprevious, slowMAprevious;
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;
switch(FastMA_Mode)
{
case 4 :
{
fastMAnow = LSMA(FastMA_Period, FastPriceMode, i);
fastMAprevious = LSMA(FastMA_Period, FastPriceMode, i+1);
break;
}
case 5 :
{
fastMAnow = iCustom(Symbol(),0,"TMA",FastMA_Period,0,i);
fastMAprevious = iCustom(Symbol(),0,"TMA",FastMA_Period,0,i+1);
// fastMAnow = TMA(FastMA_Period, FastPriceMode, i);
// fastMAprevious = TMA(FastMA_Period, FastPriceMode, i+1);
break;
}
default :
{
fastMAnow = iMA(NULL, 0, FastMA_Period, 0, FastMA_Mode, FastPriceMode, i);
fastMAprevious = iMA(NULL, 0, FastMA_Period, 0, FastMA_Mode, FastPriceMode, i+1);
}
}
switch(SlowMA_Mode)
{
case 4 :
{
slowMAnow = LSMA( SlowMA_Period, SlowPriceMode, i);
slowMAprevious = LSMA( SlowMA_Period, SlowPriceMode, i+1);
}
case 5 :
{
slowMAnow = iCustom(Symbol(),0,"TMA",SlowMA_Period,0,i);
slowMAprevious = iCustom(Symbol(),0,"TMA",SlowMA_Period,0,i+1);
// slowMAnow = TMA(SlowMA_Period, SlowPriceMode, i);
// slowMAprevious = TMA(SlowMA_Period, SlowPriceMode, i+1);
break;
}
default :
{
slowMAnow = iMA(NULL, 0, SlowMA_Period, 0, SlowMA_Mode, SlowPriceMode, i);
slowMAprevious = iMA(NULL, 0, SlowMA_Period, 0, SlowMA_Mode, SlowPriceMode, i+1);
}
}
if ((fastMAnow > slowMAnow) && (fastMAprevious < slowMAprevious))
{
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());
}
CrossUp[i] = Low[i] - Range*0.75;
}
else if ((fastMAnow < slowMAnow) && (fastMAprevious > slowMAprevious))
{
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());
}
CrossDown[i] = High[i] + Range*0.75;
}
}
return(0);
}
Comments