CTP-API开发系列之十:v6.7.0-Python版封装(Windows/Linux)(附源码)
在最开始的文章中,我们对v6.7.0版本进行了抓包分析,该版本及之后的版本使用了Lz4压缩算法,查询流量压缩效果显著,网络传输流量大大降低,所以推荐使用v6.7.0及之后的版本。
本次分享将采用Swig工具(Swig是一种软件开发工具,可将用 C 和 C++ 编写的程序与各种高级编程语言连接起来),将C++语言版本的 CTP-API 封装成 Python版本,Windows/Linux系统下分别进行封装。
最后附整个封装过程的工程项目文件,后续官网发布新版本可以自行进行封装,提供给不同需求的朋友使用。
资源获取
期货CTP-API-v6.7.0-Python版封装(Windows/Linux)(交易+行情)
准备工作
1.下载CTP-API v6.7.0版本文件
地址:http://www.sfit.com.cn/DocumentDown/api_3/5_2_2/v6.7.0_traderapi_20230209.zip
2.安装 Swig 工具
地址:https://www.swig.org/download.html
3.安装 Python 或者 Anaconda
Windows 系统下封装
1.解压 v6.7.0_traderapi_20230209.zip 文件(我这里是使用的x64版本)
cd v6.7.2_traderapi_20230913\v6.7.2_20230913_winApi\traderapi\20230913_traderapi64_se_windows
2.新建 thosttraderapi.i 文件,内容如下:
%module(directors="1") thosttraderapi
%{
#include "ThostFtdcTraderApi.h"
#include <codecvt>
#include <locale>
#include <vector>
#include <string>
using namespace std;
#ifdef \_MSC\_VER
const static locale g\_loc("zh-CN");
#else
const static locale g\_loc("zh\_CN.GB18030");
#endif
%}
%typemap(out) char[ANY], char[] {
const std::string &gb2312($1);
std::vector<wchar\_t> wstr(gb2312.size());
wchar\_t\* wstrEnd = nullptr;
const char\* gbEnd = nullptr;
mbstate\_t state = {};
int res = use_facet<codecvt<wchar\_t, char, mbstate\_t> >
(g_loc).in(state,
gb2312.data(), gb2312.data() + gb2312.size(), gbEnd,
wstr.data(), wstr.data() + wstr.size(), wstrEnd);
if (codecvt_base::ok == res)
{
wstring_convert<codecvt_utf8<wchar\_t>> cutf8;
std::string result = cutf8.to\_bytes(wstring(wstr.data(), wstrEnd));
resultobj = SWIG\_FromCharPtrAndSize(result.c\_str(), result.size());
}
else
{
std::string result;
resultobj = SWIG\_FromCharPtrAndSize(result.c\_str(), result.size());
}
}
%feature("director") CThostFtdcTraderSpi;
%ignore THOST_FTDC_VTC_BankBankToFuture;
%ignore THOST_FTDC_VTC_BankFutureToBank;
%ignore THOST_FTDC_VTC_FutureBankToFuture;
%ignore THOST_FTDC_VTC_FutureFutureToBank;
%ignore THOST_FTDC_FTC_BankLaunchBankToBroker;
%ignore THOST_FTDC_FTC_BrokerLaunchBankToBroker;
%ignore THOST_FTDC_FTC_BankLaunchBrokerToBank;
%ignore THOST_FTDC_FTC_BrokerLaunchBrokerToBank;
%feature("director") CThostFtdcTraderSpi;
%include "ThostFtdcUserApiDataType.h"
%include "ThostFtdcUserApiStruct.h"
%include "ThostFtdcTraderApi.h
3.新建 thostmduserapi.i 文件,内容如下:
%module(directors="1") thostmduserapi
%{
#include "ThostFtdcMdApi.h"
#include <codecvt>
#include <locale>
#include <vector>
#include <string>
using namespace std;
#ifdef \_MSC\_VER
const static locale g\_loc("zh-CN");
#else
const static locale g\_loc("zh\_CN.GB18030");
#endif
%}
%typemap(out) char[ANY], char[] {
const std::string &gb2312($1);
std::vector<wchar\_t> wstr(gb2312.size());
wchar\_t\* wstrEnd = nullptr;
const char\* gbEnd = nullptr;
mbstate\_t state = {};
int res = use_facet<codecvt<wchar\_t, char, mbstate\_t> >
(g_loc).in(state,
gb2312.data(), gb2312.data() + gb2312.size(), gbEnd,
wstr.data(), wstr.data() + wstr.size(), wstrEnd);
if (codecvt_base::ok == res)
{
wstring_convert<codecvt_utf8<wchar\_t>> cutf8;
std::string result = cutf8.to\_bytes(wstring(wstr.data(), wstrEnd));
resultobj = SWIG\_FromCharPtrAndSize(result.c\_str(), result.size());
}
else
{
std::string result;
resultobj = SWIG\_FromCharPtrAndSize(result.c\_str(), result.size());
}
}
%typemap(in) char \*[] {
/\* Check if is a list \*/
if (PyList\_Check($input)) {
int size = PyList\_Size($input);
int i = 0;
$1 = (char \*\*) malloc((size+1)\*sizeof(char \*));
for (i = 0; i < size; i++) {
PyObject \*o = PyList\_GetItem($input, i);
if (PyString\_Check(o)) {
$1[i] = PyString\_AsString(PyList\_GetItem($input, i));
} else {
free($1);
PyErr\_SetString(PyExc_TypeError, "list must contain strings");
SWIG_fail;
}
}
$1[i] = 0;
} else {
PyErr\_SetString(PyExc_TypeError, "not a list");
SWIG_fail;
}
}
// This cleans up the char \*\* array we malloc'd before the function call
%typemap(freearg) char \*\* {
free((char \*) $1);
}
%feature("director") CThostFtdcMdSpi;
%ignore THOST_FTDC_VTC_BankBankToFuture;
%ignore THOST_FTDC_VTC_BankFutureToBank;
%ignore THOST_FTDC_VTC_FutureBankToFuture;
%ignore THOST_FTDC_VTC_FutureFutureToBank;
%ignore THOST_FTDC_FTC_BankLaunchBankToBroker;
%ignore THOST_FTDC_FTC_BrokerLaunchBankToBroker;
%ignore THOST_FTDC_FTC_BankLaunchBrokerToBank;
%ignore THOST_FTDC_FTC_BrokerLaunchBrokerToBank;
%include "ThostFtdcUserApiDataType.h"
%include "ThostFtdcUserApiStruct.h"
%include "ThostFtdcMdApi.h"
4.cmd 进入该路径,分别运行以下命令:
swig -threads -py3 -c++ -python thosttraderapi.i
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
**因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/0cf9859a2765712d0d0b33d54d130dbc.png)
![img](https://img-blog.csdnimg.cn/img_convert/774c4b8845354e7155aa5bcd8863403a.png)
![](https://img-blog.csdnimg.cn/img_convert/46506ae54be168b93cf63939786134ca.png)
![](https://img-blog.csdnimg.cn/img_convert/252731a671c1fb70aad5355a2c5eeff0.png)
![](https://img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)
![](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Python开发知识点,真正体系化!**
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**
**如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注Python)**
![img](https://img-blog.csdnimg.cn/img_convert/bb0a84d7193b9f5fc5e66029b7e381a5.png)
**一、Python所有方向的学习路线**
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照下面的知识点去找对应的学习资源,保证自己学得较为全面。
![img](https://img-blog.csdnimg.cn/1d40facda2b84990b8e1743f5487d455.png)
![img](https://img-blog.csdnimg.cn/0fc11d4a31bd431dbf124f67f1749046.png)
**二、Python必备开发工具**
工具都帮大家整理好了,安装就可直接上手!![img](https://img-blog.csdnimg.cn/ff266f529c6a46c4bc28e5f895dec647.gif#pic_center)
**三、最新Python学习笔记**
当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。
![img](https://img-blog.csdnimg.cn/6d414e9f494742db8bcc3fa312200539.png)
**四、Python视频合集**
观看全面零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
![img](https://img-blog.csdnimg.cn/a806d9b941c645858c61d161aec43789.png)
**五、实战案例**
纸上得来终觉浅,要学会跟着视频一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。![img](https://img-blog.csdnimg.cn/a353983317b14d3c8856824a0d6186c1.png)
**六、面试宝典**
![在这里插入图片描述](https://img-blog.csdnimg.cn/97c454a3e5b4439b8600b50011cc8fe4.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/111f5462e7df433b981dc2430bb9ad39.png)
###### **简历模板**![在这里插入图片描述](https://img-blog.csdnimg.cn/646863996ac44da8af500c049bb72fbd.png#pic_center)
图片描述](https://img-blog.csdnimg.cn/97c454a3e5b4439b8600b50011cc8fe4.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/111f5462e7df433b981dc2430bb9ad39.png)
###### **简历模板**![在这里插入图片描述](https://img-blog.csdnimg.cn/646863996ac44da8af500c049bb72fbd.png#pic_center)