Name | homalos-ctp JSON |
Version |
6.7.11.4
JSON |
| download |
home_page | None |
Summary | CTP Python API extension module construction. |
upload_time | 2025-09-05 14:47:05 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.13 |
license | MIT License
Copyright (c) 2025 Homalos
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. |
keywords |
ctp
investment
quantitative
trading
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
<p align="center">
English |
<a href="https://github.com/Homalos/ctp/blob/main/README_CN.md">简体中文</a>
</p>
# Project Description
This project automatically generates Python APIs based on the CTP C++ API, making it easier for CTP Python developers to maintain the latest CTP interfaces and quickly upgrade CTP versions.
Note: This project has only been tested under CTP v6.7.11. Other versions have not been tested. The project CTP version number is configured in the `ctp/__init__.py` file.
## 1. Compilation Environment
This project is compiled using the following environment. If you use other tool versions, please make appropriate adjustments.
- **Windows 11 + MSVC 2022**
- **Python 3.13.6** virtual environment, installed by UV.
- **CTP v6.7.11**: [CTP official download link](https://www.simnow.com.cn/static/apiDownload.action)
- **Meson + Ninja**: A modern C++ extension build system.
- **Pybind11**: Python C++ bindings
- **UV**: A modern Python package manager with faster installation and smarter dependency resolution.
## 2. Project Structure
```reStructuredText
ctp/
├── 📂 ctp/ # CTP interface module
│ ├── 📂 api/ # CTP API module
│ │ ├── 📂 generator/ # C++ and Python binding generation script
│ │ ├── 📂 include/ # CTP API header files
│ │ ├── 📂 libs/ # CTP API static library files
│ │ ├── 📂 src/ # CTP and Python binding code files
│ │ ├── 📁 __init__.py # MdApi and TdApi initialization imports
│ │ ├── 📁 ctp_constant.py # CTP API Constants
│ │ ├── 📁 ctpmd.cp313-win_amd64.pyd # Market extension module compiled from C++ to Python
│ │ ├── 📁 ctpmd.pyi # Stub file for the market extension module
│ │ ├── 📁 ctptd.cp313-win_amd64.pyd # Trading extension module compiled from C++ to Python
│ │ ├── 📁 ctptd.pyi # Stub file for the trading extension module
│ │ ├── 📁 custom_constant.py # User-defined constant class
│ │ ├── 📁 thostmduserapi_se.dll # Windows CTP market API dynamic link library
│ │ ├── 📁 thostmduserapi_se.so # Linux CTP market API dynamic link library
│ │ ├── 📁 thosttraderapi_se.dll # Windows CTP trading API dynamic link library
│ │ ├── 📁 thosttraderapi_se.so # Linux CTP trading API dynamic link library
│ ├── 📁 __init__.py # CTP version configuration file
│ ├── 📁 __version__.py # Project version configuration file
│ ├── 📁 ctp.h # Task processing and encoding conversion
├── 📂 docs/ # Project documentation
├── 📁 .gitignore # Git commit ignore files, automatically generated by UV
├── 📁 .python-version # Project Python version file, automatically generated by UV
├── 📁 LICENSE # Project license file
├── 📁 README.md # Project description file in Chinese
├── 📁 README_CN.md # Project description file in English
├── 📁 build.py # Extension module automated compilation script, assembling Meson commands
├── 📁 hatch_build.py # Hatch hook, set the platform identifier when packaging with hatch
├── 📁 md_demo.py # Example of using the market extension module
├── 📁 meson.build # Meson build configuration file
├── 📁 pyproject.toml # Python project management configuration file, automatically generated by uv
├── 📁 td_demo.py # Example of using the transaction extension module
├── 📁 util.py # Public Tools
└── 📁 uv.lock # uv lock file, automatically generated by uv
```
## 3. Install the Basic Environment (skip if already installed)
1. Install UV
On Windows
**Method 1: Global Installation (Recommended, choose one)**
```bash
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
```
**Method 2: Install in a Single Python Environment (Choose one)**
```bash
pip install uv
```
On Linux
```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```
2. Install Python (Perform this step with Method 1, skip this with Method 2). I use 3.13.6; you can install your preferred version.
```bash
uv python install 3.13
```
## 4. Usage
Download the Release source code of this project or install it using the command:
```bash
pip install homalos-ctp
```
1. Install a Python virtual environment and dependencies (execute from the root directory)
```bash
# Use uv to create a Python virtual environment with a specified version in the current project.
uv venv --python 3.13 .venv
```
```bash
# Install dependent libraries
uv add meson-python
uv add pybind11
uv add pybind11-stubgen
```
2. Execute the one-click build script in the `generator` directory (to generate Python bindings for the CTP C++ API)
```bash
# Activate the Python virtual environment and enter the generator.
.venv\Scripts\activate
cd homalos-ctp\api\generator
```
```bash
# Generate binding files with one click.
python generate_onekey.py
```
3. Execute the following build script in the root directory to generate the CTP C++ API and encapsulate it into a Python-callable interface.
```bash
# Compile the CTP Python API with one click.
python build.py
```
## 5. Demo Test
1. **Testing the Market Extension Module**
In the project root directory, fill in the CTP environment information in `md_demo.py` and run it. The results are as follows:
```reStructuredText
Start connecting to CTP market server...
CtpMdApi:Trying to create an API with path D:\Project\PycharmProjects\homalos-ctp\con/md
CtpMdApi:createFtdcMdApi call succeeded.
CtpMdApi:Try initializing the API using the address:tcp://182.254.243.31:40011...
CtpMdApi:init call succeeded.
Connecting to tcp://182.254.243.31:40011...
Waiting for connection and login to complete...
ctp md api callback: onFrontConnected - The market data server is connected successfully
Start the login process
CtpMdApi:reqUserLogin call succeeded.
ctp md api callback: onRspUserLogin - The market server login is successful
Starting to subscribe to 2 contracts...
Subscription contract: SA601
Prepare subscription contract: SA601
Send subscription request SA601
Subscription request sent SA601
ctp md api callback: onRspSubMarketData - Subscription feedback, Contract=SA601, ErrorID=0
symbol: SA601
ctp md api callback: onRtnDepthMarketData
CTP Market data reception: SA601 @ 17:00:34 LastPrice=1276.0
Subscription contract: FG601
Prepare subscription contract: FG601
Send subscription request FG601
Subscription request sent FG601
ctp md api callback: onRtnDepthMarketData
CTP Market data reception: SA601 @ 17:00:35 LastPrice=1276.0
...
```
2. **Testing the transaction extension module**
Fill in the CTP environment information in `td_demo.py` in the project root directory and run it. The result is as follows:
```reStructuredText
CtpTdApi: Attempting to create an API with path D:\Project\PycharmProjects\homalos-ctp\con/td
CtpTdApi: createFtdcTraderApi call succeeded.
CtpTdApi:尝试使用地址初始化 API:tcp://182.254.243.31:30001...
CtpTdApi:init 调用成功。
Waiting for connection and login to complete...
ctp td api callback: onFrontConnected - Trading server connection successful
开始认证,auth_status: False
发送认证请求,req_id: 1
Transaction server authorization verification successful
开始登录,login_status: False
发送登录请求,req_id: 2
ctp td api callback: onRspUserLogin - Login Response, ErrorID=0
Trading server login successful
Settlement information confirmed successfully
🚀 开始下单测试...
正在委托下单...
symbol: SA601
direction: BUY_OPEN
price: 1286
volume: 1
委托请求发送成功
委托下单成功,委托号:1_-394894342_1
下单完成,订单号: 1_-394894342_1
⏰ 等待5秒观察订单状态...
ctp td api callback: onRtnOrder
订单状态更新 - 订单 ID:1_-394894342_1,状态:未知 (a)
状态变化: 新订单 -> 未知
ctp td api callback: onRtnOrder
订单状态更新 - 订单 ID:1_-394894342_1,状态:未知 (a)
状态变化: a -> 未知
ctp td api callback: onRtnOrder
订单状态更新 - 订单 ID:1_-394894342_1,状态:全部成交 (0)
订单全部成交 - 订单号: 1_-394894342_1, 合约: SA601
状态变化: a -> 全部成交
ctp td api callback: onRtnTrade
onRtnTrade trade_id: 2025090800029227, order_id: 48977, price: 1286.0, volume: 1, trade_date: 20250905, trade_time: 21:41:12
==================================================
📋 订单状态汇总
==================================================
订单号: 1_-394894342_1 | 状态: 全部成交
==================================================
...
```
## 6. Script Function Details
The generator script is located in `ctp/api/generator/`
1. `generator_function_const.py`
- **Purpose**: **Generates basic function constant files**
- **Function**:
- Reads the CTP header files `ThostFtdcMdApi.h` and `ThostFtdcTraderApi.h.h`
- Parses the functions therein and generates `ctp_function_const.py` (function constant definitions)
2. `generate_data_type.py`
- **Purpose**: **Generates data type definition files**
- **Function**:
- Reads the CTP header file `ThostFtdcUserApiDataType.h`
- Parses the `#define` constant definitions and `typedef` type definitions therein
- Generates `ctp_function_const.py`
3. `generate_struct.py`
- **Purpose**: **Generates structure definition files**
- **Function**:
- Reads the CTP header file `ThostFtdcUserApiStruct.h`
- Relies on the type mappings in `ctp_typedef.py`
- Parses the C++ structure definition and generates the Python dictionary-formatted structure definition file `ctp_struct.py`
4. `generate_api_functions.py`
- **Purpose**: **Generates API function binding code**
- **Function**:
- Reads the CTP API header files (such as `ThostFtdcTraderApi.h` and `ThostFtdcMdApi.h`)
- Relies on the structure definitions in `ctp_struct.py`
- Generates a large number of C++ source code files for Python bindings
5. `generate_dll_entry.py`
- **Purpose**: **Generates the C++ DLL entry point code file**
- **Function**:
- Generates three files: `dllmain.cpp`, `stdafx.cpp`, and `stdafx.h`.
- **dllmain.cpp**: Contains the standard DLL entry point function, handling process and thread loading/unloading.
- **stdafx.cpp**: A simple precompiled header include file.
- **stdafx.h**: Contains the Windows API header files and common definitions.
6. `generate_cpp.py`
- **Purpose**: **Generates `cpp` and `h` files**
- **Function**: **Generates `ctpmd.cpp`, `ctpmd.h`, and `ctptd.cpp`, `ctptd.h`, for `ctp.api.src.ctpmd` and `ctp.api.src.ctptd`, respectively.
- The header file contains complete class declarations and function prototypes.
- The `cpp` file contains all implementation and bindings.
7. `generate_onekey.py`
- **Purpose**: **One-click assembles all md and td header, source, and other files to generate cpp and h files**
- **Function**:
- One-click assembles the files generated by the above files, as well as header, source, and other files, to generate four files: `ctpmd.cpp`, `ctpmd.h`, and `ctptd.cpp`, `ctptd.h`.
8. `build.py`
- **Purpose**: **One-click compiles the CTP C++ API into a Python API**
- **Function**:
- One-click compiles the Python-callable CTP API files, located in `ctp/api/`. These files include:
- `ctpmd.cp313-win_amd64.pyd`
- `ctptd.cp313-win_amd64.pyd`
- `ctpmd.pyi`
- `ctptd.pyi`
File Dependencies:
1. **`generator_function_const.py`** → Generate `ctp_function_const.py`
2. **`generate_data_type.py`** → Generate `ctp_typedef.py` and `ctp_constant.py`
3. **`generate_struct.py`** (depends on `ctp_typedef.py`) → Generate `ctp_struct.py`
4. **`generate_api_functions.py`** (depends on `ctp_struct.py` and `ctp_function_const.py`) → Generate multiple API header and source binding files for `md` and `td`
5. **`generate_dll_entry.py`** → Generate `dllmain.cpp`, `stdafx.cpp`, and `stdafx.h`
6. **`generate_cpp.py`** (depends on all the above files, as well as the generated header and source files) → Generates `ctpmd.cpp`, `ctpmd.h`, and `ctptd.cpp` and `ctptd.h`
7. **`generate_onekey.py`** → Assembles `ctpmd.cpp`, `ctpmd.h`, and `ctptd.cpp` and `ctptd.h` files with one click (equivalent to executing the above process with one click)
8. **`build.py`** (depends on the `ctpmd` and `ctptd` modules in `ctp/api/src/`) → Compiles `ctpmd.cp313-win_amd64.pyd`, `ctptd.cp313-win_amd64.pyd`, `ctpmd.pyi`, and `ctptd.pyi` with one click
## 7. Script Usage
The code generated by these scripts is used to:
- Encapsulate the CTP C++ API into a Python-callable interface
- Automatically handle data type conversion
- Generate Python bindings for callback functions
- Generate Python bindings for request functions
## 8. Advantages
- Use pybind to bind C++ to the Python CTP API, offering superior performance compared to SWIG conversion.
- Automatic synchronization: When the CTP official header files are updated, the latest h, dll, so, and lib files are replaced. After executing the generated script, the script will automatically reflect the latest virtual functions.
- Easy maintenance: No need to manually update a large number of hard-coded function declarations.
- Reduced errors: Avoid omissions or errors that may result from manual maintenance.
- Improved efficiency: Developers only need to focus on business logic, without worrying about changes to the underlying interfaces.
Summary: This is a complete code generation toolchain that automatically generates Python bindings for the CTP API, eliminating the need to manually write repetitive binding code and improving maintainability and robustness.
## 9. Community Support
- **Technical Exchange (QQ Group)**: `446042777`
- [pypi.org](https://pypi.org/project/homalos-ctp)
## 10. Disclaimer
**[Disclaimer Content](docs/Disclaimer.md)**
## 11. Replenish
**Meson**: Similar to Make and CMake, its main task is to configure the compilation environment, generate compilation instructions (for example, for Ninja), and manage the entire compilation process. It does not directly compile code, but rather drives tools like Ninja to do so.
**Pybind11**: A lightweight C++ library for exposing (binding) C++ code to the Python interpreter. It allows Python code to seamlessly call C++ functions and classes, just like calling regular Python modules. Its core goal is to provide an extremely simple, nearly boilerplate-free interface that easily combines the high-performance computing capabilities of C++ with the ease of use and vast Python ecosystem.
---
*homalos-ctp*
*Last Updated: 2025-09-05*
Raw data
{
"_id": null,
"home_page": null,
"name": "homalos-ctp",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.13",
"maintainer_email": null,
"keywords": "ctp, investment, quantitative, trading",
"author": null,
"author_email": "Lumosylva <donnymoving@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/cf/ef/1f78612c3be1ad58c10a95a13f4ede61fd7c5aa018cd05c76c556ba96d35/homalos_ctp-6.7.11.4.tar.gz",
"platform": null,
"description": "<p align=\"center\">\n English |\n <a href=\"https://github.com/Homalos/ctp/blob/main/README_CN.md\">\u7b80\u4f53\u4e2d\u6587</a>\n</p>\n\n# Project Description\n\nThis project automatically generates Python APIs based on the CTP C++ API, making it easier for CTP Python developers to maintain the latest CTP interfaces and quickly upgrade CTP versions.\n\nNote: This project has only been tested under CTP v6.7.11. Other versions have not been tested. The project CTP version number is configured in the `ctp/__init__.py` file.\n\n## 1. Compilation Environment\n\nThis project is compiled using the following environment. If you use other tool versions, please make appropriate adjustments.\n\n- **Windows 11 + MSVC 2022**\n\n- **Python 3.13.6** virtual environment, installed by UV.\n\n- **CTP v6.7.11**: [CTP official download link](https://www.simnow.com.cn/static/apiDownload.action)\n\n- **Meson + Ninja**: A modern C++ extension build system.\n\n- **Pybind11**: Python C++ bindings\n\n- **UV**: A modern Python package manager with faster installation and smarter dependency resolution.\n\n## 2. Project Structure\n\n```reStructuredText\nctp/\n\u251c\u2500\u2500 \ud83d\udcc2 ctp/\t\t# CTP interface module\n\u2502 \u251c\u2500\u2500 \ud83d\udcc2 api/\t\t# CTP API module\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc2 generator/\t\t# C++ and Python binding generation script\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc2 include/\t\t# CTP API header files\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc2 libs/\t\t# CTP API static library files\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc2 src/\t\t# CTP and Python binding code files\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc1 __init__.py\t\t# MdApi and TdApi initialization imports\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc1 ctp_constant.py \t\t# CTP API Constants\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc1 ctpmd.cp313-win_amd64.pyd\t\t# Market extension module compiled from C++ to Python\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc1 ctpmd.pyi\t\t# Stub file for the market extension module\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc1 ctptd.cp313-win_amd64.pyd\t\t# Trading extension module compiled from C++ to Python\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc1 ctptd.pyi\t\t# Stub file for the trading extension module\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc1 custom_constant.py \t\t\t# User-defined constant class\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc1 thostmduserapi_se.dll\t\t# Windows CTP market API dynamic link library\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc1 thostmduserapi_se.so\t\t# Linux CTP market API dynamic link library\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc1 thosttraderapi_se.dll\t\t# Windows CTP trading API dynamic link library\n\u2502 \u2502 \u251c\u2500\u2500 \ud83d\udcc1 thosttraderapi_se.so\t\t# Linux CTP trading API dynamic link library\n\u2502 \u251c\u2500\u2500 \ud83d\udcc1 __init__.py\t\t# CTP version configuration file\n\u2502 \u251c\u2500\u2500 \ud83d\udcc1 __version__.py\t\t# Project version configuration file\n\u2502 \u251c\u2500\u2500 \ud83d\udcc1 ctp.h\t\t# Task processing and encoding conversion\n\u251c\u2500\u2500 \ud83d\udcc2 docs/\t\t# Project documentation\n\u251c\u2500\u2500 \ud83d\udcc1 .gitignore\t\t# Git commit ignore files, automatically generated by UV\n\u251c\u2500\u2500 \ud83d\udcc1 .python-version\t# Project Python version file, automatically generated by UV\n\u251c\u2500\u2500 \ud83d\udcc1 LICENSE\t\t# Project license file\n\u251c\u2500\u2500 \ud83d\udcc1 README.md\t\t# Project description file in Chinese\n\u251c\u2500\u2500 \ud83d\udcc1 README_CN.md\t\t# Project description file in English\n\u251c\u2500\u2500 \ud83d\udcc1 build.py\t\t# Extension module automated compilation script, assembling Meson commands\n\u251c\u2500\u2500 \ud83d\udcc1 hatch_build.py\t\t# Hatch hook, set the platform identifier when packaging with hatch\n\u251c\u2500\u2500 \ud83d\udcc1 md_demo.py\t\t# Example of using the market extension module\n\u251c\u2500\u2500 \ud83d\udcc1 meson.build\t\t# Meson build configuration file\n\u251c\u2500\u2500 \ud83d\udcc1 pyproject.toml\t\t# Python project management configuration file, automatically generated by uv\n\u251c\u2500\u2500 \ud83d\udcc1 td_demo.py\t\t# Example of using the transaction extension module\n\u251c\u2500\u2500 \ud83d\udcc1 util.py\t\t# Public Tools\n\u2514\u2500\u2500 \ud83d\udcc1 uv.lock\t\t# uv lock file, automatically generated by uv\n```\n\n## 3. Install the Basic Environment (skip if already installed)\n\n1. Install UV\n\nOn Windows\n\n**Method 1: Global Installation (Recommended, choose one)**\n\n```bash\npowershell -ExecutionPolicy ByPass -c \"irm https://astral.sh/uv/install.ps1 | iex\"\n```\n\n**Method 2: Install in a Single Python Environment (Choose one)**\n\n```bash\npip install uv\n```\n\nOn Linux\n\n```bash\ncurl -LsSf https://astral.sh/uv/install.sh | sh\n```\n\n2. Install Python (Perform this step with Method 1, skip this with Method 2). I use 3.13.6; you can install your preferred version.\n\n```bash\nuv python install 3.13\n```\n\n## 4. Usage\n\nDownload the Release source code of this project or install it using the command:\n\n```bash\npip install homalos-ctp\n```\n\n1. Install a Python virtual environment and dependencies (execute from the root directory)\n\n```bash\n# Use uv to create a Python virtual environment with a specified version in the current project.\n\nuv venv --python 3.13 .venv\n\n```\n\n```bash\n# Install dependent libraries\n\nuv add meson-python\n\nuv add pybind11\n\nuv add pybind11-stubgen\n\n```\n\n2. Execute the one-click build script in the `generator` directory (to generate Python bindings for the CTP C++ API)\n\n```bash\n# Activate the Python virtual environment and enter the generator.\n\n.venv\\Scripts\\activate\n\ncd homalos-ctp\\api\\generator\n```\n\n```bash\n# Generate binding files with one click.\n\npython generate_onekey.py\n```\n\n3. Execute the following build script in the root directory to generate the CTP C++ API and encapsulate it into a Python-callable interface.\n\n```bash\n# Compile the CTP Python API with one click.\n\npython build.py\n```\n\n## 5. Demo Test\n\n1. **Testing the Market Extension Module**\n\nIn the project root directory, fill in the CTP environment information in `md_demo.py` and run it. The results are as follows:\n\n```reStructuredText\nStart connecting to CTP market server...\nCtpMdApi\uff1aTrying to create an API with path D:\\Project\\PycharmProjects\\homalos-ctp\\con/md\nCtpMdApi\uff1acreateFtdcMdApi call succeeded.\nCtpMdApi\uff1aTry initializing the API using the address:tcp://182.254.243.31:40011...\nCtpMdApi\uff1ainit call succeeded.\nConnecting to tcp://182.254.243.31:40011...\nWaiting for connection and login to complete...\nctp md api callback: onFrontConnected - The market data server is connected successfully\nStart the login process\nCtpMdApi\uff1areqUserLogin call succeeded.\nctp md api callback: onRspUserLogin - The market server login is successful\nStarting to subscribe to 2 contracts...\nSubscription contract: SA601\nPrepare subscription contract: SA601\nSend subscription request SA601\nSubscription request sent SA601\nctp md api callback: onRspSubMarketData - Subscription feedback, Contract=SA601, ErrorID=0\nsymbol: SA601\nctp md api callback: onRtnDepthMarketData\nCTP Market data reception: SA601 @ 17:00:34 LastPrice=1276.0\nSubscription contract: FG601\nPrepare subscription contract: FG601\nSend subscription request FG601\nSubscription request sent FG601\nctp md api callback: onRtnDepthMarketData\nCTP Market data reception: SA601 @ 17:00:35 LastPrice=1276.0\n...\n```\n\n2. **Testing the transaction extension module**\n\nFill in the CTP environment information in `td_demo.py` in the project root directory and run it. The result is as follows:\n\n```reStructuredText\nCtpTdApi: Attempting to create an API with path D:\\Project\\PycharmProjects\\homalos-ctp\\con/td\nCtpTdApi: createFtdcTraderApi call succeeded.\nCtpTdApi\uff1a\u5c1d\u8bd5\u4f7f\u7528\u5730\u5740\u521d\u59cb\u5316 API\uff1atcp://182.254.243.31:30001...\nCtpTdApi\uff1ainit \u8c03\u7528\u6210\u529f\u3002\nWaiting for connection and login to complete...\nctp td api callback: onFrontConnected - Trading server connection successful\n\u5f00\u59cb\u8ba4\u8bc1\uff0cauth_status: False\n\u53d1\u9001\u8ba4\u8bc1\u8bf7\u6c42\uff0creq_id: 1\nTransaction server authorization verification successful\n\u5f00\u59cb\u767b\u5f55\uff0clogin_status: False\n\u53d1\u9001\u767b\u5f55\u8bf7\u6c42\uff0creq_id: 2\nctp td api callback: onRspUserLogin - Login Response, ErrorID=0\nTrading server login successful\nSettlement information confirmed successfully\n\n\ud83d\ude80 \u5f00\u59cb\u4e0b\u5355\u6d4b\u8bd5...\n\u6b63\u5728\u59d4\u6258\u4e0b\u5355...\nsymbol: SA601\ndirection: BUY_OPEN\nprice: 1286\nvolume: 1\n\u59d4\u6258\u8bf7\u6c42\u53d1\u9001\u6210\u529f\n\u59d4\u6258\u4e0b\u5355\u6210\u529f\uff0c\u59d4\u6258\u53f7\uff1a1_-394894342_1\n\u4e0b\u5355\u5b8c\u6210\uff0c\u8ba2\u5355\u53f7: 1_-394894342_1\n\n\u23f0 \u7b49\u5f855\u79d2\u89c2\u5bdf\u8ba2\u5355\u72b6\u6001...\nctp td api callback: onRtnOrder\n\u8ba2\u5355\u72b6\u6001\u66f4\u65b0 - \u8ba2\u5355 ID\uff1a1_-394894342_1\uff0c\u72b6\u6001\uff1a\u672a\u77e5 (a)\n\u72b6\u6001\u53d8\u5316: \u65b0\u8ba2\u5355 -> \u672a\u77e5\nctp td api callback: onRtnOrder\n\u8ba2\u5355\u72b6\u6001\u66f4\u65b0 - \u8ba2\u5355 ID\uff1a1_-394894342_1\uff0c\u72b6\u6001\uff1a\u672a\u77e5 (a)\n\u72b6\u6001\u53d8\u5316: a -> \u672a\u77e5\nctp td api callback: onRtnOrder\n\u8ba2\u5355\u72b6\u6001\u66f4\u65b0 - \u8ba2\u5355 ID\uff1a1_-394894342_1\uff0c\u72b6\u6001\uff1a\u5168\u90e8\u6210\u4ea4 (0)\n\u8ba2\u5355\u5168\u90e8\u6210\u4ea4 - \u8ba2\u5355\u53f7: 1_-394894342_1, \u5408\u7ea6: SA601\n\u72b6\u6001\u53d8\u5316: a -> \u5168\u90e8\u6210\u4ea4\nctp td api callback: onRtnTrade\nonRtnTrade trade_id: 2025090800029227, order_id: 48977, price: 1286.0, volume: 1, trade_date: 20250905, trade_time: 21:41:12\n\n==================================================\n\ud83d\udccb \u8ba2\u5355\u72b6\u6001\u6c47\u603b\n==================================================\n\u8ba2\u5355\u53f7: 1_-394894342_1 | \u72b6\u6001: \u5168\u90e8\u6210\u4ea4\n==================================================\n...\n```\n\n## 6. Script Function Details\n\nThe generator script is located in `ctp/api/generator/`\n\n1. `generator_function_const.py`\n\n- **Purpose**: **Generates basic function constant files**\n- **Function**:\n- Reads the CTP header files `ThostFtdcMdApi.h` and `ThostFtdcTraderApi.h.h`\n- Parses the functions therein and generates `ctp_function_const.py` (function constant definitions)\n\n2. `generate_data_type.py`\n\n- **Purpose**: **Generates data type definition files**\n- **Function**:\n- Reads the CTP header file `ThostFtdcUserApiDataType.h`\n- Parses the `#define` constant definitions and `typedef` type definitions therein\n- Generates `ctp_function_const.py`\n\n3. `generate_struct.py`\n\n- **Purpose**: **Generates structure definition files**\n- **Function**:\n- Reads the CTP header file `ThostFtdcUserApiStruct.h`\n- Relies on the type mappings in `ctp_typedef.py`\n- Parses the C++ structure definition and generates the Python dictionary-formatted structure definition file `ctp_struct.py`\n\n4. `generate_api_functions.py`\n\n- **Purpose**: **Generates API function binding code**\n- **Function**:\n- Reads the CTP API header files (such as `ThostFtdcTraderApi.h` and `ThostFtdcMdApi.h`)\n- Relies on the structure definitions in `ctp_struct.py`\n- Generates a large number of C++ source code files for Python bindings\n\n5. `generate_dll_entry.py`\n\n- **Purpose**: **Generates the C++ DLL entry point code file**\n- **Function**:\n- Generates three files: `dllmain.cpp`, `stdafx.cpp`, and `stdafx.h`.\n\n- **dllmain.cpp**: Contains the standard DLL entry point function, handling process and thread loading/unloading.\n- **stdafx.cpp**: A simple precompiled header include file.\n- **stdafx.h**: Contains the Windows API header files and common definitions.\n\n6. `generate_cpp.py`\n\n- **Purpose**: **Generates `cpp` and `h` files**\n- **Function**: **Generates `ctpmd.cpp`, `ctpmd.h`, and `ctptd.cpp`, `ctptd.h`, for `ctp.api.src.ctpmd` and `ctp.api.src.ctptd`, respectively.\n- The header file contains complete class declarations and function prototypes.\n- The `cpp` file contains all implementation and bindings.\n\n7. `generate_onekey.py`\n\n- **Purpose**: **One-click assembles all md and td header, source, and other files to generate cpp and h files**\n- **Function**:\n- One-click assembles the files generated by the above files, as well as header, source, and other files, to generate four files: `ctpmd.cpp`, `ctpmd.h`, and `ctptd.cpp`, `ctptd.h`.\n\n8. `build.py`\n\n- **Purpose**: **One-click compiles the CTP C++ API into a Python API**\n- **Function**:\n- One-click compiles the Python-callable CTP API files, located in `ctp/api/`. These files include:\n- `ctpmd.cp313-win_amd64.pyd`\n- `ctptd.cp313-win_amd64.pyd`\n- `ctpmd.pyi`\n- `ctptd.pyi`\n\nFile Dependencies:\n\n1. **`generator_function_const.py`** \u2192 Generate `ctp_function_const.py`\n2. **`generate_data_type.py`** \u2192 Generate `ctp_typedef.py` and `ctp_constant.py`\n3. **`generate_struct.py`** (depends on `ctp_typedef.py`) \u2192 Generate `ctp_struct.py`\n4. **`generate_api_functions.py`** (depends on `ctp_struct.py` and `ctp_function_const.py`) \u2192 Generate multiple API header and source binding files for `md` and `td`\n5. **`generate_dll_entry.py`** \u2192 Generate `dllmain.cpp`, `stdafx.cpp`, and `stdafx.h`\n6. **`generate_cpp.py`** (depends on all the above files, as well as the generated header and source files) \u2192 Generates `ctpmd.cpp`, `ctpmd.h`, and `ctptd.cpp` and `ctptd.h`\n7. **`generate_onekey.py`** \u2192 Assembles `ctpmd.cpp`, `ctpmd.h`, and `ctptd.cpp` and `ctptd.h` files with one click (equivalent to executing the above process with one click)\n8. **`build.py`** (depends on the `ctpmd` and `ctptd` modules in `ctp/api/src/`) \u2192 Compiles `ctpmd.cp313-win_amd64.pyd`, `ctptd.cp313-win_amd64.pyd`, `ctpmd.pyi`, and `ctptd.pyi` with one click\n\n## 7. Script Usage\n\nThe code generated by these scripts is used to:\n\n- Encapsulate the CTP C++ API into a Python-callable interface\n- Automatically handle data type conversion\n- Generate Python bindings for callback functions\n- Generate Python bindings for request functions\n\n## 8. Advantages\n\n- Use pybind to bind C++ to the Python CTP API, offering superior performance compared to SWIG conversion.\n- Automatic synchronization: When the CTP official header files are updated, the latest h, dll, so, and lib files are replaced. After executing the generated script, the script will automatically reflect the latest virtual functions.\n- Easy maintenance: No need to manually update a large number of hard-coded function declarations.\n- Reduced errors: Avoid omissions or errors that may result from manual maintenance.\n- Improved efficiency: Developers only need to focus on business logic, without worrying about changes to the underlying interfaces.\n\nSummary: This is a complete code generation toolchain that automatically generates Python bindings for the CTP API, eliminating the need to manually write repetitive binding code and improving maintainability and robustness.\n\n## 9. Community Support\n\n- **Technical Exchange (QQ Group)**: `446042777`\n- [pypi.org](https://pypi.org/project/homalos-ctp)\n\n## 10. Disclaimer\n\n**[Disclaimer Content](docs/Disclaimer.md)**\n\n## 11. Replenish\n\n**Meson**: Similar to Make and CMake, its main task is to configure the compilation environment, generate compilation instructions (for example, for Ninja), and manage the entire compilation process. It does not directly compile code, but rather drives tools like Ninja to do so.\n\n**Pybind11**: A lightweight C++ library for exposing (binding) C++ code to the Python interpreter. It allows Python code to seamlessly call C++ functions and classes, just like calling regular Python modules. Its core goal is to provide an extremely simple, nearly boilerplate-free interface that easily combines the high-performance computing capabilities of C++ with the ease of use and vast Python ecosystem.\n\n---\n\n*homalos-ctp*\n*Last Updated: 2025-09-05*",
"bugtrack_url": null,
"license": "MIT License\n \n Copyright (c) 2025 Homalos\n \n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n \n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n \n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.",
"summary": "CTP Python API extension module construction.",
"version": "6.7.11.4",
"project_urls": {
"Documentation": "https://github.com/Homalos/ctp/blob/main/README.md",
"Documentation (\u4e2d\u6587)": "https://github.com/Homalos/ctp/blob/main/README_CN.md",
"Homepage": "https://github.com/Homalos/ctp",
"Issues": "https://github.com/Homalos/ctp/issues",
"Repository": "https://github.com/Homalos/ctp"
},
"split_keywords": [
"ctp",
" investment",
" quantitative",
" trading"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "bb5a4868215005ca8d0b5262bbde80673a864bf88aeca54ad16ec292758de802",
"md5": "59ccaac2fe7a6f815c451d8c4732da28",
"sha256": "d967feecaf7a60aabec2179664e8d10863fe938f9055d3d8f662506958a9f992"
},
"downloads": -1,
"filename": "homalos_ctp-6.7.11.4-cp313-cp313-win_amd64.whl",
"has_sig": false,
"md5_digest": "59ccaac2fe7a6f815c451d8c4732da28",
"packagetype": "bdist_wheel",
"python_version": "cp313",
"requires_python": ">=3.13",
"size": 6928206,
"upload_time": "2025-09-05T14:47:02",
"upload_time_iso_8601": "2025-09-05T14:47:02.981547Z",
"url": "https://files.pythonhosted.org/packages/bb/5a/4868215005ca8d0b5262bbde80673a864bf88aeca54ad16ec292758de802/homalos_ctp-6.7.11.4-cp313-cp313-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "cfef1f78612c3be1ad58c10a95a13f4ede61fd7c5aa018cd05c76c556ba96d35",
"md5": "09fa91b55043291214afa649285d09c1",
"sha256": "c0c2ca06afc0e58414aa617bc1fda1af2f8e88da46ae580d596ffd98d009dd31"
},
"downloads": -1,
"filename": "homalos_ctp-6.7.11.4.tar.gz",
"has_sig": false,
"md5_digest": "09fa91b55043291214afa649285d09c1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.13",
"size": 6877049,
"upload_time": "2025-09-05T14:47:05",
"upload_time_iso_8601": "2025-09-05T14:47:05.989899Z",
"url": "https://files.pythonhosted.org/packages/cf/ef/1f78612c3be1ad58c10a95a13f4ede61fd7c5aa018cd05c76c556ba96d35/homalos_ctp-6.7.11.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-09-05 14:47:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Homalos",
"github_project": "ctp",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "homalos-ctp"
}