MQL5 TUTORIAL BASICS – 122 SIMPLE AVERAGE TRUE RANGE BUY STOP

video
play-rounded-fill

 

In this video we are going to use the average true range indicator to open buy trades and to set a trailing stop value for buy trades. So let’s find out how to do that with MQL 5. To get started please click on a little icon 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.

 

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 let’s also remove the two comment lines here. We start with an include statement to include the file trade.Mqh.

 

This will allow us to use the class Ctrade. We are using an instance of Ctrade called Trade here. Let’s define a few global variables. Global variables are defined outside of other functions. They are accessible for each of the functions.

 

The first one is an int variable for the basic stop point value, we will start with 100 points. The second one will be the calculated stop point value. The third one is for the ask price. Inside of the on tick function we start by creating a string variable for the signal. We don’t assign a value here because that’s what we are going to calculate later on.

 

Let’s continue and calculate the Ask price. That is done by using symbol info double for the current symbol on the chart. We use symbol underscore Ask, all in capital letters here and with normalize double and underscore digits, we make sure to calculate the right number of digits behind the dot. Now we need to create a price array, that is a double array so it can hold floating type values.

 

We are going to use the included IATR function to define the average true range parameters here, for the current symbol on the chart and the currently selected period on the chart. We want to calculate values for 14 candles to sort the array from the current candle downwards. We can use the array set as series function for our price array. With copy buffer we can fill our price array with data according to the definition that we have created here, we are going to copy values for buffer zero starting from the current candle zero, for three candles and store the result in our price array.

 

That makes it possible to calculate the average true range value for the current candle.

 

We just have to look at the value of candle zero in our price array, and I use normalize double and five again to get five digits behind the dot, like it is shown here. Let’s add another static double variable. Static means that the value for this variable will be inside of the computer memory for as long as our computer program is running. Now we can check if we have a buy signal. That would be the case if the current average true range value is bigger than the old value.

 

If that is the case, we will set our signal to buy and that’s when we use trade buy to buy ten microlot. We check if the signal equals buy and if positions total is below one, that would mean we have no open trades. And that is when we use trade buy to buy ten micro lot. This is a test position. You wouldn’t do that on a real account.

 

And to calculate the stop point value, we are now going to call a function that is called Check ATR Buy trailing stop and pass the average true range value that we have calculated as a parameter. To see what’s going on, I would like to have a command output that will show us the signal, the average true range value and the calculated stop point value on our chart. It would be better to read when we do it this way. It makes no difference for the compiler, but it’s better to read for humans.

 

After all the calculation is done.

 

We will now assign the average true range value from now as old value for the next time we call the function that’s it with the main function, but we need to create this one. It is a user defined function and we will call it check atrby trailing stop. It will take this 1 /meter for the average true range value. And here we want to calculate the stop point value by just adding the basic stop point value to the result of this multiplication. Here afterwards we can calculate the stop loss price that will be the ask price minus the calculated stop point value multiplied with the point value for the current currency pair.

 

Now we go through all the open positions by using a for loop for each position. We need to calculate the position symbol by using position get symbol for the current counter value. And if the position symbol and the symbol on the chart are equal, we use Position get integer for the position ticket to get the current position ticket. Then we use Position get double for position underscore SL to calculate the current stop loss for the position. And if the current stop loss is below the calculated stop loss price, we will use Trade dot position modify for the current position ticket and modify the stop loss with the calculated stop loss price.

 

Finally, we need to close the loops and return the calculated stop point value to our main module by using the return function and that’s about it. If this was too fast for you or if you have no idea what’s going on, maybe you want to watch one of the other basic videos first, or maybe even the premium course on our website might be interesting for you. That’s also where you can get source codes like this one in the shop. This actually was once again a suggestion of a premium course member.

 

And if you are a premium course member and have ideas for videos like this one, just let me know for now.

 

Please click on the compile button or Press F seven on your keyboard. I don’t have any errors here, and if that is the case, you can click on a little button here or Press F four to go back to Meter Trader and in MetaTrader, you want to click on View Strategy Tester or Press Control and R. Please pick the new file. Simple Average True Range Buy Stop dot ex five Mark the option for the visual mode here and start your test. Here we are this is our first position, and when the average true range value is rising, we should see that the stop loss value here will be changed.

 

The trailing stop is working, so our little expert advisor works as designed. And in this little video you have learned how to create an average true range by trailing stop that is calculated based on the ATR value here and you have coded it yourself with a few lines of MQL five code.