|
This application shows how to fetch realtime forex quotes using the DDE functionality of Metatrader 4
Create a free account and login to the forums to download the Source Code for the 4X DDE Client software
Introduction:
DDE (Dynamic Data Exchange) is an interprocess communication system that enables two applications to share the same data. This mechanism is deprecated in favor of other alternatives such as OLE or COM. In financial applications, it is popular and DDE enabled applications are still being built.
For a DDE dialog to occur, two applications must be running, the server, or provider of data, and the client, or consumer. If the server application is not running, the client connection attempts will fail. In this example, the server will be Metatrader Terminal 4.0, and the clients will be Microsoft Excel and the 4X DDE Client.
Connecting to Metatrader 4.0 using Excel:
The default installation of Metatrader does not enable the DDE server. It must be manually enabled once. To do so, click on Tools -> Options on the Server tab (selected by default), ensure the 'Enable DDE server' option is checked, click OK to save your options.
Once Metatrader is running, connected to a server, and its DDE server has been enabled, launch Excel. To test the connection, type in a cell =MT4|ASK!EURUSD , this command obtains the ASK quote for the EUR USD pair using the MT4 DDE server. The cell content should display the last ask price for the pair and automatically update this value in real time if everything is working properly. If Metatrader is not running, or the DDE server is not enabled, Excel will try to start it using the non existing MT4.exe filename, which will fail and #VALUE! or #REF! will be displayed in cells using live data.
Excel implements a DDE client with a syntax that is extremely easy to use and helps in debugging. To communicate, the client needs to know the DDE Application name, the DDE Topic, and the DDE Item. To make a request, the syntax is as follows: =DDEAppName|DDETopic!DDEItem
The DDEAppName for Metatrader 4.0 is MT4, the supported DDETopics are listed below:
| DDE Topic |
DDE Item |
Description |
Example |
Output |
| BID |
Forex Pair |
Requests Bid price for pair |
=MT4|BID!EURUSD |
1.2294 |
| ASK |
Forex Pair |
Requests Ask price for pair |
=MT4|ASK!EURUSD |
1.2298 |
| HIGH |
Forex Pair |
Requests High value price for pair |
=MT4|HIGH!EURUSD |
1.2306 |
| LOW |
Forex Pair |
Requests Low value price for pair |
=MT4|LOW!EURUSD |
1.2262 |
| QUOTE |
Forex Pair |
Returns full information on pair |
=MT4|QUOTE!EURUSD |
2006/08/30 17:12 1.2294 1.2298 |
| TIME |
|
Returns time of last tick |
=MT4|TIME |
2006/08/30 17:12 |
The DDE Topic STATUS and DDE Items ACCOUNT, BALANCE and CONNECT have been deprecated and obsoleted in version 4.0 of the client.
Connecting to Metatrader using 4X DDE Client:
The 4X DDE Client uses the NDde library to send and receive DDE messages. To see the application in action, launch it and press the Start Quotes button. As ticks are received, they will be displayed in the application. If Metatrader is not started, the application will display a message asking the user to launch it or enable the DDE server.
Source Code:
The following source code creates a DDE client, connects to the metatrader server to the QUOTE topic, then adds notifications for the items USDCHF and EURUSD. DdeClient client = new DdeClient("MT4", "QUOTE"); client.Disconnected += OnDisconnected; client.Connect(); client.StartAdvise("USDCHF", 1, true, 60000); client.StartAdvise("EURUSD", 1, true, 60000); client.Advise += OnAdvise;
Credits:
This application uses the NDde library, a C# implementation of a DDE client, server, and monitor. NDde is available at http://www.gotdotnet.com/workspaces/workspace.aspx?id=03b24eab-36c4-48ec-9a9a-c4977f46ce2c
Metatrader 4 is the source for the realtime forex quotes. It is available at http://www.metaquotes.net/terminal
|