Skip navigation.
Home
Metatrader community - Forex Trading with Metatrader

Custom indicator functions

Hi folks,

Today we are going to study a very important set of function that handles our custom indicators created in MQL4; the Custom indicator functions.

The Custom indicator functions are a set of functions that we use to set low level properties of the custom indicator we want to code, and they can not be used with the code of Expert Advisors or Scripts.

Let's give these functions a deep look:

 

IndicatorBuffers: 

Syntax

void IndicatorBuffers (int count)

 Description: 

The IndicatorBuffers function allocates the required memory (buffer) needed for the indicator calculation.
The IndicatorBuffers value range from 1 to 8 and it must be equal to or less than the buffers count indicated by the indicator_buffers property.

Note: The difference between the buffers allocated by the indicator_buffers property and  IndicatorBuffers function is that the indicator_buffers property allocate the buffers needed to draw the indicators lines while the IndicatorBuffers allocate the extra buffers that needed for extra calculation.

Parameters:

This function takes only one parameter:

 int count:

The count of the buffers, the range is 1 to 8.

 Example

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Silver
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double ind_buffer1[];
double ind_buffer2[];
double ind_buffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(3);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexDrawBegin(0,SignalSMA);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
SetIndexBuffer(0,ind_buffer1);
SetIndexBuffer(1,ind_buffer2);
SetIndexBuffer(2,ind_buffer3);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
return(0);
}

 

IndicatorCounted: 

Syntax

int IndicatorCounted()

 Description: 

The IndicatorCounted function returns the amount of the bars that the indicator has been calculated them.
In the first launch of the indicator this count will be 0 because the indicator didn’t calculate any bars yet. And after that it will be the count of total bars on the chart -1.

Parameters:

This function takes no parameters:

Example

int start()
{
int limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- the last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- main loop
for(int i=0; i<limit; i++)
{
//---- ma_shift set to 0 because SetIndexShift called abowe
ExtBlueBuffer[i]=iMA(NULL,0,JawsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
ExtRedBuffer[i]=iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
ExtLimeBuffer[i]=iMA(NULL,0,LipsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
}
//---- done
return(0);
}

 

 

IndicatorDigits: 

Syntax

void IndicatorDigits (int digits)

 Description: 

The IndicatorDigits function sets the count of digits after decimal point (precision format) of the indicator, the default value is the Symbol of the used chart precision format.

Parameters:

This function takes only one parameter:

 int digits:

The count of digits after decimal point.

 Example

int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(3);
//---- setting of drawing parameters
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexDrawBegin(0,SignalSMA);
IndicatorDigits(Digits+2);
//---- 3 allocated buffers of an indicator
SetIndexBuffer(0,ind_buffer1);
SetIndexBuffer(1,ind_buffer2);
SetIndexBuffer(2,ind_buffer3);
//---- "short name" for DataWindow and indicator subwindow
IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
return(0);
}

 

IndicatorShortName: 

Syntax

void IndicatorShortName (string name)

 Description: 

The IndicatorShortName function sets the text that will be showed on the upper left corner of the separate chart window (Figure 2).

Parameters:

This function takes only one parameter:

 string name:

The text you want to display in the indicator short name.

 Example

int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(3);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexDrawBegin(0,SignalSMA);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
SetIndexBuffer(0,ind_buffer1);
SetIndexBuffer(1,ind_buffer2);
SetIndexBuffer(2,ind_buffer3);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
return(0);
}