digitalpips

MQL4 Custom Indicator Tutorial 1

tutorial1-ind-thumb

Getting started in custom indicators is a great starting point for your mql development.  I’m going to start these tutorials in mql4 and make mirror tutorials for mql5.  But since mql4 is the most widely used and most brokers might not support mql5 yet we’re going to start there.

 

Getting Started

If your version of metatrader is on mql4 when you create your new custom indicator in metaeditor you will already have a template setup for you.  If you have mql5 you can simply create the indicator , delete the template and insert your own mql4 template.  Here is the bare bones functions we need in any mql4 indicator. I’ve commented most of the code below but lets go over it in some detail as this is the most important step to understanding how metatrader functions.

 

 

Our simple indicator

//+------------------------------------------------------------------+
//|                                                  Tutorial1.mq4   |
//|                                           Author: Brian Publik   |
//|                                                  Digitalpips.com |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
#property indicator_maximum 2
#property indicator_minimum 0

double Buffer[];  //Array that will hold and represent our indicator data

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0, Buffer);          //Set the index buffer 0 to our Buffer array
   SetIndexStyle(0, DRAW_ARROW);       //Set the style of our indicator 
   SetIndexArrow(0,217);               //Sets the icon for our indicator
   SetIndexEmptyValue(0,0);            //Sets default empty value
   SetIndexLabel(0,"Up");              //Sets a label when hovering our indicator
   return(0);
  }
//+------------------------------------------------------------------+
//| De-initalization function                                        |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Start function which runs on every tick                          |
//+------------------------------------------------------------------+
int start()
  {
  
   int counted_bars = IndicatorCounted();    //Gets how many bars we already processed
   int limit ;
   int i  = 0;

   if(counted_bars<0)return(0);              //If we don't have any bars to process
   
   limit = Bars-counted_bars-1;
   
   //Loop through all bars not yet processed
   for(i=limit; i>=0; i--)
   {
      //Do some calculation and fill our indicator buffer
     if( Close[i] > Close[i+1] ){
      Buffer[i] = 1;
      }
   }

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

Lets take a look at the first few lines, this is where you will set property settings and declare your global variables to use throughout the program.

Indicator properties and global variables

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
#property indicator_maximum 2
#property indicator_minimum 0

double Buffer[];  //Array that will hold and represent our indicator data

The first few lines tell metatrader what window to place this indicator.
it can either be indicator_separate_window, or indicator_chart_window .
In this example we are going to create the indicator in a separate window.
 
indicator_buffers tells the system how many graphical representations we want. This is very important because you only want to use a buffer, if its actually going to display something on the screen. If you are just running calculations on many datasets or temporarily holding your data, you want to use a normal array. Note that arrays and index buffers are declared the same way, its what you tell metatrader to do with them, that defines how they behave. We will see that below.
 
The next line tells what color we want the first indicator buffer color to be. Then declare we want the range of this indicator to be from 0 to 2. And lastly we declare an array to hold our indicator values.
 

The init function

int init()
  {
   SetIndexBuffer(0, Buffer);          //Set the index buffer 0 to our Buffer array
   SetIndexStyle(0, DRAW_ARROW);       //Set the style of our indicator 
   SetIndexArrow(0,217);               //Sets the icon for our indicator
   SetIndexEmptyValue(0,0);            //Sets default empty value
   SetIndexLabel(0,"Up");              //Sets a label when hovering our indicator
   return(0);
  }

The init() function is where you will setup all your index buffers and any data initialization, as well as any indicator settings.
The comments pretty much explain everything going on. We tell meta trader what array to use as our index buffer , what type – in this case we will draw an arrow symbol, what symbol, and a label. Also we set the default empty value to 0, so that we won’t draw anything if our indicator value is 0;

The start function and indicator processing

int start()
  {
  
   int counted_bars = IndicatorCounted();    //Gets how many bars we already processed
   int limit ;
   int i  = 0;

   if(counted_bars<0)return(0);              //If we don't have any bars to process
   
   limit = Bars-counted_bars-1;

   //Loop through all bars not yet processed
   for(i=limit; i>=0; i--)
   {
      //Do some calculation and fill our indicator buffer
      if( Close[i] > Close[i+1] ){
      Buffer[i] = 1;
      }
   }

   return(0);
  }

At the moment we will skip the deinit function since we don’t need it for anything in this tutorial. The start function is very important because every time your broker sends a new ‘tick’ or price change for a chart, this function will receive a call to start(). The first few lines are pretty straight forward, we call IndicatorCounted(), set the limit to the amount of bars we have yet to count and then loop through all the bars we haven’t processed , doing something with our data. The reason we use indicator counted is because most of the time there is no point to re-run our calculations for all the previous bars. But some times you will and you can easily modify the loop to check for that. The important piece is starting at limit and working your way down. Normal arrays start from 0 and go forward with new values added to the end. With mql all new values are added to the front, or Buffer[0] and all previous values are pushed back. Note the values only swap on new bars so for the current bar, all values will fill at Buffer[0] until the new bar comes in.

This simple code merely checks to see of the current bar’s close price is greater then the previous bar and then fills the indicator with a value of 1 if it is.

If you attach this to a chart you will see something like the following :

tutorial1-ss

 

That about wraps up this first simple tutorial. Hope you enjoyed, in the next post for this series, we will do some more advanced strategies and check out some other ways of drawing our indicators.

One response to “MQL4 Custom Indicator Tutorial 1”

Leave a Reply