Fresh MQL5 code release – perfect for enhancing your MT4/MT5 strategies.
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.
Smooth out market noise with the Heiken Ashi Expert Advisor for MT4 and MT5. Reliable trend-following automation. Details here.
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
- Trailing Stop Breakeven Manager
- Partial Close Multi TP Manager
- Xander Grid RSI Gold
Changelog
- v1.0 - 2026-10-25 - Initial release
Thanks for reading. Discover premium MetaTrader solutions at RobotFX.
79234