In this video we are going to code the Envelopes indicator for our Expert Advisor entry signal. That is the red and the blue line you see here on the candle chart. So let’s find out how to do that with MQL5.
The Envelopes indicator is drawn on the candle chart.
It has an upper band and a lower band.
To use it, we create a separate MQ5 file inside of the directory where the other files of the Platin System are located.
The name of the file is CheckEntry_Envelopes.mq5 and it contains a single function that is called CheckEntry.
We use it to calculate the entry signal for our trading system.
First we create a signal but we don’t assign a start value.
The type of the signal is either buy or sell, so we use a string variable.
Of course we also need to create an array.
That can be done with the MQLRates command.
To sort our array from the current candle downwards, we use ArraySetAsSeries.
And afterwards we can fill our array with data.
That is done by using the CopyRates command.
The first parameter is the current symbol on the chart.
The second parameter is for the period that is selected on that chart.
The third parameter is for the candle where we want to start, in our case it is the current candle 0.
The fourth parameter is for the number of candles that we want to store the price data for.
We pick 3 candles here.
And in the last parameter we can define the target array, which is our price information array.
Let’s create two more arrays.
One is for the upper band, so we call it upper band array.
The other one is for the lower band, that one is called lower band array.
Both arrays also need to be sorted downwards by using ArraySetAsSeries.
The next step is to create the definition for our Envelopes indicator. We use the builtin function iEnvelopes that comes with MQL5 and pass the following parameters.
Parameter 1 is the current chart symbol.
Parameter 2 is the period on that chart.
Parameter 3 is the number of candles for the calculation, we use 14 here. That is also the default value when you drag the envelopes indicator on the chart.
The next parameter is for a shift value. We don’t need to change that, so we use 0 for parameter 4.
Parameter 5 defines the moving average values for the calculation. MODE_SMA stands for simple moving average. That is what we want to use here.
Parameter 6 is used to define if we want to calculate the values based on the close prices which is what we want to do.
And the last parameter is a deviation value. The default is 0.100, so we use that.
Now we use copy buffer to fill our arrays.
We do that according to the definition that we have created above.
Either for buffer 0, that is for the upper band or for buffer 1, that is for the lower band.
We start with candle 0 and copy the values for 3 candles into our arrays.
Now we can calculate the values for both bands by looking at the value of candle 0 in our array.
We use NormalizeDouble and the value 6 to format the output for 6 digits behind the dot.
So let’s calculate the signal.
If the close price for candle 1 is below the lower band value, we consider that to be a buy signal, so we assign the word buy to our signal if that is true.
Otherwise, when the close price for candle 1 is above the upper Band value, that would be a sell signal. In that case we assign the word sell to our signal.
And if the close price for candle 1 is below the upper and above the lower envelopes band, we want to reset the signal and remove the buy or sell signal from before.
Finally we can use the return statement to give back the entry signal to our main module.
Please save your entry file, you don’t need to compile it as it will be compiled with your main file.
There you can use two slashes to outcomment other entry signals and use the include function to activate the new file CheckEntry_Envelopes.mq5 to use the envelopes indicator in your system.
Afterwards please click on Compile or press F7 to compile your new version.
You should not get any errors.
If you do, please check out the content from the other videos before or consider the premium course on our website.
Okay, by now you should see a working version of the Envelopes indicator right on your chart. You can use it with the Platin System, the Robot Trading System – and of course – you can use it in your own system.
That’s it for now. Thank you for your attention and I will see you in the next video.