MQL4 CODE BASICS

OrderClose & OrderModify not working in MQL4 (Super Simple Guide)

If your EA won’t close trades or won’t move stop loss… don’t worry. Most of the time it’s just one small mistake. This page explains it in the simplest way possible.

Big idea: These functions return true or false

true means it worked.
false means it failed.
If it fails, you should print the error number so you know why.

bool ok = OrderClose(ticket, lots, price, slippage, clrRed);

if(!ok)

{
   Print("OrderClose failed. Error = ", GetLastError());
}

1) OrderClose() (Closing a trade)

To close a trade, you MUST use the correct price:

That’s it. This is the #1 reason OrderClose fails.

✅ Simple correct close code

// First select an order, then:

RefreshRates(); // get fresh Bid/Ask

double closePrice = (OrderType() == OP_BUY) ? Bid : Ask;

closePrice = NormalizeDouble(closePrice, Digits);

bool ok = OrderClose(OrderTicket(), OrderLots(), closePrice, 3, clrRed);

if(!ok)
{
   Print("OrderClose failed. Error = ", GetLastError());
}
else
{
   Print("Trade closed!");
}

2) OrderModify() (Moving Stop Loss / Take Profit)

OrderModify is picky. You can’t say “only change stop loss”. You must send ALL the important values again.

✅ Simple “Move stop to break-even” example

// Select the order first:

RefreshRates();

double newSL = OrderOpenPrice();            // break-even price

newSL = NormalizeDouble(newSL, Digits);

bool ok = OrderModify(
   OrderTicket(),
   OrderOpenPrice(),       // keep same open price
   newSL,                  // new stop loss
   OrderTakeProfit(),      // keep same take profit
   0,
   clrYellow
);

if(!ok)
{
   Print("OrderModify failed. Error = ", GetLastError());
}
else
{
   Print("Stop moved!");
}

The 3 most common reasons these fail

✅ Reason #1: Wrong close price

BUY closes with Bid, SELL closes with Ask.

✅ Reason #2: Stop loss too close

Some brokers don’t allow stops too close to the price. So if your stop is “too close”, OrderModify fails.

int stopLevel = (int)MarketInfo(Symbol(), MODE_STOPLEVEL);

double minDist = stopLevel * Point;

if(MathAbs(Bid - newSL) < minDist)
{
   Print("Stop is too close. Move it farther away.");
}

✅ Reason #3: Not printing the error

If you don’t print the error, you won’t know what happened. Always do this:

if(!ok)
{
   Print("Failed. Error = ", GetLastError());
}

SUPER SIMPLE RULES (remember these)

That’s it. If you follow those rules, your order code will work way more often.

Click here to return to MQL4 Basics Directory