MQL5 TUTORIAL BASICS – 74 SIMPLE ENVELOPES INDICATOR

video

In this video we are going to create an expert advisor for this indicator here, this is the envelopes indicator so let’s find out how to do that with mql5.
To get started please click on the little icon here or press F4 on your keyboard. Now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file, simple envelopes, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We start with an include statement to include the file trade dot mqh, this one comes with mql5 and it will give us the ability to use simplified trading functions, now let’s create an instance of the class ctrade, we will call it trade and we are going to use it later on to open positions.
Inside of the ontick function we start by calculating the ask price that is done by using symbol info double for the current symbol on the chart and the second parameter is symbol underscore ask – all in capital letters – and I use normalize double and underscore digits to automatically calculate the right number of digits behind the dot depending on the currency pair that might be three or five digits.
Let’s repeat that for the bid price, everything is pretty similar but of course, we need to change the name for the variable and this time we are going to use symbol underscore bid.
Now let’s create a string variable called signal, this will hold the signal but we are going to calculate that later on so we don’t assign a value here.
And to get some price information we use mql rates, this is the structure that stores the information about prices, volumes and spread, it will create an array that we now need to sort from the current candle downwards, we can do that by using array set as series and with copy rates, we can now copy data for the current symbol on the chart and the currently selected period, we start with candle zero and we want to copy the price information for three candles and store it in our array but this is the old way to do it, let’s use underscore symbol and underscore period if you mark that and press F1 you will see that you can exchange the two functions here.
Okay! Now we need two more arrays, one is for the upper band and one for the lower band. Those two arrays also need to be sorted by using array set as series and now we can define the envelopes that is done by using ienvelopes for the current symbol on a chart and the currently selected period on that chart, we do that for fourteen candles, we don’t use a shift value here, the calculation method is mode underscore sma that stands for simple moving average, I would like to have the results based on the close price and this last value here is also what you will see if you click on insert, indicators, trend, envelopes, this is the deviation value and that’s what we use here.
Now we can fill our arrays with data, we use copy buffer to fill the upper band and the lower band array according to the envelopes definition that we have created here.
The upper band is for buffer zero, the lower band is for buffer one and we do it from the current candle zero for three candles and now we can calculate the values all we need to do is to look into the value for candle zero in the upper or the lower band array and I also use normalize double for six digits here because that’s what you get when you point your mouse at one of the two bands, it has six digits behind the dot.
Now let’s define the entry conditions!
If the close price for candle one is below the lower band value we would consider that to be a buy signal so we assign the word buy to our signal.
In the other case if the close price for candle one is above the upper band value we would consider that to be a sell signal and now we assign the word sell to our signal and if our signal equals sell and positions total is below one that would mean we have no open positions but a sell signal and now we want to use trade dot sell to sell ten micro lot.
Otherwise, if we have a buy signal and no open positions we would use trade dot buy and buy ten micro lot.
Finally, we use the comment statement to create a chart output that will show us the sentence, the current signal is, followed by the calculated signal.
So far so good!
If this was too fast for you or if you have no idea what all the code here does maybe you want to watch one of the other videos in this basic video series or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button or press F7, you shouldn’t get any errors here and if this is the case you can click on a little button here or press F4 to go back to Metatrader.
And inside of Metatrader, we click on view, strategy tester or press control and r, please pick the new file, simple envelopes dot ex5, mark the option for the visible mode and start a test.
Here we are, but we don’t see the envelopes and to change that we use a little trick, we just include the envelopes indicator on a simple chart, right-click, select templates, safe template and save the template as tester dot tpl, you can replace the current one, now let’s restart the test and this time we have an indicator and we already have the first trade, so our little expert advisor is working and in this little video you have learned how to code the envelopes indicator and you have done it yourself with a few lines of mql5 code.