MQL5 TUTORIAL BASICS – 82 SIMPLE SMA SELL TRAILING STOP

video
play-sharp-fill

In this video we are going to create an expat advisor for a simple moving average sell trailing stop, 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 sell 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 it provides trading functions, it also contains the class ctrade, so we are going to create an instance of that class called trade.
We start by creating a static variable for the last stop moving average value, now we want to calculate the ask price and the bid price that is done by using symbol info double for the current symbol on the chart, we use either symbol ask or symbol bid and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot.
To use a stop loss we need to open a test position, we are going to use trade dot sell to do that and the condition is whenever positions total is below one – that means we have no open positions – and that’s when we want to sell ten micro lot.
We also want to set the last stop moving average value to a very high value, actually, you could also set it to zero but you would need to add another condition because usually, we are going to check if the current value is lower than the last stop moving average value and that one will never be below zero so let’s go with the high value for now.
In the next step, we are going to create an array for the moving average. With array set as series, we are going to sort the array from the current candle downwards and now we can use the integrated ima function for the current symbol on the chart and the currently selected period on that chart to create a simple moving average for five hundred candles, we don’t use a shift value here, we are going to use mode underscore sma as it stands for simple moving average and we want to calculate the results based on the close price.
Let’s continue with copy buffer, this one is used to fill the array according to the moving average definition that we have created here, we will grab the values for buffer zero that’s the moving average signal line here, we will start with candle zero – that’s the current candle – and we will copy the values for three candles and store them in the array. And now that we have done that we can calculate the stop moving average value by looking into candle one of our moving average array.
The first condition we want to check to move a sell stop loss is if the current stop moving average array is above the ask price otherwise we would not be able to change it.
We also want to know if the current stop moving average value is below the last stop moving average value, and if this is true we are going to use a function called check sma sell trailing stop and pass the bid price and the stop moving average value as a parameter. This function doesn’t exist so far, so we need to create it now and please don’t forget to set the last stop moving average value and assign the current stop moving average value because the current one will be the last one the next time we call this function.
Our trailing stop function will use void because we don’t need to return anything, the name is check sma sell trailing stop and we pass the bid price and the stop moving average value as parameters here.
Now we use a for loop to go through all the open positions, we will count down until there are no other positions left, afterwards we use position get symbol for the current counter of the for loop to get the currency pair symbol for the current position and only if the symbol on the chart matches the currency pair symbol we are going to continue. With position get integer and parameter position underscore ticket, we can now get the current position ticket and to find out the current stop loss we are using position get double and pass the parameter position underscore sl that will allow us to check if the current stop loss is above the stop moving average value and we also need to find out if the current stop loss equals zero, this is the case when you don’t set a stop loss when you open a position, so we also need to check this one and afterwards, we use trade position modify for the current position ticket to set the desired stop moving average value, the last parameter here is for the take profit, we won’t change that one.
Finally, we need to close the if loop, the for loop and the function and that’s about it.
Well, if you have no idea what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in this basic video series first or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button, I need to set the toolbar to visible, here is the compile button and you could also press F7 on your keyboard, I don’t have any errors and if that is the case you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we want to click on view, strategy tester or press control and r, please pick the new file, simple sma sell trailing stop dot ex5, mark the option for the visual mode here and start a test.
Here is the first position, we need to zoom into the chart to see if the sell trailing stop works, this is the simple moving average and we see that it is actually creating a trailing stop for our sell position, so our little expert advisor works as expected and in this little video you have learned how to create an expert advisor that is able to create a trailing stop for a simple moving average and you have coded it yourself with a few lines of mql5 code.