In this video we are going to create an Expert Advisor that is able to use the Average True Range indicator for a sell trailing stop. So let’s find out how to do that with MQL4. To get started please click on a little button here or Press F4 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 Sell 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 want to have a basic stop point value for 100 points. So let’s define that first. And for the trailing stop we need a calculated stop point value. These are global variables so they will be available in all the functions. Inside of the Ontick function, we first want to create a string variable for the signal, but we don’t assign a value here because we are going to calculate that now. Let’s start with the included IATR function that comes with MQL4 . To calculate the average true range value it uses a few parameters. The first one is for the current symbol on the chart, the second one is for the currently selected period on the chart. We want to calculate the result based on 14 candles. This last parameter here would be a shift value. We don’t need that, so we set it to zero. Let’s create a static double variable for the old value. Because we want to be able to find out if the current average true range value is below the old value or if the old value is zero. That would be the case the very first time we run this expert advisor. And if one of the conditions is true, we want to set our signal to sell. And if we have a sell signal and the return value for orders total is zero, that would mean we have no open orders and that’s when we use order send to open a sell order for ten microlot. We would do this on a demo account, because we need to have an open position to check if the trailing stop is working. Because now we can calculate the stop point value by calling a user defined function with the name Check ATR Sell trailing stop. We will pass the average true range value. And this function doesn’t exist so far, so we need to create it in a few seconds. But before we do that, we want to use the comment function to create an output on the chart for the current signal, the average true range value, and the calculated stop point value. Afterwards we want to assign the current average true range value to the old value. And that’s it for the main function. Now we need to create this one. Our user defined function is called Check ATR Sell Trailing Stop. We pass the average true range value here. Our calculated stop point value will be the basic stop point value and we will add the result of this little calculation here. Now we can calculate the stop loss price and that will be the bid price. And we will add the calculated stop point value times the point value. Now we use a for loop to go through all the open orders. We use Order select for the current counter value and select the position for the trade. Afterwards we want to check if the current order symbol and the symbol on the chart are the same. And of course we want to know if the current order type is a sell order. That would be the case when it is equal to Op_Sell. And if the order stop loss for the current order is higher than the calculated stop loss price, that’s when we want to use Order modify for the current order ticket and the current order open price to set the stop loss to the calculated stop loss price. The order take profit, the expiration and the color will remain unchanged. Now we need to close the loops. This here is an if loop. You can see that the brackets are highlighted. So let’s end the if loop, the for loop, return the calculated stop loss value to the main function . And this is the end of our user defined function.