MQL5 TUTORIAL – Simple AMA STANDALONE WALKTRHOUGH

video
play-sharp-fill

Introduction to AMA Standalone Expert Advisor

In this tutorial, we are going to create an Expert Advisor in MQL5 that utilizes the Adaptive Moving Average (AMA) for generating trading signals. The AMA is particularly useful for capturing trends in the market due to its sensitivity to price movements. We’ll explore how to implement this indicator to determine buy and sell signals based on its interaction with the price.

Setting Up in MetaEditor

To begin, launch MetaEditor by clicking the little icon or pressing F4 on your keyboard. Once opened, navigate to: “File/New/Expert Advisor (template)” and name your file “SimpleAMAStandaloneEA”. Click “Continue” until you can finish and create the file. Initially, clear any pre-existing code above the OnTick function and remove the comment lines to prepare our workspace.

Including Essential Files and Initializing Variables

Start by including the Trade.mqh file, which provides functionalities necessary for trade operations. Create an instance of the CTrade class named trade, which we will use later for executing trades.

Copy Code

#include <Trade.mqh> CTrade trade;

Calculating Ask and Bid Prices

Within the OnTick function, calculate the current Ask and Bid prices using SymbolInfoDouble. Normalize these prices using NormalizeDouble and _Digits to ensure they adhere to the precision required by the trading server.

Copy Code

double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits); double bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);

Setting Up Price Data Arrays

Define a price array using MqlRates and sort it with ArraySetAsSeries. Populate this array with price data using CopyRates.

Copy Code

MqlRates PriceArray[]; ArraySetAsSeries(PriceArray, true); CopyRates(_Symbol, _Period, 0, 3, PriceArray);

Implementing the AMA Indicator

Utilize the iAMA function to define the AMA based on its parameters. Fill an array with the AMA data using CopyBuffer.

Copy Code

int amaDefinition = iAMA(_Symbol, _Period, 10, 2, 0, PRICE_CLOSE); double EAArray[]; ArraySetAsSeries(EAArray, true); CopyBuffer(amaDefinition, 0, 0, 3, EAArray);

Calculating and Assigning Signals

Determine the current AMA value and compare it with the close price of the last candle to assign buy or sell signals.

Copy Code

double currentAMA = EAArray[0]; string signal = ""; if(currentAMA > PriceArray[1].close) {     signal = "sell"; } else if(currentAMA < PriceArray[1].close) {     signal = "buy"; }

Executing Trades Based on Signals

Execute trades based on the calculated signals, ensuring no existing positions conflict with the new signal.

Copy Code

if(signal == "sell" && PositionsTotal() == 0) {     trade.Sell(0.1, _Symbol, bid, 0, 0, "AMA Sell"); } else if(signal == "buy" && PositionsTotal() == 0) {     trade.Buy(0.1, _Symbol, ask, 0, 0, "AMA Buy"); }

Displaying Signal on Chart and Testing

Use the Comment function to display the current trading signal on the chart. Compile the code by pressing F7 and test the Expert Advisor in MetaTrader by pressing CTRL+R and selecting the “SimpleAMAStandaloneEA”.

Conclusion

By following this tutorial, you have created an Expert Advisor that trades based on the Adaptive Moving Average. This EA can help you understand how to implement and test trading strategies in MQL5. If you found the pace fast or need more detailed explanations, consider exploring our Premium course or reviewing basic videos first.

Thank you for following along, and I look forward to seeing you in the next tutorial where we continue exploring the capabilities of MQL5 for automated trading.