In this blog post I will explain how you can program your MetaTrader 4 platform to close all open positions at a desired time. It is easier than you might think, but MT4 by default does not have such option. This is why we will need a special tool for that called “Timed Exit EA”.

The Timed Exit EA is the most simple and easy to use app to close all trades automatically at specific time each day. You simple attach the EA to the chart window, input the time and EA will do the rest when that time comes.

The main idea of this EA is to automatically close trades every day at specified time. So once you set the EA it will repeat the same every day at the same time over and over again until you stop it. For example if you set EA to close all trades at 15:29:30, EA will do this each day when markets are open.

Download the Timed Exit EA

You can set the EA to close all trades before the news or any other event. You can set it to close all trades at the end of your trading session or just before the beginning of the trading session. Basically, you can set any time you want, so this gives you complete freedom.

Click here to download the Timed Exit EA from MT4 App Market. You can also install the EA directly from your MT4 platform, just like the Partial Close EA.

If you do not know how to get apps from the MT4 market there are quite a lot of useful information on their website. Here’s the one that explains how to download Forex robots from the market. And this one explains how to test an Expert Advisor before buying.

Once you’ve downloaded the Timed Exit EA on your MT4 platform, it should appear in the Navigator window, under the “Expert Advisors -> Market” section.

Timed Exit EA in MT4 Navigator window
Timed Exit EA in MT4 Navigator window

Running the Timed Exit EA

To run the EA you need to attached it to a separate chart of any time frame on your MT4, and it will start monitoring trades immediately. By default EA has “Time Exit” function disabled. This is for your own protection. Trade closing is a very responsible thing to do, so you might want to learn and test the EA first on a demo account to get used to it before you use it on your live trading account.

Here are the EA Input settings in the image below. As you can see the EnableTimedExit option is set to FALSE, which means “Time Exit” function is disabled.

Time Exit function disabled by default in the Timed Exit EA Inputs
Time Exit function disabled by default in the Timed Exit EA Inputs

So obviously, when you attach the EA to the chart and want it to start monitoring your trades, you need to set the EnableTimedExit option to TRUE.

On EA startup it could be that the time to close the trades has passed already. For example EA is set to close the trades at 08:00 and EA is launched at 09:30. In this case EA will check if there are any open trades that should have been closed and ask if you want them to be closed or not. If you answer YES, then obviously EA will close these trades. If you answer NO, the EA will leave those trades running until next exit time.

On startup, Timed Exit EA asks a user whether it should close open positions or not, because the exit time has passed already.
On startup, Timed Exit EA asks a user whether it should close open positions or not, because the exit time has passed already.

EA can be used with any other Expert Advisors, indicators or scripts simultaneously. Just note, that you need to open a separate chart for each Expert Advisor. MT4 does not allow to run more than one EA on the same chart window, so you will have to open additional chart for each additional EA you want to run. This seems to be confusing to some people as I keep getting this question a lot. That’s why I wrote this blog post which explains how to run multiple EA on the same MetaTrader 4 platform.

When you attach the EA to the chart, it will only see the trades of that currency pair (instrument) that it is attached to. This means if you attach the EA on the EURUSD chart, then it will monitor only EURUSD positions. Important to mention, that if your broker has multiple names for the same currency pairs, like EURUSD and EURUSDc, positions for these pairs will be treated individually. Means that if you open a trade on EURUSD, the EA running on EURUSDc will not see it. You will have to run EA on EURUSDc to manage these trades. Obviously you can run EA on both pairs if you need to. EA can also be applied to a non-Forex pairs/instruments like Indices, Commodities, etc.

There’s also an option to set in the EA to manage the trades of any currency pair. By default EA will close all market/pending orders on the currency pair (instrument) it is attached to, but with the help of the option ManageOnlyCurrentPair you can control how EA should work. If you set this option to FALSE, then EA will monitor the trades of any currency pair (instrument).

With the settings below EA would close all open trades you have of any financial instrument at 16:59.

Timed Exit EA scheduled to close all open trades of any pair at 16:59
Timed Exit EA scheduled to close all open trades of any pair at 16:59

