In this video we are going to create an expert advisor that is able to adjust a sell trailing stop according to the upper band of the envelopes indicator. So let’s find out how to do that with MQL5. To get started, please click on the little button here or Press F four on your keyboard. Now you should see the Metaeditor window and here you want to click on File, new File, Expert Advisor from Template. I will call this file Simple Envelopes Sell Trailing Stop. Click on continue, continue and finish.
Now you can delete everything above the Ontick function and let’s remove the two comment lines here. We start with an include statement to include the file trade.mqh. This file is part of MQL5 and it will help us to open a test position. To do that, we need to create an instance of the class Ctrade and that will be called trade. We need to define a static double variable called Last Stop envelopes value.
This will hold the last stop loss value for each time we call the function. We also need to calculate the ask price and the bid price. That can be done by using symbol info double for the current symbol on the chart. We either use symbol underscore ask or symbol underscore bid and with normalize double and underscore digits, we make sure that we automatically calculate the right number of digits behind the dot. To test a trailing stop, we need to open a test position.
Therefore, we check if positions total is below one. If that is true, we want to reset the last value for the stop loss and set it to zero and then we use Trade.sell to sell ten micro lot. You wouldn’t do that on a real account, but that’s what demo accounts are good for. Let’s continue and create two arrays for the upper and the lower band array. We use Array set as series for both arrays to sort them from the current candle downwards. And now we can use the included function Ienvelopes for the current symbol on the chart and the currently selected period on that chart to calculate the envelopes indicator for 14 candles. We don’t use a shift value here. The next parameter is mode underscore SMA, that stands for Simple Moving Average. The result should be calculated based on the close price and this last parameter here is for the deviation, that’s the standard value. If you pick an empty chart window and click on insert, indicators, trend, envelopes, you will also see this value as a deviation value.
Here are the 14 candles. Now please click on OK, rightclick into the chart. Select Templates, save template and save this one as tester.tpl. You can override the current value. Tester.tpl is the view that we will see when we start the strategy tester.
But back to coding. We continue by using copy buffer to copy data according to the envelopes definition that we have created here, either for buffer zero or for buffer one, for candle zero. That’s the current candle and we want to copy the values for three candles and store them in the upper or in the lower band array. It’s now possible to get the current value for candle zero. I have out commented it in this version, because I’m more interested in the values for the candle before. That’s candle one. To get the last upper band value or the last lower band value, I will look into our array at candle one and with normalize double and comma six, I make sure that the value is using six digits behind the dot. Because that’s also what the original envelopes indicator will show. Now we can call a custom function called CheckEnvelopesSelltrailingStop and pass the last upper band value as a parameter. This function doesn’t exist, so we need to create it in a few seconds. The last thing in the Ontick function is to assign the last upper band value to the last stop envelopes value here, because we need that the next time we call the function.
Now let’s actually create the check envelopes sell trailing stop.
It will get the last upper band value as a parameter here. The type is void because we don’t return any values. We use a for loop to go through all the open positions and we use position get symbol for the current counter value to get the position symbol, because we only want to continue if the current symbol on the chart and the position symbol are equal. If that is the case, we use position get integer and use position underscore ticket to get the current position ticket number. And with position get double, position underscore SL, we can get the current stop loss for the current position.
And now we want to check if the current stop loss for this position is bigger than the last upper band value, or if the current stop loss is zero.
If one of those cases is true, we use trade dot position modify for the current position ticket and adjust the stop loss value to the last upper band value here. The last parameter here is for the take profit. We leave that unchanged. Finally, we need to close the loops and the function here 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 of the basic series, or maybe even the Premium course on our website might be interesting for you.
We also offer some source codes, if you need more we also have a shop. But for now please click on the compile button or Press F seven 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, we want to click on View, Strategy tester, or press control and R.
Please pick the new file Simple Envelopes Sell Trailing Stop.ex5, mark the option for the visual mode and start your test. Here we are. Let’s zoom into the chart so we can see the sell trailing stop. It should move as soon as the blue band goes down.
That actually happened right now. So our little expert advisor is working as expected. And in this little video, you have learned how to create a sell trailing stop for the envelopes indicator and you have coded it yourself with a few lines of MQL5 code.