In this video, we are going to create a trailing stop that is calculated based on this indicator value here for the parabolic sar. You see that the trailing stop moves whenever one of those dots appears on the chart. So let’s find out how to code that in mql4.
To do that please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here we click on file, new file, expert advisor from template, continue, I will call this file simple sar buy trailing stop, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here.
We start by creating a global variable called sar value and inside of the on tick function we want to open a test position, you wouldn’t do that on a real account but in our case we need to open a position or we cannot set a trailing stop.
So if orders total returns a value of zero – in other words we don’t have any open orders – and if the sar value is below the bid price we use order send for the current symbol on the chart and open a position for ten micro lot.
Afterwards, we want to calculate the sar value that is done by using the integrated isar function that comes with mql4 for the current symbol on the chart and for the currently selected period on that chart. And these two parameter values here that’s what you see if you click on insert, indicators, trend, parabolic sar, here we have a step value of zero point zero two and the max value of zero point two, so let’s go with the default values here.
And now we right-click into the chart, select template, save template and save it as tester dot tpl, you can replace the current one.
And now that we have finished the definition we want to check if the current sar value is below the bid price – that is the case when the dots are below the candles – and in that case we want to check our sar buy stop function, that function doesn’t exist so far so we need to create it now.
Our custom function will not have any return values so it is a void function, it is called check sar buy stop, we start by using a for loop to go through all the open orders and positions, afterwards we want to select an order for the current counter value, we use select buy position and mode trades here.
We also want to know if the order symbol equals the symbol on the chart and if that is the case we check if the current order type equals op buy – or in other words if it is a buy trade – and only if the current order stop loss is below the current sar value we use order modify for the current order ticket to adjust the stop loss to the current sar value. We leave all the other parameters unmodified.
Let’s close the loop and the function here and that’s about it.
Now you can click on the compile button here or press F7, you shouldn’t get any errors here but if this was too fast for you or if you don’t understand what all the code here does you maybe want to watch the other videos in this basic series first or maybe even the premium course on our website is interesting for you.
If you don’t get any errors here and the compilation process is fine please click on the little button here or press F4 to go back to Metatrader.
And in Metatrader you want to click on view, strategy tester or press control and r, please pick the new file simple sar buy trailing stop dot ex4, mark the visual mode here and start a test.
Here is our expert advisor, let’s zoom into the chart and as soon we see dots appear below the candles we should also see a trailing stop, that happened right now, so our expert advisor is working as expected and now you know how to create a parabolic sar trailing stop in mql4 and you have coded it yourself with a few lines of mql4 code in the Metaeditor.