Running several Expert Advisors on one MT4 account creates a subtle operational risk: an EA that loops through every open order may accidentally trail another strategy's stop, delete its pending entry, or close a discretionary trade. The trading signal may be perfectly valid while the order-management layer is wrong. The solution is to define an explicit ownership boundary before any trade operation.

Why Magic Number alone is not always sufficient

MT4's OrderSend() accepts a magic number assigned by the EA. OrderMagicNumber() reads it from the selected order. OrdersTotal() counts open market and pending orders across the account, not just the chart. Therefore, combine the exact symbol with a deliberately allocated nonzero magic number. A manual order often has magic zero, but zero is not proof of manual origin because an EA can also use zero.

For a single-symbol EA, use the current chart's Symbol() and a stable magic number per independent strategy instance. A multi-symbol EA should instead use its explicitly configured symbol ownership map. Do not change identifiers on every restart: existing orders retain their original identifiers.

Worked example: five orders, two eligible tickets

Assume EA A manages EURUSD with magic 4101; EA B uses magic 4202. The following order pool is hypothetical. Ticket 701 is the only eligible market order; ticket 705 is an eligible pending order, but must not be processed by market-order exit logic. Ticket 703 has matching magic but a different symbol; ticket 704 has magic zero; ticket 702 belongs to EA B.

How to Isolate EA Order Management with Magic Numbers and Symbol Filters
Hypothetical order pool: only tickets 701 and 705 belong to EA A; market and pending management remain separate.

Implement a reusable ownership check

The following MQL4 illustrative excerpt is not a complete executable EA. Call InspectOwnedOrders() from OnTick or a throttled OnTimer handler as appropriate. It only logs matching orders; it does not execute trades. Use the same StrategyMagic input in the magic argument of OrderSend when creating orders.

input int StrategyMagic = 4101;

bool IsOwnedOpenOrder(int ticket)
{
    if(!OrderSelect(ticket, SELECT_BY_TICKET))
        return false;
    if(OrderCloseTime() != 0)
        return false;
    return OrderSymbol() == Symbol()
        && OrderMagicNumber() == StrategyMagic;
}

void InspectOwnedOrders()
{
    for(int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            continue;
        int ticket = OrderTicket();
        if(!IsOwnedOpenOrder(ticket))
            continue;
        int type = OrderType();
        if(type == OP_BUY || type == OP_SELL)
            Print("Market order: ", ticket);
        else
            Print("Pending order: ", ticket);
    }
}

OrderSelect by position is useful for discovery; its ordering is not guaranteed. Selecting by ticket again refreshes the copied order properties and confirms the order is still active. When selecting by ticket, the pool parameter does not restrict selection to open trades, which is why checking OrderCloseTime is essential. Before every actual modification, deletion or close, repeat ownership and type checks and use fresh prices and order details. If selection fails, skip the action and log the error where needed.

Separate management policies

For market orders, consider trailing stops, protective exits and partial closing rules. For pending orders, implement expiration, invalidation and cancellation rules. A market close uses OrderClose; pending cancellation uses OrderDelete. OrderModify can change supported properties of either order class, but entry-price modification applies to pending orders. Check return values, broker stop and freeze levels, and the latest relevant bid/ask. Never assume that a successful ownership check guarantees execution.

Design for shared accounts and restarts

Keep a registry of magic numbers by account, strategy and instance. If two independent EURUSD instances share 4101, both pass the filter and may compete over stops. Allocate separate identifiers unless deliberate shared management is required. Changing timeframe alone does not isolate orders. When upgrading an EA, preserve its identifier or explicitly reconcile old tickets before migration. Magic numbers are conventions, not security permissions or concurrency locks.

If a loop performs trade operations, snapshot candidate tickets first where practical; closing or deleting orders changes the order pool. Re-select and validate each ticket immediately before acting. A changing pool can still cause skipped entries during a scan, so reconcile again on a later event. Avoid tight retry loops on broker errors and do not use order comments as the sole ownership key.

Validation before deployment

  • Recreate the five-ticket example on a demo account; only 701 and 705 should be logged.
  • Open another EURUSD order under magic 4202 and confirm EA A leaves it untouched.
  • Restart EA A with the original identifier and verify it recognizes existing eligible orders.
  • Cancel an eligible pending ticket between discovery and action; confirm no trade action follows a failed or historical selection.
  • Test broker symbol suffixes and deliberately duplicated magic numbers. Use the exact broker symbol and resolve collisions.

The MT4 Strategy Tester is useful for testing the filter logic, but a single-EA historical run does not fully reproduce concurrent EAs and manual orders sharing an account. Demo testing with multiple instances is important. Live fills, costs, slippage and gaps can still produce losses; ownership filtering reduces operational interference, not market risk.

Official references: OrderSelect and OrderMagicNumber. The excerpt has not been compiled or executed here.

For help integrating ownership filters into a portfolio of EAs or adapting an existing strategy, explore our Custom Trading Software Development Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.