In this video, we are going to create a buy trailing stop that is based on 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 buy trailing. Stop, 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 and we’ll include the file Trade.mqh. This one comes with MQL5 and we can create an instance of the included Class Ctrade. That instance will be called trade and we are going to use it to open a position later on. Inside of the ontick function, we start with a static variable. This is a double variable for the last value of the Stop Loss.
Afterwards, we need to calculate the Ask price and the Bid price. That can be done by using Symbolinfodouble for the current symbol on the chart, we use either symbol underscore ask or we use symbol underscore bid and with normalize double and underscore digits, we make sure that we calculate the right number of digits behind the dot, because there are currency pairs with three digits and others have five digits. For our Stop Loss, we need to open a test position. That wouldn’t be done on a real account.
But if we have no open positions and that would be true of PositionsTotal is below one, we want to open a test position on our demo account. So we use trade.Buy to buy 10 Microlot and we want to reset the last value for the Stop Loss. Because with the new position we need to set a new Stop Loss value. Now let’s continue with two arrays, one for the upper band and one for the lower end of the envelopes indicator. With arrays set as series we sort the upper and lower band array from the current candle downwards. And now we can use the included Ienvelopes function for the current symbol on the chart and the currently selected period on that chart.
We want to calculate the values for 14 candles, the next value here is a shift value. We don’t need that. So we set it to zero. We use MODE SMA for simple moving average. The result should be based on the close price and this last value here is what you see when you open a new chart and click on insert, indicators, trend, Envelopes. It’s the so-called deviation. We are going with the standard values here. Click on OK.
Here is the indicator, and now we want to right click into the chart, save the template as tester.tpl, you can override the current value because the Tester.tpl template is what we will see in the strategy tester. Now, let’s add some data. We use Copybuffer according to the envelopes definition that we have created here. To fill our array for the buffer zero and the buffer one. Buffer zero is for the upper Band and buffer one is for the lower band.
We want to copy data from the current Candle zero for three candles and store those values in our arrays. To calculate the upper band array for the current candle, we only need to look at Candle zero in our upper band array. I use Normalize double and six to cut it to six digits behind the dot. And we repeat that for the lower band value. Let’s repeat that, everything is equal, but this time we are doing it for candle one. That’s the candle before the current candle.
And of course, the variable names are different. This time, we use my last upper band value and my last lower band value. So let’s check for the conditions for the buy trailing stop. The first condition would be that the last lower band value is below the current Bid price. The second condition is we want to check if the last lower band value is bigger than the last stop envelopes value. If both conditions are true, we want to call a function that is called check envelopes buy trailing stop and we want to pass the last lower bands value as a parameter here.
This function doesn’t exist so far, so we need to create it in a few minutes. And once we have adjusted the Stop Loss, we assign the last lower band value to the variable last stop envelopes value. That’s it for the Ontick function. Let’s continue with the trailing stop function. It will get the last lower band value as a parameter. Now we need to use a for loop to go through all the open positions. We use position get symbol for the current counter value to get the symbol.
And if the current currency pair on the chart equals our position symbol, we are going to continue. We need to get the position ticket. That can be done by using position get integer and the parameter here is position underscore ticket. Let’s also get the current Stop Loss for this position. That can be done by using position get double and the parameter here is buy position underscore SL. And if the current Stop Loss is below the last lower band value, we are going to use trade position modify to move the Stop Loss to the last lower band value that we have calculated.
Now let’s end the loops here and the function 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, you maybe want to watch one of the other videos in the basic video series. Or maybe even the Premium Course on our website might be interesting for you. That’s also where you can find offers to download more of those source codes here. But for now, please click on compile or press the F7 key on your keyboard.
I have one bracket that needs to be removed. Now let’s recompile. And this time we have no errors. And if that is the case, you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader we want to click on View, Strategy tester or Control and R. Please pick the new file Simple envelopes buy trailing Stop.ex5, mark the option for the visual mode here and start your test. Here we are.
We already have an open position and we have a working trailing stop. So our little Expert Advisor is working as expected. And in this little video you have learned how to create a buy trailing stop that is triggered by the envelopes indicator and you have coded it yourself with a few lines of MQL5 code.