MQL5 TUTORIAL BASICS – 81 SIMPLE SMA BUY TRAILING STOP

video
play-sharp-fill

In this video, we are going to create an expert advisor that is able to create a a simple buy trailing stop that is based on a simple moving average value, so let’s find out how to do that with mql5.
To get started please click on a little icon here or press F4 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 sma buy trailing stop, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
First, we want to include the file trade dot mqh, this one comes with mql5 and provides simplified trading functions afterwards we create an instance of the class ctrade and call it trade.
Inside of the ontick function we start with a static double variable called last stop moving average value, we continue by calculating the ask price and the bid price that can be done by using normalize double, symbol info double for the current symbol on the chart and the second parameter is symbol underscore ask or underscore bid, with normalize double and underscore digits we make sure to calculate the right number of digits behind the dot.
To have something that we can use for the trailing stop we need to open a test position, so whenever positions total is smaller than one or in other words when we have no open position we use trade dot buy to buy ten micro lot. That will open a new position and that’s when we set the last stop moving average value to zero because for each position we need a new value.
Let’s go on and create an array for the moving average, now we want to sort the array from the current candle downwards that is done by using array set as series for our moving average array that we have created here.
Now let’s define the properties, we want to use the ima function for the current symbol on the chart and the currently selected period on that chart, we want to use it for five hundred candles starting from the current candle zero, we don’t use a shift value and we use mode underscore sma because this stands for simple moving average and the calculation should be done based on the close price.
Afterwards, we use copy buffer to fill our moving average array according to the definition that we have created here for buffer zero, we start with candle zero – that’s the current candle – and we copy the values for three candles and store them in the array.
And to get the value that we need we simply have a look at the value for candle one in our moving average array and we assign the value to stop moving average value.
Now that we have done that we can check if the stop moving average value is below the current bid price and the second condition will be if the stop moving average value is bigger than the last stop moving average value and if that is the case we want to call a function that is called check sma buy trailing stop, we will pass the ask price and the stop moving average array value as parameters here and afterwards we want to assign the current value for the next function call, so we store the value for stop moving average value in the variable called last stop moving average value. Please remember this was a static variable so it will keep the values inside of this function, well this function doesn’t exist so far, so we need to create it now, the name of the function will be check sma buy trailing stop, we use void because we don’t use a return value here and we pass the ask price and the stop moving average value as parameters.
Now let’s use a for loop to go through all the open positions, we use position get symbol to get the symbol for the current position because we need to check if the current symbol on the chart is equal to the position symbol and if this is true we get the position ticket by using the function position get integer position underscore ticket.
To get the current stop loss we use position get double and the parameter is position underscore sl and if the current stop loss is below the stop moving average value we want to change it that is done by using trade position modify for the current position ticket and we pass the stop moving average value as the second parameter. The third parameter would be used when we would like to change the take profit value but this is not the case.
Finally, please close all the loops and also close the function that we have created and this is it.
Well, if this was too fast for you or if you have no idea what all the code here does maybe you want to watch one of the other videos in this basic video series or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button here or press F7 on your keyboard, you shouldn’t get any errors and if this is the case you can click on the 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 sma buy trailing stop dot ex5, mark the option for the visual mode and start a test.
Here we are! We need to zoom into the chart because that’s when we see that the moving average is far below the current price, we also see that the stop loss is modified, if you click on the journal tab you will see that it will not be modified each time, you could change all the conditions to avoid the errors but for this simple example it is good enough and in this little video you have learned how to create an expert advisor that is able to create a buy trailing stop that is based on a simple moving average and you have coded it yourself with a few lines of mql5 code.