Meta Trader in invisible/headless mode?

 

I have a script that launches the Meta Trader terminal and runs some EAs on a regular basis. For this, I use an MQL config file that I run this way:

C:\Program Files\MetaTrader 5\terminal64.exe /config:path\to\MetaTrader\folder\config\custom_config.ini

The problem is that the Meta Trader window shows up on the screen for a second, every time the script is run. I was wondering if a "headless" option is available, i.e. an option where Meta Trader can be launched in such a way that it is not visible on the screen. 

 
Levente Csibi:

I have a script that launches the Meta Trader terminal and runs some EAs on a regular basis. For this, I use an MQL config file that I run this way:

The problem is that the Meta Trader window shows up on the screen for a second, every time the script is run. I was wondering if a "headless" option is available, i.e. an option where Meta Trader can be launched in such a way that it is not visible on the screen. 

See the following post to guide you a little ...

Forum on trading, automated trading systems and testing trading strategies

It's possible to running EA in demo/prod in terminal mode?

Fernando Carreiro, 2021.05.08 11:53

It is somewhat possible, but requires "power user" knowledge of Windows. You can have windows run MetaTrader as a service or a background task and no user or graphic interface displayed.

I don't know if this is allowed by the MetaQuote's EULA, so I will only describe the process superficially without exact specifics.

I have done it myself before, to run EAs for me without human intervention. It requires setting up a separate installation on a different folder, and doing all the initial setup of terminal manually, like hiding the various windows (Navigator, Data , etc) and only having the Chart with the EA open, but minimized so as to reduced resource usage of the "virtual screen" that windows creates for it in the background task.

You then have to define the Task settings or Service settings for Windows to launch it at startup (or when ever you decide), including Windows logon credentials to start it up even if a user is not logged onto the machine.

I also added extra code to the EA to write status information (to the log files and separate files), and also send eMails (to a local private SMTP server), which I then used to monitor it since there is no user interface.

For more advanced usage, you can even send requests to the terminal or the EA (via Win API custom messages), such as a controlled safe shutdown of the process instead of just terminating the task.

You can also use named pipes to exchange information between the EA and a controlling application if needed, but I did not require that for my setup.

All this obviously, requires advanced knowledge of working with Windows!


 
Fernando Carreiro:

See the following post to guide you a little ...


Hi @Fernando Carreiro, 

Thanks a lot for this! I had thought of Windows Task Scheduler actually but my issue is that such tasks require a trigger, e.g. "run script once a day at 12:00." I don't want to run my scripts on a precisely fixed regular basis. Instead, I have a master script that determines which EA to run exactly, creates the inputs to the EAs to be evaluated, and then opens MetaTrader and runs those selected EAs. In other words, the task event would be provided by the master script, and not by Windows' clock. Do you perhaps have any advice on this? 

 
Levente CsibiThanks a lot for this! I had thought of Windows Task Scheduler actually but my issue is that such tasks require a trigger, e.g. "run script once a day at 12:00." I don't want to run my scripts on a precisely fixed regular basis. Instead, I have a master script that determines which EA to run exactly, creates the inputs to the EAs to be evaluated, and then opens MetaTrader and runs those selected EAs. In other words, the task event would be provided by the master script, and not by Windows' clock. Do you perhaps have any advice on this? 

You could use the PowerShell cmdlet "Start-ScheduledTask" to start a task manually instead of an automatic trigger.

However, if you need to start "terminal.exe" with different parameters every time, then maybe have PowerShell create the Scheduled Task (or modify an existing one) with all the desired parameters and then start it up.

Just remember that you have to use the credentials of a different user (not logged on) for it to run in headless mode.

 
Fernando Carreiro:

You could use the PowerShell cmdlet "Start-ScheduledTask" to start a task manually instead of an automatic trigger.

However, if you need to start "terminal.exe" with different parameters every time, then maybe have PowerShell create the Scheduled Task (or modify an existing one) with all the desired parameters and then start it up.

Just remember that you have to use the credentials of a different user (not logged on) for it to run in headless mode.

Thanks a ton, @Fernando Carreiro, that's plenty of great advice for me to build on. 

There is one last thing regarding this thread that I'm wondering about. The script will have different parameters every time, so as you suggested, I'll have PowerShell to create and run the tasks for every case. However, is there any chance to run these processes in parallel, in case I would need to run many EAs "at once"? 

I took four input values and set up a simple multiprocessing example. The four inputs determined four different EAs. I set the config filenames to be unique as well, so threads pertaining to different inputs will read their respective config files. This sped up the initial processes of creating / updating the EAs and saving the config files; however, multiprocessing didn't occur on the MetaTrader platform, running the EAs happened one EA after the other. I'm quite positive what I'm trying to achieve is feasible since the platform is able to run multiple EAs even on the same symbol, provided that all EAs are attached to a different chart. I thought that if I perhaps set up different accounts for each of the parallel processes and pass those credentials in the individual config files, that might help - but that seems to me an overkill to do. Or fully separate installations of the platform and calling the individual terminal1.exe, terminal2.exe files? Or maybe using a VPS? Do you possibly have any advice on this, Fernando?

 
Levente Csibi:

Thanks a ton, @Fernando Carreiro, that's plenty of great advice for me to build on. 

There is one last thing regarding this thread that I'm wondering about. The script will have different parameters every time, so as you suggested, I'll have PowerShell to create and run the tasks for every case. However, is there any chance to run these processes in parallel, in case I would need to run many EAs "at once"? 

I took four input values and set up a simple multiprocessing example. The four inputs determined four different EAs. I set the config filenames to be unique as well, so threads pertaining to different inputs will read their respective config files. This sped up the initial processes of creating / updating the EAs and saving the config files; however, multiprocessing didn't occur on the MetaTrader platform, running the EAs happened one EA after the other. I'm quite positive what I'm trying to achieve is feasible since the platform is able to run multiple EAs even on the same symbol, provided that all EAs are attached to a different chart. I thought that if I perhaps set up different accounts for each of the parallel processes and pass those credentials in the individual config files, that might help - but that seems to me an overkill to do. Or fully separate installations of the platform and calling the individual terminal1.exe, terminal2.exe files? Or maybe using a VPS? Do you possibly have any advice on this, Fernando?

I'm not quite sure I understand your post, or your exact requirements, but all "Scheduled Tasks" currently being run, will do so at the same time in parallel, sharing in the PC's resources. You can also use PowerShell to set affinity and priority of each task, if you wish to programmatically control how the work load is distributed, but I don't think that would be necessary.

EDIT: As for running multiple EAs on a single MetaTrader 5 terminal, see next post ...

 

According to the documentation: https://www.mql5.com/en/docs/runtime/running

Program Running


Each script, each service and each Expert Advisor runs in its own separate thread. All indicators calculated on one symbol, even if they are attached to different charts, work in the same thread. Thus, all indicators on one symbol share the resources of one thread.

All other actions associated with a symbol, like processing of ticks and history synchronization, are also consistently performed in the same thread with indicators. This means that if an infinite action is performed in an indicator, all other events associated with its symbol will never be performed.

When running an Expert Advisor, make sure that it has an actual trading environment and can access the history of the required symbol and period, and synchronize data between the terminal and the server. For all these procedures, the terminal provides a start delay of no more than 5 seconds, after which the Expert Advisor will be started with available data. Therefore, in case there is no connection to the server, this may lead to a delay in the start of an Expert Advisor.

The below table contains a brief summary of MQL5 programs:

Program

Running

Note

Service

A separate thread, the number of threads for services is equal to the number of services

A looped service cannot break running of other programs

Script

A separate thread, the number of threads for scripts is equal to the number of scripts

A looped script cannot break running of other programs

Expert Advisor

A separate thread, the number of threads for Expert Advisors is equal to the number of Expert Advisors

A looped Expert Advisor cannot break running of other programs

Indicator

One thread for all indicators on a symbol. The number of threads is equal to the number of symbols with indicators

An infinite loop in one indicator will stop all other indicators on this symbol


 
Also, if it is the case that your EAs are consuming so much CPU or are otherwise resource intensive, you should consider streamlining them. Make your code more efficient. If need be, if they share similar functionality that is being replicated by all the EAs, then consider off-loading it to a MQL5 "Service" program that will act like a "cache" of sorts for all the EAs running, in order to reduce the workload.
Reason: