Summary: Gold High-Yield Hunter EA is a high-profit MQL4 expert advisor for XAUUSD. It uses breakout from 20-period high/low with martingale recovery and trailing stop. Suitable for M15 timeframe.
Gold High-Yield Hunter EA is designed specifically for gold (XAUUSD) to capture high-profit opportunities while maintaining low risk through smart position sizing. The EA uses a breakout strategy based on 20-period high/low channel with a confirmation filter. When a breakout occurs, it enters with a base lot. If the trade goes against the position, a controlled grid recovery system activates with increasing lot size but capped at maximum levels. A dynamic trailing stop locks in profits as price moves favorably. The EA includes daily profit target and drawdown protection to secure gains.
Recommended Timeframe: M15
Trading Logic:
1. Breakout Detection: Price closes above 20-period high → buy; below 20-period low → sell.
2. Confirmation: Second consecutive close beyond the level for fakeout reduction.
3. Grid Recovery: If position moves against by RecoveryStep points, open additional layer with lot multiplier (max 3 levels).
4. Risk Control: Max spread 40 points, daily profit target 10% (auto-stop trading), daily loss limit 5%, maximum grid levels 3.
```mql4
//+------------------------------------------------------------------+
//| GoldHighYieldHunter.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict
//--- input parameters
input double BaseLotSize = 0.01; // Base lot size for initial entry
input double LotMultiplier = 1.5; // Multiplier for grid recovery levels (1.5x)
input int BreakoutPeriod = 20; // Period for high/low breakout channel
input int RecoveryStep = 200; // Points to trigger next grid level (200 points)
input int TrailingStart = 300; // Points profit to start trailing stop
input int TrailingStep = 100; // Trailing stop distance in points
input int MaxGridLevels = 3; // Maximum grid recovery levels (1-5)
input int StopLossPoints = 500; // Hard stop loss in points (500)
input int TakeProfitPoints = 800; // Hard take profit in points (800)
input int MagicNumber = 202414; // Unique EA identifier
input int MaxSpread = 40; // Maximum allowed spread (in points)
input double DailyProfitTarget = 10.0; // Daily profit target in percentage (stop trading)
input double DailyLossLimit = 5.0; // Daily loss limit in percentage
input bool UseCloseOnFriday = true; // Close all trades before Friday 20:00
//--- global variables
double dailyStartBalance = 0;
datetime lastBarTime = 0;
bool isFridayCloseExecuted = false;
bool dailyTargetReached = false;
int currentGridLevel = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
isFridayCloseExecuted = false;
dailyTargetReached = false;
currentGridLevel = 0;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Daily profit target reached
double currentProfit = AccountProfit();
double profitPercent = (currentProfit / dailyStartBalance) * 100;
if(profitPercent >= DailyProfitTarget && DailyProfitTarget > 0)
{
if(!dailyTargetReached)
{
dailyTargetReached = true;
CloseAllOrders();
Comment("Daily profit target reached. No new trades.");
}
return;
}
// Daily loss protection
double currentEquity = AccountEquity();
double lossPercent = (dailyStartBalance - currentEquity) / dailyStartBalance * 100;
if(lossPercent >= DailyLossLimit)
{
if(CountPositions() > 0) CloseAllOrders();
Comment("Daily loss limit reached.");
return;
}
// Friday close before weekend
if(UseCloseOnFriday && !isFridayCloseExecuted)
{
datetime currentTime = TimeCurrent();
if(TimeDayOfWeek(currentTime) == 5 && TimeHour(currentTime) >= 20)
{
CloseAllOrders();
isFridayCloseExecuted = true;
return;
}
if(TimeDayOfWeek(currentTime) != 5)
isFridayCloseExecuted = false;
}
// Spread filter
if(MarketInfo(Symbol(), MODE_SPREAD) > MaxSpread)
{
Comment("Spread too high: ", MarketInfo(Symbol(), MODE_SPREAD));
return;
}
// Trailing stop management for existing positions
if(CountPositions() > 0)
{
ManageTrailingStop();
ManageGridRecovery();
return;
}
// New bar logic (M15)
if(Time[0] == lastBarTime)
return;
lastBarTime = Time[0];
currentGridLevel = 0;
// Reset daily balance at new day
if(TimeDayOfYear(TimeCurrent()) != TimeDayOfYear(Time[0]))
{
dailyStartBalance = AccountBalance();
dailyTargetReached = false;
}
// Calculate breakout levels
double high20 = iHigh(Symbol(), PERIOD_M15, iHighest(Symbol(), PERIOD_M15, MODE_HIGH, BreakoutPeriod, 1));
double low20 = iLow(Symbol(), PERIOD_M15, iLowest(Symbol(), PERIOD_M15, MODE_LOW, BreakoutPeriod, 1));
double close1 = iClose(Symbol(), PERIOD_M15, 1);
double close2 = iClose(Symbol(), PERIOD_M15, 2);
int cmd = -1;
double sl = 0, tp = 0;
// Buy breakout: close above high20 with confirmation
if(close1 > high20 && close2 > high20)
{
cmd = OP_BUY;
sl = SymbolInfoDouble(Symbol(), SYMBOL_BID) - StopLossPoints * Point;
tp = SymbolInfoDouble(Symbol(), SYMBOL_BID) + TakeProfitPoints * Point;
}
// Sell breakout: close below low20 with confirmation
else if(close1 < low20 && close2 < low20)
{
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, BaseLotSize, (cmd==OP_BUY?Ask:Bid), 3, sl, tp, "Gold Hunter", MagicNumber, 0, clrNONE);
if(ticket > 0)
{
currentGridLevel = 1;
Print("Initial entry opened. Level: ", currentGridLevel);
}
else
Print("OrderSend failed: ", GetLastError());
}
}
//+------------------------------------------------------------------+
//| Manage trailing stop for all open positions |
//+------------------------------------------------------------------+
void ManageTrailingStop()
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
double profitPoints = 0;
double newSL = 0;
if(OrderType() == OP_BUY)
{
profitPoints = (Bid - OrderOpenPrice()) / Point;
if(profitPoints >= TrailingStart)
{
newSL = Bid - TrailingStep * Point;
if(newSL > OrderStopLoss())
OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
}
}
else if(OrderType() == OP_SELL)
{
profitPoints = (OrderOpenPrice() - Ask) / Point;
if(profitPoints >= TrailingStart)
{
newSL = Ask + TrailingStep * Point;
if(newSL < OrderStopLoss() || OrderStopLoss() == 0)
OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Manage grid recovery when price moves against position |
//+------------------------------------------------------------------+
void ManageGridRecovery()
{
if(currentGridLevel >= MaxGridLevels) return;
double avgOpenPrice = 0;
double totalLots = 0;
int direction = 0;
double worstPrice = 0;
// Calculate average open price and direction
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
avgOpenPrice += OrderOpenPrice() * OrderLots();
totalLots += OrderLots();
if(direction == 0)
direction = OrderType();
if(OrderType() == OP_BUY)
worstPrice = MathMin(worstPrice, OrderOpenPrice());
else
worstPrice = MathMax(worstPrice, OrderOpenPrice());
}
}
}
if(totalLots > 0)
avgOpenPrice /= totalLots;
else
return;
// Calculate adverse move in points
double adversePoints = 0;
if(direction == OP_BUY)
adversePoints = (avgOpenPrice - Bid) / Point;
else if(direction == OP_SELL)
adversePoints = (Ask - avgOpenPrice) / Point;
// Trigger new grid level
if(adversePoints >= RecoveryStep * currentGridLevel && currentGridLevel < MaxGridLevels)
{
double newLotSize = BaseLotSize * MathPow(LotMultiplier, currentGridLevel);
newLotSize = MathMin(newLotSize, BaseLotSize * MathPow(LotMultiplier, MaxGridLevels-1));
int cmd = direction; // Same direction as existing positions
double sl = 0, tp = 0;
if(cmd == OP_BUY)
{
sl = SymbolInfoDouble(Symbol(), SYMBOL_BID) - StopLossPoints * Point;
tp = SymbolInfoDouble(Symbol(), SYMBOL_BID) + TakeProfitPoints * Point;
}
else if(cmd == OP_SELL)
{
sl = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + StopLossPoints * Point;
tp = SymbolInfoDouble(Symbol(), SYMBOL_ASK) - TakeProfitPoints * Point;
}
int ticket = OrderSend(Symbol(), cmd, newLotSize, (cmd==OP_BUY?Ask:Bid), 3, sl, tp, "Gold Hunter", MagicNumber, 0, clrNONE);
if(ticket > 0)
{
currentGridLevel++;
Print("Grid level ", currentGridLevel, " opened. Lot: ", newLotSize);
}
}
}
//+------------------------------------------------------------------+
//| 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;
}
//+------------------------------------------------------------------+
//| Close all orders for this symbol and magic |
//+------------------------------------------------------------------+
void CloseAllOrders()
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(OrderType() == OP_BUY)
OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrNONE);
else if(OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
}
}
}
currentGridLevel = 0;
}
//+------------------------------------------------------------------+
```
Reference: Original MQL4 code for educational purposes.
Disclaimer: Gold trading involves high risk. This EA uses grid recovery which can amplify losses. Provided as-is without profit guarantee. Test extensively on demo before live trading. Past performance does not guarantee future results.