MQL5 TUTORIAL – Advanced SAR Expert Advisor


This is the Advanced SAR Expert Advisor in MQL5 and we are just trading the Golden Goose Framework.
This is the English version.
Whenever the points here, suddenly appear on the other side of the candles this would be a buy or a sell signal.
The SAR indicator is part of MQL5 and it is also part of Metatrader 5, you could insert it on your own chart by clicking on insert, indicators, trend, Parabolic SAR.
If you click on this indicator you will see that we have a step value of 0.02 and a maximum value of 0.2, and now we want to create a MQL5 expert advisor which is able to find a buy or sell signal and trade it with the forex trading framework.
To do that please click on the little button over here and now you should see the Metaeditor.
Obviously a 5 to 10 minutes video is too short to explain a whole trading framework but basically, we import a predefined Golden Goose Framework.
That’s an ex5 file that was precompiled. It is start in the Libraries folder.
That’s the file.
It basically provides a number of functions that we can import.
Here it ends.
Everything in between was pre-coded in the GoldenGooseFramework20170408_EN.
If you don’t know what a framework is or how you can get one just follow the website at the end of this video, that’s where you can find out how to rent one or how to create your own.
To trade the SAR indicator, we need to create and include a module called CheckEntry_SAR.mq5.
So, we create a new and empty file here.
First, we need a function called CheckEntry, because that is the function that we call here to check for an entry signal and that signal will be stored in a string variable called signal and afterwards we will call the trade management, check for a new tick and pass the calculated entry signal for the SAR as a parameter.
If you find all these stuff too complicated, please make sure to first watch all the simple MQL5 tutorial videos.
Go back to topic.
Inside of the CheckEntry_SAR module we need to create a price array with MqlRates first and then sort that array from the current candle downwards by using ArraySetAsSeries.
And after that we use CopyRates to fill our PriceArray for the current symbol and the current period from candle zero (0) – that’s always the current candle that is created here – for our 3 candles.
Now we create an empty string for the signal and we need another array for the SAR values so we create an SAR array.
And the SAR definition can be created by using the MQL5 function iSAR for the current symbol and the current period and you already know these two values here.
That’s the default value for the two parameters in the Parabolic SAR indicator that you can drag on the chart.
Let’s sort the SAR array from the current candle downwards by using ArraySetAsSeries and fill it with data by using CopyBuffer.
According to the SAR definition we have created here for the current buffer zero and the current candle that’s also zero.
And we need 3 candles, so we need a 3 here and the result will be stored in mySARArray.
And now that that array is filled with data let’s actually calculate the value for the last candle.
The last candle is candle [1] in our array and we use NormalizeDouble and the 5 to get 5 digits behind the dot, because if you place your mouse over one of these dots you will see a value that also has 5 digits behind the dot.
Let’s repeat that, but this time we are doing the same calculation for a candle [2] instead of candle [1] and assign it to the variable NextToLastSARValue.
And to find the buy signal we check if the last SAR value was below the low of candle [1] and if it was above the high the candle before.
Here it is above the high and now it changes and it’s below the next candle, so this would be a good entry point for a buy trade.
And if all that is true we assign the value “buy” to the signal because now we want to buy.
Let’s also create a sell signal and to do that we check if the last SAR value was above the candle [1] high and if it was below the low candle before, that’s candle [2].
In that case we would assign the value “sell” to our signal. That’s almost it.
The last step is to return the signal to the main module by using return signal and now we can close the module with the final bracket.
Please save it, don’t compile it – because you need to compile the main module (that’s the framework in my case) so I click on compile and because I don’t see any errors here I can click the little button here to go back to Metatrader.
In Metatrader you can click on view/strategy tester or press ctrl + r to bring up the strategy tester panel here.
Our strategy tester is still running; balance and equity are both profitable now.
I’ll stop it, start a new test and now you see dots above the candles and I will speed it up a little bit because when this changes you should see that buy trade has opened and that happened actually.
We can speed up the process here and watch it trade for a whole year.
Here is the graph for the results. Let’s switch to full speed here and watch your equity and balance curve go up.
Now you know how to create an expert advisor in MQL5 that is able to create a buy or a sell signal for a Forex trading framework and you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 - Advanced SAR Expert Advisor from MQL5 Tutorial”

AdvancedSAR.txt – Downloaded 541 times – 1.69 KB