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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! 基础行情订阅示例
//!
//! 展示如何使用CTP Rust SDK订阅期货合约行情数据

use ctp_rust::api::md_api::{
    DepthMarketDataField, ForQuoteRspField, MdSpiHandler, SpecificInstrumentField,
};
use ctp_rust::api::{CtpApi, MdApi};
use ctp_rust::types::{ReqUserLoginField, RspInfoField, RspUserLoginField};
use ctp_rust::*;
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use tracing::{debug, error, info, warn};
use tracing_subscriber;

/// CTP事件类型
#[derive(Debug, Clone)]
enum CtpEvent {
    /// 连接建立
    FrontConnected,
    /// 连接断开
    FrontDisconnected(i32),
    /// 登录成功
    LoginSuccess(RspUserLoginField),
    /// 登录失败
    LoginFailed(String),
    /// 订阅成功
    SubscribeSuccess(String),
    /// 订阅失败
    SubscribeFailed(String),
    /// 行情数据
    MarketData(DepthMarketDataField),
    /// 询价应答
    ForQuoteRsp(ForQuoteRspField),
}

/// 行情处理器 - 纯事件发送器
#[derive(Clone)]
struct MarketDataHandler {
    /// 事件发送器
    event_sender: Sender<CtpEvent>,
}

impl MarketDataHandler {
    fn new(event_sender: Sender<CtpEvent>) -> Self {
        Self { event_sender }
    }
}

impl MdSpiHandler for MarketDataHandler {
    fn on_front_connected(&mut self) {
        info!("✓ 已连接到行情服务器");
        // 只发送事件,不处理业务逻辑
        if let Err(e) = self.event_sender.send(CtpEvent::FrontConnected) {
            error!("发送连接事件失败: {}", e);
        }
    }

    fn on_front_disconnected(&mut self, reason: i32) {
        warn!("✗ 与行情服务器断开连接,原因代码: {}", reason);
        // 只发送事件,不处理业务逻辑
        if let Err(e) = self.event_sender.send(CtpEvent::FrontDisconnected(reason)) {
            error!("发送断连事件失败: {}", e);
        }
    }

    fn on_rsp_user_login(
        &mut self,
        user_login: Option<RspUserLoginField>,
        rsp_info: Option<RspInfoField>,
        request_id: i32,
        is_last: bool,
    ) {
        debug!(
            "收到登录响应 - 请求ID: {}, 是否最后: {}",
            request_id, is_last
        );

        if let Some(rsp) = rsp_info {
            if rsp.is_success() {
                info!("✓ 登录成功");
                if let Some(login_info) = user_login {
                    info!(
                        "  交易日: {:?}",
                        std::str::from_utf8(&login_info.trading_day)
                            .unwrap_or_default()
                            .trim_end_matches('\0')
                    );
                    info!("  前置编号: {}", login_info.front_id);
                    info!("  会话编号: {}", login_info.session_id);

                    // 只发送事件,不处理业务逻辑
                    if let Err(e) = self.event_sender.send(CtpEvent::LoginSuccess(login_info)) {
                        error!("发送登录成功事件失败: {}", e);
                    }
                } else {
                    // 即使没有login_info也认为登录成功
                    if let Err(e) =
                        self.event_sender
                            .send(CtpEvent::LoginSuccess(RspUserLoginField {
                                trading_day: [0; 9],
                                login_time: [0; 9],
                                broker_id: [0; 11],
                                user_id: [0; 16],
                                system_name: [0; 41],
                                front_id: 0,
                                session_id: 0,
                                max_order_ref: [0; 13],
                                shfe_time: [0; 9],
                                dce_time: [0; 9],
                                czce_time: [0; 9],
                                ffex_time: [0; 9],
                                ine_time: [0; 9],
                            }))
                    {
                        error!("发送登录成功事件失败: {}", e);
                    }
                }
            } else {
                let error_msg = if let Ok(msg) = rsp.get_error_msg() {
                    msg.trim_end_matches('\0').to_string()
                } else {
                    "未知登录错误".to_string()
                };
                error!("✗ 登录失败: {}", error_msg);

                // 只发送事件,不处理业务逻辑
                if let Err(e) = self.event_sender.send(CtpEvent::LoginFailed(error_msg)) {
                    error!("发送登录失败事件失败: {}", e);
                }
            }
        }
    }

