For anyone interested in getting quotes from MT4 into Python for use with your own trading applications, see this simple way to do it:
In this HOWTO, I'll be using Pepperstone's MT4 client, which can be downloaded here.
Firstly, the pywin default DDE client seems to have trouble talking with MT4, and I've read on another site that it relates to MT4 only supporting the 'advise' method of getting data from DDE. This tripped me up long ago and it was frustrating enough that I just abandoned this method of quote collection and used MQL4 within MT4 to export the data I needed.
I recently revisited DDE and using some Google-fu, I found an alternate DDE client that appears to work fine with MT4, so I wanted to provide a HOWTO resource plus a bit of example code:
(This HOWTO has been written assuming you already know the basics of Python and have Python v2.x installed.)
Steps:
1) Enable DDE Server in MT4. Go to Tools > Options > Server Tab > Enable DDE server
2) You must get the alternate DDE client.I won't host it here, since it's someone else's code (ActiveState recipe,) but here's a direct link to it: EDIT: After finding a few bugs in the original sources work, I have forked it on github and below is the link to said fork:
https://gist.github.com/FXGears/351a447f47d9356ada6b
(It's not perfect, but I worked around an issue that makes it fail with MT4 after running for a bit.)
Save this file in your project's directory.
3) Create a new python script to test it out. This one for example:
Which outputs something like this:
Notes on the example code:
* If you broker uses different symbol naming conventions (like "EURUSD.x" instead of "EURUSD" as seen within MT4,) you'll need to edit this.
* Notice that you register the QUOTE topic in general first, then ask to be advised when a given 'item' (such as EURUSD) is updated on this topic. You may then 'request' the current value of an item within the topic.
* The DDE Client is written using direct calls to Window's C libraries.. which means that outside of the python code, the library wants to print to console, and thus every time there's a price change you'll notice a line get printed with said change outside of the quote table that prints every second. I'm working on a way to squelch this without losing the ability to get updates as they happen instead of when the user pulls them with 'request'
In this HOWTO, I'll be using Pepperstone's MT4 client, which can be downloaded here.
Firstly, the pywin default DDE client seems to have trouble talking with MT4, and I've read on another site that it relates to MT4 only supporting the 'advise' method of getting data from DDE. This tripped me up long ago and it was frustrating enough that I just abandoned this method of quote collection and used MQL4 within MT4 to export the data I needed.
I recently revisited DDE and using some Google-fu, I found an alternate DDE client that appears to work fine with MT4, so I wanted to provide a HOWTO resource plus a bit of example code:
(This HOWTO has been written assuming you already know the basics of Python and have Python v2.x installed.)
Steps:
1) Enable DDE Server in MT4. Go to Tools > Options > Server Tab > Enable DDE server
2) You must get the alternate DDE client.
https://gist.github.com/FXGears/351a447f47d9356ada6b
(It's not perfect, but I worked around an issue that makes it fail with MT4 after running for a bit.)
Save this file in your project's directory.
3) Create a new python script to test it out. This one for example:
Code:
import dde_client as ddec
import time
# Connect to MT4
# Must register BID and ASK as topics separately..
QUOTE_client = ddec.DDEClient('MT4', 'QUOTE')
# Register desired symbols..
symbols = ['EURUSD', 'AUDUSD', 'USDJPY', 'USDCAD']
for i in symbols:
QUOTE_client.advise(i)
# Prove it worked:
columns = ['Symbol', 'DATE', 'TIME', 'BID', 'ASK']
while 1:
time.sleep(1)
to_display = []
for item in symbols:
current_quote = QUOTE_client.request(item).split()
current_quote.insert(0, item)
to_display.append(current_quote)
print '\t'.join(columns)
for line in to_display:
print ' '.join(line)
Which outputs something like this:
Code:
Symbol DATE TIME BID ASK
EURUSD 2015/04/20 06:15 1.07976 1.07979
AUDUSD 2015/04/20 06:15 0.78069 0.78076
USDJPY 2015/04/20 06:15 118.860 118.863
USDCAD 2015/04/20 06:15 1.21979 1.21989
Notes on the example code:
* If you broker uses different symbol naming conventions (like "EURUSD.x" instead of "EURUSD" as seen within MT4,) you'll need to edit this.
* Notice that you register the QUOTE topic in general first, then ask to be advised when a given 'item' (such as EURUSD) is updated on this topic. You may then 'request' the current value of an item within the topic.
* The DDE Client is written using direct calls to Window's C libraries.. which means that outside of the python code, the library wants to print to console, and thus every time there's a price change you'll notice a line get printed with said change outside of the quote table that prints every second. I'm working on a way to squelch this without losing the ability to get updates as they happen instead of when the user pulls them with 'request'
Last edited: