In this video, we are going to create an expert advisor that is able to create a buy trailing stop for a simple moving average, so let’s find out how to do that with mql4.
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, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We start by creating a static variable and we call it last stop moving average value.
To have something that we can use with a buy trailing stop we need to create a test buy position so whenever we have no open orders we use order send to open a buy position for ten micro lots, please don’t do that on a real account, this is just for testing purposes, so let’s calculate the stop moving average value that is done by using the ima function for the current symbol on the chart and the currently selected period on that chart, I will use a simple moving average for five hundred candles, we don’t use a shift value, the mode is for sma simple moving average, the calculation will be based on the close price for candle one.
Now we want to check if the current value for the stop moving average is bigger than the last value and the second condition will be if the stop moving average value is below the current bid price and if that is the case we want to try to change the stop loss that’s done by calling a function check sma buy trailing stop and we are going to pass the stop moving average value that we have calculated here.
And afterwards, we want to assign the stop moving average value to the variable for the last stop moving average value for the next time we will check it because the current one will be the last one the next time we check it.
So far so simple, but this function doesn’t exist so far, so we need to create it now, the name of the function is check sma buy trailing stop, we don’t return a value so we use void and we will pass a parameter called stop moving average value from the main function, we use a for loop to go through all the open orders, each order is selected for the current counter value that we have used here in the for loop.
The next condition we need to check is if the current order symbol is equal to the symbol on the chart and as we are going to create a buy trailing stop we also want to check if the order type equals op underscore buy, we also want to know if the current order stop loss for the position is below the stop moving average value that we have calculated, and if all the conditions are true we use order modify for the current order ticket, we don’t change the order open price but we try to change the stop loss value here, we leave the other options unchanged, now let’s close the if statement and the for loop, and finally the function, and that’s about 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 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 or press F7, I have messed up one of the braces here so let’s recompile now, and this time we have no errors, please notice that we have two warnings here, order send and order modify should be checked, there are several conditions when we will not be able to modify anything, for example, if we try to replace the stop moving average value with something that the broker is not accepting or if we use the same value twice.
I will show you the modify errors in the journal.
To get back to Metatrader please click on a little icon here or press F4 on your keyboard, now you should see the Metatrader window and here you want to create a new chart, click on insert, indicators, trend, moving average, please pick a period for five hundred candles for a simple moving average applied to the close price, I will choose red and click on okay, let’s zoom into the chart, here is my moving average and now I need to right-click, select template, save template and save it as tester dot tpl, you can replace the old one because this template is what we are going to see in the next strategy test and to do that please click on view, strategy tester or press control and r, please pick the new file simple sma buy trailing stop dot ex4, mark the option for the visual mode here and start your test.
Here we are! We see that the trailing stop is working, let’s speed that up a little bit, once again I need to zoom into the chart and you see that it works but if you click on the journal tab you will see order modify errors whenever the trailing stop can’t be changed, as I said there are several reasons why this could be the case but for this simple test it is good enough and in this little video you have learned how to create a trailing stop for a simple moving average and you have coded it yourself with a few lines of mql4 code.