Moving Average Crossover Expert Advisor for Gold (XAUUSD)
Info
The Moving Average Crossover Expert Advisor for Gold (XAUUSD) is a Expert Advisor for MetaTrader 5 that two moving averages on gold (xauusd), forex, or any symbol: when the fast one crosses the slow one on a closed bar, the ea opens one position in the direction of the cross. Default is EMA 20 and EMA 50 on the close price, and method and price can be changed.
Usage
This tool is typically used for automated trading on major forex pairs and gold.
Platform
This Expert Advisor works exclusively on MetaTrader 5 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL5/Experts folder via File ? Open Data Folder in MetaTrader 5.
How to Install and Use Moving Average Crossover Expert Advisor for Gold (XAUUSD)
1. Installation: Open the "File" menu, select "Open Data Folder," navigate to MQL/Experts, paste your file, and restart the terminal.
2. Activation: Drag the EA from the Navigator onto a chart, ensure "Allow live trading" is checked in the Common tab, and verify the AutoTrading button is green.
3. Optimization: Right-click your chart, choose "Expert List," click "Properties" to adjust inputs, and save your preferred setup as a set file for future use.
4. Maintenance: Regularly check the "Experts" tab in the terminal window to monitor trade logs and potential execution errors.
Frequently Asked Questions
Q: Why is my EA not opening trades? A: Check the "AutoTrading" button, ensure "Allow live trading" is enabled, and verify your broker allows automated trading on your account type.
Q: Can I run multiple EAs on one chart? A: No, each chart can only host one active EA; however, you can open multiple charts for different currency pairs to run several EAs.
Q: What does the "smiley face" icon mean? A: A smiley face in the top-right corner of the chart indicates the EA is successfully running; a frowny face means it is disabled.
What this tool does
Two moving averages on Gold (XAUUSD), forex, or any symbol: when the fast one crosses the slow one on a closed bar, the EA opens one position in the direction of the cross.
Typical Use Case
This Expert Advisor excels in automated trading and technical analysis on MetaTrader 5.
Compatible Platform & Setup
This Expert Advisor works on MetaTrader 5. Place the file in the MQL5/Experts folder and restart the terminal.
Description & Settings
Related: Moving Average Crossover Expert Advisor - another powerful expert for MetaTrader 5 traders.
Two moving averages on Gold (XAUUSD), forex, or any symbol: when the fast one crosses the slow one on a closed bar, the EA opens one position in the direction of the cross. Default is EMA 20 and EMA 50 on the close price, and method and price can be changed.Also recommended: Relative Moving Average EA - similar expert with strong performance on MetaTrader 5.
The trade has a stop loss and a take profit in points from the entry, and the opposite cross closes it if it is still open. An optional breakeven moves the stop to the entry plus a small offset once the trade is far enough in profit. The lot is fixed, or calculated so the stop costs a chosen percent of balance or equity.
A crossover system gives many small losses in a range and a few longer runs in a trend. That is how it works, not a defect. Test it on your broker with real ticks before any live money.
Settings
Strategy
Fast moving average period (20) and Slow moving average period (50). The fast period must be smaller.
Moving average method (EMA) and Applied price (close).
Reverse signals (false): sells on the bullish cross and buys on the bearish one.
Close the position on the opposite cross (true).
Lot settings
Lot mode: fixed lot, risk percent of balance or risk percent of equity.
Fixed lot (0.01) and Risk percent per trade (1.0). The risk modes need the stop loss on, or the EA will not start.
Close / Exit
Take profit (600 points, 0 = none).
Use a stop loss (true) and Stop loss (300 points).
Risk protection
Maximum positions of this EA at the same time (1).
Move the stop to breakeven (false), Breakeven starts at this profit (300 points) and Breakeven offset (20 points).
Filters
Use the time filter (false), Start hour (8) and End hour (20), in server time. A window that crosses midnight works too.
Expert settings
Magic number, Order comment, Max slippage (30 points) and Show status on chart (true).
What it needs
MetaTrader 5 with Algo Trading enabled. It works on hedging and netting accounts and only touches its own positions, by symbol and magic number.
The cross is read on the bar that just closed, once per new bar, so a signal never appears and disappears inside a candle.
Distances are in points. On a gold quote with 2 decimals 300 points are 3.00 in price, with 3 decimals they are 0.30. Adjust them to your symbol.
Before every order it checks that the free margin covers it. If not, the order is skipped and the log says why once per bar.
The breakeven respects the stops level of your broker.
Make it yours
Run your own backtest on XAUUSD, EURUSD, GBPUSD, US30 and NAS100 on M15, H1 and H4, and compare EMA 20/50 against 9/21 or 50/200. Turn on the time filter for the London and New York hours on gold, or add a news filter. The signal function is a few lines and can be replaced with any rule you want, for example a Smart Money Concepts break of structure or a support and resistance level, and the lot, margin check, breakeven and position count functions can be copied into another EA as they are. To manage the stop of any position, see the Trailing Stop Breakeven Manager.
About this code
Open source. Use it as it is, adapt it, build your own version on top of it.
New versions and new tools are announced first on the MQL5 channel.
Questions, suggestions and your own backtests are welcome in the Xander Lab group.
More tools on the profile.
Provided as is, use at your own risk.
Related
Trailing Stop Breakeven Manager
Partial Close Multi TP Manager
Xander Grid RSI Gold
Changelog
v1.0 - 2026-10-25 - Initial release
You may also like: index moving average expert advisor - excellent alternative for expert users on MetaTrader 5.
Source Code
#property copyright "RobotFX"
#property link "https://robotfx.org"
#property version "1.00"
#property description "This code is educational. Feel free to use, modify and upgrade as you wish. Visit ROBOTFX.ORG for professional trading tools"
#include <Trade\Trade.mqh>
enum ENUM_XP_LOT_MODE
{
XP_LOT_FIXED = 0,
XP_LOT_BALANCE = 1,
XP_LOT_EQUITY = 2
.......
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.