MQL5 Tutorial

MQL5 TUTORIAL BASICS – 74 SIMPLE ENVELOPES INDICATOR


Listen Later

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 ...
...more
View all episodesView all episodes
Download on the App Store

MQL5 TutorialBy Raimund Bauer