In this video, we are going to create an Expert Advisor that is able to create a buy trailing stop that is based on 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 buy trailing stop.
Click on continue, continue and finish. Now you can delete everything above the ontick function. And let’s also remove the two comment lines here. We start by creating a static variable. It’s a double variable and we are going to use it to save the value for the last Stop Loss and to use a trailing stop, we obviously need to create a test buy position. You wouldn’t do that on a real account, but on a demo account we will check if OrdersTotal equals zero, that would mean we have no open position.
And in that case we use Ordersend to open a Buy position for 10 Microlot. Of course, we need to reset the value for the last Stop Loss, because with a new position we need a new Stop Loss level. Let’s continue and calculate the values for the lower band and for the upper band. That is done by using the included function Ienvelopes for the current symbol on the chart and the currently selected period on that chart. We want to calculate values for fourteen candles.
We use ModeUnderscoreSMA for simple moving average, no shift value. The result should be calculated based on the close price. And if you wonder what this value is, well, if you open a new chart window and click on insert indicators, trend, envelopes, the standard value is zero point ten percent. So let’s click on OK, let’s use that value in our indicator. This parameter here is for buffer two or buffer one. If you open the reference, you will see that the ienvelopes function uses mode upper or mode lower, or the values would be one for the upper line and two for the lower line.
That’s actually what we are using here. The last value here is for the candle number. We are going to use candle one. And if the last stop envelopes value is below the lower band, we want to call a function that is called check envelopes buy trailing stop. And we want to pass the lower band value here as a parameter. At the end of the ontick function, we want to save the lower band value as last stop envelopes value for the next time we call the function.
But this function doesn’t exist. So let’s create that. The return type is void, the name as check envelopes buy trailing stop. We are passing the lower band value here, afterwards we are going to use a For loop to go through all the open orders and for each order we want to use orderselect to select the order, then we want to check if the current order symbol equals the symbol on the chart. On top of that, we want to check if the order type equals OP buy.
So it’s a buy trade. And if all that is true, we also want to know of the order Stop Loss is below the lower band. If all those conditions are true, we use order modify to modify the current order and adjust the Stop Loss to the value for the lower band. Finally, please close all the open loops and functions 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 the 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 on your keyboard, you shouldn’t get any errors here. 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, you want to click on insert, indicators, trend, envelopes. Please go with the default values here. Click on OK. Now you should see the indicator on the chart. Maybe you need to zoom in or zoom out.
Now right click onto the chart, select template, save template and save it as tester.tpl. You can override the current value, because tester.tpl is what we will see in the strategy tester Now please click on View, Strategy tester or press Control and R. Please pick the new file simple envelopes buy trailing stop .EX4, mark the option for the Visual mode here and start your test. Here we are. The Expert Advisor is running. So let’s speed that up a little bit.
And as soon as we see that the lower band is rising, we should see that the trailing stop will go up. So let’s see if that happens. Here is a little movement, our Expert Advisor is working as expected, and in this little video, you have learned how to create a buy trailing stop that is based on the envelopes indicator and you have coded it yourself with a few lines of MQL4 code.