MQL5 Tutorial – Simple SAR Expert Advisor with MQL5

video
play-sharp-fill

 

Introduction to Parabolic SAR Expert Advisor (00:00 – 00:16)

Overview of creating an Expert Advisor for the parabolic SAR.
Explanation of how the parabolic SAR indicates direction changes.
Understanding Parabolic SAR Indicator (00:16 – 00:30)

Description of the parabolic SAR’s behavior in relation to price movements.
Creating the Expert Advisor in MQL5 (00:30 – 00:57)

Initial steps to create an Expert Advisor for calculating the SAR value.
Setting Up the Expert Advisor (00:57 – 01:12)

Instructions on setting up the Expert Advisor in the Meta Editor.
Defining the SAR Array and Properties (01:12 – 01:35)

Creating the SAR array and defining its properties.
Reference to the parabolic SAR on Wikipedia.
Configuring SAR Parameters (01:35 – 02:12)

Explanation of the parabolic SAR parameters and their configuration.
Implementing the iSAR Function (02:12 – 02:45)

Using the iSAR function for the current symbol and selected period.
Sorting the price array with the arraysetasseries function.
Filling the SAR Array with Data (02:45 – 03:15)

Filling the SAR array with data for the defined SAR.
Calculating and Displaying the SAR Value (03:15 – 03:53)

Calculating the SAR value for the current candle.
Displaying the SAR value on the chart.
Compiling and Testing the Expert Advisor (03:53 – 04:20)

Compiling the Expert Advisor and launching the Metatrader for testing.
Visualizing and Analyzing SAR Indicator (04:20 – 04:55)
Activating the strategy tester and analyzing the SAR indicator’s behavior.
Conclusion and Application (04:55 – 05:28)
Observing the SAR indicator’s values and their application in automated trading systems.

 

Today we are going to create a little Expert Advisor for the parabolic SAR.

Most traders love this indicator, because it will show you when the direction changes.

If the value of the parabolic SAR is above the price, it is going down for a while and if the value of the parabolic SAR is below the price, it is going up.

So if you see a change like this one and you go short here, you could have made a profit all the way down until the direction changed here.

Now how can we create an Expert Advisor in MQL 5 to calculate the SAR value here?

As always please click on this little button here or hit the F4 key to bring up the Meta Editor and now click on File, New, Expert Advisor from template, continue.

We will call it simple SAR, continue, continue, finish.

Now please delete everything above the OnTick function and the two comment lines here.

And the first thing we need is an array. So we will create one and call it my SAR array.

In the next step, we want to define the properties of the SAR by the way you can find it on Wikipedia.

It’s called parabolic SAR, it was created by a guy named J. Welles Wilder Junior to find potential reversals in the market price and you can insert it on your chart by selecting insert indicators parabolic SAR.

And if you do that you will see these green bubbles here, so let’s bring up the properties for the Expert Advisor here and we have a step of 0.02 and the maximum of 0.2.

And that’s exactly what we have chosen here. This is the same value as step value here and this is the maximum value and we want to use the iSAR function for the current symbol and the selected period on the chart with a step value of 0.02 and a maximum value of 0.2.

And afterwards we are going to sort our price array from the current candle downwards with the function arraysetasseries for mySAR array we have defined here.

And after we have done that, we want to fill it with data for the SAR definition we have created here.

This zero is used because we only have one line. This is for the current candle and we want three candles and store the result in mySAR array.

Now we want to calculate the EA for the current candle. So we use a double variable called SARvalue for the candle zero the current candle of my SAR array.

And I would like to use the function normalize double to calculate it with five digits behind the point.

And in the last step we want to output the text SAR value and SAR value we have calculated here directly on our chart.

Okay that’s it. Let’s compile the Expert Advisor and you should have no errors and no warnings here.

So let’s go to the Metatrader by pressing this little button or hitting the F4 key.

Now we want to activate the strategy tester by clicking here or pressing control and R and select the simple SAR.ex5 file, select any currency pair here.

And you should also activate the visualization option, if you don’t see it you have to drag this a little bit higher. So please enable this check mark here and start your test.

And now you should see something like this, let’s speed it up a little bit, pause it and you can see the SAR indicator was below the price and it changed its direction here and afterwards it went down all the way until it changed the direction again at this point.

And now it’s going up. If you hover your mouse above the last dot here, you can see a value of 1.50962, and that’s exactly the value you’ll see in the upper left corner here on a chart.

Now you could implement that in your system and check if the price here is above or below the last dot and use the parabolic SAR indicator for your own automated MQL 5 trading system as an entry signal.

And you did it with a few lines of MQL 5 code for this little Expert Advisor here.

Download “MQL5 Simple SAR Expert Advisor with MQL5”

SimpleSAR.txt – Downloaded 372 times – 673.00 B