    fn on_rsp_user_logout(
        &mut self,
        _user_logout: Option<()>,
        rsp_info: Option<RspInfoField>,
        request_id: i32,
        is_last: bool,
    ) {
        debug!(
            "收到登出响应 - 请求ID: {}, 是否最后: {}",
            request_id, is_last
        );

        if let Some(rsp) = rsp_info {
            if rsp.is_success() {
                info!("✓ 登出成功");
            } else {
                if let Ok(error_msg) = rsp.get_error_msg() {
                    error!("✗ 登出失败: {}", error_msg.trim_end_matches('\0'));
                }
            }
        }
        // 注意:在事件驱动架构中,状态管理应该在事件循环中处理
        // 这里可以发送一个登出事件,但当前示例中暂不实现
    }

    fn on_rsp_sub_market_data(
        &mut self,
        specific_instrument: Option<SpecificInstrumentField>,
        rsp_info: Option<RspInfoField>,
        request_id: i32,
        is_last: bool,
    ) {
        debug!(
            "收到订阅行情响应 - 请求ID: {}, 是否最后: {}",
            request_id, is_last
        );

        if let Some(rsp) = rsp_info {
            if rsp.is_success() {
                let instrument_id = if let Some(instrument) = specific_instrument {
                    if let Ok(id) = instrument.get_instrument_id() {
                        let id = id.trim_end_matches('\0').to_string();
                        info!("✓ 成功订阅合约: {}", id);
                        id
                    } else {
                        info!("✓ 订阅成功");
                        "未知合约".to_string()
                    }
                } else {
                    info!("✓ 订阅成功");
                    "未知合约".to_string()
                };

                // 只发送事件,不处理业务逻辑
                if let Err(e) = self
                    .event_sender
                    .send(CtpEvent::SubscribeSuccess(instrument_id))
                {
                    error!("发送订阅成功事件失败: {}", e);
                }
            } else {
                let error_msg = if let Ok(msg) = rsp.get_error_msg() {
                    msg.trim_end_matches('\0').to_string()
                } else {
                    "未知订阅错误".to_string()
                };
                error!("✗ 订阅失败: {}", error_msg);

                // 只发送事件,不处理业务逻辑
                if let Err(e) = self.event_sender.send(CtpEvent::SubscribeFailed(error_msg)) {
                    error!("发送订阅失败事件失败: {}", e);
                }
            }
        }
    }

    fn on_rsp_unsub_market_data(
        &mut self,
        specific_instrument: Option<SpecificInstrumentField>,
        rsp_info: Option<RspInfoField>,
        request_id: i32,
        is_last: bool,
    ) {
        debug!(
            "收到取消订阅响应 - 请求ID: {}, 是否最后: {}",
            request_id, is_last
        );

        if let Some(rsp) = rsp_info {
            if rsp.is_success() {
                if let Some(instrument) = specific_instrument {
                    if let Ok(instrument_id) = instrument.get_instrument_id() {
                        info!(
                            "✓ 成功取消订阅合约: {}",
                            instrument_id.trim_end_matches('\0')
                        );
                    } else {
                        info!("✓ 取消订阅成功");
                    }
                } else {
                    info!("✓ 取消订阅成功");
                }
            } else {
                if let Ok(error_msg) = rsp.get_error_msg() {
                    error!("✗ 取消订阅失败: {}", error_msg.trim_end_matches('\0'));
                }
            }
        }
    }

