首页 > Python资料 博客日记

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

2024-08-04 03:00:05Python资料围观55

Python资料网推荐CTP-API开发系列之十:v6.7.0-Python版封装(Windows/Linux)(附源码)这篇文章给大家,欢迎收藏Python资料网享受知识的乐趣

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
swig -threads -py3 -c++ -python thostmduserapi.i

5.此时会新增以下文件:

6.新建两个vs动态库项目 _thosttraderapi 和 _thostmduserapi(项目最后会打包提供),每个项目添加如下文件,我本地安装的是Visual Studio 2022:

7.两个项目都需要将python安装路径的头文件和lib库文件路径添加上,我本地安装的是Anaconda3:

附加包含目录:D:\software\Anaconda3\include;
附加库目录:D:\software\Anaconda3\libs;
附加项(_thostmduserapi项目):python37.lib;thostmduserapi_se.lib;
附加项(_thosttraderapi项目):python37.lib;thosttraderapi_se.lib;

8.编译两个项目,会生成 _thosttraderapi.dll 和 _thostmduserapi.dll 两个动态库,分别复制一份,将.dll改成.pyd

9.以下6个文件,就是我们最终封装好的Python版CTP-API(最后会打包提供)

Linux 系统下封装

1.解压 v6.7.0_traderapi_20230209.zip 文件,进入 v6.7.0_20230209_api_traderapi_se_linux64 目录

2.新建 thosttraderapi.i 文件 和 thostmduserapi.i 文件(内容同Windows)
3.安装swig,并执行以下命令:

yum install swig
swig -threads -py3 -c++ -python thosttraderapi.i
swig -threads -py3 -c++ -python thostmduserapi.i

4.新建 make_traderapi 文件,内容如下(需要指定自己的python路径,我的是/usr/include/python3.6m):

OBJS=thosttraderapi_wrap.o
INCLUDE=-I./ -I/usr/include/python3.6m

TARGET=_thosttraderapi.so
CPPFLAG=-shared -fPIC
CC=g++
LDLIB=-L. -lthosttraderapi_se
$(TARGET) : $(OBJS)
	$(CC) $(CPPFLAG) $(INCLUDE) -o $(TARGET) $(OBJS) $(LDLIB)
$(OBJS) : %.o : %.cxx
	$(CC) -c -fPIC $(INCLUDE) $< -o $@
clean:
	-rm -f $(OBJS)
	-rm -f $(TARGET)

5.新建 make_mduserapi 文件,内容如下(需要指定自己的python路径,我的是/usr/include/python3.6m):

OBJS=thostmduserapi_wrap.o
INCLUDE=-I./ -I/usr/include/python3.6m

TARGET=_thostmduserapi.so
CPPFLAG=-shared -fPIC
CC=g++
LDLIB=-L. -lthostmduserapi_se
$(TARGET) : $(OBJS)
	$(CC) $(CPPFLAG) $(INCLUDE) -o $(TARGET) $(OBJS) $(LDLIB)
$(OBJS) : %.o : %.cxx
	$(CC) -c -fPIC $(INCLUDE) $< -o $@
clean:
	-rm -f $(OBJS)
	-rm -f $(TARGET)

6.执行两个make文件:

mv thosttraderapi_se.so libthosttraderapi_se.so
mv thostmduserapi_se.so libthostmduserapi_se.so
make -f make_traderapi 
make -f make_mduserapi 

7.以下6个文件(4个.so文件和两个.py文件),就是我们最终封装好的Python版CTP-API(最后会打包提供)

td_md_quant_demo.py

这是一个测试demo,代码在前面的章节已经分享过,这里就不粘贴了。
资源获取:期货CTP-API-v6.7.0-Python版封装(Windows/Linux)(交易+行情)


版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