CTP Python API extension module construction.
Project description
English | 简体中文
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
-
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
ctp/
├── 📂 assets/ # Resource files
├── 📂 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
│ │ ├── 📁 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
│ │ ├── 📁 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
│ ├── 📁 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
├── 📁 demo.py # Extension module usage examples
├── 📁 hatch_build.py # Hatch hook, set the platform identifier when packaging with hatch
├── 📁 meson.build # Meson build configuration file
├── 📁 pyproject.toml # Python project management configuration file, automatically generated by uv
└── 📁 uv.lock # uv lock file, automatically generated by uv
3. Install the Basic Environment (skip if already installed)
- Install UV
On Windows
Method 1: Global Installation (Recommended, choose one)
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Method 2: Install in a Single Python Environment (Choose one)
pip install uv
On Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
- Install Python (Perform this step with Method 1, skip this with Method 2). I use 3.13.6; you can install your preferred version.
uv python install 3.13
4. Usage
- Install a Python virtual environment and dependencies (execute from the root directory)
# Use uv to create a Python virtual environment with a specified version in the current project.
uv venv --python 3.13 .venv
# Install dependent libraries
uv add meson-python
uv add pybind11
uv add pybind11-stubgen
- Execute the one-click build script in the
generatordirectory (to generate Python bindings for the CTP C++ API)
# Activate the Python virtual environment and enter the generator.
.venv\Scripts\activate
cd homalos-ctp\api\generator
# Generate binding files with one click.
python generate_onekey.py
- Execute the following build script in the root directory to generate the CTP C++ API and encapsulate it into a Python-callable interface.
# Compile the CTP Python API with one click.
python build.py
5. Demo test
Fill in the CTP environment information in demo.py in the project root directory and run
6. Script Function Details
The generator script is located in ctp/api/generator/
generator_function_const.py
- Purpose: Generates basic function constant files
- Function:
- Reads the CTP header files
ThostFtdcMdApi.handThostFtdcTraderApi.h.h - Parses the functions therein and generates
ctp_function_const.py(function constant definitions)
generate_data_type.py
- Purpose: Generates data type definition files
- Function:
- Reads the CTP header file
ThostFtdcUserApiDataType.h - Parses the
#defineconstant definitions andtypedeftype definitions therein - Generates
ctp_function_const.py
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
generate_api_functions.py
- Purpose: Generates API function binding code
- Function:
- Reads the CTP API header files (such as
ThostFtdcTraderApi.handThostFtdcMdApi.h) - Relies on the structure definitions in
ctp_struct.py - Generates a large number of C++ source code files for Python bindings
generate_dll_entry.py
-
Purpose: Generates the C++ DLL entry point code file
-
Function:
-
Generates three files:
dllmain.cpp,stdafx.cpp, andstdafx.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.
generate_cpp.py
- Purpose: Generates
cppandhfiles - Function: **Generates
ctpmd.cpp,ctpmd.h, andctptd.cpp,ctptd.h, forctp.api.src.ctpmdandctp.api.src.ctptd, respectively. - The header file contains complete class declarations and function prototypes.
- The
cppfile contains all implementation and bindings.
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, andctptd.cpp,ctptd.h.
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.pydctptd.cp313-win_amd64.pydctpmd.pyictptd.pyi
File Dependencies:
generator_function_const.py→ Generatectp_function_const.pygenerate_data_type.py→ Generatectp_typedef.pyandctp_constant.pygenerate_struct.py(depends onctp_typedef.py) → Generatectp_struct.pygenerate_api_functions.py(depends onctp_struct.pyandctp_function_const.py) → Generate multiple API header and source binding files formdandtdgenerate_dll_entry.py→ Generatedllmain.cpp,stdafx.cpp, andstdafx.hgenerate_cpp.py(depends on all the above files, as well as the generated header and source files) → Generatesctpmd.cpp,ctpmd.h, andctptd.cppandctptd.hgenerate_onekey.py→ Assemblesctpmd.cpp,ctpmd.h, andctptd.cppandctptd.hfiles with one click (equivalent to executing the above process with one click)build.py(depends on thectpmdandctptdmodules inctp/api/src/) → Compilesctpmd.cp313-win_amd64.pyd,ctptd.cp313-win_amd64.pyd,ctpmd.pyi, andctptd.pyiwith 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
10. Disclaimer
11. 补充
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file homalos_ctp-6.7.11.2.tar.gz.
File metadata
- Download URL: homalos_ctp-6.7.11.2.tar.gz
- Upload date:
- Size: 6.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e84fe262e2c2b3e1e469b86d05b11d6ac566233f73b34719b322a0b52ddf7208
|
|
| MD5 |
8b8f225a2aac1d4e937468c911a44d7d
|
|
| BLAKE2b-256 |
476ec577d1d452f911bbe0860b229c8497e4ad387287bb4e11b3f2f3fbf0d115
|
File details
Details for the file homalos_ctp-6.7.11.2-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: homalos_ctp-6.7.11.2-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 7.0 MB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
800594a52237d39b61e115e5a61caf1b2a6f6ede9ede6c5d5cff3b5536ff62b0
|
|
| MD5 |
d26983a2f5c50a630911547c98325f9e
|
|
| BLAKE2b-256 |
402daabc5dd3212cffc7abdf926d935cb077f9d918149dd3fea7aec69f3563fa
|