MQL5 TUTORIAL BASICS – 23 HOW TO CODE A SIMPLE PARABOLIC SAR EXPERT ADVISOR

video

In this video, we are going to create this interesting Expert Advisor for the Parabolic SAR Indicator.
You see the little green points here, this is the signal, whenever the points are above the price that’s a sell signal and when they are below the price that’s a buy signal until the direction changes.
So let’s find out how to automate the Parabolic SAR with MQL5.
To do that please click on a 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 (template)” from template, “Continue”, I will call this file: “SimpleParabolicSAREA”, click on “Continue”, “Continue” and “Finish”.
Now you can remove everything above the “OnTick” function and the two comment lines here.
We start with an include statement to include the file: “Trade.mqh”, this one is included in MQL5 and it comes with trading functions that we are going to use to open our positions, so let’s create an instance of “CTrade” that will be called: “trade” and we are going to use it later.
Inside of the “OnTick” function we start by calculating the Ask price and the Bid price that is done by using “SymbolInfoDouble” for the current symbol on the chart, with “SYMBOL_ASK” – all in capital letters – we will get the Ask price and with “SYMBOL_BID” we would get the Bid price. “NormalizeDouble” and “_Digits” is used to calculate the number of digits behind the dot because “_Digits” stores the number of digits after the decimal point and depending on the currency pair that can be 3 digits or 5 digits.
Let’s create a variable for the signal we will also call it “signal” and we don’t assign a value, that is going to be calculated later on.
First we need to create a price array for the prices on the chart that is done with “MqlRates”, we use “ArraySetAsSeries” for our price array (PriceArray) to sort it from the current candle downwards and now we need to fill the array (PriceArray) with price data hat is done by using “CopyRates” for the current symbol on the chart and the currently selected period.
By the way “_Symbol” and this symbol (Symbol()) is the same so we also could use “_Symbol” and the same is true for “_Period”, we start with the current candle 0 (zero) and we want to get the price data for 3 candles and store it in our price array (PriceArray).
Let’s create another array (mySARArray) for the Parabolic SAR Indicator, let’s define (SARDefinition) what we are going to do, we use the “iSAR” function that comes with MQL5 for the current symbol on the chart and the currently selected period. So let’s find out about these two values here.
Pick any chart, click on: “Insert/ Indicators/ Trend/ Parabolic SAR” and here we see we have a step value of 0.02 and a max value of 0.2, so let’s click on “OK”, now you see the Indicator and we want to right-click on the chart, select “Templates/ Save Template” and save it as “tester.tpl”, yes I want to exchange the current one because if I don’t do that I might not see the Indicator in the backtest, but back to the code here are the two values; the step value and the max value, lets also sort this array (mySARArray) by using “ArraySetAsSeries” and this time we use “CopyBuffer” to fill our price array (mySARArray) according to the SAR definition (SARDefinition) that we have created here for the 0 (zero) buffer – that’s the only buffer this Indicator has – from candle 0 (zero) – that’s the current candle – for 3 candles and store the values in the SAR array (mySARArray).
Now we can get the last SAR value (LastSARValue) by looking into candle 1 from the SAR array (mySARArray). I use “NormalizeDouble” and 5 digits because this is what you see when you point your mouse over one of those Indicator dots, we have 5 digits behind the dot.
So let’s find out if we have a buy signal and that would be if the last SAR value (LastSARValue) is below the law of candle 1 in our price array (PriceArray) or in other words if the dot is below the candle we assign the word: “buy” to our signal.
In the other case, a sell signal would be if the last SAR value (LastSARValue) is above the highest price of candle 1 in our price array (PriceArray) and if that is the case we assign the word: “sell” to our signal.
So if our signal equals sell and “PositionsTotal” is below 1 – in other words, we don’t have any open position – we use “trade.Sell” to sell 10 micro lot, otherwise, if the signal equals buy and we have no open positions we use “trade.Buy” and buy 10 micro lot.
Finally, we want to create a chart output by using the “Comment” statement. This will output the text: “The signal is:” followed by the current signal.
If you don’t know what all the code does or if I was a little bit fast for you maybe you want to watch the other videos in this basic video series or maybe even the premium course might be interesting for you.
Otherwise please click on the “Compile” button here and if you don’t get any errors you can click on the little button here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, here we pick the Simple Parabolic SAR EA (SimpleParabolicSAREA.ex5), mark the visualization option and start a test.
…and here is our little Expert Advisor in our backtest, we already have a buy position here because the price was above the dots and now you know how to code an Expert Advisor to calculate the Parabolic SAR Indicator and you have coded it yourself with a few lines of MQL5 code.