Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
rtpx (RTP)
Safe Rust bindings for CTP and its variations
๐ฆ rtpx: Safe Rust Bindings for CTP Trading System and its variations
rtpx provides safe and ergonomic Rust bindings for the CTP (Comprehensive Transaction Platform) trading system and its variants (ATP, XTP), widely used in Chinese financial markets. This project aims to bring Rust's safety and performance guarantees to the CTP ecosystem while maintaining compatibility with various CTP-compatible implementations.
Supported SDK versions
| sdk | version | original URL |
|---|---|---|
| CTP | 6.7.7_210240607 | http://www.sfit.com.cn/DocumentDown/api_3/5_2_2/v6.7.7_traderapi_20240607.zip |
| ATP | 6.3.15 | N/A |
Changelog
v1.0.5
- Fixed build issue when compiling with the ATP feature by resolving SO_NAME constant redefinition in build.rs
๐ Features
- Safe Abstractions: Zero-cost abstractions over CTP's C++ interfaces with Rust's safety guarantees
- Multiple CTP Variants: Support for CTP, ATP and XTP through feature flags
- Error Handling: Rust-native error handling replacing C++ exceptions
- Cross-Platform: Windows and Linux support
- Zero Overhead: Direct FFI bindings with no runtime cost
- Type Safety: Strong typing for market data and trading interfaces
๐ Basic Usage
use CString;
use thread;
use Duration;
// Import the RTP trader API with required features
use ;
use ;
// Create a simple TraderSpi implementation
๐ฆ Installation
Add this to your Cargo.toml:
[]
= "1.0.3"
By default, the CTP variant is enabled. To use ATP or XTP instead:
[]
= { = "1.0.3", = false, = ["atp"] }
๐ง Supported CTP Variants
Feature flags allow you to choose which implementation to use:
ctp- Standard CTP implementation (default)atp- ATP implementationxtp- XTP implementation
Only one variant can be enabled at a time.
๐ Using Different SDK Versions
This package supports multiple versions of CTP and ATP SDKs. Here's how to use different versions:
Using a Different CTP Version
-
Download your desired CTP version from the official website (http://www.sfit.com.cn/)
-
Place the SDK files in your project:
-
Update your project's build configuration:
[] = { = "1.0.4", = false, = ["ctp"] } [] = "sdk/ctp" # Path to your CTP SDK
Using a Different ATP Version
-
Obtain the ATP SDK files from your broker
-
Place the SDK files similarly:
-
Update your project's build configuration:
[] = { = "1.0.4", = false, = ["atp"] } [] = "sdk/atp" # Path to your ATP SDK
Important Notes
- Always ensure binary compatibility between your chosen SDK version and your broker's trading system
- Test thoroughly with your specific SDK version in a simulation environment before using in production
- Some features might vary between different SDK versions
- When upgrading SDK versions, review the changelog for breaking changes
- The package's default SDK versions are tested and known to work, but you can override them as shown above
๐ ๏ธ Building from Source
# Clone the repository
# Build with CTP (default)
# Build with ATP
# Run tests
๐ Running Examples
The repository includes examples demonstrating basic usage of the trading API. The most comprehensive example is basic_usage.rs, which demonstrates connecting to a trading server, logging in, and querying account information and positions.
To run the examples:
# Run the basic usage example with CTP (default)
# Run the basic usage example with ATP
# Run the basic usage example with XTP
Configuring the Examples
Before running the examples, you'll need to modify the connection parameters in the source code:
- Open the example file (e.g.,
examples/basic_usage.rs) - Update the following values with your actual credentials:
let front_address = "tcp://180.168.146.187:10130"; // Change to your server let broker_id = "9999"; // Change to your broker ID let investor_id = "your_investor_id"; // Change to your actual ID let password = "your_password"; // Change to your actual password
Note on Flow Path Files
The examples create flow files in the current directory. These files are used by the CTP/ATP/XTP APIs to store session information. The default flow path is ./flow_path, but you can change it if needed.
๐งช Testing
The library includes tests showing basic usage patterns for CTP and ATP. These are non-connecting tests that demonstrate API initialization and proper structure but don't connect to real servers.
To run the tests:
# Run CTP tests (default)
# Run ATP tests
โ ๏ธ Note on Writing Tests
When writing your own tests, be aware that the import structure uses:
use ;
use ;
๐ Troubleshooting
Working with CTP Enum Values
CTP/ATP/XTP have many enum values that are defined in their C++ headers. In the Rust binding, these are often represented as primitive types (like u8) with specific values. When working with these values, you may need to use the raw numeric values rather than trying to access enum variants.
For example, when dealing with position direction (TThostFtdcPosiDirectionType):
// Correct way - use direct numeric values:
let position_char = match position.PosiDirection ;
Common CTP enum values:
- Position Direction (TThostFtdcPosiDirectionType): 1=Net, 2=Long, 3=Short
- Direction (TThostFtdcDirectionType): '0'=Buy, '1'=Sell
- Order Status (TThostFtdcOrderStatusType): '0'=AllTraded, '1'=PartTradedQueueing, '2'=PartTradedNotQueueing, '3'=NoTradeQueueing, '4'=NoTradeNotQueueing, '5'=Canceled, 'a'=Unknown, 'b'=NotTouched, 'c'=Touched
Setting String Values in CTP Structs
When setting string values in CTP structs, you need to handle the conversion from Rust strings to C-style character arrays:
// Example of setting BrokerID and UserID in a login field
unsafe
โ ๏ธ Disclaimer
This software is provided "as is", without warranty of any kind. Trading in financial markets carries significant risk. Make sure to test thoroughly in a simulated environment before using in production.
๐ License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
๐ Acknowledgments
- CTP development team for the original C++ SDK
- Rust FFI community for guidance and tools
- Contributors who helped make this project possible
- Author of the project ctp-rs. (This project is the cornerstone of CTP migrations in the world of RustLang helped a lot of people. It is archived and read only, hopefully the author is rich enough to retire)
Note: This is an unofficial Rust binding. CTP is a trademark of Shanghai Futures Exchange.