Alert SMA-EMA CrossOver1
Download scriptThis script is designed as a technical analysis tool for the MetaTrader platform to help traders identify potential trend shifts in the financial markets. It functions by tracking two different types of moving averages—the Simple Moving Average (SMA) and the Exponential Moving Average (EMA)—and monitoring how they interact with one another in real-time.
Core Logic Overview
The primary goal of this tool is to detect "crossovers." In trading, a crossover occurs when one moving average line passes through another, often signaling a change in momentum or the start of a new price trend.
- Trend Monitoring: The script calculates two lines on your chart: a "slower" SMA and a "faster" EMA. By comparing these two, the tool can distinguish between bullish (upward) and bearish (downward) movements.
- Visual Signaling: When the script detects a specific crossover event, it automatically places visual markers (arrows) on the price chart. This allows you to see exactly where the logic triggered a signal without having to manually scan the entire history of the candles.
How the Signals Work
The script identifies two specific types of trading opportunities:
- Buy Signals: The tool looks for a moment where the fast EMA moves from below the SMA to above it. When this happens, it interprets the momentum as positive and triggers a "Buy" alert. It places an upward-pointing arrow on the chart.
- Sell Signals: Conversely, the tool monitors for the fast EMA to drop from above the SMA to below it. This is interpreted as negative momentum, triggering a "Sell" alert and placing a downward-pointing arrow on the chart.
Automated Notifications
Beyond just drawing arrows on your screen, the script is built to keep you informed proactively. When a crossover occurs, the tool performs three actions simultaneously:
- On-Screen Alert: A pop-up message appears within the trading platform to notify you of the trend change immediately.
- Audio Feedback: It plays an alert sound to ensure you don't miss a signal if you are looking away from your monitor.
- Email Notifications: If enabled in the settings, the script will automatically send an email to your registered account. This email contains specific details, including the type of trend (Buy or Sell), the current moving average value, and the price at which the signal occurred.
Customizable Parameters
The tool is designed to be flexible, allowing users to adjust several key variables to match their specific trading style:
- Moving Average Periods: You can change how "fast" or "slow" the averages react by adjusting the time periods for both the SMA and EMA.
- Timeframes: The logic is optimized to handle various chart intervals (from short-term minutes to long-term daily charts).
- Alert Settings: Users can toggle the email notification feature on or off depending on their preference for communication.
//+------------------------------------------------------------------+
//| Alert SMA-EMA CrossOver.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Aqua
#property indicator_color3 DodgerBlue
#property indicator_color4 Magenta
//---- Variables Externes
extern int SMAPeriod = 13;
extern int EMAPeriod = 6;
extern int TimeFrame = 240;
extern bool Email = True;
//---- Indicateurs
double SMACurrent, SMAPrevious, EMACurrent, EMAPrevious;
int nShift, digit, digits;
int i,j,limit,counted_bars;
//---- Buffers
double ExtMapBuffer1[]; //SMA
double ExtMapBuffer2[]; //EMA
double ExtMapBuffer3[]; //Fleche Haut
double ExtMapBuffer4[]; //Fleche Bas
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//---- Styles et couleur des Lignes
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtMapBuffer2);
//---- Styles et couleur des Fleches
SetIndexStyle(2, DRAW_ARROW, 0, 1); // Fleche vers le haut
SetIndexArrow(2, 233);
SetIndexBuffer(2, ExtMapBuffer3);
SetIndexStyle(3, DRAW_ARROW, 0, 1); // Fleche vers le bas
SetIndexArrow(3, 234);
SetIndexBuffer(3, ExtMapBuffer4);
//----
switch(Period())
{
case 1: nShift = 1; break;
case 5: nShift = 3; break;
case 15: nShift = 5; break;
case 30: nShift = 10; break;
case 60: nShift = 15; break;
case 240: nShift = 20; break;
case 1440: nShift = 80; break;
case 10080: nShift = 100; break;
case 43200: nShift = 200; break;
}
//----
digits = MarketInfo(Symbol(),MODE_DIGITS);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
counted_bars=IndicatorCounted();
//----
if(counted_bars<0)
return(-1);
if(counted_bars>0)
counted_bars--;
limit=Bars-counted_bars;
for(i=0; i<limit; i++)
{
SMACurrent=iMA(NULL,TimeFrame,SMAPeriod,0,MODE_SMA,PRICE_CLOSE,i);
SMAPrevious=iMA(NULL,TimeFrame,SMAPeriod,0,MODE_SMA,PRICE_CLOSE,i+1);
ExtMapBuffer1[i]=SMACurrent;
EMACurrent=iMA(NULL,TimeFrame,EMAPeriod,0,MODE_EMA,PRICE_CLOSE,i);
EMAPrevious=iMA(NULL,TimeFrame,EMAPeriod,0,MODE_EMA,PRICE_CLOSE,i+1);
ExtMapBuffer2[i]=EMACurrent;
//---- Dessin des fleches, future entré en trade
//---- Buy
if((EMACurrent > SMACurrent+ digit*Point) && ( EMAPrevious <= SMACurrent)) // Croisement WMA8 up bord supérieur du tunnel
{
ExtMapBuffer3[i] = Low[i] - nShift*Point;
Alert("SMA-EMA CrossOver Going for a BUY Trend Sesion ",SMACurrent," Price ",Close[1]," for ", Symbol(),"-",Period());
PlaySound("alert.wav");
if (Email)
{
SendMail("SMA-EMA CrossOver", "SMA-EMA CrossOver Going for a BUY Trend Sesion "+DoubleToStr(SMACurrent, digits)+" Price "+DoubleToStr(Close[1], digits)+" for "+Symbol()+"-"+Period());
}
}
//---- Sell
if((SMACurrent > EMACurrent+ digit*Point) && ( EMAPrevious >= SMACurrent)) // Croisement WMA8 down bord inférieur du tunnel
{
ExtMapBuffer4[i] = High[i] + nShift*Point;
Alert("SMA-EMA CrossOver Going for a SELL Trend Sesion ",SMACurrent," Price ",Close[1]," for ", Symbol(),"-",Period());
PlaySound("alert.wav");
if (Email)
{
SendMail("SMA-EMA CrossOver", "SMA-EMA CrossOver Going for a SELL Trend Sesion "+DoubleToStr(SMACurrent, digits)+" Price "+DoubleToStr(Close[1], digits)+" for "+Symbol()+"-"+Period());
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments