这是我用来通过 DDE 服务器(在本例中为 Bloomberg 数据提供程序 DDE 服务器)从提供程序获取数据的一些 VBA 代码:
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 服务器,但即使您能为我提供更通用的方法,也会非常有帮助。我只是不知道如何解决该问题,因为 DDE 是特定于 Microsoft 应用程序的。
在可能有用的事情中:
- 允许从 Python 调用 DDE 的包的名称
- 使用第三方应用程序的解决方法,可能是用 VB 编码或 VB.NET 本身从 Python 调用,有点像用 C++ 创建 DDL 然后从 VBA 调用,但恰恰相反?
编辑:不,请求的数据无法通过官方 API 获得。
谢谢
请您参考如下方法:
好的,所以 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" 的初始步骤也是如此,将 Excel 类型库导入 Python 库,然后一些 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 (因此前三行)。 Python 使用 + 连接字符串,而不是 & 符号。字符串可以用单引号和双引号分隔。使用方括号访问数组。而且您还拥有在其他 SO 问题中看到的 COM Dispatch 接口(interface)代码。我无法运行此代码,因为我没有 Bloomberg。
一些 Python 程序员可能需要整理一下。
