下面是一些用于通过DDE服务器从提供者获取数据的VBA代码(在本例中是Bloomberg数据提供程序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我正在寻找从python代码调用这样一个DDE服务器的方法。
这段代码在某种程度上是特定于Bloomberg DDE服务器的,但是即使您可以为我提供一种更通用的方法,这也是非常有用的。我只是不知道如何解决这个问题,因为DDEs是微软应用程序的具体内容。
在那些可以帮助你的事情中:
编辑:不,所请求的数据不能通过官方API获得。
谢谢
发布于 2017-05-02 22:57:11
好的,所以这三个方法DDExxx实际上是Excel.Application对象上的方法,但通常被省略,所以实际上您的代码可以这样表示。
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所以现在您需要获得一个Excel.Application对象。StackOverflow这里有一些代码,Driving Excel from Python in Windows
将makepy.py "Microsoft Excel 11.0 Object Library"类型库导入到Python库中的初始步骤是这样的(我承认我不是编写Python,而是快速搜索,我可以猜测一下)
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)因此,VBA MessageBox不同于Python, you'll need to import a Window's API MessageBox (因此是前3行)。Python将字符串与+连接起来,而不是符号和&。字符串可以用单引号和双引号分隔。使用方括号访问数组。还有COM调度接口代码,就像在其他问题中看到的那样。我无法运行这段代码,因为我没有彭博。
有些Python程序员可能需要整理一下。
https://stackoverflow.com/questions/41872117
复制相似问题