Alert_3MA_Cross_nc

Download script
Author:
Alert_3MA_Cross_nc

Technical Logic Overview: Multi-Moving Average Crossover System

This script is designed as a visual and notification tool for traders who use Moving Averages (MA) to identify trends and momentum in the financial markets. Its primary function is to automatically detect when different moving averages intersect, signaling potential shifts in market direction.

Core Functionality

The tool monitors three distinct moving averages simultaneously: a Fast, a Medium, and a Slow average. By tracking these three layers, the script helps identify "crosses"—the moment a shorter-term trend breaks through a longer-term trend, which often indicates a change in market sentiment.

How the Logic Works

The system follows a specific sequence of logic to determine when to trigger an alert or place a visual marker:

1. Calculation Parameters The script allows for high customization regarding how these averages are calculated. You can choose the type of average (such as Simple, Exponential, Smoothed, or Linear Weighted), which average price data point to use (Close, Open, High, Low, etc.), and the specific timeframe (period) for each of the three lines.

2. Crossover Detection The engine constantly compares the mathematical distance between the moving averages from one candle to the next. It specifically looks for:

  • Fast vs. Medium: The primary crossover logic.
  • Fast vs. Slow: An optional secondary check.
  • Medium vs. Slow: A tertiary check used to confirm broader trend shifts.

3. Visual Signaling When a crossover occurs, the script places a visual marker (an arrow) on your chart:

  • Upward Crosses: Signals when a faster average moves above a slower one (bullish momentum).
  • Downward Crosses: Signals when a faster average moves below a slower one (bearish momentum). The position of these arrows is dynamically adjusted based on the current market volatility to ensure they remain visible and do not clutter the price action.

4. Notification System Beyond visual cues, the script includes an automated alert system. When a valid crossover is detected, it can communicate with the user in three ways:

  • On-Screen Alerts: A pop-up notification on your trading platform.
  • Sound Notifications: An audible beep to grab your attention immediately.
  • Email Notifications: Sending a summary of the alert directly to your email inbox.

Summary for the User

Essentially, this tool acts as an automated "scout." Instead of you having to watch three different lines on a chart constantly to see when they twist and cross each other, the script does the math in the background. It alerts you the moment the trend shifts, allowing you to react more quickly to market changes.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screenIt sends emailsIt plays sound alerts
14 Views
0 Downloads
0 Favorites
Alert_3MA_Cross_nc
//+--------------------------------------------------------------------------+
//| 2008forextsd mladen                    MA_Cross_3MACross_AlertWarningSig |
//+--------------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 LawnGreen
#property indicator_color2 Magenta
#property indicator_color3 Yellow
#property indicator_color4 Gold
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 1
#property indicator_width4 1


#import

//
//
//
//
//
extern string   MA_Mode = "SMA0 EMA1 SMMA2 LWMA3";
extern string	Price1 = "0-Close 1-Open 2-High 3-Low";
extern string 	Price2 = "4-Median/2 5-Typical/3 6-Weighted/4";
extern int FasterMA        = 6;
extern int FasterMode      = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma
extern int FasterPrice     = 6; // 0 = Close, 1 = Open, 2= High, 3 = Low, 4 = Median(high+low)/2, 5 = Typical (high+low+close)/3, 6 = Weighted close price, (high+low+close+close)/4
extern int FasterShift     = 0;
extern int MediumMA        = 200; 
extern int MediumMode      = 0; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma
extern int MediumPrice     = 6; // 0 = Close, 1 = Open, 2= High, 3 = Low, 4 = Median(high+low)/2, 5 = Typical (high+low+close)/3, 6 = Weighted close price, (high+low+close+close)/4
extern int MediumShift     = 0;
extern int SlowerMA        = 25;
extern int SlowerMode      = 0; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma
extern int SlowerPrice     = 1; // 0 = Close, 1 = Open, 2= High, 3 = Low, 4 = Median(high+low)/2, 5 = Typical (high+low+close)/3, 6 = Weighted close price, (high+low+close+close)/4
extern int SlowerShift     = 0;
extern bool crossesOnCurrent = false;
extern bool ShowCrossOnSlow  = false;
extern bool alertsOn         = true;
extern bool alertsMessage    = false;
extern bool alertsSound      = false;
extern bool alertsEmail      = false;
extern double Offset		= 2.0;




//
//
//
//
//

