MQL5 Tutorial – Simple Envelopes Expert Advisor with MQL5

In this video we are going to create an Expert Advisor for the Envelopes Indicator.
The Envelopes Indicator simply draws two lines on your chart and when a candle closes above the upper line, this generates an entry signal.
We now expect the price to hit this line. It took a while but actually here it happened, the indicator is similar to the Bollenger band indicator.
If you place your mouse cursor over the line here you will see a value, in our case it is 117.4636 that matches with the calculated value here.
And the value of the lower band as 117.2289, and that also matches.
According to Wikipedia some traders think that when the price breaks through the upper line it is a sign for a strong trend and other traders consider this to be a signal for an overbought price, and they expect the price to go down.
It’s up to you how you use it but if the price is below the lower band we want to output the text lower signal here.
And if the last candle closed above the upper band, we want to output the text upper signal on the chart.
Now how can we create an Expert Advisor for the Envelopes Indicator?
First of all, please click on this little button here in MetaTrader or press the F4 key, and that will bring up the MetaEditor, here we select File, New, Expert Advisor from template, Continue, and we will give it the name Simple Envelopes.
Please click on Continue, Continue, Finish, and now remove everything above the OnTick function.
We will also delete the two comment lines here and the first thing we need to create is an array.
We use the MQLrates function and we will give it the name price information and now we need to sort the price information from the current candle to the oldest candle.
That is done with arraysetasseries, for the array we have created here.
And now we want to fill our array with price data. This is done by using copy rates; it will copy the prices for the current symbol on the chart and the selected period on the chart from the current candle for all the available candles on the chart and the prices will be stored in the price information array.
And now we need two more arrays. The first one will hold the prices for the upper band and the second one for the lower band.
Because we have two lines here, this is the upper and this is the lower envelope band, so the prices need to be sorted for both of our arrays and now we define the envelopes by using the iEnvelopes function that is built-in in MQL 5 for the current symbol and the current period.
We will use fourteen candles, no shift to the right or to the left, we will calculate based on a simple moving average for the close price of the candles on the chart and we will use a deviation 0.100, because that is what you will see when you click on the properties, here you will find the same values.
After we have defined the envelopes, we will use copybuffer to fill the two arrays according to the envelopes definition we have created here.
This zero is for the upper band, this one is for the lower band and for both bands we will copy from the current candle for three candles downwards.
And in this step we will calculate the EA value for the upper band value of the current candle and for the lower band value of the current candle.
And now we need that chart output so if the last candle of the price information, remember that is our prices array here, so if the last candle closed above the upper band value, we want to output the text upper signal.
But if the last candle closed below the lower band, we want the text lower signal on our chart.
And if the price is between the two bands, we will output the text for both bands and the calculated values.
So let’s compile the Expert Advisor, and you should have no errors and no warnings.
So let’s click the little button here to go back to MetaTrader. In Meta Trader we click on View, Strategy Tester, and you should select the Simple Envelope.ex5 file, mark the visualization option here and click on Start.
I don’t see any signals here so I must have missed something. Oh yes of course I only want to create this output if the close of the last candle is below the upper band value and above the lower band value.
So let’s recompile the code, and this time it works. It says lower signal when it’s below and it says upper signal when it’s above the upper band.
Okay, now you know how to create an Expert Advisor for the Envelopes Indicator and you have done it by creating this little Expert Advisor here in MQL 5.

Download “MQL5 Tutorial - Simple Envelopes Expert Advisor” SimpleEnvelopes.txt – Downloaded 90 times – 1 KB