MQL4 TUTORIAL

MQL4 TUTORIAL BASICS – 122 SIMPLE AVERAGE TRUE RANGE BUY STOP


Listen Later

In this video we are going to use the Average true range indicator to create buy trailing stops for buy trades. So let’s find out how to do that with MQL 4. To get started please click on on a little icon here or Press F Four on your keyboard. Now you should see the Meta Editor window and here you want to click on File, new File, Expert Advisor from Template. Continue. I will call this file Simple Average True Range Buy Stop.   Click on Continue, continue and finish. Now you can delete everything above the on tick function and we want to remove the two comment lines here. We start by creating two global variables. The first one is for the basic stop point value. We will start with 100 points.   The second one is the calculated Stop point value. This is the one we are going to use for the trailing stop. Inside the on tick function we need to create a string variable for the signal. We don’t assign a value here because we are going to calculate that now. To do that, we use the included IATR function that comes with MQL 4.   It needs a few parameters. The first parameter is for the current symbol on the chart. The second one is for the currently selected period on that chart. We want to calculate the value for 14 candles. We don’t use a shift value, so we set the last parameter to zero.   The next variable is a static variable. Static is a modifier that will keep the variable available for as long as the program is running. The name of this variable is old value and we are going to compare it with the current one, because if the average true range value is above the old value, that would be a buy signal and that is when we assign the word buy to our signal. So if our signal equals buy and orders total equals zero, that would mean we have no open orders and a buy signal. And that’s when we are going to use order send to open a buy trade for ten microlot.   This of course is just a demo account and a test position. You wouldn’t do that on a real account, but now we are going to call this function Check ATR Buytrailing stop and to check stop loss we need a position. That function doesn’t exist so far, so we need to create it now. We will pass the average true range value as a parameter. And the return value will be assigned to this variable Calculated Stop Point value. But before we need to code it, we want to create a comment statement on the chart to output the current signal, the average true range value, and the calculated stop point value.   And at the end of the main function we assign the current average true range value to the variable called old value, so we can use it the next time we call the function and that’s it for the main function. So let’s create this one. Our user defined function will be called Check ATR Buy trailing stop and we will pass a parameter. This is a double variable called Average True Range value. The calculated stop point value will be the basic stop point value. And we use the average true range value and multiply it with 100,000 to add it to the basic value.   Now we have the calculated points. So let’s get the actual calculated stop loss price and to calculate this one we use the ask price and subtract the calculated stop point value times the point value. Now we can use a for loop to go through all the open positions. We use Order Select to select each trade that we have. Then we want to know if the current order symbol equals the symbol on the chart.   If that is true, we need to know if the current order type equals Op Buy. So it is a buy trade and if the current value for the order stop loss is below the calculated stoploss price. That is when we use order modify for the current order ticket and the current order open price, we want to change the stop loss value to the calculated stoploss price. The order take profit, the expiration and the color will remain unchanged. Finally, we need to close the loops and return the calculated stop point value to the main module and that’s about it.
...more
View all episodesView all episodes
Download on the App Store

MQL4 TUTORIALBy MQL4 TUTORIAL