CTPAPI的Python接口,使用Swig技术制作,支持pip install。
由 openctp 团队提供, 使用Swig制作的Python版CTPAPI。
简化对接CTPAPI的过程,节省精力,快速上手 :rocket:
openctp-ctp | win x86 | win x64 | linux x64 | mac x64 | mac arm64 |
---|---|---|---|---|---|
6.3.15.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: |
6.3.19.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: |
6.5.1.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: |
6.6.1.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: |
6.6.7.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
6.6.9.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
6.7.0.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
6.7.1.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: |
6.7.2.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
openctp-ctp-cp | win x86 | win x64 | linux x64 | mac x64 | mac arm64 |
---|---|---|---|---|---|
6.3.19.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: |
6.5.1.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: |
6.6.1.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: |
6.6.7.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: |
6.6.9.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: |
6.7.0.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: |
6.7.2.* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: |
📌 :x:是因为CTP官方没有提供相应平台的库。:heavy_multiplication_x:是openctp还未提供支持
openctp-ctp提供了两种安装使用方式: 通过pip安装、手动下载配置。 openctp-ctp-ctp 只提供了pip安装的方式
需要自行提前准备好 Python 环境。
选择一个版本,如 6.7.2
pip install openctp-ctp==6.7.2.* -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host=pypi.tuna.tsinghua.edu.cn
zsh
安装:
pip install openctp-ctp==6.7.2.\* -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host=pypi.tuna.tsinghua.edu.cn
引用方法:
from openctp_ctp import tdapi, mdapi
更多的接口使用方法参考 代码示例
以上流程, 将 openctp-ctp 更换为 openctp-ctp-ctp 就是评测版的安装使用方式
手动下载指定版本的动态库文件,并配置库路径。
Windows
因为 windows 下,不同的 python 版本编译的动态库之间不可共用,所以不同的 python 版本需要下载指定版本对应的动态库。
swig 转换时使用 C++ 内置方式进行 GBK 和 UTF8 的编码转换。
如: 6.6.9-x64, python 3.10
从目录 6.6.9_20220820/win64
和 6.6.9_20220820/win64/py310
下载库文件
将下载的文件放在本地同一个目录下
# 下载文件
_thosttraderapi.pyd
_thostmduserapi.pyd
thosttraderapi.py
thostmduserapi.py
thosttraderapi_se.dll
thostmduserapi_se.dll
Linux
选择一个版本,如: 6.7.2
从目录6.7.2_20230913/linux64
下载所有的文件
将下载的文件放在同一目录下
_thosttraderapi.so
_thostmduserapi.so
thosttraderapi.py
thostmduserapi.py
libthosttraderapi_se.so
libthostmduserapi_se.so
将文件所在路径加入到到库路径(<specify_path>填写当前路径)
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<specify_path>
MacOS(在路上...)
为了测试是否配置成功,可以使用测试文件 demo_td.py/md_demo.py,和上面的文件放在同一个目录下即可。
测试行情接口
注意选取有效的行情前置地址, 参考openctp监控Simnow
注意选取有效的合约, 可通过交易接口查询合约
python md_demo.py
测试交易接口
需要在 simnow官网 注册账号
注意选取有效的交易前置地址, 参考openctp监控Simnow
python demo_td.py tcp://180.168.146.187:10130 <userid> <password>
参考 md_demo.py/demo_td.py/tdapi.py
在高级编辑器或IDE中,可以方便的查看接口说明及各字段含义。如下(Pycharm)
. .Linux下安装后,需要安装中文字符集,否则导入时报错:
>>> import openctp_ctp
terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid
Aborted
或
>>> import openctp_ctp
Aborted
需要安装 GB18030
字符集,这里提供 ubuntu/debian/centos 的方案:
# Ubuntu (20.04)
sudo apt-get install -y locales
sudo locale-gen zh_CN.GB18030
# Debian (11)
sudo apt install locales-all
sudo localedef -c -f GB18030 -i zh_CN zh_CN.GB18030
# CentOS (7)
sudo yum install -y kde-l10n-Chinese
sudo yum reinstall -y glibc-common
使用 openctp-ctp 进行实盘交易的后果完全由使用者自己承担!!!