How to add indicators to expert advisors on MT4
If you have been following along each page, you may have already learnt how to add indicators to your expert advisors. If not, that is ok. We will go over it here, in detail.
Step 1:What are the indicators available to us, for use in MT4 and MQL4?
There are many indicators that we can pull from the MT4 platform.
We can use the RSI indicator, CCI, Envelopes, MACD, Stochastic Oscillator, Average Directional Movement Index, Bollinger Bands, Ichimoku Kinko Hyo, Moving Average, Parabolic SAR, Standard Deviation, Average True Range, Bears Power, Bulls Powers, DeMarker, Force Index, Momentum, Moving Average of Oscillator, Relative Vigor Index, Williams Percent Range, Accumulation/Distribution, Money Flow Index, OBV, Volumes, Accelerator Oscillator, Alligator, Awesome Oscillator, Fractals, Gator Oscillator and Market Facilitation Index.
All of the above indicators can be easily found and tested in your MT4 terminal, under "View", then "Navigator". Please observe the picture below to see where...
It is a good idea to attach one or more of these indicators to a chart that you want to work on, before just throwing numbers into you expert advisor. Getting comfortable with a few main indicators can really make creating profitable EA'S a lot easier.
Step 2:How to call upon an indicator in MQL4 code.
Ok, so you have picked an indicator that you want to create an expert advisor for... Now what!?
Well now you just need to know one simple letter... That letter is the letter "i".
This letter is what you use to call on an indicator in mql4 code. For example an RSI indicator is called into use with the code iRSI.
Every indicator is called upon using the letter i before it.
MetaEditor knows that you are about to call some function, so it will attempt to fill in the blanks for you, such as in the picture below...
As you can see it autofilled the options iBands, iBandsOnArray, iBarShift and iBars. "iBands" is the code name for the Bollinger Bands Indicator.
Some indicators do not use the name you may think, so you can always check their names here.
|iNAME||Indicator||iAC||Accelerator Oscillator||iAD||Accumulation/Distribution||iADX||Average Directional Index||iAlligator||Alligator||iAO||Awesome Oscillator||iATR||Average True Range||iBearsPower||Bears Power||iBands||Bollinger Bands||iBullsPower||Bulls Power||iCCI||Commodity Channel Index||iCustom||Custom Indicators||iDeMarker||DeMarker||iEnvelopes||Envelopes||iForce||Force Index||iFractals||Fractals||iGator||Gator Oscillator||iIchimoku||Ichimoku Kinko Hyo||iBWMFI||Bill Williams Market Facilitation Index ||iMomentum||Momentum||iMFI||Money Flow Index||iMA||Moving Average||iOsMA||Moving Average of Oscillator||iMACD||Moving Average Convergence-Divergence||iOBV||On Balance Volume||iSAR||Parabolic Stop and Reverse System||iRSI||Relative Strength Index||iRVI||Relative Vigor Index||iStdDev||Standard Deviation||iStochasticRSI||Stochastic Oscillator||iWPR||Williams Percent Range|
Step 3:How to add the indicator into your Expert Advisor.
Thank you for reading this far. Now, you will learn exactly how to place the full indicator code into your expert advisor. In the next step you will learn how to use it in an opening or closing condition.
So, we understand that to call an indicator, we use the letter "i".
After we have called the indicator though, we have to fill in its parameters. Luckily MetaEditor shows us what each indicator needs.
The picture below, displays this, in action.
As you can see, for the iBands Indicator it shows that we need to fill in the following, separated by commas:
Const String Symbol, int timeframe, int period, double deviation, etc...
Don't get distracted by the "int" "double" and so on. These represent the type of inputs that are to be used. An "int" is a number, positive or negative, without decimals.
A double is a number, positive or negative, with decimals.
So, looking at these requirements, we see some familiar words, like deviation, period, timeframe etc...
You may be confused by the first two though... "Const String Symbol" and int "Timeframe".
These two represent the symbol we are trading on, such as EurUsd, Gold, Silver and so on... Timeframe represents the charts timeframe we are wanting to use, such as M1, M5, H1, D1 etc.
As a general rule of thumb, you can input these two as "NULL" and "0".
NULL is saying that the EA is to use the Symbol of the chart it is attached to and "0" is saying to use the timeframe of the chart it is attached to. You can't test in the strategy tester any other way, so this is always the best route to take.
Now let's move on to writing out the full code for an indicator. We will stick with the iBands indicator (Bollinger Bands).
Lets assume we want the indicator to operate under the time period of 150, a deviation of 2.5, a bands shift of 0, the applied price of close, operating in regards to the upper of the 3 bands, and a barshift of 1 bar back.
So, how do we code this all in???
EASY! We just follow the autofill on the MetaEditor!
First though, we need to name our indicator any name we want.
All indicators use "double" to be named because indicators return values with decimals.
We will name this indicator "mybollinger"
As you can see, we have our indicators name after "double" then an equal sign, our "i" and then all of our iBands indicator parameters in the order the auto-fill told us to.
Now, simply use this same technique to add in as many indicators as you need. Give each a unique name and you will be ready to throw them into you buying and selling conditions and closing and modifying conditions.
Step 4:How to use your indicator in opening and closing conditions.
Here is how you apply the code indicator into a order condition...
This example is simply saying, "If the close of the last bar is greater than the bollinger upper band(Because we have chosen our indicator to be based on the upper band), then proceed to opening this trade.
It is as simple as that! It works that way for all conditional uses within your coding.
If you are unfamiliar in Closing Conditions or opening trades just check out our chapters on those topics.
NEXT UP --->