Sure! Below is a detailed trading strategy for XAUUSD (Gold vs US Dollar) on MetaTrader 4 (MT4), using M15 timeframe and configurable conditions. This strategy includes the features you requested and is designed to be implemented as an Expert Advisor (EA) in MQL4.
Strategy Overview:
- Symbol: XAUUSD
- Timeframe: M15
- Lot Size: 0.05 (configurable)
- Entry Condition: Market indicator values (0.095, 2.0) — assumed to be custom indicator signals
- Features: Configurable options for trailing stop, trading time filters, one position per direction, candle filters, and martingale
Assumptions:
- The market indicator (0.095, 2.0) is a placeholder for a custom indicator that returns two values. We'll simulate this with a function.
- The EA will be coded in MQL4 for MT4.
MQL4 Expert Advisor Template:
Below is a simplified MQL4 EA template with the requested features:
//+------------------------------------------------------------------+
//| Expert Advisor for XAUUSD on M15 |
//+------------------------------------------------------------------+
#property strict
// Input Parameters
input double LotSize = 0.05;
input bool UseTrailingStop = true;
input int TrailingStop = 50; // in points
input bool UseTradingTime = true;
input int StartHour = 9;
input int EndHour = 17;
input bool OneTradePerDirection = true;
input bool UseCandleFilter = true;
input bool UseMartingale = false;
input double MartingaleMultiplier = 2.0;
// Global Variables
bool BuyTradeOpen = false;
bool SellTradeOpen = false;
int MagicNumber = 123456;
//+------------------------------------------------------------------+
//| Custom Indicator Simulation (Replace with actual indicator) |
//+------------------------------------------------------------------+
bool CheckEntrySignal(double &val1, double &val2)
{
// Simulate indicator values
val1 = iCustom(Symbol(), 0, "YourCustomIndicator", 0, 0);
val2 = iCustom(Symbol(), 0, "YourCustomIndicator", 1, 0);
if (val1 == 0.095 && val2 == 2.0)
return true;
return false;
}
//+------------------------------------------------------------------+
//| Check Candle Filter |
//+------------------------------------------------------------------+
bool CandleFilter()
{
double body = MathAbs(Open[1] - Close[1]);
double range = High[1] - Low[1];
if (body / range > 0.5) // Strong candle
return true;
return false;
}
//+------------------------------------------------------------------+
//| Check Trading Time |
//+------------------------------------------------------------------+
bool IsTradingTime()
{
if (!UseTradingTime) return true;
int hour = TimeHour(TimeCurrent());
return (hour >= StartHour && hour < EndHour);
}
//+------------------------------------------------------------------+
//| Check Open Trades |
//+------------------------------------------------------------------+
void CheckOpenTrades()
{
BuyTradeOpen = false;
SellTradeOpen = false;
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if (OrderType() == OP_BUY) BuyTradeOpen = true;
if (OrderType() == OP_SELL) SellTradeOpen = true;
}
}
}
}
//+------------------------------------------------------------------+
//| Entry Logic |
//+------------------------------------------------------------------+
void CheckEntry()
{
double val1, val2;
if (!CheckEntrySignal(val1, val2)) return;
if (!IsTradingTime()) return;
if (UseCandleFilter && !CandleFilter()) return;
CheckOpenTrades();
double lot = LotSize;
if (UseMartingale)
{
int lossCount = CountConsecutiveLosses();
lot = LotSize * MathPow(MartingaleMultiplier, lossCount);
}
if (val1 == 0.095 && val2 == 2.0)
{
if (!BuyTradeOpen || !OneTradePerDirection)
{
OrderSend(Symbol(), OP_BUY, lot, Ask, 3, 0, 0, "Buy Order", MagicNumber, 0, clrGreen);
}
}
else if (val1 == -0.095 && val2 == -2.0)
{
if (!SellTradeOpen || !OneTradePerDirection)
{
OrderSend(Symbol(), OP_SELL, lot, Bid, 3, 0, 0, "Sell Order", MagicNumber, 0, clrRed);
}
}
}
//+------------------------------------------------------------------+
//| Count Consecutive Losses (for Martingale) |
//+------------------------------------------------------------------+
int CountConsecutiveLosses()
{
int losses = 0;
for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if (OrderProfit() < 0)
losses++;
else
break;
}
}
}
return losses;
}
//+------------------------------------------------------------------+
//| Trailing Stop Logic |
//+------------------------------------------------------------------+
void ApplyTrailingStop()
{
if (!UseTrailingStop) return;
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if (OrderType() == OP_BUY)
{
if (Bid - OrderOpenPrice() > TrailingStop * Point)
{
double newSL = Bid - TrailingStop * Point;
if (OrderStopLoss() < newSL)
OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrGreen);
}
}
else if (OrderType() == OP_SELL)
{
if (OrderOpenPrice() - Ask > TrailingStop * Point)
{
double newSL = Ask + TrailingStop * Point;
if (OrderStopLoss() > newSL || OrderStopLoss() == 0)
OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrRed);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Main Function |
//+------------------------------------------------------------------+
void OnTick()
{
CheckEntry();
ApplyTrailingStop();
}
How to Use:
- Replace the CheckEntrySignal() function with your actual custom indicator logic.
- Compile the EA in MetaEditor and attach it to the XAUUSD chart on M15 timeframe.
- Configure the input parameters as needed.
Optional Enhancements:
- Add Stop Loss and Take Profit levels.
- Add logging and alerts.
- Add risk management (e.g., max drawdown, max trades per day).
Let me know if you want the full .mq4 file or help with integrating a specific indicator.