MQL5 TUTORIAL - Accumulation Distribution Oscillator explained (in 5 min)

MQL5 Tutorial
MQL5 Tutorial
146 بار بازدید - 2 ماه پیش -
https://mql5tutorial.com/?s=IAD

In this video, we are going to create an Expert Advisor. Let's see how we can do that.

We start by opening Metaeditor, which you can do by clicking on the little icon or pressing F4.

First, we include the Trade.mqh library to use the trading functions provided by MQL5.

Next, we create an instance of the CTrade class called trade, which will handle our buy and sell operations.

The OnTick function is the main function that will be executed every time a new tick is received for the symbol.

We calculate the Ask price by using the SymbolInfoDouble function with the _Symbol and SYMBOL_ASK parameters. The result is normalized to the number of digits for the current symbol using NormalizeDouble and _Digits.

Similarly, we calculate the Bid price using SymbolInfoDouble with the _Symbol and SYMBOL_BID parameters, and normalize it with NormalizeDouble and _Digits.

We create a string variable named signal to store our trading signal.

We also create two arrays, myPriceArray and myMovingAverageArray, to hold price data and moving average values, respectively.

We set these arrays to be sorted in descending order from the current candle using ArraySetAsSeries with the true parameter.

We define the properties of the Moving Average using the iMA function with parameters _Symbol, _Period, 20, 0, MODE_SMA, and PRICE_CLOSE. This sets up a Simple Moving Average (SMA) with a period of 20 based on the closing prices.

We sort the myMovingAverageArray in descending order from the current candle using ArraySetAsSeries with the true parameter.

We copy the moving average values into myMovingAverageArray using CopyBuffer with parameters movingAverageDefinition, 0, 0, and 3. This copies the values for the last 3 candles.

We calculate the moving average value for the current candle by accessing the first element of myMovingAverageArray.

We define the properties for the Accumulation/Distribution (IAD) indicator using the iAD function with parameters _Symbol, _Period, and VOLUME_TICK.

We copy the IAD values into myPriceArray using CopyBuffer with parameters IADDefinition, 0, 0, and 11. This copies the values for the last 11 candles.

We calculate the IAD value for the current candle by accessing the first element of myPriceArray.

We also calculate the IAD value for 10 candles before the current candle by accessing the 11th element of myPriceArray.

If the Ask price is above the moving average value, we check if the IAD value is rising by comparing it to the value 10 candles before. If both conditions are true, we set the signal to "buy".

If the Bid price is below the moving average value, we check if the IAD value is falling by comparing it to the value 10 candles before. If both conditions are true, we set the signal to "sell".

If the signal is "sell" and there are no open positions, we execute a sell order for 0.10 lots (10 Micro Lots) using the trade.Sell function. The parameters are the lot size, NULL for the symbol, Bid price, 0 for stop loss, and Bid minus 150 points for take profit.

If the signal is "buy" and there are no open positions, we execute a buy order for 0.10 lots (10 Micro Lots) using the trade.Buy function. The parameters are the lot size, NULL for the symbol, Ask price, 0 for stop loss, and Ask plus 150 points for take profit.

Finally, we create a chart output to display the current signal using the Comment function.

To compile the code, press F7.

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 everything works, go back to MetaTrader by pressing F4 or clicking on the icon.

Back in MetaTrader, 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.

You should now 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.

We have learned how to create an Expert Advisor that uses a Simple Moving Average and the Accumulation/Distribution indicator to generate buy and sell signals. We have coded this Expert Advisor ourselves with a few lines of MQL code.

Thanks for watching, and I will see you in the next video.
2 ماه پیش در تاریخ 1403/04/25 منتشر شده است.
146 بـار بازدید شده
... بیشتر