Directory of RSS feeds

RSS feeds in the directory: 2818

Added today: 0

Added yesterday: 0

Business / Finance

MQL4: computer Management of the EA

Portal Forex trader 19.03.2019 at 10:08

Forex trading strategies, expert advisors, indicators, video training trade

It is Sometimes necessary to programmatically click in a third-party app to read text with a specific component, start another terminal, any other program to open a text file, shut down the computer at a certain time, to make any transactions. Here you can use the WinAPI function — ShellExecute, which is in the library shell32.dll and library WinUser32.mqh. How can I automate this whole process — we learn from our new tutorial on programming the Forex robots. The theme of charm, greatly increasing your skills as a programmer.

More information about the ShellExecute function you can get the MSDN help. There you can get details about all the functions available from the library WinUser32.mqh. I will give a brief description of each of them:

ShellExecute — performs an operation with the specified file; SendMessage — Sends the specified message to a window or Windows, awaiting a response; SendNotifyMessage is the same, but waits for no answer; PostMessage puts a message in a thread without waiting for a reply; keybd_event – synthesizes pressing a specific key on the keyboard; mouse_event — synthesizes mouse motion and button presses; FindWindow — retrieves the window handle of the top level; SetWindowText – places the text in the window title; GetWindowText reads the text from the title of a window; GetWindowTextLength – reads the number of characters in the title of the window; GetWindow — retrieves the window handle that is relevant to the specified window; GetParent — retrieves the handle of the parent window; UpdateWindow updates the program window; EnableWindow to enable or disable the keyboard and mouse to the specified window or control; DestroyWindow — the window deactivates or removes from it the keyboard focus; ShowWindow sets the show state of the specified window; SetActiveWindow — activates the window; AnimateWindow — allows you to create special effects when showing or hiding Windows; FlashWindow – flash makes the specified window once. CloseWindow minimizes (but not closing) the window; MoveWindow changes the position and dimensions of the specified window; SetWindowPos changes the size and position of child, pop-UPS or top-level window; IsWindowVisible — determines the visibility state of the specified window IsIconic — 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 the handle of the window that has the keyboard focus; GetActiveWindow gets the handle of the active window; IsWindowEnabled determines whether the specified window for keyboard and mouse; MessageBox displays a modal dialog box that contains a system icon, a set of buttons and a brief message for a particular application, such as status information or error; MessageBeep — plays a sound with the help of system dynamics, GetSystemMetrics — retrieves the specified system metric or configuration setting system; ExitWindows — logoff interactive user, shuts down the system or shutdown and restart the system; SwapMouseButton — changes or resets 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 very long time and, besides, I don't want to Rob you of the opportunity to self-study. So we just solve a few simple tasks, such as:

turn off or restart computer; launch another program from a terminal (e.g., google chrome); replacement name of the program window.

Yes, it's very simple tasks, but their decision may well serve as a good example to demonstrate the capabilities of libraries and the building on the basis of these examples of more complex algorithms.

Run application

the First thing to do is to declare function ShellExecuteW:

//-- import functions from an external DLL

#import "shell32.dll"

int ShellExecuteW(int hwnd,string lpOperation,string lpFile,string lpParameters,string lpDirectory,int nShowCmd);


Then, in onStart() start the desired program:

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

Drop the script on any chart and we will open the specified program.

Now turn in our script library terminal WinUser32.mqh:


Now we need to find the handle of window of running program. This can be done using WinApi — FindWindowW.

In the library of the terminal function is described as follows:

int FindWindowW(string lpClassName,string lpWindowName);

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

Utility is very easy to use. Just run the program, click the button with the binoculars and drag the crosshairs on the desired window, and we immediately see his name, class and other information. In function FindWindowW you can specify the class name and the window name (or separately). It suffices to specify only the name of the window:

int handle=FindWindowW(NULL," New tab — Google Chrome");

the screen display information with handloom found Windows. If the handle is 0, then the window is not found. Getting handle of the main window, you can do with the app all we want: we need to look for a child window, to read out and transmit the text programmatically press buttons, and so on.

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

SetWindowTextW(handle,"My Shomik");

To turn off the computer find a program on the disk shutdown.exe. And just as in the previous case, prescribe the functions of the program with certain parameters:

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

turn off the computer, enter the following line:

shutdown.exe -s-t 00

to reboot the computer, enter the following line:

shutdown.exe -r-t 00

To view the settings for different versions of Windows — click on the following link.


In the library WinUser32.mqh you can find a lot of other cool features that will allow you to run third party software directly from the EA written in MQL4. Full list of WinApi functions you can find on MSDN.

This way you can fully control your computer from the mql script to automate your actions on preparation for trade: open trades magazines, news sites when you open a terminal, and so on.

you can Also write an EA that would trade in another trading platform that does not support automated trading, for example, in a browser-based application for binary options or platform for stock trading. It opens really ample opportunities for work and a variety of interesting ways to use the great variety of programs for receiving, processing and manipulation of others.

forum Topic

best regards, Dmitry aka Silentspec