Watch how to download trading robots for free
Find us on Facebook!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Experts

Moving Average Crossover EA Gold - expert for MetaTrader 5

Cristhian Alexander Gaibor Cuasquer
Cristhian Alexander Gaibor Cuasquer
  • Founder & Systems Engineer at  Xander Pro
  • Ecuador
  • 7596
4.8 (38)
Trader and MQL5 member since 2017.
Views:
65
Rating:
(1)
Published:
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

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.

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.

Moving Average Crossover EA Gold opening one position per EMA 20 and 50 cross on a XAUUSD chart in MetaTrader 5

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 my 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 my MQL5 channel: https://www.mql5.com/en/channels/xander

Questions, suggestions and your own backtests are welcome in the Xander Lab group: https://www.mql5.com/en/messages/02eec85e4364dd01

More tools on my profile: https://www.mql5.com/en/users/09151993a

Provided as is, use at your own risk.

Related

Changelog

  • v1.0 - 2026-10-25 - Initial release
Auto Fibonacci Retracement Swing Levels Auto Fibonacci Retracement Swing Levels

Auto Fibonacci retracement for Gold (XAUUSD), forex and any symbol: draws the levels on the last completed swing leg by itself, with the 61.8 in its own colour, prices on each level and an optional alert. Open source code.

Forex Session Boxes London New York Asia Forex Session Boxes London New York Asia

Forex session boxes for Gold (XAUUSD), forex and any symbol: draws the Asian, London and New York sessions with their high and low extended to the right and the range in points, in your broker server time. Open source code.

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

MACD Signals MACD Signals

Indicator edition for new platform.