EMA_CROSS_IND

Download script
Author: Codersguru
EMA_CROSS_IND

This tool is designed to help traders visualize and identify significant shifts in market momentum by tracking the interaction between two moving averages: a "Short" Exponential Moving Average (EMA) and a "Long" Exponential Moving Average (EMA).

Core Logic Overview

The primary objective of this script is to detect "crosses"—the specific moments when the fast-moving price average passes through the slower-moving price average. In technical analysis, these crossovers are often used as signals for potential changes in trend direction.

How It Works

  • Trend Tracking: The script continuously monitors two lines on your chart. The Short EMA reacts more quickly to recent price changes, while the Long EMA provides a broader view of the overall trend.
  • Crossover Detection: The logic is programmed to recognize when the position of these two lines swaps. For example, if the Short EMA moves from below the Long EMA to above it, the script identifies this as an upward movement. Conversely, moving from above to below is identified as a downward movement.
  • Visual Signaling: When a crossover occurs, the tool automatically places a visual marker (an arrow) on the chart at the exact point of the crossing. This allows you to see historical trend changes clearly without having to manually scan the price history.
  • Alert System: To ensure you don't have to watch the screen constantly, the script features an automated alert system. When a live crossover happens in real-time, it generates a notification informing you whether the move was upward or downward.

Key Features for the User

  • Customizable Sensitivity: You can adjust the lengths of the Short and Long EMAs to suit different trading styles (e.g., making them shorter for day trading or longer for long-term investing).
  • Visual Clarity: The tool distinguishes between types of crosses using different colors and arrow codes, making it easy to differentiate between "buy" and "sell" signals at a glance.
  • Automatic Cleanup: The script is designed to manage its own visual objects, ensuring that your chart remains organized and clear by handling the removal of old data when necessary.

In summary, this tool automates the observation of trend shifts, turning complex price movements into simple, actionable visual cues and real-time notifications.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
11 Views
0 Downloads
0 Favorites
EMA_CROSS_IND
//+------------------------------------------------------------------+
//|                                                EMA_CROSS_IND.mq4 |
//|                                                       Codersguru |
//|                                         http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Codersguru"
#property link      "http://www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue


extern int ShortEma = 10;
extern int LongEma = 80;

extern color UpCross = Gold;
extern color DownCross = DarkGreen; 
extern int ArrowCode1 = 233; 
extern int ArrowCode2 = 234; 

extern bool reversal = true; 
extern bool Alert_On = true;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];

void DrawArrow( int bar , double value , int trend)
   {
      
      static int cnt = 0;
      cnt++;
      string crossing = "CROSSING_" + cnt;
      string label = "LABEL" + cnt;

      ObjectCreate(crossing, OBJ_ARROW, 0, Time[bar], value);
      
      if(reversal==false)
      {
         if (trend == 1) trend = 2;
         else if (trend == 2) trend = 1;
      }
      
      if(trend == 1)
      {
         ObjectSet(crossing, OBJPROP_ARROWCODE, ArrowCode1);
         ObjectSet(crossing, OBJPROP_COLOR , UpCross);
      }
      
      if(trend == 2)
      {
         ObjectSet(crossing, OBJPROP_ARROWCODE, ArrowCode2);
         ObjectSet(crossing, OBJPROP_COLOR , DownCross);

      }

      ObjectSet(crossing, OBJPROP_WIDTH  , 1);
      ObjectsRedraw();  
   }


int Crossed (double line1 , double line2)
   {
      static int last_direction = 0;
      static int current_direction = 0;
      
      //Don't work in the first load, wait for the first cross!
      static bool first_time = true;
      if(first_time == true)
      {
         first_time = false;
         return (0);
      }
      
      if(line1>line2)current_direction = 1; //up
      if(line1<line2)current_direction = 2; //down

      if(current_direction != last_direction) //changed 
      {
            last_direction = current_direction;
            return (last_direction);
      }
      else
      {
            return (0); //not changed
      }
   }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE, STYLE_SOLID,2);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE, STYLE_SOLID,2);
   SetIndexBuffer(1,ExtMapBuffer2);

   string short_name = "EMA CROSS INDICATOR!";
   IndicatorShortName(short_name);
//----

   
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   //Delete all the drawn objects!
   int I = WindowsTotal();
   for (int count = 0; count < WindowsTotal(); count++)
   {
      ObjectsDeleteAll(count, OBJ_ARROW);
      ObjectsDeleteAll(count, OBJ_TEXT);
   
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
   double SEma, LEma;

   int    counted_bars=IndicatorCounted();
   if (counted_bars<0) return(-1);
   if (counted_bars>0) counted_bars--;
   int    pos=Bars-counted_bars;
   
   static int isCrossed  = 0;
   
//---- main calculation loop
   while(pos>=0)
     {
         
         SEma = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,pos);
         LEma = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_CLOSE,pos);
   
   
         isCrossed = Crossed (LEma,SEma);
         if(isCrossed > 0)
         {
            DrawArrow (pos + 1 , SEma , isCrossed);

         }

         
         ExtMapBuffer1[pos]= SEma;
         ExtMapBuffer2[pos]= LEma;
         
         pos--;
     }
//----

      SEma = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,0);
      LEma = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_CLOSE,0);
      isCrossed = Crossed (LEma,SEma);
      if(isCrossed > 0)
         {
            if(Alert_On) 
             {
                 if (isCrossed == 1) Alert("Short EMA(" + ShortEma + ") has been crossed Long EMA(" + LongEma + ") Upward");
                 if (isCrossed == 2) Alert("Short EMA(" + ShortEma + ")has been crossed Long EMA(" + LongEma + ") Downward");
              }
         }   

   return(0);
  }
//+------------------------------------------------------------------+

Comments