How to Add Technical Indicators on Your Forex EA

Just one more topic in this forex expert advisor tutorial series before we start wrapping it all up! In this article, I’ll teach you how to add and tweak technical indicators in your code.

As I’ve mentioned in my earlier blog posts, what’s awesome about MQL4 or MQL5 language is that there are a bunch of functions that area already built in. In other words, you simply have to input the magic words and your forex robot will automatically know what to do.

When it comes to using technical indicators in your forex EA, there’s no need to delve into the nitty-gritty mathematical aspects of exponential moving averages or MACD in order to use these to generate trade signals. You’d be impressed to know that MQL4 comes with a set of 37 technical indicator functions that you can mix and match. RSI, Ichimoku Kinko Hyo, Bollinger bands… Just take your pick!

Of course each type of technical indicator function comes with a proper syntax that you’d have to follow in order to tweak its parameters properly. But there’s no need to memorize any input sequences too, as MQL4 conveniently provides a handy-dandy “cheat sheet” when you hit the F1 key while highlighting the corresponding function on your forex EA. You can also view more details in the MQL4 documentation for technical indicators.

Here’s a quick example for adding moving averages:

Suppose you want a moving average crossover system that makes use of the 50 SMA and 10 SMA in generating a buy signal for an upward crossover and a sell signal for a downward crossover. First you need to declare variables for the previous and current moving average values that you will compare later on:

forex ea moving average

Ain’t it cool that MetaEditor provides some suggestions on the technical indicator functions that you can use? For moving averages, the proper function is iMA and its syntax is as follows:

double iMA (string symbol, int timeframe, int ma_period, int ma_shift, int ma_method, int applied_price, int shift);


where symbol stands for the currency pair (or you can input NULL to denote the current pair),

timeframe refers to the chart time frame (or you can input 0 to apply it on the current time frame),

ma_period sets the number of bars used in the moving average calculation (50 or 10 in this example),

ma_shift specifies any offset or delay in the moving average (none or 0 in this case),

ma_method determines if it’s a simple (MODE_SMA), exponential (MODE_EMA), or linear-weighed (MODE_LWMA) moving average,

applied_price refers to the actual price used in the moving average calculation such as the close (PRICE_CLOSE), open (PRICE_OPEN), high (PRICE_HIGH), low (PRICE_LOW), or median (PRICE_MEDIAN),

and shift allows you to look at the moving averages for previous candles, which is useful in comparing MA values to gauge if a crossover has taken place or not.

forex ea SMA

See how we made use of the shift input to have the previous 50 SMA value look two candles back and the current 50 SMA value look at one candle back? Now we’ll also make use of this in assigning values to the previous and current 10 SMA:

forex ea sma crossover

The next step is to compare these SMA values to see if upward or downward crossovers have occurred. Any guesses on how the forex robot can be able to do that? I’ll give y’all the answers next week when we try to put all the lessons together in creating a basic moving average crossover EA. See you then!



  • Luis Miguel

    I hope you show us a completed EA, that would be great!