    fn on_rtn_depth_market_data(&mut self, market_data: DepthMarketDataField) {
        // 只发送事件,不处理业务逻辑
        if let Err(e) = self.event_sender.send(CtpEvent::MarketData(market_data)) {
            error!("发送行情数据事件失败: {}", e);
        }
    }

    fn on_rtn_for_quote_rsp(&mut self, for_quote_rsp: ForQuoteRspField) {
        // 只发送事件,不处理业务逻辑
        if let Err(e) = self.event_sender.send(CtpEvent::ForQuoteRsp(for_quote_rsp)) {
            error!("发送询价应答事件失败: {}", e);
        }
    }
}

/// 应用状态
#[derive(Debug)]
struct AppState {
    connected: bool,
    logged_in: bool,
    subscribed: bool,
    start_time: std::time::Instant,
}

impl AppState {
    fn new() -> Self {
        Self {
            connected: false,
            logged_in: false,
            subscribed: false,
            start_time: std::time::Instant::now(),
        }
    }
}

/// 处理行情数据显示
fn display_market_data(market_data: &DepthMarketDataField) {
    if let Ok(instrument_id) = market_data.get_instrument_id() {
        let instrument_id = instrument_id.trim_end_matches('\0');
        if !instrument_id.is_empty() {
            // 构建行情数据字符串
            let mut market_info = format!(
                "📈 {}: 最新价{:.4} 成交量{} 持仓量{}",
                instrument_id,
                market_data.last_price,
                market_data.volume,
                market_data.open_interest
            );

            if market_data.bid_price1 > 0.0 {
                market_info.push_str(&format!(
                    " 买一{:.4}({})",
                    market_data.bid_price1, market_data.bid_volume1
                ));
            }
            if market_data.ask_price1 > 0.0 {
                market_info.push_str(&format!(
                    " 卖一{:.4}({})",
                    market_data.ask_price1, market_data.ask_volume1
                ));
            }

            // 解析交易时间
            if let Ok(update_time) = market_data.update_time.to_utf8_string() {
                let update_time = update_time.trim_end_matches('\0');
                if !update_time.is_empty() {
                    market_info.push_str(&format!(
                        " 时间{}.{:03}",
                        update_time, market_data.update_millisec
                    ));
                }
            }

            info!("{}", market_info);
        }
    }
}

