Popular Posts

Editor'S Choice - 2020

MQL4: Computer Management from an Advisor

Sometimes it is necessary to programmatically press a button in a third-party application, read the text from a specific component, start another terminal, any other program, open a text file, turn off the computer at a certain time, and perform some other operations. The WinAPI function ShellExecute, which is located in the shell32.dll library and the WinUser32.mqh library, will help us here. How to automate this whole process - we learn from our new lesson on programming forex robots. The topic is unusual, it will greatly expand your skills as a programmer.

For more information on ShellExecute, see MSDN Help. There you can get detailed information about all the functions available from the WinUser32.mqh library. I will give a brief description of each of them:

  • Shellexecute - performs an operation with the specified file;
  • Sendmessage - Sends the specified message to the window or windows, waiting for a response;
  • SendNotifyMessage - the same, but does not wait for an answer;
  • Postmessage - puts the message into the stream without waiting for a response;
  • keybd_event - synthesizes pressing a given key on the keyboard;
  • mouse_event - synthesizes mouse movement and button presses;
  • Findwindow - retrieves the handle to the top-level window;
  • SetWindowText - places the text in the window title;
  • GetWindowText - reads text from the window name;
  • GetWindowTextLength - reads the number of characters in the window title;
  • Getwindow - retrieves a window handle that is related to the specified window;
  • Getparent - retrieves the handle of the specified or parent window;
  • Updatewindow - updates the program window;
  • Enablewindow - enables or disables keyboard and mouse input into the specified window or control;
  • DestroyWindow - deactivates the window or removes keyboard focus from it;
  • Showwindow - sets the display state of the specified window;
  • SetActiveWindow - activates the window;
  • Animatewindow - allows you to create special effects when displaying or hiding windows;
  • Flashwindow - makes the specified window flash once;
  • Closewindow - minimizes (but does not close) the specified window;
  • Movewindow - changes the position and size of the specified window;
  • SetWindowPos - changes the size and position of a child, pop-up, or top-level window;
  • IsWindowVisible - determines the visibility state of the specified window;
  • Isicon - determines whether the specified window is minimized;
  • IsZoomed - determines whether the window is maximized;
  • Setfocus - sets the keyboard focus to the specified window;
  • Getfocus - retrieves a window handle that has keyboard focus;
  • GetActiveWindow - gets the handle of the active window;
  • IsWindowEnabled - determines whether the specified window for keyboard and mouse input is enabled;
  • Messagebox - displays a modal dialog box that contains a system icon, a set of buttons, and a short message for a specific application, such as status or error information;
  • MessageBeep - reproduces sound using the system speaker;
  • GetSystemMetrics - retrieves the specified system metric or system configuration parameter;
  • Exit windows - logout of the interactive user, shutting down the system or shutting down and rebooting the system;
  • Swapmousebutton - changes or restores the value of the left and right mouse buttons.

What are we going to do today?

We will not consider each function from the list - it would take a lot of time and, besides, I do not want to take away the possibility of independent study from you. Therefore, we simply solve a few simple problems, such as:

  • shutting down or restarting the computer;
  • launching another program from the terminal (for example, google chrome);
  • Replacing the program window name

Yes, these are very simple tasks, but their solution may well serve as a good example to demonstrate the capabilities of libraries and build more complex algorithms based on these examples.

Application launch

The first thing to do is declare a ShellExecuteW function:

// - import a function from an external DLL
#import "shell32.dll"
int ShellExecuteW (int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

Then, in the onStart () method, run the desired program:

ShellExecuteW (NULL, NULL, "C: Program Files (x86) GoogleChromeApplicationchrome.exe", NULL, NULL, 1);

Let's drop the script on any chart and we will open the specified program.
Now we will include the WinUser32.mqh terminal library in our script:


Now we need to find the window handle of the running program. This can be done using the WinApi function - FindWindowW.
In the terminal library, the function is described as follows:

int FindWindowW (string lpClassName, string lpWindowName);

This means that you must enter the class name of our application in the string lpClassName parameter, and instead of the string lpWindowName parameter, the name of the program window is “New Tab”. The name of the application class can be found using utilities such as: Spy ++, WinSight, WinInspector and others. Download and install the Spy ++ program.

The utility is very easy to use. It is enough to start the program, press the button with the binoculars and drag the sight to the window we need, and we will immediately see its name, class and other information. In the FindWindowW function, you can specify both the class name and the window name (or individually). It will be enough for us to specify only the window name:

int handle = FindWindowW (NULL, "New Tab - Google Chrome");

Information with the handle of the found window is displayed on the screen. If the handle is 0, then the window was not found. Having received the handle of the main window, you can do whatever you like with the application: look for the child window we need, read and send text, press buttons programmatically, and so on.

Add another simple SetWindowTextW function to our code. It will change the text of the window to any other:

SetWindowTextW (handle, "My Chromchik");

To turn off the computer, we find the shutdown.exe program on the disk. And just as in the previous case, we write in the function the program address with the parameters we need:

ShellExecuteW (NULL, NULL, "C: WindowsSystem32shutdown.exe", "- s -t 00", NULL, 1);

To turn off the computer, enter the following line:

shutdown.exe -s -t 00

To restart the computer, enter the following line:

shutdown.exe -r -t 00

To see the options for different versions of Windows - follow this link.


You can also find many other interesting functions in the WinUser32.mqh library that will allow you to manage a third-party program directly from an adviser written in MQL4. A complete list of WinApi features can be found on MSDN.

Thus, you can fully control your computer from an mql script, automate all your preparations for trading: open transaction logs, news sites when opening a terminal, and so on.

You can also write an adviser that would trade in another trading terminal that does not support automated trading, for example, in a browser application for binary options or in a platform for trading stocks. This opens up really great opportunities for work and many different interesting ways to use a great variety of programs for receiving, processing data and manipulating other software.

Watch the video: Mql4 Coding: Allowing Only One Trade At a Time (April 2020).

Leave Your Comment