CTP-API开发系列之十:v6.7.0-Python版封装(Windows/Linux)

本文是CTP-API开发系列之十,推荐使用v6.7.0及之后版本,因其采用Lz4压缩算法,降低网络传输流量。采用Swig工具将C++版CTP-API封装成Python版,分别介绍了在Windows和Linux系统下的资源获取、准备工作及封装步骤。

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.
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值