Summary: Gold Trend Pulse EA is an MQL4 expert advisor for XAUUSD using EMA crossover for trend direction and RSI for entry filtering. Suitable for H1 timeframe with fixed stop loss and take profit.




Gold Trend Pulse EA is designed specifically for gold (XAUUSD) trading. It combines two exponential moving averages (EMA) to determine the overall trend direction and uses the RSI indicator to filter optimal entry points. The EA only enters trades when the trend is clearly defined and momentum is confirmed, reducing false signals during choppy markets. Each trade includes a fixed stop loss and take profit with a trailing stop option to lock in profits. The EA also features a daily loss limit and spread control.

Recommended Timeframe: H1
Trading Logic:
1. Trend Filter: Fast EMA (12) above Slow EMA (26) indicates uptrend; opposite indicates downtrend.
2. Entry Confirmation: In uptrend, RSI(14) must be above 50 but below 70 (not overbought). In downtrend, RSI must be below 50 but above 30 (not oversold).
3. Entry Signal: Buy when price closes above fast EMA and RSI > 50. Sell when price closes below fast EMA and RSI < 50.
4. Risk Control: Fixed SL (350 points) and TP (700 points) with optional trailing stop starting after 200 points profit.

```mql4
//+------------------------------------------------------------------+
//| GoldTrendPulseEA.mq4 |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict

//--- input parameters with comments
input double LotSize = 0.01; // Fixed lot size (0.01 lot for gold)
input int FastEMAPeriod = 12; // Fast EMA period for trend detection
input int SlowEMAPeriod = 26; // Slow EMA period for trend detection
input int RSIPeriod = 14; // RSI period for momentum filtering
input int StopLossPoints = 350; // Stop loss in points (350 points = 350 pips for gold)
input int TakeProfitPoints = 700; // Take profit in points (2:1 risk-reward)
input bool UseTrailingStop = true; // Enable trailing stop
input int TrailingStart = 200; // Trailing activates after profit reaches this many points
input int TrailingStep = 50; // Trailing stop distance in points
input int MagicNumber = 202415; // Unique EA identifier
input int MaxSpread = 40; // Maximum allowed spread (in points)
input double DailyLossLimit = 5.0; // Daily loss limit in percentage of balance

//--- global variables
double dailyStartBalance = 0;
datetime lastBarTime = 0;

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}

//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Daily equity protection
double currentEquity = AccountEquity();
double lossPercent = (dailyStartBalance - currentEquity) / dailyStartBalance * 100;
if(lossPercent >= DailyLossLimit)
{
Comment("Daily loss limit reached. No new trades.");
return;
}

// Spread filter
if(MarketInfo(Symbol(), MODE_SPREAD) > MaxSpread)
{
Comment("Spread too high: ", MarketInfo(Symbol(), MODE_SPREAD));
return;
}

// New bar logic (H1)
if(Time[0] == lastBarTime)
return;
lastBarTime = Time[0];

// Check for existing position
if(CountPositions() > 0)
{
if(UseTrailingStop)
ApplyTrailingStop();
return;
}

// Get indicator values
double fastEMA = iMA(Symbol(), PERIOD_H1, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double slowEMA = iMA(Symbol(), PERIOD_H1, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double rsi = iRSI(Symbol(), PERIOD_H1, RSIPeriod, PRICE_CLOSE, 1);
double close1 = iClose(Symbol(), PERIOD_H1, 1);

int cmd = -1;
double sl = 0, tp = 0;

// Uptrend condition: Fast EMA > Slow EMA, RSI between 50 and 70, price above fast EMA
if(fastEMA > slowEMA && rsi > 50 && rsi < 70 && close1 > fastEMA)
{
cmd = OP_BUY;
sl = SymbolInfoDouble(Symbol(), SYMBOL_BID) - StopLossPoints * Point;
tp = SymbolInfoDouble(Symbol(), SYMBOL_BID) + TakeProfitPoints * Point;
}
// Downtrend condition: Fast EMA < Slow EMA, RSI between 30 and 50, price below fast EMA
else if(fastEMA < slowEMA && rsi < 50 && rsi > 30 && close1 < fastEMA)
{
cmd = OP_SELL;
sl = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + StopLossPoints * Point;
tp = SymbolInfoDouble(Symbol(), SYMBOL_ASK) - TakeProfitPoints * Point;
}

if(cmd != -1)
{
int ticket = OrderSend(Symbol(), cmd, LotSize, (cmd==OP_BUY?Ask:Bid), 3, sl, tp, "Gold Trend Pulse", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("OrderSend failed: ", GetLastError());
}
}

//+------------------------------------------------------------------+
//| Apply trailing stop to open position |
//+------------------------------------------------------------------+
void ApplyTrailingStop()
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
double currentStop = OrderStopLoss();
double newStop = 0;

if(OrderType() == OP_BUY)
{
double profitPoints = (Bid - OrderOpenPrice()) / Point;
if(profitPoints >= TrailingStart)
{
newStop = Bid - TrailingStep * Point;
if(newStop > currentStop)
{
if(OrderModify(OrderTicket(), OrderOpenPrice(), newStop, OrderTakeProfit(), 0, clrNONE))
Print("Trailing stop updated for BUY #", OrderTicket());
}
}
}
else if(OrderType() == OP_SELL)
{
double profitPoints = (OrderOpenPrice() - Ask) / Point;
if(profitPoints >= TrailingStart)
{
newStop = Ask + TrailingStep * Point;
if(newStop < currentStop || currentStop == 0)
{
if(OrderModify(OrderTicket(), OrderOpenPrice(), newStop, OrderTakeProfit(), 0, clrNONE))
Print("Trailing stop updated for SELL #", OrderTicket());
}
}
}
break;
}
}
}
}

//+------------------------------------------------------------------+
//| Count open positions with this MagicNumber |
//+------------------------------------------------------------------+
int CountPositions()
{
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;
}
//+------------------------------------------------------------------+
```
Reference: Original MQL4 code for educational purposes.
Disclaimer: Trading Forex and Gold involves substantial risk of loss. This EA is provided as-is without any guarantee of profit. Always test on a demo account before live trading. Past performance does not guarantee future results.