MA_Crossover_Signal_v2

Download script
Author: Copyright � 2005, Jason Robinson (jnrtrading)
MA_Crossover_Signal_v2

This script is designed as a visual trading tool for the MetaTrader platform, specifically created to highlight "crossovers" between two moving averages. In technical trading, a crossover occurs when a fast-moving price average intersects with a slower-moving one, which many traders use as a signal for a potential change in market direction.

Core Functionality

The primary purpose of this tool is to make these crossover points easily visible on your chart. Often, when you are looking at short-term periods, the moving average lines can become cluttered or obscured by the actual price candles (bars). This script solves that problem by identifying the exact moment the lines cross and placing a clear visual marker at that specific point.

How the Logic Works

To determine where to place these markers, the script follows a three-step logical process:

1. Calculation of Moving Averages The user can define two different moving averages (a "Faster" one and a "Slower" one). The script continuously calculates the current value of both averages, as well as their values from the previous bar and the following bar in the sequence.

2. Detection of the Crossover The script looks for a specific mathematical shift between consecutive bars:

  • Upward Signal: It checks if the Faster Moving Average was previously below the Slower one but has now moved above it.
  • Downward Signal: It checks if the Faster Moving Average was previously above the Slower one but has now moved below it.

3. Dynamic Marker Placement Instead of simply drawing a line, the script calculates the "Range" (the average height of recent price candles) to ensure the markers are placed in a visually balanced position.

  • When an Upward Cross is detected, it places a marker slightly below the low of the candle.
  • When a Downward Cross is detected, it places a marker slightly above the high of the candle.

Key Benefits for the User

  • Visual Clarity: It allows you to keep your chart clean by removing the actual moving average lines while still seeing the critical entry and exit signals they provide.
  • Precision: By calculating the specific point of intersection, it removes the guesswork of trying to identify exactly when a trend shifted during a fast-moving market.
  • Customization: It provides flexibility in choosing different types of moving averages (such as Simple, Exponential, or Weighted) to suit various trading styles.
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
9 Views
0 Downloads
0 Favorites
MA_Crossover_Signal_v2
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                         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 DodgerBlue
#property indicator_color2 Red

double CrossUp[];
double CrossDown[];
extern int FasterMode = 1; //0=sma, 1=ema, 2=smma, 3=lwma
extern int FasterMA =   13;
extern int SlowerMode = 1; //0=sma, 1=ema, 2=smma, 3=lwma
extern int SlowerMA =   25;
//+------------------------------------------------------------------+
//| 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*0.5;
      }
      else if ((fasterMAnow < slowerMAnow) && (fasterMAprevious > slowerMAprevious) && (fasterMAafter < slowerMAafter)) {
         CrossDown[i] = High[i] + Range*0.5;
      }
   }
   return(0);
}

Comments