MQL5 TUTORIAL BASICS – 44 SIMPLE SAR BUY TRAILING STOP

video
play-sharp-fill

 

  1. Introduction to Creating a Buy Trailing Stop for the Parabolic SAR Indicator (00:00 – 00:15) Introduction to coding a stop loss that moves based on the Parabolic SAR indicator value.
  2. Starting in MetaEditor and Creating a New Expert Advisor File (00:15 – 00:44) Instructions on opening MetaEditor and creating a new expert advisor file named “Simple SAR Buy Trailing Stop.”
  3. Setting Up the Code Structure and Creating a Test Position (00:44 – 02:01) Deleting unnecessary code and creating a test position if there are no open positions using ‘trade.buy’.
  4. Creating an Array for the SAR and Defining the Indicator (02:01 – 02:50) Creating an array for the SAR indicator and defining it with default step and maximum values.
  5. Filling the Array with SAR Values (02:50 – 03:24) Using ‘copy buffer’ to fill the array with SAR values for three candles, starting from the current candle.
  6. Calculating the SAR Value (03:24 – 03:35) Calculating the SAR value for the current candle and normalizing it to five digits.
  7. Creating the Check SAR Buy Trailing Stop Function (03:35 – 05:16) Creating a function to check and adjust the buy trailing stop based on the SAR value, modifying the stop loss if necessary.
  8. Finalizing the Code and Compiling (05:16 – 05:56) Finalizing the code and compiling it in the MetaEditor.
  9. Testing the Expert Advisor in MetaTrader (05:56 – 06:47) Instructions on testing the expert advisor in MetaTrader using the strategy tester.
  10. Demonstration of the Buy Trailing Stop in Action (06:47 – 07:12) Demonstrating the buy trailing stop following the SAR indicator values in a MetaTrader chart.

 

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.