In this video we are going to create a standalone Expert Advisor that is actually able to trade the Parabolic SAR Indicator.
This is a very nice Indicator, you can see the dots above and below the price and whenever the direction changes here you will see that we have a buy or a sell signal. The signal output will be in the left upper corner here; this Expert Advisor will be able to automatically trade the signal. We have already an open position here and now we want to find out how we can code such a standalone Expert Advisor in MQL5.
To do that please click on the little button here or press F4 on your keyboard and 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 version: “SimpleSARStandaloneEA” (Expert Advisor), click on: “Continue”, “Continue” and “Finish”.
Now we delete everything that is above the “OnTick” function and let’s also remove the two comment lines here.
We start by importing the file: “Trade.mqh” that will enable us to create an instance of “CTrade” that will be called: “trade” and we use it later on to open the positions.
Inside the “OnTick” function we create a price array (PriceArray) that is done by using the “MqlRates” function that is included in MQL5, now we use “ArraySetAsSeries” to sort our price array (PriceArray) from the current candle downwards and with “CopyRates” we fill it for the current symbol on the chart and a currently selected period, we start with the current candle 0 (zero) and we copy price data for 3 candles and store it in our price array (PriceArray).
We also need to create a string for the signal, that variable will be also called: “signal” and we don’t assign a value here because we are going to calculate that later on.
First we need to get the Ask and the Bid price, that can be done by using “SymbolInfoDouble” for the current symbol, we use “SYMBOL_ASK” to get the Ask price and “SYMBOL_BID” to get the Bid price, and with “NormalizeDouble” and “_Digits“ we calculate the number of digits behind the dot – you see that this currency pair has 3 digits while other currency pairs may have 5 digits – and if you mark “_Digits“ and press F1 you will see that the “_Digits“ variable stores the number of digits after the decimal point.
Let’s create another array for our SAR Indicator that will be called my SAR array (mySARArray), it’s a double array so it can hold floating type values and by using a “iSAR” we create an SAR definition (SARDefinition) for the symbol on the chart and the currently selected period. Now you might ask yourself; what these two parameters are?
Well, if you click on “Insert/ Indicators/ Trend/ Parabolic SAR” you will see that we have a step value of: 0.02 and a max value of: 0.2, that’s exactly what we are using here.
Now it’s time to sort the SAR array (mySARArray) with “ArraySetAsSeries” and we use “CopyBuffer” one more time to fill our SAR array (mySARArray) for the first buffer – that’s the one that is used for the dots here – from the current candle 0 (zero) for 3 candles and store the values in our SAR array (mySARArray). Now we are able to calculate the value for the last candle, I will call it: “LastSARValue”, I use ”NormalizeDouble” here and a 5 because when you pause the Strategy Tester and hold your mouse above one of these dots you will see that we have 5 digits behind the dot of the value – that’s what “NormalizeDouble” does; if I would use “NormalizeDouble” and a 4 we would have 4 digits behind the dot – and we get the value for the last candle by looking at candle 1 in our SAR array (mySARArray).
We actually do the same here, this time for candle 2 – that’s the candle before the last candle – so I call it: “NextToLastSARValue” and whenever the “LastSARValue” is below the lowest price of candle 1 – or in other words; whenever the dots are below the candles – and if the “NextToLastSARValue” was greater than the highest price for the candle before that would be a buy signal so we assign the word: “buy” to our signal.
Otherwise if the “LastSARValue” was above candle 1 and the value was below the candles before that would be a sell signal and now we would assign the word: “sell” to our signal. And if our signal equals sell and “PositionsTotal” is below 1 – in other words we have no open positions – we use “trade.Sell” to sell 10 micro lot, otherwise if the signal equals buy and we have no open positions we would use “trade.Buy” to buy 10 micro lot.
Finally we create a chart output here, we use the “Comment” function to output the words: “The current signal is:” followed by our calculated “signal”.
Okay. That’s it. Please click on the “Compile” button or press F7, you should get no errors and no warnings here and in that case we can click here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, please select the file: “SimpleSARStandaloneEA.ex5”, mark the visualization option here and start a test.
…and here we are! Our Expert Advisor is actually trading, this is our first position and in this little video you have learned how to create a standalone Expert Advisor that is actually trading the SAR Indicator and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE SAR STANDALONE EXPERT ADVISOR”SimpleSARStandaloneEA.txt – Downloaded 339 times – 2 KB