In this video I wanted to talk about something that I call: “Signal Stacking”; you see 4 lines here, each is a Moving Average for a different period. As long as all the 4 lines are going downwards without a crossover that would be a pretty stable sell trend and you could stay in your short position and be very sure that you don’t lose any money, in fact we would have made some money here, probably a descent amount.
The only thing you need to wait for is: you need to wait for a crossover for one or more of these lines to get out.
Let’s speed that up a little bit and here is the crossover, now you would get out and when you see a complete crossover for each of the lines like in this case that would create a reverse trend, this is a very good example and now you would open a long position and trade it until the next crossover takes place.
Now we want to find out how we can create an Expert Advisor that is able to produce these 4 lines and create buy or sell signals for stable trends.
To do that please click on the little button here or press F4 in your Metatrader.
Now you should see the Metaeditor window and here you want to click on “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleSignalStacking”, click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and the two comment lines.
We want to start by creating an empty string for the signal that will be also called: “signal” and we don’t assign a value here.
Let’s create an array for several prices; in fact we are going to create four.
We will call them my Moving Average array 1 (myMovingAverageArray1), my Moving Average array 2 (myMovingAverageArray2), my Moving Average array 3 (myMovingAverageArray3) and my Moving Average array 4 (myMovingAverageArray4).
For each of the arrays we use the “iMA” function for the current symbol and the current period to create an Exponential Moving Average by using “MODE_EMA” that will be calculated on the close price and it will be calculated based on the current candle.
The first Moving Average will be for the last 50 candles, the second one for the last 100 candles, the third one is for the last 150 candles and the last one calculates the last 200 candles.
By using “ArraySetAsSeries” we are going to sort each of our arrays from the current candle downwards; this is array 3 and this is array 4, I simply used “copy and paste” that saves some typing work.
Now we fill each of our arrays by using “CopyBuffer”.
According to the Moving Average definition (movingAverageDefinition) that we have created here each of our EMA’s has only 1 buffer, that’s buffer 0 and we want to fill our array from the current candle 0 for 3 candles and store the result in our Moving Average arrays (myMovingAverageArray) and now we can calculate our Moving Average values (myMovingAverageValue) by looking into candle 0 of our Moving Average arrays (myMovingAverageArray). These are the values and when you place your mouse over each of the lines at the end of the line you will see the current value for candle 0 and to calculate the sell signal we would check if “myMovingAverageValue1” is below “myMovingAverageValue2” and if “myMovingAverageValue2” is below “myMovingAverageValue3” and if “myMovingAverageValue3” is below “myMovingAverageValue4”. So, these 3 “if” statements stack our entry signals and only if all are true we would create a sell signal.
For the buy signal it’s almost exactly the same but in this case we are checking if the value for each of our Moving Averages is greater than the one on the right side. If this is the case we create a buy signal.
In the last step we use the ”Comment” function to output our signal on the screen followed by each of the values that we have calculated.
When you are done please click on the “Compile” button here, that should work without any errors and without any warnings and if this is the case please click on the little button here or press F4 in your Metaeditor to go back to Metatrader.
In Metatrader you want to select the file: “SimpleSignalStacking.ex5”, pick a currency pair and mark the visualization option here.
Now you can click on “Start” and here’s our little Expert Advisor at work, its producing a pretty stable trend signal and now you know how to calculate each of the four Exponential Moving Averages here and how to output buy or sell signals and you have coded it yourself with a few lines of MQL5 code.


SimpleSignalStacking.txt – Downloaded 390 times – 3.36 KB