Gold (Xau/USD) trends exceptionally well, making momentum and trend-following approaches highly effective. This strategy waits for short-term momentum to align with a medium-term trend before executing a trade on the closing of a candle.
Asset: XAUUSD (Gold)Timeframe: M15 (15-Minute Chart)
Indicators:Fast EMA: 20-periodSlow EMA: 50-period RSI: 14-period
Buy Rules: The 20 EMA crosses above the 50 EMA AND the RSI closes above 50.
Sell Rules: The 20 EMA crosses below the 50 EMA AND the RSI closes below 50.
Risk Management: Set a fixed Stop Loss of 50 pips and a Take Profit of 100 pips (a 1:2 Risk-to-Reward ratio).
Disclaimer: Please run this on a DEMO account first. Gold is volatile and spreads can widen during NY/London overlap. Let me know your results or if you have ideas on how to improve the exit logic!
MT4 (MQL4) Expert Advisor Code
To use this, open your MetaEditor in MT4, click New > Expert Advisor, name it GoldenMomentum, and paste this code over the default template. Compile it, and it will appear in your Navigator panel.
Code: Select all
//+------------------------------------------------------------------+
//| Golden_Momentum_XAU.mq4 |
//| Open Source |
//+------------------------------------------------------------------+
#property strict
//--- Input parameters
input double LotSize = 0.01; // Lot Size
input int FastEMA = 20; // Fast EMA Period
input int SlowEMA = 50; // Slow EMA Period
input int RSIPeriod = 14; // RSI Period
input int StopLoss = 500; // Stop Loss (in points, 500 = 50 pips on 2-digit brokers)
input int TakeProfit = 1000; // Take Profit (in points, 1000 = 100 pips)
input int MagicNumber = 88888; // EA Magic Number to track trades
//+------------------------------------------------------------------+
//| Check if we already have open orders for this EA |
//+------------------------------------------------------------------+
int OpenOrdersCount()
{
int count = 0;
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
count++;
}
}
return count;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// 1. Ensure only one trade is open at a time
if(OpenOrdersCount() > 0) return;
// 2. Indicator Calculations (Shift 1 = previous closed candle, Shift 2 = candle before that)
// We use closed candles to prevent the EA from entering on a repainting signal
double emaFastCurrent = iMA(Symbol(), 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, 1);
double emaFastPrev = iMA(Symbol(), 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, 2);
double emaSlowCurrent = iMA(Symbol(), 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, 1);
double emaSlowPrev = iMA(Symbol(), 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, 2);
double rsiCurrent = iRSI(Symbol(), 0, RSIPeriod, PRICE_CLOSE, 1);
double pt = Point; // Broker's point value
// 3. Buy Setup: Fast EMA crosses above Slow EMA AND RSI > 50
if(emaFastPrev <= emaSlowPrev && emaFastCurrent > emaSlowCurrent && rsiCurrent > 50)
{
double sl = Ask - (StopLoss * pt);
double tp = Ask + (TakeProfit * pt);
int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, sl, tp, "Golden Momentum Buy", MagicNumber, 0, clrBlue);
if(ticket < 0) Print("Buy Order Failed! Error: ", GetLastError());
}
// 4. Sell Setup: Fast EMA crosses below Slow EMA AND RSI < 50
if(emaFastPrev >= emaSlowPrev && emaFastCurrent < emaSlowCurrent && rsiCurrent < 50)
{
double sl = Bid + (StopLoss * pt);
double tp = Bid - (TakeProfit * pt);
int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, sl, tp, "Golden Momentum Sell", MagicNumber, 0, clrRed);
if(ticket < 0) Print("Sell Order Failed! Error: ", GetLastError());
}
}
//+------------------------------------------------------------------+