pythonvbaexcelbloombergdde

Calling an existing DDE from Python (Bloomberg data provider)


Here is some VBA code I use to get data from a provider through a DDE server (in this case the Bloomberg data provider DDE Server):

Sub bloom_get()

   nChan = DDEInitiate("BLP", "S")
   sSecurity1 = "JBIG Index" & ", [MLI_DATE=" & datestr & ",MLI_TOT_RTN_LOC="", MLI_PX_RTN_LOC="", MLI_EFF_DUR=""]"""
   vrtResult1 = DDERequest(nChan, sSecurity1)
   MsgBox (vrtResult1(1) & "  " & vrtResult1(2) & "  " & vrtResult1(3) & "  " & vrtResult1(4) & "  ")
   DDETerminate (nChan)

End Sub

I am looking for a way to call such a DDE server from python code.

This code is specific somehow to the Bloomberg DDE server, but even if you can provide me with a more general approach that would be very helpful. I just have no idea whatsoever in how to solve that issue since DDEs are microsoft apps specifics.

Among the things that could be helful:

Edit: No, the requested data is not availbale through the official API.

Thanks


Solution

  • Ok, so the three methods DDExxx are in fact methods on the Excel.Application object but is conventionally omitted so actually your code can in fact be expressed thus

    Sub bloom_get()
    
       nChan = Application.DDEInitiate("BLP", "S")
       sSecurity1 = "JBIG Index" & ", [MLI_DATE=" & datestr & ",MLI_TOT_RTN_LOC="", MLI_PX_RTN_LOC="", MLI_EFF_DUR=""]"""
       vrtResult1 = Application.DDERequest(nChan, sSecurity1)
       MsgBox (vrtResult1(1) & "  " & vrtResult1(2) & "  " & vrtResult1(3) & "  " & vrtResult1(4) & "  ")
       Application.DDETerminate (nChan)
    
    End Sub
    

    So now you need to get hold of an Excel.Application object. StackOverflow has some code here Driving Excel from Python in Windows

    So do the initial steps of makepy.py "Microsoft Excel 11.0 Object Library" to import the Excel type library into a Python library then some Python code would look like this (can I confess I do not write Python but a quick bit of Googling and I can make a guess)

    Import ctypes
    from win32com.client import Dispatch
    MessageBox = ctypes.windll.user32.MessageBoxA
    
    xlApp = Dispatch("Excel.Application")
    #hide app and alerts
    xlApp.Visible = 0
    xlApp.Application.DisplayAlerts = 0
    nChan = xlApp.Application.DDEInitiate("BLP", "S")
    time.sleep(1) # wait for the dde to load 
    sSecurity = t + ", [MLI_DATE=" + datestring + "," + fieldstring + "=""]"""
    vrtResult = xlApp.DDErequest(nChan, sSecurity)
    

    So some explanation, VBA MessageBox is different from Python's, you'll need to import a Window's API MessageBox (hence top 3 lines). Python concatenates strings with + and not ampersand &. Strings can be delimited by single quote as well as double. Arrays are accessed with square brackets. And also you have the COM Dispatch interface code as seen in other SO question. I can't run this code as I have no Bloomberg.

    Some Python programmer probably needs to tidy it up some.