2MA Crossover_003

Download script
Author: Copyright � 2005-07, Jason Robinson (jnrtrading)
2MA Crossover_003

This script is a specialized charting tool designed to help traders identify specific trend reversals by monitoring the interaction between two different Moving Averages (MAs). Rather than just displaying lines on a chart, it acts as an automated signaling system that flags "crossovers" with visual markers and real-time notifications.

Core Logic and Functionality

1. Dual Moving Average Tracking The tool monitors two independent Moving Averages simultaneously. You can customize several factors for each average, including:

  • Type: Choose between Simple (SMA), Exponential (EMA), Smoothed (SMMA), or Linear Weighted (LWMA) averages.
  • Period: Define how many bars of data the average should consider (e.g., a short-term 5-period average vs. a longer-term 8-period average).
  • Price Source: Select whether the average should be calculated based on the Close, Open, High, or Low prices of the candles.

2. Crossover Detection The "intelligence" of the script lies in its ability to detect the exact moment one moving average line passes through another. It looks at three specific points in time: the current bar, the previous bar, and the subsequent data point. If it detects that the first average has moved from below to above (or vice versa) the second average, it identifies this as a "crossover" event.

3. Visual Signaling To make these signals easy to see—especially when you are zoomed out or looking at dense price action—the script places distinct visual symbols on the chart:

  • Upward Crosses: When the first average crosses above the second, an arrow appears below the candle.
  • Downward Crosses: When the first average crosses below the second, an arrow appears above the candle.
  • The script automatically calculates the distance of these arrows based on the recent volatility of the market to ensure they are clearly visible without overlapping the price bars.

4. Automated Notifications Beyond just visual arrows, the tool provides active alerts so you don't have to watch the chart every second:

  • Pop-up Alerts: It can trigger an audible/visual alert on your trading platform the moment a crossover occurs.
  • Email Alerts: It has the capability to send an email notification directly to your inbox when a signal is triggered.
  • Smart Filtering: The script includes logic to ensure alerts are only sent once per new candle, preventing the system from sending repeated notifications for the same crossover event.

Summary for the User

In plain terms, this tool simplifies trend analysis. Instead of you having to manually watch two lines on a screen to see if they touch or cross, the script does the math for you. It "watches" the lines 24/7 and "pokes" you with an arrow and a notification the moment the market sentiment shifts from one direction to another.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screenIt sends emails
12 Views
0 Downloads
0 Favorites
2MA Crossover_003
//+------------------------------------------------------------------+
//|                                  2MA Crossover.mq4 modified from |
//|                                         EMA-Crossover_Signal.mq4 |
//|         Copyright © 2005-07, Jason Robinson (jnrtrading)         |
//|                   http://www.jnrtading.co.uk                     |
//+------------------------------------------------------------------+

/*
  +------------------------------------------------------------------+
  | Allows you to enter two ema 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 emas   |
  | from the chart. (emas are initially set at 5 and 6)              |
  +------------------------------------------------------------------+
*/   
#property copyright "Copyright © 2005-07, Jason Robinson (jnrtrading)"
#property link      "http://www.jnrtrading.co.uk"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_width1 3
#property indicator_color2 Magenta
#property indicator_width2 3

extern string note1 = "First Moving Average";
extern int MA1 =   5;
extern string note2 = "0=sma, 1=ema, 2=smma, 3=lwma";
extern int MA1Mode = 3; //0=sma, 1=ema, 2=smma, 3=lwma
extern string note3 = "0=close,1=open,2=high,3=low";
extern int PriceField1 = 0;
extern string note4 = "--------------------------------------------";
extern string note5 = "Second Moving Average";
extern int MA2 =   8;
extern string note6 = "0=sma, 1=ema, 2=smma, 3=lwma";
extern int MA2Mode = 3; //0=sma, 1=ema, 2=smma, 3=lwma
extern string note7 = "0=close,1=open,2=high,3=low";
extern int PriceField2 = 1;
extern string note8 = "--------------------------------------------";
extern string note9 = "Arrow Type";
extern string note10 = "use WingDings font";
extern int CrossUpSymbol=200;
extern int CrossDownSymbol=202;
extern string note12 = "--------------------------------------------";
extern string note13 = "turn on Alert = true; turn off = false";
extern bool AlertOn = true;
extern string note14 = "--------------------------------------------";
extern string note15 = "send Email Alert = true; turn off = false";
extern bool SendAnEmail=false;

double CrossUp[];
double CrossDown[];
string AlertPrefix, MA1short_name, MA2short_name;
string GetTimeFrameStr() {
   switch(Period())
   {
      case 1 : string TimeFrameStr="M1"; break;
      case 5 : TimeFrameStr="M5"; break;
      case 15 : TimeFrameStr="M15"; break;
      case 30 : TimeFrameStr="M30"; break;
      case 60 : TimeFrameStr="H1"; break;
      case 240 : TimeFrameStr="H4"; break;
      case 1440 : TimeFrameStr="D1"; break;
      case 10080 : TimeFrameStr="W1"; break;
      case 43200 : TimeFrameStr="MN1"; break;
      default : TimeFrameStr=Period();
   } 
   return (TimeFrameStr);
   }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
      SetIndexStyle(0,DRAW_ARROW);
      SetIndexArrow(0, CrossUpSymbol);
      SetIndexStyle(1,DRAW_ARROW);
      SetIndexArrow(1, CrossDownSymbol);
      SetIndexBuffer(0, CrossUp);
      SetIndexBuffer(1, CrossDown);

//---- indicator short name
   switch(MA1Mode)
     {
      case 1 : MA1short_name="EMA";  break;
      case 2 : MA1short_name="SMMA"; break;
      case 3 : MA1short_name="LWMA"; break;
      default :
         MA1Mode=0;
         MA1short_name="SMA";
     }
   switch(MA2Mode)
     {
      case 1 : MA2short_name="EMA";  break;
      case 2 : MA2short_name="SMMA"; break;
      case 3 : MA2short_name="LWMA"; break;
      default :
         MA2Mode=0;
         MA2short_name="SMA";
     }

   AlertPrefix=Symbol()+" ("+GetTimeFrameStr()+"):  ";
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
bool NewBar()
{
   static datetime lastbar;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
   {
      lastbar=curbar;
      return (true);
   }
   else
   {
      return(false);
   }
}   
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit, i, counter;
   double MA1now, MA2now, MA1previous, MA2previous, MA1after, MA2after;
   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;
       
      MA1now = iMA(NULL, 0, MA1, 0, MA1Mode, PriceField1, i);
      MA1previous = iMA(NULL, 0, MA1, 0, MA1Mode, PriceField1, i+1);
      MA1after = iMA(NULL, 0, MA1, 0, MA1Mode, PriceField1, i-1);

      MA2now = iMA(NULL, 0, MA2, 0, MA2Mode, PriceField2, i);
      MA2previous = iMA(NULL, 0, MA2, 0, MA2Mode, PriceField2, i+1);
      MA2after = iMA(NULL, 0, MA2, 0, MA2Mode, PriceField2, i-1);
      
      if ((MA1now > MA2now) && (MA1previous < MA2previous) && (MA1after > MA2after)) {
                   CrossUp[i] = Low[i] - Range*1.5;
                   if (AlertOn && NewBar()) {
                      Alert(AlertPrefix+MA1short_name+" ("+MA1+") "+"crosses UP " + MA2short_name+" ("+MA2+")");
                   }   
              	    if (SendAnEmail && NewBar()) {
                      SendMail(AlertPrefix,MA1short_name+" ("+MA1+") "+"crosses UP " + MA2short_name+" ("+MA2+")");
              	    }
      }
      else if ((MA1now < MA2now) && (MA1previous > MA2previous) && (MA1after < MA2after)) {
                   CrossDown[i] = High[i] + Range*1.5;
                   if (AlertOn && NewBar()) {
                      Alert(AlertPrefix+MA1short_name+" ("+MA1+") "+"crosses DOWN " + MA2short_name+" ("+MA2+")");
                   }   
              	    if (SendAnEmail && NewBar()) {
                      SendMail(AlertPrefix,MA1short_name+" ("+MA1+") "+"crosses DOWN " + MA2short_name+" ("+MA2+")");
              	    }
      }
   }
   return(0);
}

Comments