NewFree guide: The EA Reality Check
BytePip
← Guides Guide: trade management

How to close all trades at a specific time in MT5 (by magic number)

MetaTrader 5 has no built-in option to close positions at a set time. Here are the ways to do it, what can go wrong at the moment of the close, and a setup that closes each Expert Advisor at its own time.

Why close at a fixed time

Many strategies are built around a window of the day. A London breakout EA has no business holding a trade into the Asian session, a scalper should not sit through the daily rollover with its wider spreads, and a lot of traders want to be flat before the weekend gap. Prop firms add their own reasons: some do not allow holding over the weekend or around news.

If the EA has its own "close at" input, use it. The problem starts when it does not, when you run several copies of the same EA that need different times, or when the trades are manual.

Three ways to do it in MT5

  • By hand. Right click in the Trade tab and close. It works until the day you are not at the screen, which is exactly when it matters.
  • A "close all" script. One click closes everything, but you still have to be there to click, and it cannot tell one EA from another.
  • A scheduled close EA. It runs on a spare chart, watches the clock and closes what you told it to close, at the time you set, every day. This is the only one that works while you sleep.

Magic numbers: closing one EA and not the others

Every order an Expert Advisor sends carries a magic number, an ID that tells its trades apart from the rest. Manual trades have magic number 0. Most EAs have an input to set it, and if you run the same EA on several charts, give each copy a different one.

Closing by magic number is what lets you end the London EA at 17:00 while the New York EA keeps running until 22:00, on the same account. One rule to remember: on a netting account there is only one position per symbol, so two EAs on the same symbol share it. To close them at different times you need a hedging account.

Step by step: one line per close

This is the setup with Auto Close Trades by Time and Magic Number. Each close is a line of the form [days] HH:MM = magic numbers, separated by semicolons.

  1. Write down the magic number of each EA from its inputs. Use 0 for manual trades and * for every trade of the account.
  2. Attach the utility to any spare chart. It watches the whole account, whatever the chart symbol.
  3. Write your close lines in Close times, for example 05:00=105; 14:30=106,107; Fri 21:50=*. Days go in front: Mon-Thu, Mon,Wed,Fri or none for every day.
  4. Choose what to close: positions and pending orders, only positions or only pending orders, and optionally limit it to some symbols.
  5. Check the clock. By default the times are the broker's server time, the one you see on the chart and in Market Watch. You can switch to the computer's clock.
  6. Read the schedule on the chart: how many lines were read, the next run of each one and what it would close right now.
One EA per hour, each copy closed at the end of its own hour

What can go wrong at the close

  • The broker rejects the close (requote, off quotes, market closed). A good tool tries again until nothing is left instead of giving up after one attempt.
  • The terminal was off at that minute. Decide how late a missed close may still run. Closing a trade hours later, by surprise, is worse than not closing it.
  • Algo Trading is off. Then nothing can be closed. Check the button in the toolbar, or use a tool that warns you on the chart.
  • Daylight saving time. Many brokers move their server time one hour with DST. If your close times are tied to a session, check them after the change.
  • Text inputs have a length limit. MetaTrader leaves a text input empty when it is longer than allowed (about 240 characters), so long lists must be split.
Every close written in the MetaTrader Experts tab with ticket, magic number and profit

Every EA flat at its own time

Auto Close Trades by Time and Magic Number: one line per close, days of the week, retries until done and every close logged.

See how it works

Questions

Can MT5 close trades at a specific time by itself?

Not natively. MetaTrader 5 has no scheduled close. You need an EA with its own close-time input, or a utility EA on a spare chart that closes the trades at the time you set.

How do I close only the trades of one EA?

By its magic number, the ID every EA puts on its orders. Manual trades have magic number 0. Give each copy of an EA its own magic number if you run it on several charts.

Which time is used, the broker's or mine?

Usually the broker's server time, the one shown on the chart and in Market Watch. Check it after daylight saving changes, because many brokers move their server time by one hour.

What happens if the terminal is off at the close time?

Nothing can be closed while MetaTrader is off. A good tool runs the missed close when the terminal comes back within a few minutes, and skips it if it is much later. A VPS avoids the problem.

The EA Reality Check, free guide

Know if an EA works before you risk real money

The EA Reality Check, a free guide: the 12 tests I run on every strategy before it goes near a live account, with a one-page score sheet. Free PDF.

Where are you right now?