CTP Python API(利用Swig 封装)Windows版(mduserapi)
2017-08-17 19:18
826 查看
1. 准备工作
和上文traderapi一致。此版本是在原先版本上的升级,解决了onfrontconnected回调的宕机问题。感谢知乎网友@warlock 一起探讨。
2. 通过Swig得到python接口文件
新建文件
这里主要的变化就在于我添加了一个订阅行情的函数,因为我实际操作过程中发现CTP订阅行情参数是二级指针,转化为python后我每次调用都会宕机。没办法我折中写了个参数为一级指针的函数包装了下CTP订阅行情函数,自己手写的函数每次只能订阅一个合约。
在
等到运行完成后,可以看到当前目录下生成了
thostmduserapi_wrap.h
thostmduserapi_wrap.cxx
thostmduserapi.py
3. 通过C++得到python可调用的pyd动态库
与上文traderapi中一致。
4. Python Demo
新建文件mduserapi_demo.py,注意文件同目录底下要有如下三个文件:
thostmduserapi.py
thostmduserapi.dll
_thostmduserapi.pyd
本demo实现登录成功后订阅两个合约行情的功能。完整的demo代码如下:
本人所编译的CTP0606版本的交易、行情
和上文traderapi一致。此版本是在原先版本上的升级,解决了onfrontconnected回调的宕机问题。感谢知乎网友@warlock 一起探讨。
2. 通过Swig得到python接口文件
新建文件
thostmduserapi.i,内容如下
%module(directors="1") thostmduserapi %{ #include "ThostFtdcMdApi.h" %} %feature("director") CThostFtdcMdSpi; %include "ThostFtdcUserApiDataType.h" %include "ThostFtdcUserApiStruct.h" %include "ThostFtdcMdApi.h" %inline %{ int SubscribeMarketDataByManual(CThostFtdcMdApi * arg1,char *arg2){ char *ppInstrumentID[1]; ppInstrumentID[0] = arg2; return (int)(arg1)->SubscribeMarketData(ppInstrumentID,1); } %}
这里主要的变化就在于我添加了一个订阅行情的函数,因为我实际操作过程中发现CTP订阅行情参数是二级指针,转化为python后我每次调用都会宕机。没办法我折中写了个参数为一级指针的函数包装了下CTP订阅行情函数,自己手写的函数每次只能订阅一个合约。
在
cmd中切换到当前文件夹下,运行命令
swig -threads -c++ -python thostmduserapi.i
等到运行完成后,可以看到当前目录下生成了
thostmduserapi_wrap.h
thostmduserapi_wrap.cxx
thostmduserapi.py
3. 通过C++得到python可调用的pyd动态库
与上文traderapi中一致。
4. Python Demo
新建文件mduserapi_demo.py,注意文件同目录底下要有如下三个文件:
thostmduserapi.py
thostmduserapi.dll
_thostmduserapi.pyd
本demo实现登录成功后订阅两个合约行情的功能。完整的demo代码如下:
# -*- coding: gbk -*- import thostmduserapi as mdapi class CFtdcMdSpi(mdapi.CThostFtdcMdSpi): tapi='' def __init__(self,tapi): mdapi.CThostFtdcMdSpi.__init__(self) self.tapi=tapi def OnFrontConnected(self): print "OnFrontConnected" loginfield = mdapi.CThostFtdcReqUserLoginField() loginfield.BrokerID="8000" loginfield.UserID="000005" loginfield.Password="123456" loginfield.UserProductInfo="python dll" self.tapi.ReqUserLogin(loginfield,0) def OnRspUserLogin(self, *args): print "OnRspUserLogin" rsploginfield=args[0] rspinfofield=args[1] print "SessionID=",rsploginfield.SessionID print "ErrorID=",rspinfofield.ErrorID print "ErrorMsg=",rspinfofield.ErrorMsg mdapi.SubscribeMarketDataByManual(self.tapi,"sc1412") mdapi.SubscribeMarketDataByManual(self.tapi,"sc1512") def OnRtnDepthMarketData(self, *args): print "OnRtnDepthMarketData" field=args[0] print "InstrumentID=",field.InstrumentID print "LastPrice=",field.LastPrice def main(): mduserapi=mdapi.CThostFtdcMdApi_CreateFtdcMdApi() mduserspi=CFtdcMdSpi(mduserapi) mduserapi.RegisterFront("tcp://172.19.125.39:39235") mduserapi.RegisterSpi(mduserspi) mduserapi.Init() mduserapi.Join() if __name__ == '__main__': main()
本人所编译的CTP0606版本的交易、行情
Python API,点击下载。声明:仅是个人爱好编译,对此API引起的你的任何损失不负责任。
相关文章推荐
- CTP Python API(利用Swig 封装)Windows版(traderapi)
- CTP JAVA API(JCTP)编译(利用Swig封装C++动态库)windows版
- 上期CTP 封装JAVA API window Swig
- Python量化交易平台开发教程系列2-类CTP交易API的Python封装设计
- 利用python3.4爬取学校教务系统,并封装成api供app调用(一)
- 如何利用Python2.7、py2exe、PyInstaller-2.1将*.py打包成*.exe,再利用Inno Setup Compiler封装成windows安装文件
- CTP JAVA_API(JCTP)编译(利用Swig封装C++动态库)linux版64位
- 利用python3.4爬取学校教务系统,并封装成api供app调用(二)之不带验证码模拟登录
- Windows 系统利用anaconda/Pycharm解决 python2 与 python3 共存问题
- Python_API_File and Directory Access_os.path.expanduser
- 使用Swig封装C++到Python的心得--01
- Revit中Dynamo编程——在Python中怎么利用RevitAPI来写一个过滤
- SWIG,把C/C++ API 导出成Java,C#,Python,Ruby,Php的类库
- API json多层数据实体类封装(利用泛型技术)
- 推荐一个封装的API库:Managed Windows API
- Linux下利用SWIG完成Python对使用OpenCV的C++文件的调用
- Windows环境下,在Eclipse下利用Pydev为Python配置开发环境
- 对MFC封装Windows通讯API的研究
- caffe 利用Python API 做数据输入层
- python利用wmi模块统计windows下硬盘信息