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

video
play-sharp-fill

In this video we are going to automate this Indicator, it’s the Parabolic SAR Indicator.
You see the little dots here, they are creating buy and sell signals depending if the dots are above or below the candles, so let’s find out how to automate that with MQL4.
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/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleParabolicSAR”, click on “Continue”, “Continue” and “Finish”.
Now you can delete everything above the “OnTick” function and the two comment lines here.
We start by creating a string variable for the signal that will also be called: “signal”, we don’t assign any value because we need to calculate that. This is done by using the “iSAR” function that comes with MQL4.
The first parameter is the current symbol, the second parameter is for the current period on the chart, so let’s find out about those values, pick any chart, click on: “Insert/ Indicators/ Trend/ Parabolic SAR” and you will see a step value of 0.02 and a max value of 0.2, now we click on “OK” and here are the little dots and if you want to see the Indicator values in the backtest, you now need to right-click on the chart, select “Templates/ Save Template” and save it as “tester.tpl” because this is the template that is going to be used in the Strategy Tester.
We can replace the current one, so now we know this is the step value, this is the max value and the last parameter here would be a shift value, we don’t want to use any shift values so this is 0 (zero) and if the SAR value (SARValue) is below the Bid price we have a buy signal so we assign the word: “buy” to our signal.
Otherwise if the SAR value (SARValue) is bigger than the Ask price that would be a sell signal and that’s when we assign the word: “sell” to our signal and whenever the signal equals buy and “OrdersTotal” equals 0 (zero) that would mean that we have a buy signal and no open positions and that’s when we use “OrderSend” to buy 10 micro lot.
Otherwise, if the signal equalled sell and we also have no open positions that’s when we use “OrderSend” to sell 10 micro lot.
Finally, we create a chart output using the “Comment” statement to output the text: “The signal is:” and the calculated signal.
That’s all, if you don’t know what all the code does or if this was too fast for you maybe you want to watch the other basic videos in this video series or maybe you are even interested in the premium course.
Otherwise when you’re done you can click on the “Compile” button here or press F7, now you shouldn’t get any errors and in that case, you can click on the little button here or you can press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R. Please pick the new file: “SimpleParabolicSAR.ex4”, mark the visual mode option here and start the test.
…and this is our little Expert Advisor at work, we already have bought, so it works and now you know how to create an automated Expert Advisor for the Parabolic SAR Indicator and you have coded it yourself with a few lines of MQL4 code.