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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
//! FFI绑定模块
//!
//! 直接与CTP C++库的底层接口绑定

use std::os::raw::{c_char, c_int, c_void};

// Debug日志配置结构体
#[repr(C)]
pub struct CtpLogConfig {
    pub enable_debug: c_int,           // 0=关闭, 1=开启
    pub log_file_path: *const c_char,  // 日志文件路径,NULL=控制台输出
    pub max_file_size_mb: c_int,       // 最大文件大小(MB)- 预留功能
    pub max_backup_files: c_int,       // 最大备份文件数 - 预留功能
}

// SPI回调结构体
#[repr(C)]
pub struct MdSpiCallbacks {
    pub user_data: *mut c_void,
    pub on_front_connected: Option<extern "C" fn(*mut c_void)>,
    pub on_front_disconnected: Option<extern "C" fn(*mut c_void, c_int)>,
    pub on_heart_beat_warning: Option<extern "C" fn(*mut c_void, c_int)>,
    pub on_rsp_user_login:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_user_logout:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_error: Option<extern "C" fn(*mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_sub_market_data:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_unsub_market_data:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rtn_depth_market_data: Option<extern "C" fn(*mut c_void, *mut c_void)>,
    pub on_rtn_for_quote_rsp: Option<extern "C" fn(*mut c_void, *mut c_void)>,
}

// 交易SPI回调结构体
#[repr(C)]
pub struct TraderSpiCallbacks {
    pub user_data: *mut c_void,
    pub on_front_connected: Option<extern "C" fn(*mut c_void)>,
    pub on_front_disconnected: Option<extern "C" fn(*mut c_void, c_int)>,
    pub on_heart_beat_warning: Option<extern "C" fn(*mut c_void, c_int)>,
    pub on_rsp_authenticate:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_user_login:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_user_logout:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_error: Option<extern "C" fn(*mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_order_insert:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_order_action:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rtn_order: Option<extern "C" fn(*mut c_void, *mut c_void)>,
    pub on_rtn_trade: Option<extern "C" fn(*mut c_void, *mut c_void)>,
    pub on_rsp_qry_trading_account:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_investor_position:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,

    // 第一阶段新增回调
    pub on_err_rtn_order_insert: Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void)>,
    pub on_err_rtn_order_action: Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void)>,
    pub on_rsp_qry_order:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_trade:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_instrument:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,

    // 第二阶段新增回调
    pub on_rsp_qry_instrument_margin_rate:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_instrument_commission_rate:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_exchange:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_product:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_settlement_info_confirm:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_parked_order_insert:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_parked_order_action:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,

    // 第三阶段新增回调
    pub on_rsp_exec_order_insert:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_exec_order_action:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_for_quote_insert:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_quote_insert:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_quote_action:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_batch_order_action:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_remove_parked_order:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_remove_parked_order_action:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_max_order_volume:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_depth_market_data:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_settlement_info:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_transfer_bank:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_investor_position_detail:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
    pub on_rsp_qry_notice:
        Option<extern "C" fn(*mut c_void, *mut c_void, *mut c_void, c_int, c_int)>,
}

// SPI桥接函数
#[link(name = "ctp_wrapper")]
extern "C" {
    pub fn CreateMdSpiBridge(callbacks: *const MdSpiCallbacks) -> *mut c_void;
    pub fn DestroyMdSpiBridge(spi_bridge: *mut c_void);
    pub fn CreateTraderSpiBridge(callbacks: *const TraderSpiCallbacks) -> *mut c_void;
    pub fn DestroyTraderSpiBridge(spi_bridge: *mut c_void);
    
    // Debug日志接口
    pub fn CTP_InitializeDebugLogging(config: *const CtpLogConfig);
    pub fn CTP_CleanupDebugLogging();
}

// CTP行情API的FFI声明
pub mod md_api {
    use super::*;

    // 创建行情API实例
    //
    // # 参数
    // * `flow_path` - 存储流文件的目录,默认为当前目录
    // * `is_using_udp` - 是否使用UDP协议接收多播数据
    // * `is_multicast` - 是否使用组播方式
    //
    // # 返回值
    // 行情API实例指针
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_CreateFtdcMdApi(
            flow_path: *const c_char,
            is_using_udp: bool,
            is_multicast: bool,
            is_production_mode: bool,
        ) -> *mut c_void;
    }

    // 获取API版本号
    //
    // # 返回值
    // 版本号字符串
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_GetApiVersion() -> *const c_char;
    }

    // 释放API实例
    //
    // # 参数
    // * `api` - API实例指针
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_Release(api: *mut c_void);
    }

    // 初始化API
    //
    // # 参数
    // * `api` - API实例指针
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_Init(api: *mut c_void);
    }

    // 等待API线程结束运行
    //
    // # 参数
    // * `api` - API实例指针
    //
    // # 返回值
    // 线程退出代码
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_Join(api: *mut c_void) -> c_int;
    }

    // 获取当前交易日
    //
    // # 参数
    // * `api` - API实例指针
    //
    // # 返回值
    // 交易日字符串(YYYYMMDD格式)
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_GetTradingDay(api: *mut c_void) -> *const c_char;
    }

    // 注册前置机网络地址
    //
    // # 参数
    // * `api` - API实例指针
    // * `front_address` - 前置机网络地址,格式为:"protocol://ipaddress:port",
    //                     如:"tcp://127.0.0.1:17001"
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_RegisterFront(api: *mut c_void, front_address: *const c_char);
    }

    // 注册名字服务器网络地址
    //
    // # 参数
    // * `api` - API实例指针
    // * `ns_address` - 名字服务器网络地址,格式同前置机地址
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_RegisterNameServer(api: *mut c_void, ns_address: *const c_char);
    }

    // 注册名字服务器用户信息
    //
    // # 参数
    // * `api` - API实例指针
    // * `user_info` - 用户信息
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_RegisterFensUserInfo(api: *mut c_void, user_info: *const c_void);
    }

    // 注册回调接口
    //
    // # 参数
    // * `api` - API实例指针
    // * `spi` - 派生自CThostFtdcMdSpi的实例指针
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_RegisterSpi(api: *mut c_void, spi: *mut c_void);
    }

    // 用户登录请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 登录请求
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_ReqUserLogin(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 登出请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 登出请求
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_ReqUserLogout(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 订阅行情
    //
    // # 参数
    // * `api` - API实例指针
    // * `instrument_ids` - 合约ID数组
    // * `count` - 合约个数
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_SubscribeMarketData(
            api: *mut c_void,
            instrument_ids: *const *const c_char,
            count: c_int,
        ) -> c_int;
    }

    // 退订行情
    //
    // # 参数
    // * `api` - API实例指针
    // * `instrument_ids` - 合约ID数组
    // * `count` - 合约个数
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_UnSubscribeMarketData(
            api: *mut c_void,
            instrument_ids: *const *const c_char,
            count: c_int,
        ) -> c_int;
    }

    // 订阅询价
    //
    // # 参数
    // * `api` - API实例指针
    // * `instrument_ids` - 合约ID数组
    // * `count` - 合约个数
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_SubscribeForQuoteRsp(
            api: *mut c_void,
            instrument_ids: *const *const c_char,
            count: c_int,
        ) -> c_int;
    }

    // 退订询价
    //
    // # 参数
    // * `api` - API实例指针
    // * `instrument_ids` - 合约ID数组
    // * `count` - 合约个数
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcMdApi_UnSubscribeForQuoteRsp(
            api: *mut c_void,
            instrument_ids: *const *const c_char,
            count: c_int,
        ) -> c_int;
    }
}

// CTP交易API的FFI声明
pub mod trader_api {
    use super::*;

    // 创建交易API实例
    //
    // # 参数
    // * `flow_path` - 存储流文件的目录,默认为当前目录
    //
    // # 返回值
    // 交易API实例指针
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_CreateFtdcTraderApi(
            flow_path: *const c_char,
            is_production_mode: bool,
        ) -> *mut c_void;
    }

    // 获取API版本号
    //
    // # 返回值
    // 版本号字符串
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_GetApiVersion() -> *const c_char;
    }

    // 释放API实例
    //
    // # 参数
    // * `api` - API实例指针
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_Release(api: *mut c_void);
    }

    // 初始化API
    //
    // # 参数
    // * `api` - API实例指针
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_Init(api: *mut c_void);
    }

    // 等待API线程结束运行
    //
    // # 参数
    // * `api` - API实例指针
    //
    // # 返回值
    // 线程退出代码
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_Join(api: *mut c_void) -> c_int;
    }

    // 获取当前交易日
    //
    // # 参数
    // * `api` - API实例指针
    //
    // # 返回值
    // 交易日字符串(YYYYMMDD格式)
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_GetTradingDay(api: *mut c_void) -> *const c_char;
    }

    // 注册前置机网络地址
    //
    // # 参数
    // * `api` - API实例指针
    // * `front_address` - 前置机网络地址
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_RegisterFront(api: *mut c_void, front_address: *const c_char);
    }

    // 注册名字服务器网络地址
    //
    // # 参数
    // * `api` - API实例指针
    // * `ns_address` - 名字服务器网络地址
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_RegisterNameServer(api: *mut c_void, ns_address: *const c_char);
    }

    // 注册名字服务器用户信息
    //
    // # 参数
    // * `api` - API实例指针
    // * `user_info` - 用户信息
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_RegisterFensUserInfo(api: *mut c_void, user_info: *const c_void);
    }

    // 注册回调接口
    //
    // # 参数
    // * `api` - API实例指针
    // * `spi` - 派生自CThostFtdcTraderSpi的实例指针
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_RegisterSpi(api: *mut c_void, spi: *mut c_void);
    }

    // 获取已连接的前置信息
    //
    // # 参数
    // * `api` - API实例指针
    // * `front_info` - 前置信息结构体指针
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_GetFrontInfo(api: *mut c_void, front_info: *mut c_void);
    }

    // 注册微信用户系统信息
    //
    // # 参数
    // * `api` - API实例指针
    // * `user_system_info` - 微信用户系统信息
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_RegisterWechatUserSystemInfo(
            api: *mut c_void,
            user_system_info: *mut c_void,
        ) -> c_int;
    }

    // 上报微信用户系统信息
    //
    // # 参数
    // * `api` - API实例指针
    // * `user_system_info` - 微信用户系统信息
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_SubmitWechatUserSystemInfo(
            api: *mut c_void,
            user_system_info: *mut c_void,
        ) -> c_int;
    }

    // 客户端认证请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 认证请求
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqAuthenticate(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 注册用户终端信息,用于中继服务器多连接模式
    //
    // # 参数
    // * `api` - API实例指针
    // * `user_info` - 用户终端信息
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_RegisterUserSystemInfo(
            api: *mut c_void,
            user_info: *const c_void,
        ) -> c_int;
    }

    // 上报用户终端信息,用于中继服务器操作员登录模式
    //
    // # 参数
    // * `api` - API实例指针
    // * `user_info` - 用户终端信息
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_SubmitUserSystemInfo(
            api: *mut c_void,
            user_info: *const c_void,
        ) -> c_int;
    }

    // 用户登录请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 登录请求
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqUserLogin(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 登出请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 登出请求
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqUserLogout(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询资金账户
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询资金账户请求字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryTradingAccount(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询投资者持仓
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询投资者持仓请求字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryInvestorPosition(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 第一阶段新增API方法

    // 报单录入请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 报单录入请求字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqOrderInsert(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 报单操作请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 报单操作请求字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqOrderAction(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询报单
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询报单请求字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryOrder(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询成交
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询成交请求字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryTrade(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询合约
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询合约请求字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryInstrument(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 第二阶段新增API方法

    // 请求查询合约保证金率
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询合约保证金率请求字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryInstrumentMarginRate(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询合约手续费率
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询合约手续费率请求字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryInstrumentCommissionRate(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询交易所
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询交易所请求字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryExchange(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询产品
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询产品请求字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryProduct(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 投资者结算结果确认
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 投资者结算结果确认信息
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqSettlementInfoConfirm(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 预埋单录入请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 预埋单字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqParkedOrderInsert(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 预埋撤单录入请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 预埋撤单字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqParkedOrderAction(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 第三阶段新增API方法

    // 执行宣告录入请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 执行宣告录入字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqExecOrderInsert(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 执行宣告操作请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 执行宣告操作字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqExecOrderAction(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 询价录入请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 询价录入字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqForQuoteInsert(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 报价录入请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 报价录入字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQuoteInsert(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 报价操作请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 报价操作字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQuoteAction(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 批量报单操作请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 批量报单操作字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqBatchOrderAction(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 删除预埋单请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 删除预埋单字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqRemoveParkedOrder(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 删除预埋撤单请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 删除预埋撤单字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqRemoveParkedOrderAction(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 查询最大报单数量请求
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询最大报单数量字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryMaxOrderVolume(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询行情
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询行情字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryDepthMarketData(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询投资者结算结果
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询投资者结算结果字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQrySettlementInfo(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询转帐银行
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询转帐银行字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryTransferBank(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询投资者持仓明细
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询投资者持仓明细字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryInvestorPositionDetail(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }

    // 请求查询客户通知
    //
    // # 参数
    // * `api` - API实例指针
    // * `req` - 查询客户通知字段
    // * `request_id` - 请求ID
    //
    // # 返回值
    // 0表示成功,非0表示失败
    #[link(name = "ctp_wrapper")]
    extern "C" {
        pub fn CThostFtdcTraderApi_ReqQryNotice(
            api: *mut c_void,
            req: *const c_void,
            request_id: c_int,
        ) -> c_int;
    }
}

// SPI回调函数类型定义
pub mod callbacks {
    use super::*;

    // 连接建立回调
    pub type OnFrontConnectedCallback = extern "C" fn();

    // 连接断开回调
    pub type OnFrontDisconnectedCallback = extern "C" fn(reason: c_int);

    // 心跳超时警告回调
    pub type OnHeartBeatWarningCallback = extern "C" fn(time_lapse: c_int);

    // 登录响应回调
    pub type OnRspUserLoginCallback = extern "C" fn(
        user_login: *const c_void,
        rsp_info: *const c_void,
        request_id: c_int,
        is_last: bool,
    );

    // 登出响应回调
    pub type OnRspUserLogoutCallback = extern "C" fn(
        user_logout: *const c_void,
        rsp_info: *const c_void,
        request_id: c_int,
        is_last: bool,
    );

    // 深度行情通知回调
    pub type OnRtnDepthMarketDataCallback = extern "C" fn(depth_market_data: *const c_void);

    // 订阅行情应答回调
    pub type OnRspSubMarketDataCallback = extern "C" fn(
        specific_instrument: *const c_void,
        rsp_info: *const c_void,
        request_id: c_int,
        is_last: bool,
    );

    // 取消订阅行情应答回调
    pub type OnRspUnSubMarketDataCallback = extern "C" fn(
        specific_instrument: *const c_void,
        rsp_info: *const c_void,
        request_id: c_int,
        is_last: bool,
    );

    // 认证响应回调
    pub type OnRspAuthenticateCallback = extern "C" fn(
        rsp_authenticate: *const c_void,
        rsp_info: *const c_void,
        request_id: c_int,
        is_last: bool,
    );

    // 错误响应回调
    pub type OnRspErrorCallback =
        extern "C" fn(rsp_info: *const c_void, request_id: c_int, is_last: bool);
}

// 确保FFI绑定在编译时正确链接
#[cfg(test)]
mod tests {
    use std::ptr;

    #[test]
    fn test_ffi_declarations_compile() {
        // 这个测试确保FFI声明可以正确编译
        // 实际的FFI调用需要真实的CTP库
        let _api_ptr: *mut std::os::raw::c_void = ptr::null_mut();
        assert!(true, "FFI声明编译成功");
    }
}