MQL5 Tutorial - Simple SAR Expert Advisor with MQL5

MQL5 Tutorial
MQL5 Tutorial
5.6 هزار بار بازدید - 8 سال پیش -
https://mql5tutorial.com/?s=sar

With MQL5 for Metatrader5 you can calculate the SAR Indicator for your Expert Advisor by using the function iSAR with a few lines of MQL5 code. This will help you to find an entry for your trading robot that can tell you if the direction of the forex price is going to change...


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.
8 سال پیش در تاریخ 1395/11/25 منتشر شده است.
5,614 بـار بازدید شده
... بیشتر