MQL5 TUTORIAL BASICS – 45 SIMPLE SAR SELL TRAILINGSTOP

video
play-sharp-fill

 

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

 

In this video we are going to create a sell stop for this parabolic sar indicator, so let’s find out how to do that with mql5.
To get started please click on the little button 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 sell 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 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 and we needed to open a test position, so that we have something where we can place the trailing stop.
As we are talking about a sell trailing stop we need to calculate the bid price that is done by using symbol info double for the current symbol on the chart, we use symbol underscore bid – all in capital letters – and with normalize double and underscore digits we make sure that we have the right number of digits behind the dot.
Depending on the currency pair that could be either three digits or five digits and now we want to check if the number of current positions is below one, so if we don’t have any open positions we use trade dot sell to open a sell position for ten micro lot. Please remember we only do this on a demo account, we wouldn’t do that on a real account.
Now let’s create an array and once that is done we use the integrated function isar for the current symbol on the chart and the currently selected period on that chart and these two parameters here are what we see if you click on insert, indicators, trend, parabolic sar, here we have a step value of zero point zero two and a max value of zero point two, so let’s click on ok, right-click into the chart, select templates, save template and save it as tester dot tpl because this is the template that we are going to use in the strategy test, so let’s click on ok, we can replace the old value here and go back to the Metaeditor, so let’s use array set as series to sort the array from the current candle downwards and in the next step we can use copy buffer to fill our sar array according to the sar definition that we have created here.
We fill it for buffer zero – that’s the first indicator line – in our case that’s the dotted line here, and we fill it from candle zero for three candles and store the results in our sar array because that makes it possible to calculate the current sar value by looking into candle zero in our sar array and with normalize double and comma five we make sure that the value will be calculated according to the definition of the parabolic sar.
You see here is the value zero point eight eight nine nine seven, that’s five digits behind the dot and that’s also what we get when we do it this way.
And the last part of the on tick function is to call a function called check sar sell trailing stop, this one doesn’t exist so far so we need to code it now and we will pass the bid price and the sar value as parameters.
So this is the function, it is called check sar sell trailing stop, we take the two parameters here, the first one for the bid price is a double value and that’s also true for the second parameter the sar value.
Now we use a for loop to go through all the open positions, first we use position get symbol for the current counter value to get the currency for the current position, afterwards we want to check if the current symbol on the chart and the position symbol are equal, and if that is the case we use position get integer and position underscore ticket – all in capital letters – to get the ticket number for the position.
We use position get double, position underscore sl to get the current stop loss and we only want to modify our stop loss if the current stop loss is above the current sar value or if the current stop loss equals zero, that would be the case when we start up the expert advisor and if you missed that part you will never see any kind of trailing stop on your chart.
If all of our conditions are true we use trade dot position modify for the current position ticket and set the stop loss to the sar value.
In this example we will not modify the take profit value, now finally we want to close all the loops 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 one of 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 here or press F7 on your keyboard…
…I made a mistake, usually that’s a mistake about one of these curly braces, you see when I mark this one the second one that is highlighted is the one above, so let’s add one here, this one closes the if statement here, this one belongs to the if statement above, this one is for the for loop, and this one is the end of the function.
So let’s recompile the code and this time it worked. If this is also true in your case please click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we have saved the template as tested dot tpl before, if you haven’t done that please do that now, because now we need to start the strategy tester, so we click on view, strategy tester or press control and r, please pick the new file simple sar sell trailing stop dot ex5, mark the visual mode here and start a test.
Here we are! We have our first sell position and as soon as the candles are on the other side of the indicator we should see a sell trailing stop.
Here we are! So our little expert advisor is working and now you know how to code an sar sell trailing stop in mql5 and you have done it yourself with a few lines of code in the Metaeditor.