MQL Custom Indicator Hammer Candlestick Pattern


Price Action is one of the most useful strategies you can add to your trading arsenal.  I always recommend starting out visually detecting price action patterns, but once you understand how simple they are, you can easily start to code your own price action indicators.  Most of the candlestick patterns simply compare open, close, high and low price points of the current, or set of candles.  We’ll show you how to code a simple indicator to detect one of the most useful candlestick patterns.


The Hammer Candlestick Pattern

The hammer candlestick pattern is quite simple, any candle who’s wick is longer then the body and tip is close to the body or nonexistent is essentially a hammer.  These rules are simple, but also very subjective.  For example how long should a tip or wick be in order to justify a true hammer ?  Should there even be a tip at all ?  These subtle differences can be tweaked but for now, we are going to to assume two things :

  1. The wick of the candle should be twice as long as the body of the candle
  2. The tip should less then half the size of the body

These rules will give us large enough wicks and also make sure we have a very small tip.  Eventually you could add a rule to justify the size of the actual body as well, when you want to fine tune your hammer detection further.

Setting up our indicator

Create a new mql indicator in metaeditor and make sure you select to have this in the chart window.  Your basic setup for this indicator is follows :


#property link ""
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 clrMediumOrchid

double BufferBullishHammer[];

//| Custom indicator initialization function                         |
int init()
   SetIndexLabel(0,"Bullish Hammer");

Coding the indicator

You’ll want to then go to your start function and setup your normal indicator bar counting logic, as used in our other tutorials this is pretty straightforward.

  int counted_bars = IndicatorCounted();
   int limit ;
   int i  = 0;

   limit = Bars-counted_bars-1;  

Now we can start defining our custom variables and code our loop that will check for the hammer candlestick Below is the code and we can go into detail.

  //Define some variables to get the values of our candle
   double open1;
   double close1;
   double wick = 0.0; 
   double body = 0.0;
   double tip = 0.0;

   //Loop through all bars and detect any bullish candles	   
   for(i=limit; i>=1; i--)
      //Open and Close price  
      open1 = Open[i]; 
      close1 = Close[i];
	 //Bullish Hammer   
	 if( close1 < open1 ){
	      wick = Close[i] - Low[i];
	      body = Open[i] - Close[i];
	      tip = High[i] - Open[i];
	      if( wick > ( body*2.0 ) && tip < (body/2) ){    
	        BufferBullishHammer[i] = Low[i] - 20*Point;


       if( close1 > open1 ){
	      wick = Open[i] - Low[i];
	      body = Close[i] - Open[i];
	      tip = High[i] - Close[i];
	      if( wick > ( body*2.0 ) && tip < (body/2) ){
	         BufferBullishHammer[i] = Low[i] - 20*Point;

Most of this should be obvious but you probably notice we have to check for two distinct types of candles, Bullish and Bearish candles. So we simply check the closing price compared to the open price to determine what type of candle this was, then you can determine the wick, body and tip.

  //We have a bearish candle
  if( close1 < open1 )

  //We have a bullish candle
  if( close1 > open1 )

The next lines grab our wick, body and tip depending on what type of candle we have, and then check for our Hammer requirements .

Remember, our wick is twice the size of our body, and the tip is half the body size so its simply :

if( wick > ( body*2.0 ) && tip < (body/2) ){
   BufferBullishHammer[i] = Low[i] - 20*Point;

With the above code you should be able to identify Hammer patterns and modify the code to your liking.

Hope you enjoyed this bit of code , ask any questions you have and I will get back to you.

Leave a Reply