fn main() -> CtpResult<()> {
    // 初始化日志
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .with_file(true)
        .with_line_number(true)
        .with_target(true)
        .init();

    info!("🚀 CTP行情订阅示例启动 (事件驱动架构)");
    info!("==========================================");

    // 从环境变量加载配置
    let config = CtpConfig::from_env().map_err(|e| {
        error!("配置加载失败: {}", e);
        CtpError::InitializationError(format!("配置加载失败: {}", e))
    })?;

    info!("配置信息:");
    info!("  服务器地址: {}", config.md_front_address);
    info!("  经纪公司代码: {}", config.broker_id);
    info!("  投资者账号: {}", config.investor_id);
    info!("  流文件路径: {}", config.flow_path);
    info!("  订阅合约: {:?}", config.instruments);
    info!("==========================================");

    // 创建事件通道
    let (event_sender, event_receiver): (Sender<CtpEvent>, Receiver<CtpEvent>) = mpsc::channel();

    // 创建行情API实例
    info!("📡 创建行情API...");
    let md_api = MdApi::new(Some(&config.flow_path), false, false, true)?;
    let api_arc = Arc::new(Mutex::new(md_api));

    // 创建事件处理器
    let handler = MarketDataHandler::new(event_sender);

    // 注册事件处理器
    api_arc.lock().unwrap().register_spi(handler)?;

    // 注册前置机地址
    info!("🔗 注册前置机地址...");
    api_arc
        .lock()
        .unwrap()
        .register_front(&config.md_front_address)?;

    // 初始化API
    info!("⚡ 初始化API...");
    api_arc.lock().unwrap().init()?;

    // 应用状态
    let mut app_state = AppState::new();

    info!("🔄 开始事件循环...");
    info!("==========================================");

    // 事件循环
    loop {
        match event_receiver.recv_timeout(Duration::from_millis(100)) {
            Ok(event) => {
                match event {
                    CtpEvent::FrontConnected => {
                        info!("📡 收到连接事件");
                        app_state.connected = true;

                        // 发送登录请求
                        info!("🔐 连接成功,发送登录请求...");
                        thread::sleep(Duration::from_millis(1000)); // 稍微等待

                        let login_result = (|| -> Result<(), Box<dyn std::error::Error>> {
                            let login_req = ReqUserLoginField::new(
                                config.broker_id.as_str(),
                                config.investor_id.as_str(),
                                config.password.as_str(),
                            )?
                            .with_product_info("RustCTP")?;

                            api_arc.lock().unwrap().req_user_login(&login_req)?;
                            Ok(())
                        })();

                        match login_result {
                            Ok(_) => info!("📤 登录请求已发送"),
                            Err(e) => error!("❌ 发送登录请求失败: {}", e),
                        }
                    }
                    CtpEvent::FrontDisconnected(reason) => {
                        warn!("💔 连接断开,原因: {}", reason);
                        app_state.connected = false;
                        app_state.logged_in = false;
                        app_state.subscribed = false;
                    }
                    CtpEvent::LoginSuccess(_login_info) => {
                        info!("✅ 登录成功!");
                        app_state.logged_in = true;

                        // 发送订阅请求
                        info!("📊 登录成功,发送订阅请求...");
                        let instruments_refs: Vec<&str> =
                            config.instruments.iter().map(|s| s.as_str()).collect();
                        match api_arc
                            .lock()
                            .unwrap()
                            .subscribe_market_data(&instruments_refs)
                        {
                            Ok(_) => info!("📤 订阅请求已发送"),
                            Err(e) => error!("❌ 发送订阅请求失败: {}", e),
                        }
                    }
                    CtpEvent::LoginFailed(error_msg) => {
                        error!("❌ 登录失败: {}", error_msg);
                        break;
                    }
                    CtpEvent::SubscribeSuccess(instrument_id) => {
                        info!("✅ 订阅成功: {}", instrument_id);
                        app_state.subscribed = true;
                        info!("🎉 开始接收行情数据...");
                    }
                    CtpEvent::SubscribeFailed(error_msg) => {
                        error!("❌ 订阅失败: {}", error_msg);
                    }
                    CtpEvent::MarketData(market_data) => {
                        display_market_data(&market_data);
                    }
                    CtpEvent::ForQuoteRsp(for_quote_rsp) => {
                        if let Ok(instrument_id) = for_quote_rsp.instrument_id.to_utf8_string() {
                            let instrument_id = instrument_id.trim_end_matches('\0');
                            if !instrument_id.is_empty() {
                                info!("💬 收到询价应答: {}", instrument_id);
                            }
                        }
                    }
                }
            }
            Err(mpsc::RecvTimeoutError::Timeout) => {
                // 检查是否运行超过30秒
                if app_state.subscribed && app_state.start_time.elapsed() > Duration::from_secs(30)
                {
                    info!("⏰ 运行时间到达30秒,准备退出...");
                    break;
                }
            }
            Err(mpsc::RecvTimeoutError::Disconnected) => {
                warn!("📡 事件通道断开,退出事件循环");
                break;
            }
        }
    }

    // 清理工作
    if app_state.subscribed {
        info!("📤 取消订阅行情数据...");
        let instruments_refs: Vec<&str> = config.instruments.iter().map(|s| s.as_str()).collect();
        if let Err(e) = api_arc
            .lock()
            .unwrap()
            .unsubscribe_market_data(&instruments_refs)
        {
            error!("取消订阅失败: {}", e);
        }
        thread::sleep(Duration::from_secs(1));
    }

    info!("✅ 示例程序结束");
    Ok(())
}