It is also possible to filter trades by magic number. By default EA is programmed to monitor trades of any magic number – ManageMagicNumber is set to -1 (negative one). But obviously you can change this if needed. If you set ManageMagicNumber to zero, EA will see only manually opened trades. If you set this value to anything above zero, EA will only monitor and close the trades of that specific magic number.

Magic number is assigned to positions only by other EA. MT4 does not allow to choose what magic number to use when you’re opening the trade manually, but if you use app like Trade on Chart – it becomes possible. ToC allows you to set what magic number it should use when opening the trade.

With the settings below EA would close all manually open trades 30 seconds before the London trading session open (having in mind that our broker clock is in the UK time zone). Note the EA is set to manage positions only with the magic number of zero. This means only manually open trades will be closed. If there will be any positions opened by Trader on Chart app or any other MT4 EA, Timed Exit EA will not touch them and leave as they are.

Timed Exit EA scheduled to close all manually open positions 30 seconds before London open
Timed Exit EA scheduled to close all manually open positions 30 seconds before London open

Timed Exit EA Inputs explained

  • EnableTimedExit – this tells the EA whether the EXIT function is enabled or disabled. By default it is disabled for security reasons. You can to turn it on by setting the value to TRUE.
  • ExitTime – this tells the EA at what time exactly the trades should be closed. Input format is HH:MM:SS or HH:MM and EA will not work if you enter anything else. Note that EA uses broker time, which is also displayed in the dashboard and your MT4 Market Watch window.
  • ManageMagicNumber – this tells the EA to close trades only with certain magic number you choose. If this value is set to zero EA will see only manually opened trades. If you set this to -1 (negative one) EA will see all of the trades running on the account (default value).
  • ManageOnlyCurrentPair – this tells the EA if it should monitor trades only of the currency pair (instrument) it is currently running on, or it should monitor trades of any currency pair. By default it is set to TRUE and EA will see only trades of the currency pair it is running on.
  • SlippageClosePips – this is the slippage setting EA will use to exit the market (close the trade). Note that not all brokers use this and this has no effect on ECN/STP accounts. If you do not know what that is simply leave it as is.
  • DashboardDisplay – this options controls how the EA dashboard is displayed. 0 = hide the dashboard; 1 = show only text without background; 2 = show text with background (default value).
  • DashboardColor – this is where you can set background color or the EA dashboard.

Closing trades at multiple times

You can run multiple instances of the Timed Exit EA if you open additional chart window for each EA instance. This way you can have any combinations of EA settings you want and this is how you set the EA to close trades at multiple times, on multiple currency pairs, and multiple choices of magic numbers.

In the image below you can see 3x Timed Exit EA running on 3x separate chart windows.

First EA will close all positions of any instrument at 08:00 every day.

Second EA will close all positions of any instrument at 17:00 every day.

Third EA will close positions of only USDCHF currency pair every day at midnight and only those positions I will open using the Trader on Chart app.

Fourth EA is the Trader on Chart app which is set to use the magic number of 123, which is exactly the same number as set in the third instance of the Timed Exit EA.

3x instances of Timed Exit EA on the same MT4 platform with Trader on Chart
3x instances of Timed Exit EA on the same MT4 platform with the “Trader on Chart”

Conclusion

Even though there is no option in the MT4 to schedule the time at which your open positions should be closed, you can always do this with the Timed Exit EA. I receive quite a lot requests to create such trading tool and finally it is available for anyone to use.

How will you use it? Post your ideas in the comments below.


Rimantas Petrauskas
Rimantas Petrauskas

First I am a father, a husband and then the author of the book “How to Start Your Own Forex Signals Service”. I am also a Forex trader, a programmer, an entrepreneur, and the founder of ea-coder.com Forex blog. I have created two of the most popular trade copiers and other trading tools for MT4 that are already used world wide by hundreds of currency traders.

    32 replies to "How to Close All Open Trades on MT4 at Specific Time"

    • Mouner

      Thank you RIMANTAS this EA looks interesting i might use it in the near future, well done you’re an artist programmer

    • Ken

      I would like to buy ur timed exit EA.
      However, i can’t find it in the mql5 markets?
      Pls advice how to purchase this EA, tq.

      • Rimantas Petrauskas

        Dear Ken,
        there is this big red button saying “Download the Timed Exit EA” on this page. Click it and you’ll go straight to the EA download/purchase page on MQL4 Market.

        Regards,
        Rimantas

    • Cleve

      Is it possible to delete pending orders only? Not close open positions. I am happy to pay extra for a modified version.

      • Rimantas Petrauskas

        Hi, this EA does not have such option. It will close and delete all positions and orders it will find on the account. You can filter by magic number, so if your pending orders have different magic number then you could set EA to delete only them.

        See my policy for custom programming here and contact me here if you want to order.

        Regards,
        Rimantas

    • Mike Dvd

      Hello, is it possible to set an EA or paste code into an existing EA that will trigger all trades to close if ATR (Average True Range) reaches below or above a specific level?

      Thank you,

    • Daneil Elton

      Do i need any vps for the system for running the system??

    • Almo

      HOW TO CLOSE ORDERS MATCHING THE SAME COMMENT ?

    • Canoz

      Will orders close at the set time when my PC is in hibernate or sleep mode?

      • Rimantas Petrauskas

        Hi, if your PC is in sleep/hibernate mode it means no computer application is running including MT4 and EA. In other words, it is not possible to make it work in sleep/hibernation mode.
        Solution is to have a VPS server from company like vpsforextrader.com and then host your MT4 in there, where it will run 24 hours per day, even while your computer is off, sleeping or hibernating.

    • Canoz

      hi, 2nd question. It appears the exit order is within a 24hr period based on the MT4 machine time. As i am in australia, this time kicks over to a new 24 hour period mid day my time, meaning i can only set an exit order via your EA after mid day. Is this correct…or is there anyway to set my exit time in the morning (in prior 24 hr period) to exit in afternoon (next 24 hr period)?

      Thanks

    • Gabriel Olteanu

      Can this be used to start trades at specific times? I would need a similar feature but to start trades on specific times.

    • William Henderson

      Thank you RIMANTAS! I think I am asking the impossible, but can another option be added that will not only close trades, but also switch off/disable live trading for the other EA’s? I know this is a catch 22 as you EA would probably have to turn itself off. But if it did that after closing trades and disabling all other EA’s, then that would do it. But as I say, I think this would be impossible.

      Te reason I ask is that the EA’s I am using will often open another trade if the the existing one is shut down, so the closing of trades only would be pointless. Thanks.

    • Jay

      Hi Rimantas

      You are a quality developer – would you consider developing an EA for me?
      It would be like an auto-bot but must follow my specific rules using my indicators.

    • Tom L

      okay I get the timed closing of opened positions but could it work if I wanted to close all and maybe scale out of some trades thereby leaving specific trades open?

    • QuantATC

      Hie,
      I have been searching for such an instrument, but how can i attach this on my EA for strategy testing purpose

      • Rimantas Petrauskas

        This cannot be used in Strategy Tester. To use it in ST you need to have “close trades” function implemented in your EA directly. Try to find a coder for such work on http://www.mql5.com

    • Janine

      can i closed 1 asset in 2 diff. times?
      GBPUSD – market order 1 PM and 5 PM ?

    • Alex

      Hi Rimantas, I think this option for the robot is very interesting. I was looking through the network for a robot that would close operations that take a certain time open (more than a day, for example 3 days). I would like to know if there is a more or less simple way to incorporate this condition to the robot and if you would know how to do it. I need it for a strategy that I am currently developing. Regards, and thank you very much

      • Rimantas Petrauskas

        Yes, technically it is possible, but unfortunately I am not available for such work. I might refer you to my programmer if you want to create a new EA. Contact support@ea-coder.com if you need programmer recommendation.

Leave a Reply to Daneil Elton Cancel Reply

Your email address will not be published.