ctp-rust 1.0.3

Safe Rust bindings for CTP (Comprehensive Transaction Platform) and its variations for Chinese financial markets
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# CTP Rust SDK

[![Crates.io](https://img.shields.io/crates/v/ctp-rust.svg)](https://crates.io/crates/ctp-rust)
[![Documentation](https://docs.rs/ctp-rust/badge.svg)](https://docs.rs/ctp-rust)
[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](https://github.com/deepissue/ctp-rust)

🚀 **安全、高效、易用的CTP (综合交易平台) Rust绑定库**

为中国金融市场提供完整的期货交易功能,支持同步和异步API,具备类型安全、内存安全和并发安全的特性。

## ✨ 主要特性

- 🔒 **类型安全**: 完整的Rust类型系统支持,编译时错误检查
- 🌍 **跨平台支持**: Linux (x86_64) 和 macOS (x86_64/Apple Silicon) 
-**异步支持**: 提供async/await和tokio集成的异步API
- 📝 **编码处理**: 自动GB18030到UTF-8编码转换
- 🧵 **线程安全**: 内置线程安全保护和错误处理
- 🐛 **调试支持**: 集成Debug日志系统,支持文件和控制台输出
- 📚 **完整文档**: 详细的中文文档和使用示例

## 🏗️ 系统要求

### Linux
- Ubuntu 18.04+ / CentOS 7+ / Debian 9+
- GCC 7+ 或 Clang 6+
- glibc 2.17+

### macOS
- macOS 11.0+ (Big Sur)
- Xcode Command Line Tools
- 支持Intel和Apple Silicon架构

## 📦 快速开始

### 1. 添加依赖

在 `Cargo.toml` 中添加:

```toml
[dependencies]
ctp-rust = "1.0.1"
tokio = { version = "1.42", features = ["full"] }
tracing = "0.1"
tracing-subscriber = "0.3"
dotenvy = "0.15"
```

### 2. CTP SDK配置

#### 自动下载(推荐)
SDK将在构建时自动配置,无需手动下载。

#### 手动配置
如需手动配置CTP SDK,请将库文件放置在以下目录:

```
libs/ctp/
├── linux/
│   ├── include/          # CTP头文件
│   │   ├── ThostFtdcTraderApi.h
│   │   ├── ThostFtdcMdApi.h
│   │   └── ...
│   └── lib/             # CTP动态库
│       ├── libthosttraderapi_se.so
│       └── libthostmduserapi_se.so
└── mac64/
    ├── include/          # CTP头文件
    └── lib/             # CTP动态库
        ├── libthosttraderapi_se.dylib
        └── libthostmduserapi_se.dylib
```

### 3. 环境变量配置

创建 `.env` 文件:

```env
# CTP服务器配置
CTP_TRADER_FRONT=tcp://180.168.146.187:10201
CTP_MD_FRONT=tcp://180.168.146.187:10211

# 账户配置  
CTP_BROKER_ID=9999
CTP_INVESTOR_ID=your_account
CTP_PASSWORD=your_password

# 可选配置
CTP_APP_ID=simnow_client_test
CTP_AUTH_CODE=0000000000000000
CTP_FLOW_PATH=./flow/
```

### 4. 基础使用示例

#### 同步交易API
```rust
use ctp_rust::{CtpConfig, CtpResult, TraderApi};
use ctp_rust::types::*;
use std::sync::{Arc, Mutex};

fn main() -> CtpResult<()> {
    // 加载配置
    let config = CtpConfig::from_env()?;
    
    // 创建交易API
    let trader_api = TraderApi::new(Some(&config.flow_path), Some(true))?;
    let api_arc = Arc::new(Mutex::new(trader_api));
    
    // 注册事件处理器
    let handler = MyTraderHandler::new();
    api_arc.lock().unwrap().register_spi(handler)?;
    
    // 连接和初始化
    api_arc.lock().unwrap().register_front(&config.trader_front_address)?;
    api_arc.lock().unwrap().init()?;
    
    // 等待连接...
    std::thread::sleep(std::time::Duration::from_secs(30));
    
    Ok(())
}
```

#### 异步交易API
```rust
use ctp_rust::api::AsyncTraderApi;
use tokio;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = CtpConfig::from_env()?;
    
    // 创建异步交易API
    let mut async_trader = AsyncTraderApi::new(Some(&config.flow_path))?;
    
    // 连接服务器
    async_trader.connect(&config.trader_front_address).await?;
    
    // 用户登录
    let login_req = ReqUserLoginField::new(
        &config.broker_id,
        &config.investor_id, 
        &config.password
    )?;
    
    async_trader.login(login_req).await?;
    
    // 查询账户信息
    let account_query = QryTradingAccountField::new(&config.broker_id, &config.investor_id)?;
    let account_info = async_trader.query_trading_account(account_query).await?;
    
    println!("可用资金: {:.2}", account_info.available);
    
    Ok(())
}
```

## 📋 完整示例

运行内置示例:

```bash
# 基础交易示例
cargo run --example trader_basic

# 异步交易示例  
cargo run --example async_trader_basic

# 行情订阅示例
cargo run --example md_basic

# 异步行情示例
cargo run --example async_md_basic

# 编码处理示例
cargo run --example encoding_demo

# 错误处理示例
cargo run --example error_handling
```

## 🔧 调试配置

### 启用Debug日志

```rust
use ctp_rust::config::DebugConfig;

// 创建debug配置
let debug_config = DebugConfig {
    enable_debug: true,
    log_file_path: Some("./debug.log".to_string()),
    max_file_size_mb: 100,
    max_backup_files: 5,
};

// 应用配置
debug_config.apply();
```

### 环境变量调试

```bash
# 设置日志级别
export RUST_LOG=debug

# Linux库路径
export LD_LIBRARY_PATH=./libs/ctp/linux/lib:$LD_LIBRARY_PATH

# macOS库路径  
export DYLD_LIBRARY_PATH=./libs/ctp/mac64/lib:$DYLD_LIBRARY_PATH

# 运行示例
cargo run --example trader_basic
```

## 🔄 手动更新SDK方法

当CTP发布新版本时,可以手动更新SDK:

### 1. 下载新版本SDK

从上期技术官网下载最新的CTP SDK:
- Linux: `v6.7.0_20220613_api_traderapi_se_linux64.tar.gz`
- macOS: `v6.7.0_20220613_api_traderapi_se_macos.tar.gz`

### 2. 替换库文件

```bash
# Linux
cp new_sdk/libthosttraderapi_se.so libs/ctp/linux/lib/
cp new_sdk/libthostmduserapi_se.so libs/ctp/linux/lib/
cp new_sdk/*.h libs/ctp/linux/include/

# macOS  
cp new_sdk/libthosttraderapi_se.dylib libs/ctp/mac64/lib/
cp new_sdk/libthostmduserapi_se.dylib libs/ctp/mac64/lib/
cp new_sdk/*.h libs/ctp/mac64/include/
```

### 3. 更新API绑定

如果新版本添加了新的API方法:

```rust
// 1. 在 src/ffi.rs 中添加FFI声明
extern "C" {
    pub fn CThostFtdcTraderApi_ReqNewMethod(
        api: *mut c_void,
        req: *const NewMethodField,
        request_id: c_int,
    ) -> c_int;
}

// 2. 在 src/api/trader_api.rs 中添加Rust包装
impl TraderApi {
    pub fn req_new_method(&mut self, req: &NewMethodField) -> CtpResult<i32> {
        let request_id = self.get_next_request_id();
        let result = unsafe {
            ffi::CThostFtdcTraderApi_ReqNewMethod(
                self.api_ptr,
                req as *const _ as *const c_void,
                request_id,
            )
        };
        self.handle_api_result(result, request_id)
    }
}

// 3. 在 src/types.rs 中添加新的类型定义
#[repr(C)]
#[derive(Debug, Default, Clone)]
pub struct NewMethodField {
    pub field1: [c_char; 32],
    pub field2: i32,
    // ... 其他字段
}
```

### 4. 重新编译和测试

```bash
# 清理并重新编译
cargo clean
cargo build

# 运行测试
cargo test

# 测试示例
cargo run --example trader_basic
```

## 🏛️ API架构

### 核心模块

- **`api`** - 高级API接口
  - `TraderApi` - 同步交易API
  - `MdApi` - 同步行情API  
  - `AsyncTraderApi` - 异步交易API
  - `AsyncMdApi` - 异步行情API

- **`types`** - CTP数据类型定义
  - 登录请求/响应类型
  - 查询请求/响应类型
  - 订单和交易类型
  - 行情数据类型

- **`ffi`** - 底层C++绑定
  - 原生CTP API的Rust FFI声明
  - 内存安全的指针操作

- **`encoding`** - 编码转换
  - GB18030 ↔ UTF-8 自动转换
  - 字符串处理工具

- **`error`** - 错误处理
  - 统一的错误类型定义
  - 详细的错误信息

### 异步架构

异步API基于tokio运行时,使用以下模式:
- **事件驱动**: 使用mpsc channels处理回调
- **Future-based**: 所有API调用返回Future
- **超时控制**: 内置请求超时机制
- **线程安全**: 跨线程安全的状态管理

## 🔍 故障排查

### 常见问题

#### 1. 连接失败
```
错误: 连接超时失败
```

**解决方案:**
- 检查网络连接: `ping 180.168.146.187`  
- 验证服务器地址和端口
- 确认防火墙设置
- 检查是否在交易时间段

#### 2. 动态库加载失败
```
错误: cannot open shared object file
```

**解决方案:**
```bash
# Linux
export LD_LIBRARY_PATH=./libs/ctp/linux/lib:$LD_LIBRARY_PATH

# macOS
export DYLD_LIBRARY_PATH=./libs/ctp/mac64/lib:$DYLD_LIBRARY_PATH
```

#### 3. 登录失败
```
错误: 投资者账号错误或密码错误  
```

**解决方案:**
- 验证环境变量配置
- 检查账号是否激活
- 确认密码是否正确
- 检查是否超过登录限制

#### 4. 编译错误
```
错误: linker `cc` not found
```

**解决方案:**
```bash
# Ubuntu/Debian
sudo apt install build-essential

# CentOS/RHEL  
sudo yum groupinstall "Development Tools"

# macOS
xcode-select --install
```

### 调试技巧

#### 启用详细日志
```bash
export RUST_LOG=ctp_rust=debug,tokio=info
export RUST_BACKTRACE=1
```

#### 网络诊断
```bash
# 测试连接
telnet 180.168.146.187 10201

# 检查DNS解析
nslookup 180.168.146.187
```

## 🤝 开发贡献

### 环境准备

```bash
# 克隆项目
git clone https://github.com/deepissue/ctp-rust.git
cd ctp-rust

# 安装依赖
cargo build

# 运行测试
cargo test

# 检查代码格式
cargo fmt
cargo clippy
```

### 提交规范

- 遵循 [Conventional Commits]https://conventionalcommits.org/ 规范
- 提供详细的测试用例
- 更新相关文档

## 📄 许可证

本项目采用 MIT/Apache-2.0 双许可证。

- MIT License: [LICENSE-MIT]LICENSE-MIT
- Apache License 2.0: [LICENSE-APACHE]LICENSE-APACHE

## 🔗 相关链接

- [CTP官方文档]http://www.sfit.com.cn/5_2_DocumentDown.htm
- [SimNow模拟交易]http://www.simnow.com.cn/
- [Rust官方文档]https://doc.rust-lang.org/
- [Tokio异步运行时]https://tokio.rs/

## 📞 技术支持

- 🐛 [Issues]https://github.com/deepissue/ctp-rust/issues - 问题报告和功能请求
- 💬 [Discussions]https://github.com/deepissue/ctp-rust/discussions - 讨论和提问
- 📧 Email: 36625090@qq.com

---

**⚠️ 风险提示**: 期货交易具有高风险,可能导致投资损失。请在充分理解风险的基础上进行交易,本SDK仅供技术学习和测试使用。