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.

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

  1. Nieco Pama says:

    I have an indicator that shows arrows on the chart. I try to add it in programming my EA. But when I test it, as the arrow appears..the EA opens a multiple trade. What should i do to make it open 1 trade in every arrow? Is there any codes to add?

    1. Yannick says:

      Maybe add something similar like in lesson 1.

  2. Abdul says:

    Hello Yannik,
    I am from Valais(Wallis). So nice of you to teach EA programming.
    I have a quiz. Will you do an EA for Jforex3 web platform of SWFX.
    I am using this on a demo account from Dukascopy.
    I want to go for Live with 10k in near future .
    Could you please forward me your private prog.

    1. Yannick says:

      No, I will only make EA for MT4 and in the future MT5. I suggest you take a look at MT5. It is far better than MT4 and Jforex, but not much broker use it. MT5 will be the platform that most will use in the future (my opinion). Some broker like Swissquote Ltd do already allow hedging in MT5. If you live in Switzerland you could even open an MT5 account with Swissquote Bank.

  3. Leon says:

    Hi Yannick,
    i have many questions :
    1. how to use custom indicator like FFC for filter news …
    2. how to close order from signal
    3. how to use trailingstop

    1. Yannick says:

      1) I do not know the FFC indicator. Maybe there is a manual with the indicator?
      2) Use the OrderClose function
      3) If (currentPrice – SL) >= TrailingStop, set the SL to (currentPrice – TrailingStop). I’m sure you’ll find a sample code on mql5.com

      1. Leon says:

        thank you…for no 2 and 3 …
        here are link FFC …
        https://www.mql5.com/en/code/15931
        here are review …
        https://www.mql5.com/en/forum/94492
        may be you can explain better to me …

        and i have question again… how to make EA stop work on friday at 20:00 broker time…
        thank you..really appreciate…

        1. Leon says:

          i’ve done with stop ea on friday …

          1. Yannick says:

            Glad you figured it out 🙂

        2. Yannick says:

          I’ve never used this indicator, but I’m sure traders will reply to your questions in the forum.

          1. Leon says:

            but unfortunately my questions no one reply/answer … this indicator very powerful to filter news…

          2. Yannick says:

            Sorry to hear that. Unfortunately, I can’t help you either.

  4. Darren says:

    Really great tutorial. It’ll get me well underway with my endeavors. Much appreciated and thanks.

    1. Yannick says:

      You’re welcome 🙂

  5. Karolex says:

    Hi great work, really helpful. I have a question, is it possible to automatically draw a custom indicator form withing EA or does it have to be attached to the chart manually first ?

    Thanks,

    1. Yannick says:

      No, you have to add the indicator manually. Or you can create a template and name it default.tpl. This will automatically load the default template if you open a new chart. Or you can use tester.tpl in the strategy tester.

  6. Peter says:

    Hi Yannick,
    Many thanks for the great tutorials. They have been of great benefit to me as my only programming experience was in the late eighties with dBase 3+ and Pascal. I am in front by far with my EAs thanks to your Tutorials. Hope to see more.
    All the best.

    1. Yannick says:

      Very glad you like it. Not sure if I will make more tutorials. But now that you know the basics you can use the MQL4 forum to search for more advanced features.

    2. Peter says:

      Understandable. I was dreaming that you might move onto double dim arrays with custom data types 🙂

      It’s good to dream.

      Best regards

      1. Yannick says:

        What exactly do you mean by that? Sure there are many ways how to do things. But I try to keep it as simple as possible in this tutorial.

        1. Peter says:

          Not sure where you are coming from.

          Understandable meant, I understand that you would be busy,
          or
          I’m trying to get my head around using my custom data type for a 2 dimension array. Can’t get it working so I’ve gone back to single array with standard data types. That’s all, no offence intended.

          1. Yannick says:

            From Switzerland 🙂
            I see. Ok, just stay with easy and readable code. The more you code the more you can use advanced code.

  7. Nirav says:

    hey many thanks for the tutorial!!!!! very well thought out

    1. Yannick says:

      Thanks. Glad you like it 🙂

  8. Joe says:

    This is one of the best tutorials for MQL4 I have found! Thanks for putting this together!

    1. Yannick says:

      Glad you liked it 🙂

  9. mike says:

    Hi, great tutorials!
    How do I know only top 6 parameters for icustom? Why is Max Bars and the boolean inputs not included in EA icustom?
    Thanks!

    1. Yannick says:

      They are not included because I do not need them.

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.