Camovia Tray™

Blog · Knowledge

Close Orders in MQL4: A Practical Guide to OrderClose and Position Management

Camovia Tray Team · 2026-09-10

The chart was moving fast. A buy position that looked solid just minutes ago was now showing signs of reversal. You need to exit—and you need to do it now. But by the time you navigate from the chart to the Terminal window, locate the Trade tab, find the right position, right-click, select "Close Order," and confirm, the market has already moved against you.

If you've ever been in this situation, you know the problem isn't the trade itself—it's the friction between seeing what needs to happen and executing it. This is why understanding how to programmatically close orders in MQL4 is more than a technical exercise. It's about taking control of the gap between market movement and your response.

Understanding OrderClose in MQL4

In MQL4, closing a market order is handled by the OrderClose() function. The syntax is straightforward, but the details matter:

bool OrderClose( int ticket, double lots, double price, int slippage, color arrow_color )

The OrderTicket() identifier tells the terminal exactly which position to close. But in practice, many developers fall into a common trap: they try to close positions by looping through the orders pool and calling OrderClose() inside the loop without reselecting the order after the previous one closes .

Here's why that fails: when you close one position, the order pool shifts. The position that was at index 0 is gone, and everything shifts up. If you're iterating forward, you'll miss positions or try to close an order that no longer exists at that index, triggering error 4051: invalid ticket for OrderClose function .

The solution is either to iterate backward:

for(int i = OrdersTotal() - 1; i >= 0; i--)
{
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if(OrderSymbol() == Symbol())
        {
            OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE);
        }
    }
}

Or to use a while loop that always selects position 0 until none remain .

The Price Trap

Another subtle but critical detail: the close price you pass to OrderClose() must be correct for the order type. For a Buy position, you need the current Bid price. For a Sell position, you need the current Ask price .

A common mistake is hardcoding Bid for all orders:

// This will fail for Sell positions
OrderClose(OrderTicket(), OrderLots(), Bid, 3);

The proper approach checks the order type first:

if(OrderType() == OP_BUY) 
    price = Bid;
if(OrderType() == OP_SELL) 
    price = Ask;
OrderClose(OrderTicket(), OrderLots(), price, 3, clrNONE);

For multi-symbol EAs, you must also fetch the correct price for each symbol using MarketInfo(OrderSymbol(), MODE_BID) or MODE_ASK .

Why Manual Close Is Still the Default

Despite these technical nuances being well-documented, most traders still close positions the manual way—because writing and testing an EA just for order management feels like overkill for a routine task. The risk of accidentally closing the wrong position due to a coding bug often outweighs the convenience.

But the underlying friction remains: you still have to leave your chart view, find the position in the Terminal, and go through a confirmation step. The mental context switch alone can cost you a few pips in a fast-moving market.

A Different Approach to Closing Positions

What if the act of closing a position could live where you already are—without opening the full MT4 terminal?

This is where the idea behind Camovia Tray comes into play. It rethinks the workflow: instead of requiring traders to navigate through the full terminal interface to manage positions, it surfaces position data directly from the system tray. The key insight is that viewing open positions—their current P/L, entry price, and volume—and acting on them doesn't require the full terminal window to be open and in focus .

The OrderClose() function is still the mechanism. But the user experience changes: a trader can see their open positions in a compact view and close a position with a tap, without having to interrupt their chart analysis to hunt through the Terminal window. The same safety mechanisms apply—like requiring MT4's AutoTrading (Ctrl+E) to be enabled for close operations—but the friction of navigating to the order window is removed.

Making Order Management Part of Your Flow

Understanding OrderClose() and the loop mechanics is essential if you're building custom EAs. But for daily trading, the goal isn't to write code—it's to reduce the time between decision and execution. Whether that's done through a carefully coded EA or through a tool that surfaces position management to where you're already working, the principle is the same: get the friction out of the way, so you can focus on the market, not the interface.

Turn MT5 / MT4 into a Tray Tool

Check quotes, manage positions, and hide in one click.

Download Camovia Tray

Frequently Asked Questions

Which systems and terminals are supported?

Windows 10 / 11, with MetaTrader 5 or MetaTrader 4 (installed and logged in; MT4 needs the bridge EA attached once).

Is the MT4 bridge EA safe? What do I need to enable?

Yes. The bridge EA (CamoviaBridge) is bundled with the app - it only reads quotes/positions locally and executes close commands; no DLLs, no data uploads. Closing positions requires turning on AutoTrading in the MT4 toolbar.

How much does it cost? Is there a free trial?

Subscription pricing starts at $2.49/month (also $6.99/3 months, $13.49/6 months, $23.99/year), all plans with full features. New users can try it free before subscribing.

How do I restore the MT5/MT4 window after hiding it?

Choose "Show MT5/MT4" from the system tray menu and the window returns to its previous position. You can also hover the tray to check quotes and manage positions without opening the terminal.

Popular Symbols Trading Hours

Open/close times, weekend hours & live market status: