Brokers may need logic that standard MT4 and MT5 settings do not provide: a custom commission, different swap conditions, specific bonus logic or an extra risk check. When the logic must act at the moment the server processes an operation, it has to run inside the server. This is what server MetaTrader plugins are for.
MetaTrader server plugins run inside the broker’s server and work through the Server API, the interface between the server and the plugin:
The server calls the plugin at set processing stages.
The plugin uses the API to read data and perform supported operations.
A MetaTrader server plugin is delivered as a Windows DLL, or dynamic-link library: a compiled software module that the server loads into its own running process.
Once loaded, the plugin shares the server’s memory and processing resources. It can work with server data and participate in operations without sending each decision to an external application over a network.
Server Plugins vs Expert Advisors and Manager API Applications
This distinguishes a server plugin from two other types of MetaTrader software:
| Software type | Where it runs | How it interacts with MetaTrader |
|---|---|---|
| Server plugin | Inside the broker’s server process | Uses the Server API and receives direct calls from the server |
| Expert Advisor | In a trader’s terminal | Performs automated trading tasks for the account used by the terminal |
| Manager API application | Outside the server | Connects over the network using a manager account |
A server plugin applies to the groups, accounts, and symbols configured for it. Its operation does not depend on whether a client’s trading terminal is online.
The distinction matters because some broker requirements involve changing a decision before the server completes it. A custom commission calculation, for example, needs to participate in the calculation itself.
How MetaTrader plugins are built also differs: they are written in C++ using the Server API, while Expert Advisors are written in MQL4 or MQL5, and Manager API applications are typically written in C++ or C#.
How the Server Loads a Plugin
Before a plugin can handle trading operations, the server needs to identify it, load its configuration, and give it access to the Server API. MT4 and MT5 provide different mechanisms for this lifecycle.
MT4: loading at server startup
When the MT4 server starts, it scans its plugins folder. For each plugin module, it requests identifying information through a function called MtSrvAbout and checks its digital signature.
If the plugin is enabled for loading at startup, the server calls MtSrvStartup. This gives the plugin the server interface it will use to read data and perform operations.
The plugin’s parameters are managed through MT4 Administrator. When several plugins use the same hook, their order in Administrator determines which acts first, which is key to avoiding conflicts in a multi-plugin setup. Enabling, disabling, or changing the order of plugins takes effect at the next server restart.
At shutdown, the server calls MtSrvCleanup. The plugin must then stop its own worker threads and finish its interaction with the server.
MT5: starting from a plugin configuration
MT5 identifies available modules and checks the Server API version they support. An administrator creates a configuration for the required plugin and assigns it to a server. One module can have several configured instances with different parameters.
For an enabled configuration, the server creates the plugin instance and calls its Start method once the databases are ready. During startup, the plugin receives the Server API interface and subscribes to the hooks and events it needs.
When the plugin stops, it releases its resources and its subscriptions are removed.
MT5 supports enabling and disabling plugins without restarting the platform. An update can therefore be performed by disabling the configuration, replacing the plugin module, and enabling it again while the rest of the server continues running.
Hooks and Events: How the Server Calls a Plugin
A loaded plugin does not independently take control of every server operation. It participates through defined points provided by the platform.
These interactions fall into two categories: hooks and events.
Hooks can influence an operation
A hook gives a plugin an opportunity to act while the server is processing an operation. Depending on the hook, the plugin can allow processing to continue, adjust a value, or reject the operation.
For example, MT4 provides a commission hook that can replace the standard commission calculation. It also provides a rollover hook for swap calculations and a quote-filtering hook before a quote enters the price stream.
The purpose of the hook determines what the plugin can change. A commission hook is a defined opportunity to calculate commission. It does not give the plugin an unrestricted way to alter every part of the trade.
Events notify plugins about what has happened
An event tells subscribed plugins that an operation or change has occurred. The notification data is read-only, so the event itself cannot change the completed operation.
A recording tool, for example, can receive a notification about a completed deal and record its details. It does not need to intervene in execution to do that job.
This separation allows plugins to perform different roles. Some participate in decisions, while others observe the results.
Where Plugins Enter the Trading Process
The server provides several opportunities for plugin logic to run because checking a request, routing it, executing it, and applying the result are separate stages.
In MT4, MtSrvTradeRequestFilter receives a client’s trade request before it enters the queue and can reject it. Once the request is queued, MtSrvTradeRequestApply allows an execution plugin to confirm, requote, or take it over.
MT4 also provides hooks for operations initiated by the server, including pending-order activations, Stop Loss, Take Profit, and stop-outs.
MT5 makes the stages of the request pipeline explicit:
| MT5 hook | When it runs |
|---|---|
| HookTradeRequestAdd | After the server’s own checks |
| HookTradeRequestRoute | Before routing |
| HookTradeRequestProcess | Immediately before execution |
| HookTradeExecution | Before a gateway’s execution result is applied |
These stages let developers place a rule where it belongs. A request check and an adjustment to an execution result are different tasks, so they use different entry points.
After the relevant operation occurs, event notifications provide information to subscribed plugins. A request can therefore pass through processing hooks and subsequently produce notifications used for recording or monitoring.
What the Server API Lets a Plugin Do
Hooks and events determine when a plugin is called. The Server API also provides methods that determine what it can do with the server:
- Accounts and trading. A plugin can read and update account information, perform balance and credit operations, and work with orders. In MT5, it can work directly with positions, orders, and deals.
- Calculations. A plugin can use the server’s calculations for margin, profit, and conversion rates.
- Prices. The API provides access to current and historical prices and supports adding ticks to the price stream. This allows a plugin to act as a quote source or participate in quote filtering.
- Configuration. Access covers groups, symbols, managers, gateways, and other server settings.
- Reporting and communication. A plugin can read daily reports, respond to stages of the trading-day close, and send supported notifications.
These are platform capabilities. An individual product uses the functions needed for its purpose and applies its business rules to the configured accounts and symbols.
For example, a swap-management plugin uses the relevant account, configuration, and rollover functionality. It does not need to perform every operation available through the API.
MetaTrader plugin pricing therefore depends on the product and the task it performs, not on the full range of Server API capabilities.
Why Running Inside the Server Requires Careful Design
Direct access makes server plugins useful, but it also means their implementation can affect the trading server itself.
A plugin shares resources with the platform. If it holds a processing thread for too long, mishandles memory, or allows an error to escape, the consequences can extend beyond that plugin’s own function.
Processing must return promptly to the server
When the server calls a hook, it needs the plugin’s response so processing can continue. Lengthy network requests, disk operations, and large calculations should therefore run on the plugin’s own workers rather than keep the server thread occupied.
For example, writing a log record does not need to hold up the processing of a trading request. The record can be queued for writing while the hook returns control to the server.
Simultaneous operations must be handled safely
In MT5, hooks and events can be called simultaneously on different server threads. A plugin must coordinate access to its own data so that concurrent operations do not interfere with each other.
The server also protects certain data while processing operations. Plugins need to respect those protections and avoid creating situations in which one operation waits indefinitely for another. Such a situation is called a deadlock.
Memory and error handling affect the shared process
Because the plugin runs inside the server, memory use is part of the server’s overall resource budget. This is particularly significant for MT4’s 32-bit process.
Errors also need to be handled at the plugin’s entry points. An unhandled failure can affect the trade server, so fault handling is part of the plugin’s core design.
These requirements explain why implementing a trading rule is only one part of developing a server plugin. The rule must also operate correctly within the platform’s processing environment.
Conclusion
A MetaTrader server plugin runs inside the server and uses the Server API to participate in defined operations. Hooks allow it to influence processing, events notify it about completed actions, and API methods provide access to server data and supported functions.
For a broker, this makes it possible to apply custom commission, swap, execution, and risk management logic directly within the trading platform. Reliable implementation depends on prompt hook responses, careful resource handling, validated settings, and diagnostics that make the plugin’s behavior traceable.