MQL5 TUTORIAL BASICS – 125 SIMPLE ATR SELL TRAILING STOP

video
play-rounded-fill

 

In this video we are going to create a sell trailing stop that is triggered by the Average True Range indicator. So let’s find out how to do that with MQL 5. To get started please click on the little button here or Press F4 on your keyboard. Now you should see the Meta Editor window until you want to click on File, new File, Expert Advisor from Template, continue. I will call this file Simple Average True Range Sell Stop.

 

Click on Continue, continue and finish. Now you can delete everything above the on tick function and let’s remove the two comment lines here. We start with an Include statement to include the file trade mqh. This one contains the class Ctrade and we are going to use an instance called Trade. Let’s set a basic variable for the stop loss points.

 

We also need to calculate the stop point value and we use a global declaration for the bid price. And inside of the Ontick function we want to have a string variable for our signal. Now we are going to calculate the bid price. That is done by using symbol info double. For the current symbol on the chart we use symbol underscore bid. And with normalize double and underscore digits we can automatically calculate the right number of digits behind the dot depending on the currency pair. Now let’s create a price array.

 

This is a double array and let’s define the Average True range here. That is done by using the IATR function that is included in MQL5. It uses a few parameters. The first one is for the current symbol on the chart, the second is for the selected period on that chart and we will calculate the result based on 14 candles. Now we use array set as series to sort our price array from the current candle downwards. And with copy buffer we fill our price array according to the definition that we have created here for buffer zero starting from the current candle zero and we want to copy the data for three candles and store them in the array.

 

Now we can get the Average True Range value for the current candle by looking at candle zero in our price array. I use Normalize Double and Five to cut off five digits. Because that is how we see the Average True range indicator is shown on the chart. I also want to declare a static variable for the old value because I want to know if the current value is below the old value and if that is the case, we want to set our signal to sell and now we use trade sell. And if the signal equals Sell and positions total is below one, we have no open positions but a sell signal and that’s when we use Trade sell to sell it 10 microlot. This is a test position.

 

We do it on a demo account because to trigger a sell trailing stop we need to have a position. And now we want to get the calculated stop point value by calling a user defined function called Check ATR Sell trailing stop. We pass the average true range value that we have calculated as a parameter. Now let’s add a chart output by using the comment statement. This will output the current signal, the average true range value, and the calculated stop point value. Afterwards we assign the average true range value to the old value for the next time. And that’s it for the main function. But this function doesn’t exist, so we need to create it right now.

 

Our user defined function will have the name Check ATR Sell trailing stop. It gets the average true range value as a parameter here. We use this little formula to calculate the stop point value by using the defined basic stop point value and we will add this multiplication here. Now we are able to calculate the stop loss price. That is the current bid price plus the calculated stop point value times the point value. With the for loop we go through all the open positions to get the symbol of the current position we use Position get symbol for the current counter value. And if the current symbol on the chart equals the position symbol we want to continue. So let’s get the position ticket by using position get integer and the parameters we use is position underscore ticket. To get the current stop loss we use Position get double and we use the parameter position underscore SL.

 

And if the current stop loss is greater than the calculated stop loss price or the current stop loss equals zero, that would be the case the very first time we run the Expert Advisor. That’s when we use Trade dot position modify for the current position ticket. To modify the calculated stoploss price. The last parameter would be the take profit value, but we want to leave that unchanged. All we need to do now is to close the loops, return the calculated stop point value to the main function, and close our function. That’s about it.

 

If this was too fast for you, or if you have no idea what all the code here does, you maybe want to watch one of the former basic videos, or maybe even the premium course might be interesting for you. If that is the case, you can find it on our website where we also have a link to the shop where you can download source codes. If you don’t want to type everything yourself, let’s remove the additional bracket here and click on the compile button or Press F 7. You shouldn’t get any errors here, and if that is the case, you can click on a little button here or Press F4 to go back to MetaTrader. And in MetaTrader we click on View, Strategy Tester or press Control and R. Please pick the new file simple Average true range sell stop ex5. Mark the option for the visible mode here and start your test. Here is one of our stop losses so let’s see if it is trailing. It actually does.

 

And in this little video you have learned how to create a trailing stop for the average true range indicator. And you have coded it yourself with a few lines of MGL5 code.