In this video we are going to create an expert advisor that is able to calculate this indicator, it’s the envelopes indicator so let’s find out how to do that with mql4.
To get started please click on a 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 indicator, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
First, we want to create a string variable called signal, we don’t assign any values now because we are going to calculate that later.
And now we are going to use the included function ienvelopes to calculate the lower band. This indicator has two bands, a lower band and a higher band so we need to use two different buffers here and for the lower band we use the current symbol on a chart and the currently selected period on that chart, we want to calculate the indicator based on fourteen candles, we use mode sma for simple moving average, this zero here is the shift value, we don’t want to use a shift value, the result should be calculated based on the close price and this value here is the so-called deviation value, if you right-click on the properties for the indicator you will see that you can change the value here. The bigger the value the fewer signals you will get and I decided to use zero point zero five in this case.
This parameter here is for the buffer. So what is a buffer?
If you mark ienvelopes and press F1 you will see that the mode parameter here can be used for different things, two is for the lower and one is for the upper band so let’s actually replace that with mode lower, here we are and the last parameter is for the current candle.
To calculate the upper band we do the same, this is all pretty much the same code but this time we are going to use mode upper and if the close price for candle one is below the lower band we would consider that to be a buy signal so now we assign the word buy to our signal.
Otherwise, if the close price for candle one is above the upper band we would consider that to be a sell signal and now we would assign the word sell to our signal.
And if our signal equals buy and the return value for orders total is zero – that would mean that we have no open orders and a buy signal – and in that case, we use order send to buy ten micro lot.
Otherwise, if the signal equals sell and we have no open orders we would use order send to sell ten micro lot.
Finally, we use the comment statement to output the values for the upper band, the lower band and the signal and that’s about it.
Well, 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 please click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we need to change the template otherwise we wouldn’t see the indicator so let’s open a new chart window, click on insert, indicators, trend, envelopes, pick a period for fourteen candles, the ma method should be simple, apply to the close price and the deviation value in this example is zero point zero five, of course, you could change that but please keep in mind that you also have to change it in the source code. Now let’s click on okay, here is the indicator and now we want to right-click, select template, save template and save it as tester dot tpl, you can overwrite the current one because tester dot tpl is what you will see in the next strategy test.
If you don’t see the strategy tester please click on view, strategy tester or press control and r, please pick the new file, simple envelopes indicator dot ex4, mark the option for the visual mode here and start a test.
Here we are!
We already see the first buy trade because candle one was below the lower line here and that’s because of this gap so our little expert advisor works as expected and in this little video you have learned how to create an expert advisor that is able to calculate the envelopes indicator and you have done it yourself with a few lines of mql4 code.