AutoTrading Scheduler is a MetaTrader expert advisor that lets you control the status of AutoTrading button in your MetaTrader platform based on a weekly or long-term 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 the 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 or disabled. 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).
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 button right below the Status field is used to toggle the schedule between the one when AutoTrading is allowed to the one when AutoTrading is denied.
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 once per each time point — either when the period starts and ends or when the Scheduler is turned on inside a period.
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
Weekly schedule
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 \MQL4\Files\
(or \MQL5\Files\
) 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
>>
:
Long-term schedule
You can also define a long-term schedule via a file. For example, it is possible to set a schedule for the current month or the entire year, or even several years in advance.
Such schedule should be set for each day separately using YYYY-MM-DD format for dates and the usual hour/minute format for the daily schedule itself. Here is an example schedule, which defines allowed periods for 4 days in July 2024:
2024-07-05 13:00-18:30, 20:30-21:45 2024-07-12 12:50-18:40, 20:25-21:50 2024-07-19 14:00-15:30, 19:50-22:00 2024-07-26 00:00-23:59
Such a long-term schedule, if loaded, will override any weekly schedule, and all the days of the week on the panel will be showing the <<
FILE
>>
placeholder.
A long-term schedule isn't cyclical and becomes obsolete once the date has passed.
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) — the default state of the scheduler's 'on/off' switch.
- DefaultTime (default = Local) — the default time type to use — either Local time or Server time.
- DefaultMonday (default = "") — the default periods to enable AutoTrading on Monday.
- DefaultTuesday (default = "") — the default periods to enable AutoTrading on Tuesday.
- DefaultWednesday (default = "") — the default periods to enable AutoTrading on Wednesday.
- DefaultThursday (default = "") — the default periods to enable AutoTrading on Thursday.
- DefaultFriday (default = "") — the default periods to enable AutoTrading on Friday.
- DefaultSaturday (default = "") — the default periods to enable AutoTrading on Saturday.
- DefaultSunday (default = "") — the default periods to enable AutoTrading on Sunday.
- DefaultClosePos (default = false) — the default state of the 'Close all trades' checkbox.
- DefaultEnforce (default = true) — the default state of the 'Always enforce schedule' checkbox.
- DefaultAllowDeny (default = true) — the default state of the 'Allow/Deny' button.
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
\MQL4\Files\
subfolder for MT4 or in the\MQL5\Files\
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.03, 2024-06-17)
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.03 — 2024-06-17
- Added an option to load a text file with a long-term schedule.
- Added some helpful tooltips to the panel's controls.
- Added a switch between Enabled schedule and Disabled schedule.
- Changed how non-enforced schedule works. It will now also attempt to toggle AutoTrading either off or on when the Scheduler is launched inside a period.
- Fixed an off-screen position of the panel after a chart resizing event.
- Fixed a crash that could occur if the panel is loaded without enabling DLL imports.
- Fixed a bug that prevented the schedule from resetting when the ScheduleFile input parameter was cleared or changed.
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.