Metatrader4/5 Bridge Plugin
Metatrader4™ (MT4) and Metatrader5™ (MT5) by MetaQuotes™ are popular trading platforms, and at least one of them is supported by most brokers. With the MT4/5 bridge, Zorro can control MT4 or MT5 as an Expert Advisor (EA) and this way trade with all brokers that provide any of the Metatrader platforms for their clients.
MT4 client with attached Zorro
The MetaTrader software is split in two parts: The client terminal runs on the trader's PC and communicates with the MetaTrader server via Internet. The server generates prices and slippage as set up by the broker, and manages the trades. The MT4/5 bridge DLL has also two sides. On the MT4/5 side it appears as a 32 bit or 64 bit library DLL that can be called from any EA. On the Zorro side it appears as a Broker Plugin DLL. This way the bridge can theoretically be used not only by Zorro, but also by any other software for reading prices and sending orders to MT4 or MT5.
||Account number (as in the MMT4/5 title bar)
Installation and startup - MT4 and MT5
If you want to run Zorro on a VPS together with the MT4/5 bridge and have limited PC experience, you can subscribe a VPS installation service on the download page. Otherwise, here are the 4 steps for connecting Zorro to Metatrader. First make sure to have installed Zorro and the broker's MT4 or MT5 platform (for MT5 the 64 bit version is required). Connect to your MT4/5 account with the login data from your broker.
- For MT4: Locate the ZorroMT4.zip archive in your Zorro folder. In the Metatrader 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 Metatrader 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 Metatrader 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 Metatrader directory - that's NOT the folder that you need, and copying files there will have no effect!
- In the Metatrader terminal, check the Expert Options under Tools / Options / Expert Advisors, and 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, first compile the Zorro EA: double click on Zorro.mq4/mq5
in the Data Folder for opening it with the MetaEditor, then hit
Compile), afterwards Refresh
again. Make sure that the terminal is connected to a valid account and that you are logged in;
otherwise some Metatrader versons will crash when 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 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 Metatrader account number (visible in the MT4 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 Metatrader title bar. For a demo account the word "- Demo Account - " must be visible in the MT4 title bar. Otherwise, open a true demo account in MT4 through File / Open an Account. If the account number and type are correct, clicking [Trade] will now attach Zorro to Metatrader and handle price quotes and orders through the Metatrader server.
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 Metatrader chart window. The asset prices are now downloaded from the server. When accessing an asset the very first time, Metatrader will need a minute or longer for downloading it from the server (equivalent to the "Waiting For Update" message when opening a Metatrader chart the first time). In that case you'll 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 .
Most Metatrader 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 adaption is required on your side.
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 Metatrader symbols must be entered directly in the Symbol column of the used asset list. Otherwise your selected assets are not found in the Metatrader symbol list, and you'll get "asset unavailable" error messages when starting your script.
All assets you want to trade must be visible in the Market Watch window. If you still get error messages about unavailable assets, read the Metatrader Experts log. It normally tells you hat the problem is.
MT4/MT5 Installation issues:
- The MT4/5bridge of the free Zorro version supports demo accounts only. A MT4/5 bridge for real accounts is automatically included with
Zorro S. The Metatrader client terminal and Zorro must run on the same PC in the same Windows environment, and must be started by the same user.
- With Zorro S, several Zorro instances can connect simultaneously to a single Metatrader client terminal. Otherwise, only one Zorro instance can connect. Do not attach the Zorro EA to more than one chart window in a single Metatrader instance. For running several Metatrader instances simultaneously for special purposes - for instance, signal providing - install several Metatrader clients in separate folders.
- MT4 and MT5 are resource and bandwidth hungry, which can cause long response times of the MT4/5 bridge when several processes run in parallel. Depending on the Internet bandwidth, you should normally not run more than one Metatrader client on a low-performance VPS (such as the free Amazon VPS), and not more than 3 clients on a high-performance VPS. Make sure that only the really traded assets are visible in the
Market Watch window.
- While Zorro is connected to MT4 or MT5 in [Trade] mode, do not close or reset the Metatrader 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.
- When getting a practice account number and password directly from a broker, it's sometimes displayed as "Real Account" in the MT4/5 title bar. Since the 'demo only' version of the bridge can not trade with real accounts, you need to open a true demo account. You can open any number of demo accounts directly in MT4 by selecting
File / Open an Account.
- The MT4/5 bridge was tested with all currently available Metatrader client 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 Metatrader upgrades behind your back and you experience problems afterwards, please report on the user forum or to Zorro Support.
MT4/MT5 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 daily log is also stored in the Data Folder
under Logs (MT4) or Experts/Logs (MT5).
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, market
closure, FIFO violation, or 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.
- Some MT4/5 client terminals don't have 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.
- The MT4/5 clients of a few brokers consume a lot of CPU resources
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.
- Some MT4/5 brokers provide correct rollover, margin, and commission
values, others don't; it depends on the MT4 server setup. If trading cost
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 asset. You might need to repeat that step
a few times until all assets load properly.
- Some MT4/5 servers have no sufficient price history for systems with a long lookback period, such as Z1, Z2, or Z12 - especially when starting after a holiday. The strategy will then not start, and a Error 047 message is displayed. In that case use the PRELOAD flag or the Preload .ini setup for using Zorro's own history instead of the Metatrader server.
- 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 handled
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.
- 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
- 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 Metatrader client terminal displays prices as Bid prices, while Zorro always uses
Ask prices. So you'll see different prices on the Metatrader chart 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 Metatrader 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 preferable. The API is faster than the Metatrader server, and Metatrader accounts are often slightly more expensive due to additional spread and slippage.
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. Normally not
supported by MT5.
The MT4/5 bridge supports the brokerCommand function with the following commands:
- SET_LIMIT (MT4 only, returns
success, but creates
11-minutes pending order)
- 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 Metatrader side and thus affect all Zorro instances connected to that client - not only the instance that sent the parameter.
Zorro EA functions
Zorro can directly trade with Metatrader through the Zorro Expert Advisor. This EA is included in MQL4/MQL5 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
Brokers, brokerCommand, order, broker plugin