MQL5 TUTORIAL BASICS – 44 SIMPLE SAR BUY TRAILING STOP

video

In this video we are going to create a buy trailing stop for the parabolic sar indicator you see the little dots here, our stop loss is trailing as our price is rising, so let’s find out how to code a stop loss that moves based on an indicator value.
To do that please click on view, toolbars, standard and click this 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 sar buy trailing stop, click on continue, continue and finish.
Now we can delete everything that is above the on tick function and let’s also delete the two comment lines here.
We start with an include statement to include the file trade dot mqh, afterwards, we create an instance of the class ctrade that will be called trade.
Inside of the on tick function we need to calculate the ask price, that is done with symbol info double for the current symbol on the chart, we use symbol underscore ask and with normalize double and underscore digits we calculate the right number of digits behind the dot.
For our little demo account, we want to create a test position, you wouldn’t do that on your real account but we need something to test, so if we have no open positions and positions total delivers a return value below one we use trade dot buy to buy ten micro lot.
Afterwards, we create an array for the sar and we use the integrated function isar for the current symbol on the chart and the currently selected period on that chart.
These two parameters here is what you see when you click on insert, indicators, trend, parabolic sar.
Here we have a step value of zero point zero two and the maximum value of zero point two, these are the default values so that’s what we use here.
Let’s use array set as series to sort the sar array from the current candle downwards and now we use copy buffer according to the sar definition that we have created here to fill our array for three candles starting with candle zero.
This parameter here is for the buffer, this indicator only has one buffer because it only produces one line of dots. And now we can actually calculate the sar value by looking into our array especially at candle zero. I use normalize double and the value five because each of those dots here has five digits behind the dot.
And now we want to call a function called check sar buy trailing stop, we pass two parameters, the first one is the ask price and the second one is the sar value and as this function does not exist so far we need to create it now.
Our trailing stop function will be called check sar buy trailing stop, it will take two parameter values from the main function, the first one is the ask price and the second one is the sar value.
Now we use a for loop to go through all the open positions, first we need to get the symbol of the current position, that is done by using position get symbol for the current counter value and we only want to continue if the symbol on the chart and the position symbol are equal.
In that case we use position get integer for the position ticket – all in capital letters – to get the ticket number and with position get double we can get the current stop loss by using position underscore sl.
And if the current stop loss is below the sar value we use trade dot position modify for the current position ticket to move the stop loss to the sar value.
This last parameter here is for the take profit value, we don’t modify that, actually we could but this is something for another video.
Finally, we close all the loops here and that’s about it.
Well, if you don’t understand what all the code here does or if this was too fast for you maybe you want to watch the other videos in this video series or maybe even the premium course on our website is interesting for you, for now, please click on the compile button…
I made a mistake here, I think I have missed a curly bracket here, let’s recompile the code and this time it works!
So now we can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we click on insert, indicators, trend, parabolic sar, let’s go with the default values here, right-click into the chart, select save templates and save it as tester dot tpl.
And if you don’t see the strategy tester here please click on view, strategy tester or press control and r, pick the new file simple sar buy trailing stop dot ex5, mark the option for the visual mode here and start a test.
Here we are! Let’s zoom into the chart, this is the first buy position, here comes the stop loss and it follows the sar indicator values and now you know how to create a trailing stop that is based on an indicator value for the parabolic sar and you have coded it with a few lines of mql5 code.