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.
Well if this was too fast for you, or if you have no idea what all the code here means, 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 source codes like this one in the shop. By the way, this once again was a suggestion of a Premium Course member. So if you are a Premium Course member and have an idea for a video like this one, just let me know. For now, please click on the compile button or Press F seven.
I have an error because I have an additional bracket here. So let’s remove that and Press F seven. Yes, I messed up the brackets, here is one missing. So let’s add that and Press F seven. And this time it worked. And if that is the case you can click on a little button here or Press F four to go back to Meta Trader. And in Meta Trader please pick an empty chart. Click on Insert, Indicators, Average True Range , we will keep the default values for 14 candles.
Now let’s click on okay, here is the Oscillator. Now right click into the chart, select Template, save Templates and save it as Tester TPL. You can replace the current one because Tester TPL is what we will see in the Strategy tester. So let’s click on View, Strategy Tester or press Control and R. Here we want to pick the new file simple average true range buystop ex four. Please Mark the option for the visual mode here and start your test. This is the first trailing stop, we see that it is actually working.
The dynamic calculation does its job. So our little expert advisor works as designed and in this little video you have learned how to create a buy trailing stop that is calculated based on the value of the average true range indicator. And you have coded it yourself with a few lines of MQL 4 code.