[ Ссылка ]
In this video, we are going to create an Expert Advisor that uses the Parabolic SAR indicator to generate buy and sell signals. Let's see how we can do that.
We start Metaeditor by clicking the little icon or pressing F4.
First, we include the Trade library to use trading functions. We create an instance of the CTrade class to handle trading operations.
Inside the OnTick function, we calculate the Ask price using the SymbolInfoDouble function with the SYMBOL_ASK parameter and normalize it to the number of digits for the current symbol. Similarly, we calculate the Bid price using the SYMBOL_BID parameter and normalize it.
We create an empty string called signal to store our trading signal. We also create a price array of type MqlRates to hold price data. We set the array as a series to sort it from the current candle downwards. We then fill the array with price data for the last three candles using the CopyRates function.
Next, we create an array to hold the SAR values. We define the SAR indicator using the iSAR function with parameters for the symbol, period, step, and maximum. We set the SAR array as a series to sort it from the current candle downwards. We fill the SAR array with data from the indicator buffer for the last three candles using the CopyBuffer function.
We calculate the value of the SAR for the last candle and normalize it to five decimal places.
To generate a buy signal, we check if the last SAR value is below the low of the previous candle. If it is, we set the signal to "buy". For a sell signal, we check if the last SAR value is above the high of the previous candle. If it is, we set the signal to "sell".
If the signal is "sell" and there are no open positions, we execute a sell order for 10 microlots at the Bid price, with a take profit 200 points above and a stop loss 150 points below. If the signal is "buy" and there are no open positions, we execute a buy order for 10 microlots at the Ask price, with a take profit 200 points below and a stop loss 150 points above.
Finally, we output the signal to the chart using the Comment function.
We press F7 to compile the code.
If this was too fast for you or if you don't understand what all the code is doing, you may want to check out the Premium course on our website, or watch one of the basic videos first.
If the compilation is successful, we go back to MetaTrader by pressing F4 or clicking on the icon. Back in MetaTrader, we press Control and R to start the strategy tester, pick the Expert Advisor that we have just created, enable the visual mode, and start a strategy test. We should see the Expert Advisor on the chart.
If you are already a Premium course member and have an idea for a video like this one, you can send us an email.
In this video, we have learned how to create an Expert Advisor that uses the Parabolic SAR indicator to generate buy and sell signals with just a few lines of MQL code.
Thanks for watching and I will see you in the next video.
Ещё видео!