If you operate a VPS with Expert Advisors for automatic trading, you cannot independently check around the clock that everything is working smoothly. Especially the use of VPS and Expert Advisors are perfectly suited for automatic trading in the background. Due to unforeseen circumstances or complications, the user should, among other things, ensure that
- the VPS is started
- Windows is started and the Windows user is logged in
- Windows has an active Internet connection
- all relevant MetaTrader terminals are started
- the correct profile is loaded in MetaTrader
- there is a connection between MetaTrader and the broker’s server
It can always happen that a MetaTrader terminal crashes. In this case, it must be automatically detected and the affected terminal must be restarted, making sure that the correct profile is loaded. It is also possible that a terminal loses the connection to the broker server. This problem must also be detected and corrected. It must also be assumed that the VPS or also the Windows server can crash due to an error and must be restarted. In this case, the Windows user must be logged in completely automatically and all relevant MetaTrader terminals must be started with the correct profile.
In the following, we show from years of experience how we set up our VPS and protect ourselves from VPS total failure.
Preparing Windows and VPS
This section is dedicated to the settings, for preparing the VPS and the Windows server for a total failure.
The figure below shows the simplified overview of the preparation of the VPS, Windows Server and MetaTrader 4.
Enable Autostart of the VPS
First, you should make sure that your server is restarted after a failure or crash. Every VPS provider offers a corresponding setting in the administration area of the server. The picture below shows this setting at netcup.
Enable Autologin to Windows
Next, enable the Autologin for Windows users. This will automatically log in the user when Windows starts. The following method works on Windows Server 2008 R2 and newer.
Copy the following text into a text file. The text parts "Windows User" and "Pasword" must be adapted according to your login data (quotation marks must remain). Then save the file as "Windows Auto Login Enable.reg". Now execute the registry file created in this way with administrator rights.
Windows Auto Login Enable.reg
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] "DefaultUserName"="Windows User" "DefaultPassword"="Password" "AutoAdminLogon"="1"
To disable Autologin save the text below as "Disable auto login to Windows.reg" and execute it with administrator privileges.
Disable auto login to Windows.reg
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] "AutoAdminLogon"="0"
Alternatively, the two ready-made registry files can also be used. In the login file, however, the "Windows User" and the corresponding "Pasword" must be adapted. For this purpose, the two files can be opened and edited with a text editor.
It should be noted that the password is stored unencrypted in the registry, so that any Windows user can see it. With the tool Autologon by Mark Russinovich the password can be entered encrypted. Follow the link for more information.
Disable Shutdown Event Tracker in Windows Server
The "Shutdown Event Tracker" from the Windows server should be turned off so as not to interfere with the user’s autologin. If the shutdown event tracker is enabled and the server shuts down or restarts unexpectedly (either due to a hardware failure or a power interruption), the first user to log in when the server restarts is prompted to enter a reason. Furthermore, the server cannot be shut down or restarted without specifying a reason.
The image below shows an example of a prompt when trying to shut down the server.
You can disable the Shutdown Event Tracker by following the steps below.
- In the start menu, type "gpedit.msc" and launch it.
- Go to: Computer Configuration > Administrative Templates > System > Display Shutdown Event Tracker
- Edit the policy and set to
Uptime monitoring of the VPS
To make sure that our VPS is online, we use UptimeRobot. With this online service a ping is sent to the VPS every few minutes. If the VPS does not respond, we are notified by email, SMS or push notification via the iOS or Android app and we can take care of the problem. The service of UptimeRobot can be used up to 50 monitors with 5-minute checks for free. Here’s how to set it up.
Preparing Windows Server
On your Windows server, you must first enable the feature that the VPS should respond to ping requests. On a Windows Server 2008 and newer you can enable this rule from the command prompt. Run as Administrator "cmd.exe", type and then execute the following:
- Enable Ping Replies: "netsh firewall set icmpsetting 8"
- Disable Ping Replies: "netsh firewall set icmpsetting 8 disable"
Set up UptimeRobot
Open an account to use UptimeRobot at uptimerobot.com and go to "Add New Monitor". Here you have to select "Ping", then choose any "Name" for the VPS and enter the IP of the VPS, which is also used to login via Remote Desktop. The whole thing will look like this.
Sometimes it can be helpful to check when the server was last started. Start "cmd" with administrator rights and execute the following: "wmic os get lastbootuptime". The time windows was last rebooted will be displayed.
Preparing MetaTrader 4
This section is dedicated to the settings, for the preparation of MetaTrader terminals.
Auto Launch and Watch Function for MetaTrader 4 Terminals
To ensure that all desired MetaTrader 4 terminals are executed, we use the MT4BAR from Fx1.net. Even though further development was discontinued some time ago, this program still runs very stable. MT4BAR monitors all relevant MetaTrader terminals to see if they are running. If the VPS or Windows server is restarted or if one or more terminals crash, the terminals are restarted by MT4BAR. Additionally, you can be notified by email when a terminal is started, so you can check promptly if there is a need for action. Unfortunately, the email notification was poorly implemented or it is already outdated, we could only successfully integrate the outlook.com email service. With many other email providers it failed because of the security settings of the providers. In addition, this program is very well suited to manage all MetaTrader terminals, as they are displayed very clearly. Furthermore, MT4BAR includes an indicator, but it is not mandatory for the described functions.
After installing MT4BAR, a shortcut of this program should be added to the startup folder so that MT4BAR starts automatically with Windows. This is how you can open the folder:
Start > All Programs, then right-click on Startup and select "Open".
The image below shows the Dashboard of MT4BAR as an example.
To use MT4BAR you should watch the short but very informative video.
Watchdog for the connection between MetaTrader 4 and the Forex broker
It may happen that MetaTrader loses connection to the broker’s server. This can be fatal, because then the Expert Advisor is no longer able to act and high losses can occur during trading, especially if you have not set a stop loss. See also Our Requirements on Expert Advisors. To ensure that there is always a connection to the broker and if the connection is lost it is restored, we use the Watchdog developed by us. The Watchdog is added to a chart like an Expert Advisor, then it continuously monitors the connection between MetaTrader 4 and the broker. If the connection is lost, it restores it, for this purpose the watchdog uses the broker account added to the favorites. Currently, the Watchdog is only available for MetaTrader 4 and must be set up as follows.
Add broker account to favorites
Use the Watchdog on EACH MetaTrader terminal you want to monitor. Add the desired broker account to the Favorites in MetaTrader, please add only one account to the Favorites. To do this, right-click on the account and select "Add to Favorites".
Uncheck "Disable automated trading when the account changes"
Furthermore, each terminal should be configured so that automated trading is NOT disabled when the account changes. To do this go to "Tools > Options > Expert Advisors" and uncheck the option "Disable automated trading when the account has been changed".
Download and start the watchdog
Download the Watchdog for MetaTrader 4.
Now the watchdog can be added to any chart. Make sure that the option "Allow DLL imports" is enabled. Furthermore you can enter the interval in seconds for the connection check under Inputs. With a click on "OK" you activate the watchdog.
Set the profile to be loaded at MetaTrader 4 startup (Important!)
Unfortunately, it can happen at any time that the MetaTrader 4 terminal crashes due to a program error. This happens rather rarely, but there is a certain probability, especially if an Expert Advisor causes an error, or if too many Expert Advisors are loaded. The next time the terminal is started (both after a crash and after a proper terminal exit), the profile that is entered in the "lastprofile.ini" file is loaded (by default, the profile with the name "Default" is entered). The MetaTrader terminal will replace the profile with the one that was last loaded after a proper terminal shutdown. When the terminal crashes, this file is not updated, so the next time the terminal is started, the profile that was last used by the user when the terminal crashed is not necessarily loaded. Therefore, one should deal with how to set the desired profile to be loaded both after a proper exit and after a terminal crash.
Set up default profile
When MetaTrader 4 terminal is started, the profile that is entered in the "lastprofile.ini" file, located in the "profiles" folder, is always loaded automatically. By default, the profile with the name "Default" is entered. The desired profile, which should be loaded at startup, should best be saved under this name ("Default"), this is the recommended procedure. However, if you want to save a profile, which should be loaded automatically at startup, under a name other than "Default", you can enter the name of the desired profile in the file "lastprofile.ini" or manually close the terminal with the currently loaded profile, this will automatically enter the name of the last profile in the "lastprofile.ini" file (Note: this option only works if the terminal was closed properly by hand, it does not work if the terminal crashed, because the "lastprofile.ini" file is not changed when the terminal crashes).
Create a backup of the default profile
It is very worthwhile to create a backup of the profile for all MetaTrader terminals. You can do it as follows.
Create a backup of the profile:
- File > Profiles > Save As
- Enter a backup profile name (e. g. "Backup") & click OK
After that, you should load the default profile again. Our tip: Save the profile you just saved under "Default" again, this is faster than loading a profile.
Load the default or backup profile (Better: save as "Default" again):
- File > Profiles
- Select your default or backup profile (e. g. "Backup")
Backup via Batch File
Alternatively, you can use a batch script to create a backup profile from all default profiles. To do this, you have to create a batch file with the following content and adjust the three variables under "Variables" to your needs.
@echo off setlocal setlocal enabledelayedexpansion @echo off :: Variables set "_folder_root=%USERPROFILE%\AppData\Roaming\MetaQuotes\Terminal" set "_folder_main=profiles\default" set "_folder_main_appendix=_backup\" :: Backup Process for /d /r %_folder_root% %%i in (%_folder_main%) do ( @if exist "%%i" ( @set _path=%%i @echo !_path! @set "_pathnew=!_path!!_folder_main_appendix!" rmdir !_pathnew! /s /q xcopy /e /y !_path! !_pathnew! ) ) endlocal ::pause
You can also download the following batch file and adapt it accordingly:
There will be moderation of all comments. Please also read our comment rules: