MQL5 Tutorial

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

MQL5 TutorialBy Raimund Bauer