Problem Description
Solution
To retrieve information about the trading terminal's environment in MQL4, you primarily use the TerminalInfoString() and TerminalInfoInteger() functions. These functions allow you to access specific properties defined by the ENUM_TERMINAL_INFO_STRING and ENUM_TERMINAL_INFO_INTEGER enumerations.
Key Functions
-
TerminalInfoString(int property_id): Retrieves text-based properties.TERMINAL_PATH: The folder from which the terminal was launched.TERMINAL_DATA_PATH: The folder where the terminal stores data (Experts, Indicators, Logs, etc.). This is often different from the installation path due to Windows security permissions.TERMINAL_COMMONDATA_PATH: The common folder shared by all MetaTrader terminals on the computer.TERMINAL_COMPANY: The name of the broker/company.
-
TerminalInfoInteger(int property_id): Retrieves integer or boolean properties.TERMINAL_BUILD: The build number of the client terminal.TERMINAL_CONNECTED: Returns1if connected to the server,0otherwise.TERMINAL_MAXBARS: The maximum number of bars on the chart.
MQL4 Script Implementation
The following script demonstrates how to retrieve and print the most relevant environment information to the "Experts" log tab.
//+------------------------------------------------------------------+
//| GetTerminalEnvironment.mq4 |
//| Copyright 2023, Quantitative AI |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Quantitative AI"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- Retrieve String Properties
string installPath = TerminalInfoString(TERMINAL_PATH);
string dataPath = TerminalInfoString(TERMINAL_DATA_PATH);
string commonPath = TerminalInfoString(TERMINAL_COMMONDATA_PATH);
string companyName = TerminalInfoString(TERMINAL_COMPANY);
string termName = TerminalInfoString(TERMINAL_NAME);
string language = TerminalInfoString(TERMINAL_LANGUAGE);
//--- Retrieve Integer Properties
int buildNumber = TerminalInfoInteger(TERMINAL_BUILD);
bool isConnected = (bool)TerminalInfoInteger(TERMINAL_CONNECTED);
bool isDllAllowed = (bool)TerminalInfoInteger(TERMINAL_DLLS_ALLOWED);
bool isTradeAllowed= (bool)TerminalInfoInteger(TERMINAL_TRADE_ALLOWED);
bool isEmailEnabled= (bool)TerminalInfoInteger(TERMINAL_EMAIL_ENABLED);
bool isFtpEnabled = (bool)TerminalInfoInteger(TERMINAL_FTP_ENABLED);
int cpuCores = TerminalInfoInteger(TERMINAL_CPU_CORES);
int memoryPhysical = TerminalInfoInteger(TERMINAL_MEMORY_PHYSICAL); // In MB
//--- Output to Experts Log
Print("========== TERMINAL ENVIRONMENT INFO ==========");
Print("Terminal Name: ", termName);
Print("Company: ", companyName);
Print("Build Number: ", buildNumber);
Print("Language: ", language);
Print("CPU Cores: ", cpuCores);
Print("Physical Memory: ", memoryPhysical, " MB");
Print("");
Print("--- Paths ---");
Print("Installation Path: ", installPath);
Print("Data Path (MQL4): ", dataPath);
Print("Common Data Path: ", commonPath);
Print("");
Print("--- Permissions & Status ---");
Print("Connected: ", isConnected ? "Yes" : "No");
Print("DLL Calls Allowed: ", isDllAllowed ? "Yes" : "No");
Print("Trade Allowed: ", isTradeAllowed ? "Yes" : "No");
Print("Email Enabled: ", isEmailEnabled ? "Yes" : "No");
Print("FTP Enabled: ", isFtpEnabled ? "Yes" : "No");
Print("===============================================");
}
//+------------------------------------------------------------------+
Explanation of Paths
It is crucial to understand the difference between the paths returned:
- Installation Path (
TERMINAL_PATH): This is where theterminal.exefile resides (e.g.,C:\Program Files (x86)\MetaTrader 4). You usually cannot write files here programmatically. - Data Path (
TERMINAL_DATA_PATH): This is the "Sandbox" where MQL4 programs operate. Your Expert Advisors, Indicators, and the\Filesfolder for file operations are located here. This path is usually in the user'sAppDatafolder. - Common Data Path (
TERMINAL_COMMONDATA_PATH): This folder is used to share data (like files) between different MetaTrader terminals installed on the same computer.
Q&A
Q: Can I change the terminal environment settings using MQL4?
A: No, TerminalInfo functions are read-only. You cannot change the installation path or enable DLLs programmatically; these must be configured by the user in the terminal settings or during installation.
Q: Why is TERMINAL_DATA_PATH different from TERMINAL_PATH?
A: Modern Windows security (UAC) prevents applications from writing to the Program Files directory. MetaTrader 4 separates the executable (Installation Path) from the user's editable data (Data Path) to comply with these security standards.
Q: How do I check if the terminal is connected to the internet?
A: Use TerminalInfoInteger(TERMINAL_CONNECTED). However, note that this checks the connection to the trade server, not necessarily the general internet connection.
Q: How can I check if my EA is allowed to trade?
A: Use TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) to check if trading is enabled globally in the terminal, and IsTradeAllowed() to check if the specific Expert Advisor instance is allowed to trade.