Chapter 5: Adding a custom indicator to our EA

In this chapter, I will show you how you can add a custom indicator to your EAs with the icustom function. We use the free Triple Bollinger Bands indicator, which you can download below.

We start with a new EA project in the MetaEditor. I will name it “icustomEA”. Since you already know how to write an EA, I will only use the OnTick function in this lesson.

Before we start coding the EA, we have to know what parameters the Triple Bollinger Bands indicator uses and what prices we can receive from it. We attach the indicator to the chart and look at the input parameters.

customIndicator-TBB

We can see that we are only interested in the top 6 parameters. They are all integers (int) or double.  And one of them is an enumeration. We want to be able to adjust these settings directly from our EA, so we write them in the in the above area of our code.

input int period = 20;         // Period
input double deviation1 = 2.0; // Deviation 1
input double deviation2 = 3.0; // Deviation 2
input double deviation3 = 4.0; // Deviation 2
input ENUM_APPLIED_PRICE appliedPrice = PRICE_CLOSE; // Applied price
input int shift = 0;           // Shift
dataWindow

In the Data Window (View > Data Window OR Ctrl+D) we see the prices of the different bands. The first one (Bands Upper 1) stored in Buffer 0.  And this indicator has 7 Buffers.

Now we know everything to use the icustom function. In this example, I will store the “Bands Lower 2” value (buffer 3) inside the band2_lo variable. The 3 first parameters of the icustom function are the symbol, the period, and the path/name of the compiled indicator. I put the Triple Bands indicator in the main MQL4/indicators folder so I only write its name in it. Then I add the parameters of the Triple Bands indicator in the same order as it is in the indicator => period, deviation1, ,deviation2, deviation3, appliedPrice, shift. The next value is the buffer (3 in this case), the last value is the shift of the buffer (0 for current bar, 1 for previous bar).

To see if it works we comment its value to the chart.

 double band2_lo = iCustom(_Symbol,_Period,"Triple Bollinger Bands", period, deviation1, deviation2, deviation3, appliedPrice, shift, 3, 0);
 Comment(NormalizeDouble(band2_lo,4));
If this works, we can add all other bands and print the values to the chart
 // store the prices inside the variables
 double band1_up = iCustom(_Symbol,_Period,"Triple Bollinger Bands", period, deviation1, deviation2, deviation3, appliedPrice, shift, 0, 0);
 double band1_lo = iCustom(_Symbol,_Period,"Triple Bollinger Bands", period, deviation1, deviation2, deviation3, appliedPrice, shift, 1, 0);
 double band2_up = iCustom(_Symbol,_Period,"Triple Bollinger Bands", period, deviation1, deviation2, deviation3, appliedPrice, shift, 2, 0);
 double band2_lo = iCustom(_Symbol,_Period,"Triple Bollinger Bands", period, deviation1, deviation2, deviation3, appliedPrice, shift, 3, 0);
 double band3_up = iCustom(_Symbol,_Period,"Triple Bollinger Bands", period, deviation1, deviation2, deviation3, appliedPrice, shift, 4, 0);
 double band3_lo = iCustom(_Symbol,_Period,"Triple Bollinger Bands", period, deviation1, deviation2, deviation3, appliedPrice, shift, 5, 0);
 double band_main = iCustom(_Symbol,_Period,"Triple Bollinger Bands", period, deviation1, deviation2, deviation3, appliedPrice, shift, 6, 0);
 
 // prepare the comment
 string comment = 
 "Band Upper 1 - "+(string)NormalizeDouble(band1_up,4)+"\n"+
 "Band Lower 1 - "+(string)NormalizeDouble(band1_lo,4)+"\n"+
 "Band Upper 2 - "+(string)NormalizeDouble(band2_up,4)+"\n"+
 "Band Lower 2 - "+(string)NormalizeDouble(band2_lo,4)+"\n"+
 "Band Upper 3 - "+(string)NormalizeDouble(band3_up,4)+"\n"+
 "Band Lower 3 - "+(string)NormalizeDouble(band3_lo,4)+"\n"+
 "Band Main - "+(string)NormalizeDouble(band_main,4);
 
 // print the comment to the chart
 Comment(comment);

Now we know how to use any custom indicators inside our EAs. But I want to point out one thing. Remember the last parameter of the icustom function is the shift. If you want the EA to open a trade if the price crosses the “Bands Upper 3” line, we do not only need the current price of this line, but also the price of the previous bar. It would look like this.

  // a simple strategy
 double band3_up_curr = iCustom(_Symbol,_Period,"Triple Bollinger Bands", period, deviation1, deviation2, deviation3, appliedPrice, shift, 4, 0);
 double band3_up_prev = iCustom(_Symbol,_Period,"Triple Bollinger Bands", period, deviation1, deviation2, deviation3, appliedPrice, shift, 4, 1);
 
 if(High[1] < band3_up_prev && High[0] > band3_up_curr)
   {
     // open a buy order
     Print("open a buy order");
   }

Get the Triple Bands indicator and the full source code of this EA: Download

I have not yet wrote a tutorial about how to close trades, but if you want to know how to do it, please check this topic:
https://quivofx.com/boards/topic/orderclose-function/

I hope this lesson helped you and you now know how to implement any custom indicator to your EA.

If you have questions, write them in the comments below.

2 thoughts on “Chapter 5: Adding a custom indicator to our EA”

  1. chrisian says:

    Good day,
    Would you know how to add the NonLagDot indicator to the Blessing EA that has already 4 selectable indicators for triggering Buy and Sell signal.
    Thank you
    Chris

    1. Yannick says:

      If you followed all chapters, you should be able to do it. Else you could always search for help in the official mql4 forum.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.