Author: Harmeet Singh
EM12-21-89

This tool is designed to assist traders by identifying specific trend-reversal moments on a price chart. It works by analyzing "Exponential Moving Averages" (EMAs), which are smoothed lines that follow the general direction of price action over a set period of time.

Core Logic and Functionality

The primary objective of this script is to detect "crosses"—the specific moment when a fast-moving average line cuts through a slower-moving average line. These crossovers are often used by traders as signals that momentum is shifting from bearish to bullish (or vice versa).

1. Multi-Layered Analysis The script monitors two distinct sets of moving averages simultaneously:

  • Short-Term Crosses: It tracks a 12-period average against a 21-period average. This is typically used to identify quicker, more immediate shifts in price momentum.
  • Long-Term Crosses: It tracks a 12-period average against an 89-period average. This is generally used to identify major trend changes over a longer duration.

2. Signal Confirmation To ensure accuracy and avoid "false signals" caused by minor price fluctuations, the script doesn't just look at a single point in time. It validates a cross by checking three consecutive data points:

  • It looks at where the lines were in the previous candle.
  • It checks where the lines are in the current candle.
  • It confirms the positioning in the next available data point.

A signal is only generated if the fast line moves from being "below" to "above" the slow line (a Bullish/Buy signal) or from "above" to "below" the slow line (a Bearish/Sell signal).

3. Visual Representation Once a confirmed crossover is detected, the script places visual markers directly on your chart:

  • Buy Signals: An arrow appears slightly below the lowest price of the candle where the upward cross occurred.
  • Sell Signals: An arrow appears slightly above the highest price of the candle where the downward cross occurred.

4. Dynamic Scaling The script calculates a "Range" value by looking at the volatility (the difference between highs and lows) of the last 10 candles. It uses this average range to determine exactly how far away from the candle the arrows should be placed, ensuring that the markers remain clearly visible and do not clutter the price action regardless of whether the market is moving slowly or rapidly.

Summary for the User

Essentially, this script automates the tedious task of watching two sets of moving averages at once. It acts as an early-warning system, highlighting potential entry points by marking exactly where trends are beginning to flip, allowing you to focus on high-probability momentum shifts rather than manually tracking every price movement.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
EM12-21-89
//+------------------------------------------------------------------+
//|                                         EM5-Cross.mq4            |
//|         Copyright © 2005, Harmeet Singh                          |
//|                   grabapip@yahoo.com                             |
//+------------------------------------------------------------------+


#property copyright "Harmeet Singh"
#property link      "grabapip@yahoo.com"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Blue
#property indicator_color2 Yellow
#property indicator_color3 Orange
#property indicator_color4 Aqua
#property indicator_color5 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 4
#property indicator_width5 4

extern int AllBars=250;

double ExtMapBuffer[];
extern bool Use12_21_Cross = false;
double CrossUp12_21[];
double CrossDown12_21[];
extern int FasterEMA = 12;
extern int SlowerEMA = 21;

double CrossUp12_89[];
double CrossDown12_89[];
extern int FastEMA = 12;
extern int SlowEMA = 89;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexShift(0,0);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   
//---- indicators
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexArrow(1, 233);
   SetIndexBuffer(1, CrossUp12_21);
   SetIndexStyle(2, DRAW_ARROW, EMPTY);
   SetIndexArrow(2, 234);
   SetIndexBuffer(2, CrossDown12_21);


   SetIndexStyle(3, DRAW_ARROW, EMPTY);
   SetIndexArrow(3, 233);
   SetIndexBuffer(3, CrossUp12_89);
   SetIndexStyle(4, DRAW_ARROW, EMPTY);
   SetIndexArrow(4, 234);
   SetIndexBuffer(4, CrossDown12_89);
   //---- indicator short name
   IndicatorShortName("EMA_12-89");
   SetIndexDrawBegin(0,240);
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int i, counter;
   
   double fasterEMAnow, slowerEMAnow, fasterEMAprevious, slowerEMAprevious, fasterEMAafter, slowerEMAafter, cu;
   double fastEMAnow, slowEMAnow, fastEMAprevious, slowEMAprevious, fastEMAafter, slowEMAafter;
   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--;
   int limit = Bars-counted_bars-1;
   if (limit > AllBars) limit = AllBars;
   
   for(i=limit; i > 0; i--) {
   
      counter=i;
      Range=0;
      AvgRange=0;
            
      for (counter=i ;counter<=i+9;counter++)
      {
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10;
       
      if (Use12_21_Cross)
      {
         fasterEMAnow = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i);
         fasterEMAprevious = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i+1);
         fasterEMAafter = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i-1);

         slowerEMAnow = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i);
         slowerEMAprevious = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i+1);
         slowerEMAafter = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i-1);
      }
      
      fastEMAnow = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i);
      fastEMAprevious = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i+1);
      fastEMAafter = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i-1);

      slowEMAnow = iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i);
      slowEMAprevious = iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i+1);
      slowEMAafter = iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i-1);
      
      if (Use12_21_Cross)
      {
         if ((fasterEMAnow > slowerEMAnow) && (fasterEMAprevious < slowerEMAprevious) && (fasterEMAafter > slowerEMAafter)) {
            CrossUp12_21[i] = Low[i] - Range*0.5; 
//          if(limit<=2)
//            {
//            Alert (Symbol()," ",Period(),"-Min. BUY Crossed Up at ",  CrossUp12_21[i]);
//            }
         
         }
         else if ((fasterEMAnow < slowerEMAnow) && (fasterEMAprevious > slowerEMAprevious) && (fasterEMAafter < slowerEMAafter)) {
            CrossDown12_21[i] = High[i] + Range*0.5; 
         
//           if(limit<=2)
//           {
//            Alert (Symbol()," ",Period(),"-Min. SELL Crossed Down at ",  CrossDown12_21[i]);
//           }
      
         }
      }
      
      if ((fastEMAnow > slowEMAnow) && (fastEMAprevious < slowEMAprevious) && (fastEMAafter > slowEMAafter)) {
         CrossUp12_89[i] = Low[i] - Range*0.5;
//       if(limit<=2)
//            {
//            Alert (Symbol()," ",Period(),"-Min. BUY Crossed Up at ",  CrossUp12_89[i]);
//            }
      }
      else if ((fastEMAnow < slowEMAnow) && (fastEMAprevious > slowEMAprevious) && (fastEMAafter < slowEMAafter)) {
         CrossDown12_89[i] = High[i] + Range*0.5;
//        if(limit<=2)
//           {
//          Alert (Symbol()," ",Period(),"-Min. SELL Crossed Down at ",  CrossDown12_89[i]);
//         }
      }
       
   }
   return(0);
}

Comments