openctp-ctp库是由 openctp 使用Swig技术制作的Python版CTPAPI。
简化了对接CTPAPI的过程,节省精力,快速上手 🚀
| openctp-ctp | win x86 | win x64 | linux x64 | mac x64 | mac arm64 | 对应CTPAPI |
|---|---|---|---|---|---|---|
| 6.3.15.* | ✔️ | ✔️ | ✔️ | 6.3.15_P1 | ||
| 6.3.19.* | ✔️ | ✔️ | ✔️ | 6.3.19 | ||
| 6.5.1.* | ✔️ | ✔️ | ✔️ | 6.5.1 | ||
| 6.6.1.* | ✔️ | ✔️ | ✔️ | 6.6.1_P1 | ||
| 6.6.7.* | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | 6.6.7 |
| 6.6.9.* | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | 6.6.9 |
| 6.7.0.* | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | 6.7.0 |
| 6.7.1.* | ✔️ | ✔️ | ✔️ | 6.7.1 | ||
| 6.7.2.* | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | 6.7.2 |
| 6.7.7.* | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | 6.7.7 |
| 6.7.8.* | ✔️ | ✔️ | ✔️ | 6.7.8 | ||
| 6.7.9.* | ✔️ | ✔️ | ✔️ | 6.7.9 | ||
| 6.7.10.* | ✔️ | ✔️ | ✔️ | 6.7.10 | ||
| 6.7.11.* | ✔️ | ✔️ | ✔️ | 6.7.11 |
需要自行提前准备好 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.cnzsh安装:
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-cp 就是评测版的安装使用方式
-
交易接口demo:td_demo.py
-
行情接口demo:md_demo.py
更多示例参见 https://github.com/Jedore/ctp.examples
在高级编辑器或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 # CentOS (8/9) sudo yum install -y langpacks-zh_CN.noarch
-
Mac下报错
Fatal Python error: config_get_locale_encoding: failed to get the locale encoding: nl_langinfo(CODESET) failed Python runtime state: preinitialized设置
export LANG="en_US.UTF-8"并使之生效
-
openctp-ctp库默认只支持CTP柜台,如需连接TTS、XTP、TORA等柜台,可以使用openctp的CTPAPI兼容接口方式,将CTP的dll(如thosttraderapi_se.dll)替换为相应柜台的版本即可,具体见openctp
-
CTPAPI的Python版开发技术: swig转换CTPAPI为Python攻略。
用于实盘前请充分测试相应的功能,openctp不对此承担任何责任。


