MA_Crossover_Signal[1]

Download script
Author: Copyright � 2005, Jason Robinson (jnrtrading)
MA_Crossover_Signal[1]

This tool is designed to provide traders with clear visual signals based on the interaction between two moving averages. In technical trading, moving averages are used to smooth out price fluctuations to identify trends; however, when two averages get very close together or cross each other, these lines can sometimes become visually difficult to distinguish on a crowded chart. This script solves that problem by identifying the exact moments those crossings occur and marking them with distinct visual signals.

How the Logic Works

The script monitors two different moving averages simultaneously—a "Faster" average and a "Slower" average. You can customize both the speed (period) and the specific type of calculation used for each (such as Simple, Exponential, Smoothed, or Linear Weighted).

By constantly analyzing the relationship between these two lines, the tool looks for three specific conditions to occur in sequence:

1. Identifying Upward Crosses (Buy Signals) The script looks for a moment where the Faster moving average moves from being below the Slower average to being above it. To ensure accuracy and filter out "noise," it checks the current position, the previous bar, and the following bar. When it confirms that the Faster line has moved upward over the Slower line, it places a green arrow below the price candle.

2. Identifying Downward Crosses (Sell Signals) Conversely, the script monitors for moments when the Faster moving average drops from above the Slower average to below it. Once this downward shift is confirmed across the same three-bar sequence, it places a red arrow above the price candle.

3. Dynamic Visual Positioning To ensure the signals are easy to read, the tool does not simply place the arrows directly on the candles, which could lead to clutter. Instead, it calculates the average "range" (the distance between the highs and lows of recent price action). It then places the signals at a set distance away from the candles based on this range, ensuring that the alerts remain clearly visible regardless of how much you zoom in or out on your chart.

Key Benefits

  • Clarity: It eliminates the need to squint at overlapping lines by providing distinct markers for trend changes.
  • Customization: Users can adjust the settings to match their specific trading style, whether they prefer fast-acting signals or smoother, long-term trends.
  • Visual Cleanliness: Because it marks the crossover points as arrows, you have the option to remove the actual moving average lines from your chart entirely while still keeping the important entry/exit signals visible.
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
16 Views
0 Downloads
0 Favorites
MA_Crossover_Signal[1]
//+------------------------------------------------------------------+
//|                                         EMA-Crossover_Signal.mq4 |
//|         Copyright © 2005, 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, Jason Robinson (jnrtrading)"
#property link      "http://www.jnrtrading.co.uk"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DarkGreen
#property indicator_width1 3
#property indicator_color2 Red
#property indicator_width2 3

double CrossUp[];
double CrossDown[];
extern string note1 = "0=sma, 1=ema, 2=smma, 3=lwma";
extern int FasterMode = 1; //0=sma, 1=ema, 2=smma, 3=lwma
extern string note2 = "First MA";
extern int FasterMA =   5;
extern string note3 = "0=sma, 1=ema, 2=smma, 3=lwma";
extern int SlowerMode = 1; //0=sma, 1=ema, 2=smma, 3=lwma
extern string note4 = "Second MA";
extern int SlowerMA =   12;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexArrow(1, 234);
   SetIndexBuffer(1, CrossDown);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit, i, counter;
   double fasterMAnow, slowerMAnow, fasterMAprevious, slowerMAprevious, fasterMAafter, slowerMAafter;
   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;
       
      fasterMAnow = iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE, i);
      fasterMAprevious = iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE, i+1);
      fasterMAafter = iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE, i-1);

      slowerMAnow = iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_CLOSE, i);
      slowerMAprevious = iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_CLOSE, i+1);
      slowerMAafter = iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_CLOSE, i-1);
      
      if ((fasterMAnow > slowerMAnow) && (fasterMAprevious < slowerMAprevious) && (fasterMAafter > slowerMAafter)) {
         CrossUp[i] = Low[i] - Range*1.5;
      }
      else if ((fasterMAnow < slowerMAnow) && (fasterMAprevious > slowerMAprevious) && (fasterMAafter < slowerMAafter)) {
         CrossDown[i] = High[i] + Range*1.5;
      }
   }
   return(0);
}

Comments