CTP Python API(利用Swig 封装)Windows版(mduserapi)
来源:互联网 发布:淘宝助手客服电话 编辑:程序博客网 时间:2024/06/07 02:12
1. 准备工作
和上文traderapi一致。
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 -c++ -python thostmduserapi.i
等到运行完成后,可以看到当前目录下生成了
thostmduserapi_wrap.h
thostmduserapi_wrap.cxx
thostmduserapi.py
3. 通过C++得到python可调用的pyd动态库
与上文traderapi中一致,切记注释SwigDirector_CThostFtdcMdSpi::OnFrontConnected()
中那段,否则回调会宕。
4. Python Demo
新建文件mduserapi_demo.py,注意文件同目录底下要有如下三个文件:
thostmduserapi.py
thostmduserapi.dll
_thostmduserapi.pyd
本demo实现登录成功后订阅两个合约行情的功能。完整的demo代码如下:
# -*- coding: gbk -*-import timeimport thostmduserapi as mdapi class CFtdcMdSpi(mdapi.CThostFtdcMdSpi): tapi='' def __init__(self,tapi): mdapi.CThostFtdcMdSpi.__init__(self) self.tapi=tapi 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.LastPricedef main(): mduserapi=mdapi.CThostFtdcMdApi_CreateFtdcMdApi() mduserspi=CFtdcMdSpi(mduserapi) loginfield = mdapi.CThostFtdcReqUserLoginField() loginfield.BrokerID="8000" loginfield.UserID="000005" loginfield.Password="123456" loginfield.UserProductInfo="python dll" mduserapi.RegisterFront("tcp://172.19.125.39:39235") mduserapi.RegisterSpi(mduserspi) mduserapi.Init() time.sleep(1) mduserapi.ReqUserLogin(loginfield,0) mduserapi.Join()if __name__ == '__main__': main()
本人所编译的CTP0606版本的交易、行情Python API
,点击下载。声明:仅是个人爱好编译,对此API引起的你的任何损失不负责任。
阅读全文
0 0
- CTP Python API(利用Swig 封装)Windows版(mduserapi)
- CTP Python API(利用Swig 封装)Windows版(traderapi)
- CTP JAVA API(JCTP)编译(利用Swig封装C++动态库)windows版
- CTP JAVA_API(JCTP)编译(利用Swig封装C++动态库)linux版64位
- SWIG and Python(用SWIG将C/C++封装为Python扩展模块)
- Python量化交易平台开发教程系列2-类CTP交易API的Python封装设计
- PB12预览版(CTP)
- Python调用C/C++(使用SWIG)
- swig封装c++至python可调用
- 使用Swig封装的python对象
- win7 swig c++封装 python 扩展
- Python编程.利用ctypes调用Windows API
- 封装(python 版)
- 【语言桥接】(windows)swig-C#封装 C++教程
- swig-python
- swig python
- swig-python
- swig python
- 确定两串乱序同构
- HDU6044 Limited Permutation[快速输入][分治]
- 利用数组显示杨辉三角
- 【国家集训队2011】数颜色 (莫队)
- 矩阵转换
- CTP Python API(利用Swig 封装)Windows版(mduserapi)
- JDK、Eclipse、Myeclipse、Tomcat等各种软件的版本详解(写给对版本和兼容性问题焦头烂额的你)
- 第一次Filter过滤器的简单使用总结(二)
- 深度学习系列教程
- java中的日期实用类的一些举例
- 冒泡排序--java
- Java实现计算当前月天数
- Java_15 集合类接口的常用方法
- window.onload 和$(document).ready的区别