double CrossUp[];
double CrossDown[];
double CrossWUp[];
double CrossWDown[];


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW); SetIndexArrow(0, 233);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW); SetIndexArrow(1, 234);
   SetIndexBuffer(2, CrossWUp);   SetIndexStyle(2, DRAW_ARROW); SetIndexArrow(2, 233);
   SetIndexBuffer(3, CrossWDown); SetIndexStyle(3, DRAW_ARROW); SetIndexArrow(3, 234);
   
      
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{ 
   int    limit, i; 
   int    counted_bars=IndicatorCounted();
   double fasterMAnow, fasterMAprevious, fasterMAafter;
   double mediumMAnow, mediumMAprevious, mediumMAafter;
   double slowerMAnow, slowerMAprevious, slowerMAafter;
   

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;

   //
   //
   //
   //
   //
   //
   
   for(i = limit; i >=0; i--)
   {      
      double Range = 0.0;
      for (int counter=i ;counter<=i+9;counter++) Range += MathAbs(High[counter]-Low[counter]);
                                              Range /= 10;

      //
      //
      //
      //
      //
      
         fasterMAnow      = iMA(NULL, 0, FasterMA, FasterShift, FasterMode, FasterPrice, i);
         fasterMAprevious = iMA(NULL, 0, FasterMA, FasterShift, FasterMode, FasterPrice, i+1);
         mediumMAnow      = iMA(NULL, 0, MediumMA, MediumShift, MediumMode, MediumPrice, i);
         mediumMAprevious = iMA(NULL, 0, MediumMA, MediumShift, MediumMode, MediumPrice, i+1);
         slowerMAnow      = iMA(NULL, 0, SlowerMA, SlowerShift, SlowerMode, SlowerPrice, i);
         slowerMAprevious = iMA(NULL, 0, SlowerMA, SlowerShift, SlowerMode, SlowerPrice, i+1);

      //iMA(
      //
      //    avoid current bar if not allowed to check crosses on current (i==0)
      //
      //

         if (crossesOnCurrent==false && i==0) continue;
      
      //
      //
      //
      //
      //
      
      int    crossID=0;
      double curr;
      double prev;
      double point;
      while (true)
         {
            curr  =  fasterMAnow      - mediumMAnow;
            prev  =  fasterMAprevious - mediumMAprevious;
            point = (fasterMAnow      + fasterMAprevious)/2;
                  if (curr*prev<=0) { crossID=1; break; }
            if (ShowCrossOnSlow)
            {      
               curr  =  fasterMAnow      - slowerMAnow;
               prev  =  fasterMAprevious - slowerMAprevious;
                     if (curr*prev<=0) { crossID=2; break; }
               curr  =  mediumMAnow      - slowerMAnow;
               prev  =  mediumMAprevious - slowerMAprevious;
               point = (mediumMAnow      + mediumMAprevious)/2;
                     if (curr*prev<=0) { crossID=3; break; }
            }         
                                                 break;
         }
      
         //
         //
         //    the interesting thing is the direction of the crossing
         //    which MA is the primary and which one the secondary
         //    if you do not know that you can not determine the "direction"
         //    of the cross
         //
         //
         //
         
         CrossUp[i]    = EMPTY_VALUE;
         CrossDown[i]  = EMPTY_VALUE;
         CrossWUp[i]   = EMPTY_VALUE;
         CrossWDown[i] = EMPTY_VALUE;
         if (crossID>0)
         {
            if (alertsOn)
            if ((i==0 && crossesOnCurrent==true) || (i==1 && crossesOnCurrent==false))
            {
               switch (crossID)
               {
                  case 1: 
                        if(curr>0) 
                        {
                        	doAlert(" 3MACross: Fast MA crossed Medium MA UP");
                        }	
                        else       
                        {
                        	doAlert(" 3MACross: Fast MA crossed Medium MA DOWN"); 
                        }                        
                        break;
                  case 2: 
                        if(curr>0) 
                        {
                        	doAlert(" 3MACross: Fast MA crossed Slow MA UP");
                        }	
                        else
                        {
                        	doAlert(" 3MACross: Fast MA crossed Slow MA DOWN"); 
                        }	
                        break;
                  case 3: 
                        if(curr>0) 
                        {
                        	doAlert(" 3MACross: Medium MA crossed Slow MA UP");
                        }	
                        else       
                        {
                        	doAlert(" 3MACross: Medium MA crossed Slow MA DOWN"); 
                        }	
                        break;
               }
            }               

            if (i==0)
            {
               if (curr>0)
                     CrossWUp[i]   = point - Range*Offset;
               else  CrossWDown[i] = point + Range*Offset;
            }               
            else
            {               
               if (curr>0)
                     CrossUp[i]   = point - Range*Offset;
               else  CrossDown[i] = point + Range*Offset;
            }               
         }               
   }   
   return(0);
 }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//


 
void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //
//        if time needed :
//        message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," @",Bid," ", doWhat);
//
  
          message =  StringConcatenate(Symbol()," at ",Bid," ", doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," 3MACross:"," M",Period()),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}


Comments