Many trading strategies are based on a crossover of indicators. For example, if you are using a fast moving average and a slow moving average, your entry signal is triggered when the fast MA crosses the slow MA. It is important to learn how to detect a crossover of indicators with MQL4 language if you want to create your own expert advisor or indicator. This guide shows how to detect these events.

It is useful to see some examples of crossover first, so that you can have a better idea of where you can apply this knowledge; the principle remains the same.

It doesn't matter which indicator or indicators you are going to examine, the rule is always the same and, generically, we can see the lines as follows:

To code an MQL4 program to spot a crossover, we need to translate everything into the language of mathematics. We can get the values of Indicator 1 and Indicator 2 at specific points in time and from there we will need to compare these values.

We define:

*I1(t1)*as the value of Indicator 1 at the instant*t1*.*I1(t2)*as the value of Indicator 1 at the instant*t2*.*I2(t1)*as the value of Indicator 2 at the instant*t1*.*I2(t2)*as the value of Indicator 2 at the instant*t2*.

With these values, we can establish the rules for a crossover:

- Indicator 2 crosses Indicator 1 from below if,
*I2(t1)*is less than*I1(t1)*and*I2(t2)*is greater than*I1(t2)*. - Indicator 2 crosses Indicator 1 from above if,
*I2(t1)*is greater than*I1(t1)*and*I2(t2)*is less than*I1(t2)*.

We can see a simple example of MQL4 code that detects a crossover of a fast moving average with a slow moving average.

// Define two variables that can become true in case of a verified crossover: bool CrossToBuy = false; bool CrossToSell = false; // Define the periods of the two indicators: int MASlowPeriod = 50; int MAFastPeriod = 10; // Define a function to detect a crossover: void CheckMACross() { CrossToBuy = false; CrossToSell = false; // iMA is the function to get the value of a moving average indicator. // MASlowCurr is the value of the slow moving average at the current instant. double MASlowCurr = iMA(Symbol(), 0, MASlowPeriod, 0, MODE_SMA, PRICE_CLOSE, 0); // MASlowPrev is the value of the slow moving average at the last closed candle/bar. double MASlowPrev = iMA(Symbol(), 0, MASlowPeriod, 0, MODE_SMA, PRICE_CLOSE, 1); // MAFastCurr is the value of the fast moving average at the current instant. double MAFastCurr = iMA(Symbol(), 0, MAFastPeriod, 0, MODE_SMA, PRICE_CLOSE, 0); // MAFastPrev is the value of the fast moving average at the last closed candle/bar. double MAFastPrev = iMA(Symbol(), 0, MAFastPeriod, 0, MODE_SMA, PRICE_CLOSE, 1); // Compare the values and detect if one of the crossovers has happened. if ((MASlowPrev > MAFastPrev) && (MAFastCurr > MASlowCurr)) { CrossToBuy = true; } if ((MASlowPrev < MAFastPrev) && (MAFastCurr < MASlowCurr)) { CrossToSell = true; } }

The code above could be adapted to any pair of indicators to spot crossovers. You can improve and adapt this piece of code according to your own needs. It is always a good idea to try and experiment!