MQL5 Tutorial

MQL5 TUTORIAL BASICS – 115 SIMPLE ENVELOPESBUY TRAILING STOP


Listen Later

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

MQL5 TutorialBy Raimund Bauer