The Meta-Trader 4/5 Bridge
Meta-Trader 4 and 5 (MT4 and MT5 in short) are popular trading platforms that are
provided by many Forex or CFD brokers, sometimes under different names. Due to the
many shortcomings in algo trading, professional
trading systems normally win't run directly on MT4 or MT5. But the platforms can
be used for connecting to the broker and executing trades for a system that runs
on a different software such as Zorro. The connection from Zorro to MT4/5 is
established by a bridge software.
Zorro's MT4/5 bridge allows C/C++ algo trading strategies to receive prices or trade
assets through the MT4 or MT5 "client terminal". This way Zorro can trade with almost all Forex/CFD brokers.
MT4 Terminal with attached Zorro
The client terminal runs on the trader's PC and communicates with the
broker's MT4 / MT5 server via Internet. This server generates prices with a
mechanism determined by the broker, manages the trades, and calculates their
wins or losses. The MT4/5 bridge DLL has two sides. On the MT4/5 side it appears as a 32 bit or 64 bit library DLL that can be called from any
Expert Advisor (EA). On the Zorro side it appears as a Broker Plugin.
The bridge can theoretically be used not only by the Zorro platform, but also by any other software for reading prices and sending orders to
the MT4 or MT5 terminal.
Zorro login fields:
||Account number (as in the terminal's title bar)
Accounts.csv example entries:
Installation and setup
Installing the MT4/5 bridge requires copying some files. For this you should be
familiar with the concept of files and folders on a Windows PC. You
should also know your broker's provided assets and their symbols. First make sure that you have installed Zorro and the broker's
MT4 or MT5 platform (for MT5 the 64 bit version is required).
Despite the lower version number, MT4 is better suited
than MT5 for automated trading, since it requires less code, is more
robust, and supports more broker commands. If your broker offers both,
use MT4. Start the platform and connect to your account with the login data from your broker.
- For MT4: Locate the ZorroMT4.zip archive in your Zorro folder. In the Terminal, select File / Open Data Folder for locating the MQL4\Experts and MQL4\Libraries folders. Unzip the archive. It contains 3 files: Zorro.mq4 goes into the Experts folder, and ZorroMT4.ex4 and ZorroMT4.dll both go into the Libraries folder. Zorro.mq4 is the EA, ZorroMT4.ex4 is its library for connecting
MT4 to Zorro, and ZorroMT4.dll handles the communication between the two programs.
- For MT5: Locate the ZorroMT5.zip archive in your Zorro folder. In the Terminal, select File / Open Data Folder for locating the MQL5\Experts\Advisors and MQL5\Libraries folders. Unzip the archive. It contains 4 files: Zorro.mq5 and zmq4.mqh go into the Experts\Advisors folder, ZorroMT64.ex5 and ZorroMT64.dll both go into the Libraries folder. Zorro.mq5 is the EA, zmq4.mqh is a header file for converting mql4 to mql5 code, ZorroMT64.ex5 is the library for connecting
MT5 to Zorro, and ZorroMT64.dll is a 64 bit DLL that handles the communication between the two programs.
As long as your MT4/5 version is not running in "portable mode", the Experts and Libraries folders are normally located in your user directory (under C:\Users\YourName\AppData\Roaming\MetaQuotes\Terminal\SomeStrangeCodeSequence). There is also a MQL4 or MQL5 subfolder directly
in the terminal's installation directory - that's NOT the folder that you need, and copying files there will have no effect!
- In the Terminal, open the Expert Options under Tools / Options / Expert Advisors.
Make sure that [Allow Automated Trading] and [Allow DLL Imports] are
both enabled. The Auto Trading button below the menu bar must also be enabled. In the Navigator under Expert Advisors you should now find the entry [Zorro].
If not, right click on Expert Advisors and select Refresh.
If you copied a new version or modified the Zorro EA, compile it: double click on Zorro.mq4/mq5
in the Data Folder for opening it with the MetaEditor, then hit
Compile, afterwards do a Refresh
again. Make sure that the terminal is connected to a valid account and that you are logged in;
otherwise it can crash as soon as you start an EA. Open a chart window with the asset that you want to trade; for multiple assets use an EUR/USD chart. The time frame of the chart does not matter.
Drag the Zorro EA onto the chart window. Confirm in the pop-up dialog that DLL imports and live trading are enabled. You should now see the comment "Controlled by Zorro
EA" with a version number in the upper left corner of the chart, and a smiling face (MT4) or a head with a green arrow button (MT5) next to "Zorro" in the upper right corner. If the EA does not start, look for the reason under the Experts
tab: you might have forgotten to copy a file or to check an option.
- Check the assets that you want to trade, and make sure that they are available in the Market Watch window (a right click and Show All enables all assets). Make also sure that the assets have similar names as the assets traded by your strategy. Forex pairs that differ in a missing slash and an added suffix (f.i. "EURUSDpro" instead of "EUR/USD") are corrected automatically. But if you want to trade other assets that have
very different names (f.i. "GOLD" instead of "XAU/USD", or "DE30EUR" instead of "GER30"), convert the names as described below under Symbols. Also make sure that the time and time zone on your PC or VPS are set up correctly - Zorro needs the correct time while trading.
- Now start Zorro. Select [TradeTest] in Zorro's Script scrollbox and [MT4/5] or [MT4/5 (demo)] in the Account scrollbox. Enter your account number (visible in the
terminal's title bar) in the [User] field. The content of the [Password] field does not matter. Make sure that [Demo] / [Real] is set to the same account type as in the
terminal's title bar. For a demo account the word "- Demo Account - " must be visible in the title bar. Otherwise, open a demo account in
the terminal through File / Open an Account. If the account number and type are correct, clicking [Trade] will now attach Zorro to
the terminal and handle price quotes and orders through the broker's Meta
You should see the broker's name in Zorro's message window on login, and a "Zorro connected" comment in the upper left corner of the
terminal's hart window. The asset prices are now downloaded from the server. When accessing an asset the very first time,
the terminal will need a minute or longer for downloading it from the server (equivalent to the "Waiting For Update" message when opening a chart
in the terminal the first time). You'll then see an error message in the Zorro window since the data is not yet available; just connect again after a minute until all assets are loaded. If you get other error messages, look under Known issues.
For some reason, following the 4 steps above turned out to be not easy for all users.
Failure in installing the bridge is one of the most frequent support issues. The
most common mistakes are installing the bridge in a wrong folder, not starting
the Zorro EA, or not enabling automated trading in the platform. If you had no
success in installing the bridge, here's what to check first:
- Do you see the Zorro EA in the list under Expert Advisors?
If not, you either installed it in a wrong folder, or the list was not yet
refreshed, or the EA was not automatically compiled. Try harder.
- Do you see the "Zorro" icon in the upper right corner of the chart? If
not, you have not successfully dragged the Zorro EA on the chart. Aim
- Do you see the "Zorro EA" message in the upper left corner? If not, you
have probably not enabled automated trading or DLL import. Try again.
- Do you get a Zorro error message on session start? You might have mistyped the
account number, or the demo account is in fact a real account. See below.
- Look in the Appdata/Local/Temp folder in your
user directory. Upon starting the connection, you should see a file with the
name mt4sock[some number].sys. The number ends
with your account number. This is the driver file generated when the bridge
is started the first time.
- Does it still not work? Check out if one of the other connection issues
mentioned below affects your configuration.
- Still no luck? We can help. See below.
By carefully following the installation steps, anyone so far got the bridge
running - success rate 100%. If the above installation instructions are not
enough, you can get a much more detailed instruction with many illustrations and
screenshots from a RobotWealth Algo Bootcamp. But if you
really feel unable to install the bridge for whatever reason,
we can do it for you. Order the installation service on the
We'll then install Zorro with MT4/5 bridge on your PC or VPS by remote access,
ready to trade and optionally with an asset list adapted to your broker. This, also, has 100% success
MT4/5 connection issues
- The MT4/5 bridge requires Zorro S for
trading on real accounts. The "Meta"-Trader terminal and Zorro must run on the same PC in the same Windows environment, and must be started by the same user.
- When receiving a practice account number and password directly from a broker, it's sometimes
in fact for a "real" MT4/5 account, even when it's dubbed "demo" and cannot trade
money. You can see the true account type - demo or real - in the start
message of the Zorro EA under the Experts tab.
This type must match the account type in the Zorro scrollbox, otherwise
you'll get an
"Access denied" error. Since the free Zorro version only supports MT4//5 demo
accounts, open a 'true' demo account directly in
MT4 by selecting File / Open an Account. If the
broker has disabled this feature, your only options for MT4/5 demo trading are either Zorro
S or a different broker.
- Using nonexisting assets, for instance by wrong symbols in the
asset list, causes the EA to terminate with a
"Zero divide" error.
- With Zorro S, several Zorro instances can connect simultaneously to a single terminal. Otherwise, only one Zorro instance can connect. Do not attach the Zorro EA to more than one chart window in a single
terminal instance. For running several terminals simultaneously for special purposes - for instance, signal providing - install
them several times in separate folders.
- MT4 and MT5 both have a heavy resource and bandwidth footprint. This can cause long response times, dependent on VPS performance and Internet connection.
Do not run more than a single MT4/5 client on a low-performance VPS (such as the free Amazon VPS) and not more than 3 clients on a high-performance VPS.
Connect not more than 2 or 3 Zorros to a single MT4/5 client. For making
MT4/5 a bit faster, close all chart windows except for the Zorro EA chart,
and remove all assets except for the traded assets from the
Market Watch window.
- While Zorro is connected to MT4/5 in [Trade] mode, do not close or reset the chart with the Zorro EA. Closing the chart will stop the EA and disconnect Zorro (visible by a red square next to the
Server window, and a frozen server time). It must then be stopped and restarted. Same issue with changing the account: the Zorro EA must be restarted by
removing and attaching it again.
- Some MT4 versions are set up to not automatically compile EAs. In this case the
Zorro EA will appear grey in the Navigator and can not be attached to a chart. Open it
in the Experts folder with the MetaQuotes Language Editor and click [Compile] - this will encode the source code and store it as an
.ex4 file, which can then be attached to a chart.
- Some old MT4 versions physically delete .ex4 files that have been generated with a newer
MT4 version. The reason for that bizarre behavior is unknown, but you'll find that the
ZorroMT4.ex4 file suddenly disappears from the libraries folder upon lauching
MT4. Consequently the Zorro EA will generate an error message because the ZorroMT4 library is not found. In that case just update
MT4 to the current version, and copy
ZorroMT4.ex4 again into the libraries folder.
- The MT4/5 bridge was tested with all currently available Meta Trader
terminal builds, and is tested with any new upgrade. Since new builds are often not fully compatible to their predecessors, do not upgrade a working system.
If the terminal upgrades behind your back and you experience problems afterwards, please report on the user forum or to Zorro Support.
MT4/MT5 live trading issues
- If a trade does not open or close (Error 075), you can
see the reason in the MT4/5 Experts Log. The live log is displayed under the
Experts tab. The Experts log is also stored in the Data Folder
under MQL4\Logs (MT4) or MQL5\Logs (MT5).
There's also a journal unter Logs. Valid reasons for not opening or closing a trade are not enough funds, no
permission to trade that asset, a wrong asset symbol, a wrong NFA flag, a
closure, FIFO violation, a wrong stop distance. All this, except for the
funds and permission, can be fixed in your script, in the account list, or in the asset list.
If you see "requote" messages in the Expernts
log, the broker rejected the trade at current market price. You can allow a
worse price by increasing the allowed slippage (default 100 points) with the
- If trading with the current symbol is suspended (MODE_TRADEALLOWED is
false), the server window displays CLOSED.
- The OrderLimit variable is accepted by MT4, but
does not create a limit order, but only an 11 minutes pending trade.
- Some MT4/5 client terminals have not all assets enabled by default. Disabled assets are not visible in the "Market Watch" window and not available for trading. For enabling them, right click on "Market Watch", select "Symbols", select the symbol(s) you want to trade, and click "Show". But do not select more than you actually trade, since any additional asset in the Market Watch requires bandwidth, memory, and CPU resources.
- Some MT4/5 brokers - for instance, LMAX - provide
several different symbols for the same asset. Only one of these symbols is really traded, so make sure that you have the right symbol on the chart window with the Zorro EA.
- MT4/5 clients of some brokers can consume a lot of
especially when many assets are traded simultaneously. This is apparently
caused by a slow access to the broker's MT4/5 server. For reducing the CPU
load in such a case, edit the Zorro.mq4/mq5 EA and change
the MSTIMER definition at the begin of the EA to a higher
number, such as 500. Then compile the EA again. The
disadvantage is a slower reaction to commands.
- The trade volume displayed on the MT4 /
MT5 terminal is not in lots. It is sometimes
in units of 10,000 contracts (often for forex), sometimes in 0.1, 1, 10, or
25 contract units (for CFDs). MT4 / MT5 lots are not identical to the
- Some MT4/5 servers are reported to return incorrect
asset parameters, such as rollover, margin, lot amount, and commission.
Especially wrong lot amounts can be harmful in live trading when they lead
to too large trade sizes. Use the SET_PATCH
command to override wrong server parameters. In that case or when parameters are unavailable, the values entered in the
asset list are used. Asset lists downloaded from
MT4 servers often need manual correction.
- Some MT4/5 servers need a long time for the initial access of the price history of previously unused assets. This can cause a timeout. The strategy will then not start, and a Error 054 or Error 055 message is displayed. In that case just restart the strategy by clicking [Trade] again. Inbetween the server had enough time to upload the missing
histroy data. You might need to repeat that step
a few times until all assets load properly.
- Some MT4/5 servers have temporarily or permanently
insufficient price history for systems with long lookback periods,
such as Z1, Z2, or Z12 - especially when starting after a holiday. The the
lookback period will then be incomplete, indicated with an Error 047 message.
If the problem persists, set the PRELOAD flag or the Preload
z.ini setting for using Zorro's own price history instead.
- Some MT4 brokers were reported to route historical price data requests
on weekends to a different server and return dubious
price data for out-of-market hours.
- Most MT4/5 servers can download only a limited live price history, such as 2000 hours. Therefore the assetHistory function and the Download script normally does not work with
MT4 or MT5; downloading large historical price data requires direct access to your broker's API.
- Trades via MT4/5 are often executed not on the real market, but on a
virtual market generated
by the MT4 server. In that case you must NOT set the NFA flag, as the virtual trades are not NFA compliant. Still, hedging can be prohibited, so set
Hedge mode to 0 or 4 in that case. If the account complies with NFA FIFO rules (first in, first out), you must close old trades before closing new trades. Otherwise you'll see an error message like
"Close failed - prohibited by FIFO rules" in the Experts log. Use
Hedge = 4 in such a case, which automatically ensures FIFO compliant closing of trades. Note that you can not run multiple strategies in parallel on no-hedging or FIFO compliant accounts.
- The MT4/5 bridge does not allow opening trades that are immediately
closed on the MT4/5 side due to a too tight Stop
distance. Set StopFactor = 0 to be
on the safe side.
- Not all MT4/5 brokers support partial closing of trades. You can see this from an error message like
"invalid trade volume" in the Experts log,
or from 'remainders' of trades that stay open due to failed partial closes.
Some brokers emulate partial closing by closing the whole position and opening
a new one with the remainder. If the closing fails, trades are not under Zorro
control anymore. In case of any problems with partial closing, set
Hedge to 4 or below for disabling
- Some MT4/5 brokers use odd PIP sizes for CFDs, such as 0.25, and don't accept stop limits that are not a multiple of this pip size. In this case set up the correct PIP size and PIPCost value in the
- Some MT4/5 brokers use their own units for rollover. Sometimes it's per
lot, sometimes per 10,000 contracts. Check the rollover and other parameters
in your asset list when trading through MT4 or MT5. Wrong values are not
harmful for trading, but give a wrong result in the backtest.
- If the communication between the MT4/5 client and server breaks down,
the Experts Log contains messages such as "communication lost"
or "trade timeout". In the latter case, orphaned positions
are possible. It is not recommended to trade with an unstable client/server
- The terminal displays prices as Bid prices, while Zorro always uses
Ask prices. So you'll see different prices on the
terminal's charts and in the Zorro window. This has no effect on trading, as Zorro internally converts all prices to Bid or Ask dependent on the trade direction.
- Communication between Zorro and the terminal on the same PC is normally very fast, so trading with the
MT4/5 bridge does not add noticeable
latency compared to directly trading with an "Expert Advisor". Still, if a broker offers both API and
MT4/5 access, using the API is normally preferable. If they offer both MT4 and
MT5, MT4 is faster and thus preferable. The API is faster than the MT4/5 server, and MT4/5 accounts are
reported to be often slightly more expensive due to additional spread and slippage.
- When a Zorro connects to a MT4/MT5 terminal, its used assets are
subscribed and subsequently permanent
updated. Once an asset is subscribed, it stays so even when Zorro
disconnects. To prevent continued updating of unused assets after Zorro
disconnection, restart the terminal. This will unsubscribe all assets.
Many MT4/5 versions use special asset names with broker specific suffixes. For instance, the "EUR/USD" currency pair is called "EURUSD" in the
MT4 version by FXCM™, "EURUSDm" in the IBFX™ version, "EURUSDi" in the version by Traders Way™, and "EURUSD.G" in the Global Prime™ version. The Zorro EA automatically adapts the names by removing the slash and adding the broker specific suffix from EURUSD symbol name of the EA chart window. So no
symbol adaption is required on your side.
If this automated suffix appending is not desired, use a chart window for
Zorro with an asset that does not contain a suffix.
For non-currency assets with names longer than 6 characters, or for names that can not be automatically adapted this way - for instance, "GOLD" instead of "XAU/USD" - the symbols
used by Meta Trader must be entered directly in the Symbol column of the used asset list. Otherwise your selected assets are not found in the
terminal's symbol list, your script won't start, and you'll get "asset
unknown" error messages in the Experts log. Fix the asset name, then
close MT4 and Zorro and start them again.
All assets you want to trade must be visible in the Market Watch window. If you still get error messages,
you can find the reason in the Experts Log that the terminal generates. It normally tells you
what the problem is.
Additional data and commands
The MT4/5 bridge supports the following features:
- marketVal: Not supported in historical data,
spread in live data.
- marketVol: Normally not supported in historical data, tick frequency in live data.
- Partial closing: Supported by some, but not all MT4 brokers; see
remarks above. Not
supported by MT5.
- OrderLimit: MT4/5 supports no limit orders. A
pending trade is instead opened.
The MT4/5 bridge supports the brokerCommand function with the following commands:
- GET_POSITION (MT4 only)
- PLOT_HLINE (MT4 only)
- PLOT_TEXT (MT4 only)
- PLOT_MOVE (MT4 only)
- PLOT_REMOVE (MT4 only)
- PLOT_REMOVEALL (MT4 only)
- PLOT_STRING (MT4 only)
You can add your own commands for sending or retrieving further information from the
MT4/MT5 client. For this, edit the zorro.mq4 / zorro.mq5 EA. Any brokerCommand(Command,Parameter) call in a Zorro script triggers the CMD_BCOMMAND part of the Zorro EA run loop. Use a Command number between 2000 and 2999 for not getting into conflict with future official broker commands. The parameter is transferred in arr and can be used for calling arbitrary
MT4 / MT5 functions. Use arr for transferring a numerical return value back to Zorro. Be aware that sent parameters are stored on the
terminal side and thus affect all Zorro instances connected to that terminal - not only the instance that sent the parameter.
Zorro EA functions
Zorro can directly trade with MT4/5 through the Zorro Expert Advisor. This EA is included in MQ4/MQ5 source code, so it can be used as a template for writing own EAs that evaluate trade signals from Zorro. For this the following four functions are available in the ZorroMT4.ex4 or ZorroMT5.ex5 libraries:
ZorroInit () : int
Open the Zorro plugin. Normally called in the MT4 init() function.
0 when the initialization failed, otherwise nonzero.
Release the Zorro plugin. Normally called in the MT4 deinit() function.
ZorroRequest (double& array): int
Receive a request from the DLL, f.i. for opening a trade. Normally called in a
loop in the MT4 start() function.
||Array of up to 10 double variables that are filled with the request parameters.
0 if no request is pending, otherwise one of the following commands: CMD_COMMENT, CMD_PRINT, CMD_ASSET, CMD_HISTORY, CMD_BALANCE, CMD_BUY, CMD_TRADE, CMD_SELL, CMD_STOP, CMD_BCOMMAND (see Zorro.mq4). The zstring() function returns a string containing the requested asset name or a text message. Asset names need possibly be converted to the specific symbol names of the
MT4 version (see remarks).
ZorroRespond (int cmd, double& array)
Respond to a request from the DLL.
Normally called to transfer the result after receiving a request.
||One of the following commands: CMD_ASSET, CMD_TICK, CMD_HISTORY, CMD_BALANCE, CMD_BUY, CMD_TRADE, CMD_SELL,CMD_STOP, CMD_BCOMMAND (see Zorro.mq4).
||Array of up to 10 double variables containing the response parameters.
Example: see Zorro.mq4 or Zorro.mq5