MQL4 does not utilize the standard CTrade library, relying instead on raw OrderSend() functions and required price normalizations. Save this as an .mq4 file.
Code: Select all
//+------------------------------------------------------------------+
//| D1M15_Sweep_MT4.mq4 |
//+------------------------------------------------------------------+
#property strict
input double RiskPercent = 1.0; // Risk Per Trade (%)
input double RiskRewardRatio = 2.0; // Risk/Reward Ratio
input int MagicNumber = 123456; // EA Magic Number
void OnTick()
{
static datetime lastTime = 0;
datetime currentTime = iTime(Symbol(), PERIOD_M15, 0);
if(currentTime == lastTime || currentTime == 0) return;
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
return;
}
}
lastTime = currentTime;
double pdh = iHigh(Symbol(), PERIOD_D1, 1);
double pdl = iLow(Symbol(), PERIOD_D1, 1);
if(pdh == 0 || pdl == 0) return;
double m15Open = iOpen(Symbol(), PERIOD_M15, 1);
double m15High = iHigh(Symbol(), PERIOD_M15, 1);
double m15Low = iLow(Symbol(), PERIOD_M15, 1);
double m15Close = iClose(Symbol(), PERIOD_M15, 1);
bool bearishSweep = (m15High > pdh) && (m15Close < pdh) && (m15Open < pdh);
bool bullishSweep = (m15Low < pdl) && (m15Close > pdl) && (m15Open > pdl);
if(bearishSweep)
{
double hardStop = m15High;
double entryPrice = Bid;
double riskAmount = AccountEquity() * (RiskPercent / 100.0);
double tickSize = MarketInfo(Symbol(), MODE_TICKSIZE);
double tickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
double stopLossTicks = (hardStop - entryPrice) / tickSize;
if(stopLossTicks <= 0) return;
double rawLots = riskAmount / (stopLossTicks * tickValue);
double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
double minLot = MarketInfo(Symbol(), MODE_MINLOT);
double maxLot = MarketInfo(Symbol(), MODE_MAXLOT);
double lots = MathFloor(rawLots / lotStep) * lotStep;
if(lots > maxLot) lots = maxLot;
if(lots >= minLot)
{
double tp = entryPrice - ((hardStop - entryPrice) * RiskRewardRatio);
// MT4 requires StopLoss and TakeProfit to be strictly normalized to chart digits
hardStop = NormalizeDouble(hardStop, Digits);
tp = NormalizeDouble(tp, Digits);
int ticket = OrderSend(Symbol(), OP_SELL, lots, entryPrice, 3, hardStop, tp, "Sweep Short", MagicNumber, 0, clrRed);
}
}
else if(bullishSweep)
{
double hardStop = m15Low;
double entryPrice = Ask;
double riskAmount = AccountEquity() * (RiskPercent / 100.0);
double tickSize = MarketInfo(Symbol(), MODE_TICKSIZE);
double tickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
double stopLossTicks = (entryPrice - hardStop) / tickSize;
if(stopLossTicks <= 0) return;
double rawLots = riskAmount / (stopLossTicks * tickValue);
double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
double minLot = MarketInfo(Symbol(), MODE_MINLOT);
double maxLot = MarketInfo(Symbol(), MODE_MAXLOT);
double lots = MathFloor(rawLots / lotStep) * lotStep;
if(lots > maxLot) lots = maxLot;
if(lots >= minLot)
{
double tp = entryPrice + ((entryPrice - hardStop) * RiskRewardRatio);
hardStop = NormalizeDouble(hardStop, Digits);
tp = NormalizeDouble(tp, Digits);
int ticket = OrderSend(Symbol(), OP_BUY, lots, entryPrice, 3, hardStop, tp, "Sweep Long", MagicNumber, 0, clrBlue);
}
}
}