Gold Trading System with 70% win rate

MQ4 Expert Advisor (EA) for the gold trading system described requires coding the rules in MetaEditor (MQL4). Below is a step-by-step breakdown of how to build the EA, including key logic, entry/exit conditions, and risk management.


Step 1: Define EA Parameters (Input Variables)

These are adjustable in the MT4 settings:

// Input parameters
input double LotSize = 0.1;          // Fixed lot size
input int StopLoss = 30;            // SL in pips
input int TakeProfit = 60;          // TP in pips (1:2 risk ratio)
input int EMA_Fast_Period = 9;      // Fast EMA
input int EMA_Medium_Period = 21;   // Medium EMA
input int EMA_Slow_Period = 50;     // Slow EMA (trend filter)
input int MinCandleSize = 10;       // Min candle size (pips) to avoid noise
input bool UseCandlePatterns = true;// Enable candlestick patterns
input string TradeSession = "08:00-17:00"; // London/NY session

Step 2: Trend Filter (EMA Alignment)

Check if EMAs are aligned for uptrend (buy) or downtrend (sell):

bool IsUptrend() {
   double emaFast = iMA(NULL, 0, EMA_Fast_Period, 0, MODE_EMA, PRICE_CLOSE, 1);
   double emaMedium = iMA(NULL, 0, EMA_Medium_Period, 0, MODE_EMA, PRICE_CLOSE, 1);
   double emaSlow = iMA(NULL, 0, EMA_Slow_Period, 0, MODE_EMA, PRICE_CLOSE, 1);
   return (emaFast > emaMedium && emaMedium > emaSlow && Close[1] > emaSlow);
}

bool IsDowntrend() {
   double emaFast = iMA(NULL, 0, EMA_Fast_Period, 0, MODE_EMA, PRICE_CLOSE, 1);
   double emaMedium = iMA(NULL, 0, EMA_Medium_Period, 0, MODE_EMA, PRICE_CLOSE, 1);
   double emaSlow = iMA(NULL, 0, EMA_Slow_Period, 0, MODE_EMA, PRICE_CLOSE, 1);
   return (emaFast < emaMedium && emaMedium < emaSlow && Close[1] < emaSlow);
}

Step 3: Candlestick Pattern Detection

Detect bullish/bearish reversal patterns (e.g., engulfing, pin bars):

bool IsBullishEngulfing() {
   if (Close[1] > Open[1] && Open[2] > Close[2] && Close[1] > Open[2] && Open[1] < Close[2]) 
      return true;
   return false;
}

bool IsBearishEngulfing() {
   if (Close[1] < Open[1] && Open[2] < Close[2] && Close[1] < Open[2] && Open[1] > Close[2]) 
      return true;
   return false;
}

Step 4: Entry Logic

Combine trend + pullback + candlestick signal:

void CheckEntries() {
   // Check if in trading session (optional)
   if (!IsTradingSession()) return;

   // Long entry (Buy)
   if (IsUptrend() && IsPullbackToEMA21() && (IsBullishEngulfing() || !UseCandlePatterns)) {
      double sl = Bid - StopLoss * Point;
      double tp = Bid + TakeProfit * Point;
      OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, sl, tp, "Gold EA Buy", 0, 0, Green);
   }

   // Short entry (Sell)
   if (IsDowntrend() && IsPullbackToEMA21() && (IsBearishEngulfing() || !UseCandlePatterns)) {
      double sl = Ask + StopLoss * Point;
      double tp = Ask - TakeProfit * Point;
      OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, sl, tp, "Gold EA Sell", 0, 0, Red);
   }
}

Step 5: Exit Logic

Close trades when TP/SL is hit or trend reverses:

void CheckExits() {
   for (int i = OrdersTotal() - 1; i >= 0; i--) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         // Close if trend reverses
         if ((OrderType() == OP_BUY && IsDowntrend()) || (OrderType() == OP_SELL && IsUptrend())) {
            OrderClose(OrderTicket(), OrderLots(), OrderType() == OP_BUY ? Bid : Ask, 3, Red);
         }
      }
   }
}

Step 6: Full EA Structure

int OnInit() { return INIT_SUCCEEDED; }

void OnTick() {
   CheckExits();  // Close trades if needed
   CheckEntries(); // Open new trades
}

How to Use the EA

  1. Copy the code into MetaEditor (File → New → Expert Advisor).
  2. Compile (F7) and attach to an MT4 chart (XAU/USD M15/H1).
  3. Adjust parameters (e.g., lot size, SL/TP, EMAs).
  4. Backtest in Strategy Tester (Ctrl+R) to verify the 70%+ win rate.

Optimization Tips

  • Test different EMA periods (e.g., 5-9-21 vs. 9-21-50).
  • Adjust TP/SL ratios (e.g., 1:1.5 vs. 1:2).
  • Add trailing stops to maximize profits.

Would you like me to add additional features (e.g., news filter, trailing stop)? Let me know! 🚀

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *