Advertisements
$ £ ¥
¥ £ $

AutoTrading Scheduler

AutoTrading Scheduler is a MetaTrader expert advisor that lets you control the status of AutoTrading button in your MetaTrader platform based on a weekly schedule. In addition to enabling and disabling AutoTrading, this EA can also attempt to close all trades when disabled AutoTrading period is reached. The expert advisor is available for both MT4 and MT5.

This scheduler requires DLL imports to work properly. It imports one function from the standard Windows DLL file (user32.dll). Do not forget to allow DLL imports when attaching this expert advisor to a chart.


Interface

The panel controls are pretty straightforward. The Switch button lets you turn the scheduler on and off. The group of radio buttons to the right lets you switch between the local and server time. The status will inform you if DLL's are turned off, if connection is missing, or if AutoTrading is already disabled. The seven entry fields, each for every day of the week, serve to enter time ranges when you want AutoTrading to be enabled. The Set to all empty button will set the value entered for Monday to all other empty days of the week — this can be useful to quickly set the same schedule for each day. The first checkbox below the days of the week lets you make the EA to attempt closing all trades before disabling AutoTrading. Finally, the checkbox at the bottom sets whether the scheduler works in a strict enforcing mode (ticked; the EA will always try to enable/disable AutoTrading according to the schedule) or not (unticked; the EA will only enable/disable AutoTrading when the next period starts or ends).

AutoTrading Scheduler - Interface


Usage

When Scheduler is off, it will do nothing. You have to turn it on for it to operate.

Changing the time from Local to Server can be useful if you want Scheduler to work based on your broker's trading server time, for example, during daylight saving time switching weeks.

The status can be OK, No autotrading, No connection, or DLLs disabled. The first and the second one don't affect anything — they just inform you. No connection means that the EA won't be able to close any trades before disabling AutoTrading, but it will disable AutoTrading without a problem. DLLs disabled is a real problem because enabling/disabling AutoTrading will be impossible.

The main part of working with the Scheduler is to set a valid weekly schedule. AutoTrading will be enabled during the time periods you set.

The time input fields work with time ranges given in 24-hour format with start and finish time separate with a hyphen. For example, the range 9-17 will be interpreted as "from 9:00 till 17:00" (or "from 9 a.m. till 17 p.m.").

More than one range can be given for the day using comma as a separator. For example, the range 13:30-16:30, 19:30-20:05 will enable AutoTrading during two periods: from 13:30 till 16:30 and from 19:30 till 20:05.

By default, the scheduler will always try to switch AutoTrading off outside of allowed periods and on inside such periods. Even if you switch AutoTrading on or off yourself, the scheduler will override. To avoid this, you can untick the checkboox at the bottom of the panel — this will make the scheduler toggle AutoTrading only when the allowed period starts or ends and only once per each start or end.

The main objective of AutoTrading Scheduler is to control the status of the AutoTrading button in MetaTrader. However, it can also be used to close the trades before disabling AutoTrading. This is attained by ticking the checkbox below the days of the week. The expert advisor will attempt to close the positions and delete pending orders, but there is no guarantee that it will succeed in doing so. It will not re-try closing the trades because its first priority is to disable AutoTrading and after AutoTrading is disabled, managing trades becomes impossible.

Schedule file

Unfortunately, MetaTrader doesn't allow entering strings longer than 63 characters into panel edit fields. If you want to use elaborate schedules that don't fit into the limit (for example, to turn AutoTrading on and off every hour), you should use a schedule file. You can do that by creating a simple text file in the MQL4Files (or MQL5Files) subfolder of your MetaTrader's data folder and setting its filename into the ScheduleFile input parameter.

The file format should follow one simple rule. A line with the weekday's name should be followed by a line with this day's schedule. For example, if you want AutoTrading to be on only during the first 10 minutes of every hour on Tuesday and Wednesday, the text file's content should be the following:

Tue
0-0:10,1-1:10,2-2:10,3-3:10,4-4:10,5-5:10,6-6:10,7-7:10,8-8:10,9-9:10,10-10:10,11-11:10,12-12:10,13-13:10,14-14:10,15-15:10,16-16:10,17-17:10,18-18:10,19-19:10,20-20:10,21-21:10,22-22:10,23-23:10
Wednesday
0-0:10,1-1:10,2-2:10,3-3:10,4-4:10,5-5:10,6-6:10,7-7:10,8-8:10,9-9:10,10-10:10,11-11:10,12-12:10,13-13:10,14-14:10,15-15:10,16-16:10,17-17:10,18-18:10,19-19:10,20-20:10,21-21:10,22-22:10,23-23:10

You can use either full or shortened names for the days of the week.

You don't need to set up a schedule for all days inside such a file. Only use it to set schedules that are too long to be set via the panel.

Once the schedule file is successfully loaded, the respective days are displayed as inactive and contain the string <<FILE>>:

AutoTrading Scheduler - Schedule from File


Input parameters

Notifications

  • EnableNativeAlerts (default = false) — if true, a native MetaTrader popup alert will be used when AutoTrading is toggled.
  • EnableEmailAlerts (default = false) — if true, an email message will be sent when AutoTrading is toggled Email should be properly configured in MetaTrader via Tools->Options->Email.
  • EnablePushAlerts (default = false) — if true, an email message will be sent when AutoTrading is toggled Notifications should be properly configured in MetaTrader via Tools->Options->Notifications.

Defaults

  • DefaultTurnedOn (default = false) — default state of the scheduler's 'on/off' switch.
  • DefaultTime (default = Local) — default time type to use — either Local time or Server time.
  • DefaultMonday (default = "") — default periods to enable AutoTrading on Monday.
  • DefaultTuesday (default = "") — default periods to enable AutoTrading on Tuesday.
  • DefaultWednesday (default = "") — default periods to enable AutoTrading on Wednesday.
  • DefaultThursday (default = "") — default periods to enable AutoTrading on Thursday.
  • DefaultFriday (default = "") — default periods to enable AutoTrading on Friday.
  • DefaultSaturday (default = "") — default periods to enable AutoTrading on Saturday.
  • DefaultSunday (default = "") — default periods to enable AutoTrading on Sunday.
  • DefaultClosePos (default = false) — default state of the 'Close all trades' checkbox.
  • DefaultEnforce (default = true) — default state of the 'Always enforce schedule' checkbox.

Miscellaneous

  • Slippage (default = 2) — slippage value given in broker's pips to be used when closing positions.
  • ScheduleFile (default = "") — the name of the text file containing the schedule. The file should be located in the MQL4Files subfolder for MT4 or in the MQL5Files subfolder for MT5.
  • WaitForNoPositions (default = false) — if true, the Scheduler will wait for all positions to be closed before turning AutoTrading off.
  • WaitForNoOrders (default = false) — if true, the Scheduler will wait for all pending orders to be deleted before turning AutoTrading off.

Download (ver. 1.02, 2023-12-07)

AutoTrading Scheduler is being developed via a dedicated GitHub repository. You are encouraged to actively participate in the improvement of this expert advisor by submitting your own features via pull-requests and by reviewing existing suggestions, changes, fixes, and so on.

AutoTrading Scheduler for MetaTrader 4 AutoTrading Scheduler for MetaTrader 5

To install — unzip and copy the entire AutoTrading Scheduler folder to MQL4/Experts/ or MQL5/Experts/ (if you are on MetaTrader 5) of your terminal's data folder.


Discussion

Do you have any suggestions or questions regarding this expert advisor?

Discuss AutoTrading Scheduler with other traders and MQL coders on the forum.

Changelog

1.02 — 2023-12-07

  • Added an option to load the schedule via a text file (ScheduleFile).
  • Added a way to make the Scheduler to wait until there are no open positions (WaitForNoPositions) or pending orders (WaitForNoOrders) before switching AutoTrading off.

1.01 — 2023-06-06

  • Added optional alerts when AutoTrading is toggled.
  • Added non-enforced schedule mode to only toggle AutoTrading when a new period stats or ends.
  • Added input parameters with default values for all panel fields. This will simplify setup and copying the panel settings from one platform to another.
  • Optimized processing of chart timeframe/symbol change.
  • Fixed a bug that prevented the panel from maximizing after minimization.
  • Fixed a bug that could cause multiple triggers when the scheduler was set to close trades.
  • Fixed a bug with deletion of leftover chart objects from a previously crashed ATS panel.
  • Removed the PanelOnTopOfChart parameter. This should be handled by chart settings.