Here’s a template Expert Advisor for MT4 (MQL4) that trades XAUUSD on M15, lot size 0.05, and opens a new trade every time your “market indication” function (using two parameters: IndParam1 and IndParam2) fires a signal. All of the extra features you requested (trading stop, time filters, single‐direction limits, candle filters, martingale) can be turned on/off via inputs.
You can copy & paste this into MetaEditor as “XAU_MarketInd_EA.mq4” and compile.
//+------------------------------------------------------------------+
//| XAU_MarketInd.mq4
//| Configurable XAUUSD strategy on M15 |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property link ""
#property version "1.00"
#property strict
//--- EA Inputs
extern double Lots = 0.05; // Base lot size
extern int MagicNumber = 202304; // Unique ID
extern bool EnableTradingStop = true; // Enable SL/TP?
extern double StopLoss_Points = 200; // Stop loss in points (XAUUSD points=0.01)
extern double TakeProfit_Points = 400; // Take profit in points
extern bool EnableTimeFilter = false; // Enable trading time window?
extern int TradeStartHour = 8; // Hour of day 0–23
extern int TradeEndHour = 18;
extern bool EnableOneDir = true; // Only one open per direction?
extern bool EnableCandleFilter = false; // Candle size filter?
extern double MinBodySize_Points = 30; // e.g. 30 points = 0.30$
extern bool EnableMartingale = false; // Martingale on loss?
extern double MartingaleFactor = 2.0; // Lot × factor after loss
// Market‐indication parameters
extern double IndParam1 = 0.095; // Threshold for our custom indicator
extern int IndParam2 = 2; // Period for custom indicator
//+------------------------------------------------------------------+
//| Expert initialization |
//+------------------------------------------------------------------+
int OnInit()
{
// Nothing special for now
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator: here linear‐regression‐slope over IndParam2 |
//+------------------------------------------------------------------+
double MarketIndication()
{
// Compute slope of linear regression on close prices over IndParam2 bars
double lr_slope = iCustom(Symbol(),Period(),
"Examples\\LinearRegSlope", // Make sure file is in /MQL4/Indicators
IndParam2, 0, 0);
// If you don’t have LR slope indicator, simply use:
// lr_slope = (Close[0] - Close[IndParam2]) / IndParam2;
return(lr_slope);
}
//+------------------------------------------------------------------+
//| Check time filter |
//+------------------------------------------------------------------+
bool IsInTradeTime()
{
if(!EnableTimeFilter) return(true);
int h = TimeHour(TimeCurrent());
if(TradeStartHour < TradeEndHour)
return(h >= TradeStartHour && h < TradeEndHour);
else // e.g. overnight window
return(h >= TradeStartHour || h < TradeEndHour);
}
//+------------------------------------------------------------------+
//| Count open orders by direction |
//+------------------------------------------------------------------+
int CountOpen(int cmd)
{
int cnt=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS)
&& OrderMagicNumber()==MagicNumber
&& OrderSymbol()==Symbol()
&& OrderType()==cmd)
cnt++;
}
return(cnt);
}
//+------------------------------------------------------------------+
//| Place a Market Order |
//+------------------------------------------------------------------+
bool SendMarketOrder(int cmd,double lots)
{
double price = (cmd==OP_BUY) ? Ask : Bid;
double sl=0, tp=0;
if(EnableTradingStop)
{
if(cmd==OP_BUY)
{
sl = price - StopLoss_Points*_Point;
tp = price + TakeProfit_Points*_Point;
}
else
{
sl = price + StopLoss_Points*_Point;
tp = price - TakeProfit_Points*_Point;
}
}
int ticket = OrderSend(Symbol(), cmd, lots, price,
3, sl, tp,
"XAU_MktInd", MagicNumber, 0, clrOrange);
return(ticket>0);
}
//+------------------------------------------------------------------+
//| Expert tick |
//+------------------------------------------------------------------+
void OnTick()
{
if(Period()!=PERIOD_M15) return; // guard timeframe
if(!IsInTradeTime()) return; // time filter
double slope = MarketIndication();
double lots = Lots;
// Check last trade for martingale
if(EnableMartingale)
{
// if last closed order was a loss, muliply lot
if(HistorySelect(TimeCurrent()-86400, TimeCurrent()))
{
int totalHist = HistoryTotal();
if(totalHist>0)
{
if(HistoryOrderSelect(totalHist-1,SELECT_BY_POS))
{
if(HistoryOrderMagicNumber()==MagicNumber
&& HistoryOrderSymbol()==Symbol()
&& HistoryOrderProfit()<0)
lots = Lots * MartingaleFactor;
}
}
}
}
// CANDLE FILTER
if(EnableCandleFilter)
{
double body = MathAbs(Open[1]-Close[1]);
if(body < MinBodySize_Points*_Point) return;
}
// BUY logic: slope > threshold
if(slope > IndParam1)
{
if(!EnableOneDir || CountOpen(OP_BUY)==0)
SendMarketOrder(OP_BUY, lots);
}
// SELL logic: slope < -threshold
if(slope < -IndParam1)
{
if(!EnableOneDir || CountOpen(OP_SELL)==0)
SendMarketOrder(OP_SELL, lots);
}
}
//+------------------------------------------------------------------+
Input Parameters
• Lots – Starting position size
• EnableTradingStop – Use SL/TP?
• StopLoss_Points – SL in points (0.01 = 1 point for XAUUSD)
• TakeProfit_Points – TP in points
• EnableTimeFilter – Restrict hours
• TradeStartHour – First tradable hour (0–23)
• TradeEndHour – Last tradable hour (0–23)
• EnableOneDir – Only one buy & one sell open at a time
• EnableCandleFilter – Require previous candle body ≥ MinBodySize
• MinBodySize_Points – Minimum candle body in points
• EnableMartingale – On loss, multiply lot by MartingaleFactor
• MartingaleFactor – e.g. 2 = double size after losing trade
• IndParam1 – Threshold for our “market indication” (e.g. slope)
• IndParam2 – Period used by the indication function
How It Works
- On each M15 tick, we compute a simple “market indication” via a linear‑regression‑slope over IndParam2 bars (or a vanilla price‐diff if you don’t have an LR indicator).
- If slope > +IndParam1 → Buy ; if slope < –IndParam1 → Sell.
- We check all enabled filters/time windows.
- If allowed, we send a market order with optional SL/TP.
- When martingale is on, we look at the last closed trade: if it lost, we multiply the base lot by the factor.
Next Steps
• Tweak IndParam1/2 to match your “market indicates (0.095, 2.0).”
• Backtest across different spreads & slippages.
• Add your favorite candle‐pattern or momentum filters as needed.
Let me know if you need any refinements! 🚀