ctp_rust/
types.rs

1//! CTP数据类型定义
2//!
3//! 对应CTP C++库中的数据类型,提供安全的Rust封装
4
5use crate::encoding::GbkConverter;
6use crate::error::CtpResult;
7
8/// 交易员代码类型 (21字符)
9pub type TraderIdType = [u8; 21];
10
11/// 投资者代码类型 (13字符)
12pub type InvestorIdType = [u8; 13];
13
14/// 经纪公司代码类型 (11字符)
15pub type BrokerIdType = [u8; 11];
16
17/// 经纪公司简称类型 (9字符)
18pub type BrokerAbbrType = [u8; 9];
19
20/// 经纪公司名称类型 (81字符)
21pub type BrokerNameType = [u8; 81];
22
23/// 合约代码类型 (31字符)
24pub type InstrumentIdType = [u8; 31];
25
26/// 密码类型 (41字符)
27pub type PasswordType = [u8; 41];
28
29/// 用户代码类型 (16字符)
30pub type UserIdType = [u8; 16];
31
32/// 产品信息类型 (11字符)
33pub type ProductInfoType = [u8; 11];
34
35/// 币种代码类型 (4字符)
36pub type CurrencyIdType = [u8; 4];
37
38/// 业务类型 (1字符)
39pub type BizTypeType = [u8; 1];
40
41/// 投资者账户代码类型 (13字符)
42pub type AccountIdType = [u8; 13];
43
44/// 交易所代码类型 (9字符)
45pub type ExchangeIdType = [u8; 9];
46
47/// 投资单元代码类型 (17字符)
48pub type InvestUnitIdType = [u8; 17];
49
50/// 协议信息类型 (11字符)
51pub type ProtocolInfoType = [u8; 11];
52
53/// Mac地址类型 (21字符)
54pub type MacAddressType = [u8; 21];
55
56/// 认证码类型 (17字符)
57pub type AuthCodeType = [u8; 17];
58
59/// 应用单元代码类型 (21字符)
60pub type AppIdType = [u8; 21];
61
62/// 客户端IP地址类型 (16字符)
63pub type IpAddressType = [u8; 16];
64
65/// 客户端IP端口类型 (6字符)
66pub type IpPortType = [u8; 6];
67
68/// 第一阶段新增类型定义
69
70/// 报单编号类型 (21字符)
71pub type OrderSysIdType = [u8; 21];
72
73/// 时间类型 (9字符)
74pub type TimeType = [u8; 9];
75
76/// 成交编号类型 (21字符)
77pub type TradeIdType = [u8; 21];
78
79/// 合约在交易所的代码类型 (31字符)
80pub type ExchangeInstIdType = [u8; 31];
81
82/// 报单操作引用类型 (13字符)
83pub type OrderActionRefType = [u8; 13];
84
85/// 操作标志类型
86pub type ActionFlagType = u8;
87
88/// IP地址类型 (16字符)
89pub type IPAddressType = [u8; 16];
90
91/// 报单引用类型 (13字符)
92pub type OrderRefType = [u8; 13];
93
94/// 请求编号类型
95pub type RequestIdType = i32;
96
97/// 前置编号类型
98pub type FrontIdType = i32;
99
100/// 会话编号类型
101pub type SessionIdType = i32;
102
103/// 业务单元类型
104pub type BusinessUnitType = [u8; 21];
105/// 客户代码类型
106pub type ClientIdType = [u8; 11];
107/// 预埋单编号类型
108pub type ParkedOrderIdType = [u8; 13];
109/// 交易日类型
110pub type TradingDayType = [u8; 9];
111/// 银行代码类型
112pub type BankIdType = [u8; 4];
113/// 银行分支机构代码类型
114pub type BankBrchIdType = [u8; 5];
115/// 银行名称类型
116pub type BankNameType = [u8; 101];
117
118/// 价格类型
119pub type PriceType = f64;
120
121/// 日期类型 (9字符)
122pub type DateType = [u8; 9];
123
124/// 安装编号类型
125pub type InstallIdType = i32;
126
127/// 本地报单编号类型 (13字符)
128pub type OrderLocalIdType = [u8; 13];
129
130/// 会员代码类型 (11字符)
131pub type ParticipantIdType = [u8; 11];
132
133/// 报单操作状态类型
134pub type OrderActionStatusType = u8;
135
136/// 错误信息类型 (81字符)
137pub type ErrorMsgType = [u8; 81];
138
139/// 营业部编号类型 (9字符)
140pub type BranchIdType = [u8; 9];
141
142/// 数量类型
143pub type VolumeType = i32;
144
145/// 恢复类型枚举
146#[repr(C)]
147#[derive(Debug, Clone, Copy, PartialEq, Eq)]
148pub enum ResumeType {
149    /// 重新开始
150    Restart = 0,
151    /// 从本交易日开始传回
152    Resume = 1,
153    /// 从上次收到的续传
154    Quick = 2,
155    /// 只传送登录后的流内容
156    None = 3,
157}
158
159impl Default for ResumeType {
160    fn default() -> Self {
161        ResumeType::Restart
162    }
163}
164
165/// 字符串转换特质
166///
167/// 为固定长度的字节数组提供UTF-8字符串转换功能
168pub trait StringConvert {
169    /// 将字节数组转换为UTF-8字符串
170    fn to_utf8_string(&self) -> CtpResult<String>;
171
172    /// 从UTF-8字符串创建字节数组
173    fn from_utf8_string(s: &str) -> CtpResult<Self>
174    where
175        Self: Sized;
176}
177
178macro_rules! impl_string_convert {
179    ($type:ty, $size:expr) => {
180        impl StringConvert for $type {
181            fn to_utf8_string(&self) -> CtpResult<String> {
182                GbkConverter::fixed_bytes_to_utf8(self)
183            }
184
185            fn from_utf8_string(s: &str) -> CtpResult<Self> {
186                GbkConverter::utf8_to_fixed_bytes::<$size>(s)
187            }
188        }
189    };
190}
191
192// 为所有固定长度类型实现字符串转换
193impl_string_convert!([u8; 21], 21); // TraderIdType, MacAddressType, AppIdType, OrderSysIdType, TradeIdType
194impl_string_convert!([u8; 13], 13); // InvestorIdType, AccountIdType, OrderActionRefType, OrderRefType
195impl_string_convert!([u8; 11], 11); // BrokerIdType
196impl_string_convert!([u8; 9], 9); // BrokerAbbrType, ExchangeIdType, TimeType
197impl_string_convert!([u8; 81], 81); // BrokerNameType
198impl_string_convert!([u8; 31], 31); // InstrumentIdType, ExchangeInstIdType
199impl_string_convert!([u8; 41], 41); // PasswordType
200impl_string_convert!([u8; 17], 17); // AuthCodeType, InvestUnitIdType
201impl_string_convert!([u8; 16], 16); // UserIdType, IpAddressType, IPAddressType
202impl_string_convert!([u8; 6], 6); // IpPortType
203impl_string_convert!([u8; 4], 4); // CurrencyIdType
204impl_string_convert!([u8; 1], 1); // BizTypeType
205
206/// 用户登录请求
207#[repr(C)]
208#[derive(Debug, Clone)]
209pub struct ReqUserLoginField {
210    /// 交易日
211    pub trading_day: [u8; 9],
212    /// 经纪公司代码
213    pub broker_id: BrokerIdType,
214    /// 用户代码
215    pub user_id: UserIdType,
216    /// 密码
217    pub password: PasswordType,
218    /// 用户端产品信息
219    pub user_product_info: ProductInfoType,
220    /// 接口端产品信息
221    pub interface_product_info: ProductInfoType,
222    /// 协议信息
223    pub protocol_info: ProtocolInfoType,
224    /// Mac地址
225    pub mac_address: MacAddressType,
226    /// 动态密码
227    pub one_time_password: PasswordType,
228    /// 客户端IP地址
229    pub client_ip_address: IpAddressType,
230    /// 客户端IP端口
231    pub client_ip_port: IpPortType,
232    /// 登录备注
233    pub login_remark: [u8; 36],
234}
235
236impl Default for ReqUserLoginField {
237    fn default() -> Self {
238        Self {
239            trading_day: [0; 9],
240            broker_id: [0; 11],
241            user_id: [0; 16],
242            password: [0; 41],
243            user_product_info: [0; 11],
244            interface_product_info: [0; 11],
245            protocol_info: [0; 11],
246            mac_address: [0; 21],
247            one_time_password: [0; 41],
248            client_ip_address: [0; 16],
249            client_ip_port: [0; 6],
250            login_remark: [0; 36],
251        }
252    }
253}
254
255impl ReqUserLoginField {
256    /// 创建登录请求
257    pub fn new(broker_id: &str, user_id: &str, password: &str) -> CtpResult<Self> {
258        let mut req = Self::default();
259
260        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
261        req.user_id = UserIdType::from_utf8_string(user_id)?;
262        req.password = PasswordType::from_utf8_string(password)?;
263
264        Ok(req)
265    }
266
267    /// 设置产品信息
268    pub fn with_product_info(mut self, product_info: &str) -> CtpResult<Self> {
269        self.user_product_info = ProductInfoType::from_utf8_string(product_info)?;
270        self.interface_product_info = ProductInfoType::from_utf8_string(product_info)?;
271        Ok(self)
272    }
273
274    /// 设置认证码
275    pub fn with_auth_code(mut self, auth_code: &str) -> CtpResult<Self> {
276        self.one_time_password = PasswordType::from_utf8_string(auth_code)?;
277        Ok(self)
278    }
279
280    /// 设置Mac地址
281    pub fn with_mac_address(mut self, mac_address: &str) -> CtpResult<Self> {
282        self.mac_address = MacAddressType::from_utf8_string(mac_address)?;
283        Ok(self)
284    }
285
286    /// 设置客户端IP
287    pub fn with_client_ip(mut self, ip: &str, port: &str) -> CtpResult<Self> {
288        self.client_ip_address = IpAddressType::from_utf8_string(ip)?;
289        self.client_ip_port = IpPortType::from_utf8_string(port)?;
290        Ok(self)
291    }
292}
293
294/// 用户登录响应
295#[repr(C)]
296#[derive(Debug, Clone)]
297pub struct RspUserLoginField {
298    /// 交易日
299    pub trading_day: [u8; 9],
300    /// 登录成功时间
301    pub login_time: [u8; 9],
302    /// 经纪公司代码
303    pub broker_id: BrokerIdType,
304    /// 用户代码
305    pub user_id: UserIdType,
306    /// 交易系统名称
307    pub system_name: [u8; 41],
308    /// 前置编号
309    pub front_id: i32,
310    /// 会话编号
311    pub session_id: i32,
312    /// 最大报单引用
313    pub max_order_ref: [u8; 13],
314    /// 上期所时间
315    pub shfe_time: [u8; 9],
316    /// 大商所时间
317    pub dce_time: [u8; 9],
318    /// 郑商所时间
319    pub czce_time: [u8; 9],
320    /// 中金所时间
321    pub ffex_time: [u8; 9],
322    /// 能源中心时间
323    pub ine_time: [u8; 9],
324}
325
326impl Default for RspUserLoginField {
327    fn default() -> Self {
328        Self {
329            trading_day: [0; 9],
330            login_time: [0; 9],
331            broker_id: [0; 11],
332            user_id: [0; 16],
333            system_name: [0; 41],
334            front_id: 0,
335            session_id: 0,
336            max_order_ref: [0; 13],
337            shfe_time: [0; 9],
338            dce_time: [0; 9],
339            czce_time: [0; 9],
340            ffex_time: [0; 9],
341            ine_time: [0; 9],
342        }
343    }
344}
345
346/// 响应信息
347#[repr(C)]
348#[derive(Debug, Clone)]
349pub struct RspInfoField {
350    /// 错误代码
351    pub error_id: i32,
352    /// 错误信息
353    pub error_msg: [u8; 81],
354}
355
356impl Default for RspInfoField {
357    fn default() -> Self {
358        Self {
359            error_id: 0,
360            error_msg: [0; 81],
361        }
362    }
363}
364
365impl RspInfoField {
366    /// 获取错误信息的UTF-8字符串
367    pub fn get_error_msg(&self) -> CtpResult<String> {
368        GbkConverter::fixed_bytes_to_utf8(&self.error_msg)
369    }
370
371    /// 检查是否成功(错误代码为0)
372    pub fn is_success(&self) -> bool {
373        self.error_id == 0
374    }
375}
376
377#[cfg(test)]
378mod tests {
379    use super::*;
380
381    #[test]
382    fn test_string_convert() {
383        let test_str = "测试用户";
384        let user_id = UserIdType::from_utf8_string(test_str).unwrap();
385        let converted = user_id.to_utf8_string().unwrap();
386        assert_eq!(converted.trim_end_matches('\0'), test_str);
387    }
388
389    #[test]
390    fn test_login_request_creation() {
391        let req = ReqUserLoginField::new("9999", "investor1", "123456").unwrap();
392
393        assert_eq!(
394            req.broker_id
395                .to_utf8_string()
396                .unwrap()
397                .trim_end_matches('\0'),
398            "9999"
399        );
400        assert_eq!(
401            req.user_id.to_utf8_string().unwrap().trim_end_matches('\0'),
402            "investor1"
403        );
404        assert_eq!(
405            req.password
406                .to_utf8_string()
407                .unwrap()
408                .trim_end_matches('\0'),
409            "123456"
410        );
411    }
412
413    #[test]
414    fn test_rsp_info_success() {
415        let mut rsp = RspInfoField::default();
416        assert!(rsp.is_success());
417
418        rsp.error_id = -1;
419        assert!(!rsp.is_success());
420    }
421}
422
423// 查询资金账户字段
424#[repr(C)]
425#[derive(Debug, Clone)]
426pub struct QryTradingAccountField {
427    // 经纪公司代码
428    pub broker_id: BrokerIdType,
429    // 投资者代码
430    pub investor_id: InvestorIdType,
431    // 币种代码
432    pub currency_id: CurrencyIdType,
433    // 业务类型
434    pub biz_type: BizTypeType,
435    // 投资者账户代码
436    pub account_id: AccountIdType,
437}
438
439impl Default for QryTradingAccountField {
440    fn default() -> Self {
441        unsafe { std::mem::zeroed() }
442    }
443}
444
445impl QryTradingAccountField {
446    pub fn new(broker_id: &str, investor_id: &str) -> CtpResult<Self> {
447        let mut req = Self::default();
448        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
449        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
450        // 不设置币种代码,让CTP服务器使用默认值
451        Ok(req)
452    }
453
454    pub fn with_currency(mut self, currency_id: &str) -> CtpResult<Self> {
455        self.currency_id = CurrencyIdType::from_utf8_string(currency_id)?;
456        Ok(self)
457    }
458}
459
460// 查询投资者持仓字段
461#[repr(C)]
462#[derive(Debug, Clone)]
463pub struct QryInvestorPositionField {
464    // 经纪公司代码
465    pub broker_id: BrokerIdType,
466    // 投资者代码
467    pub investor_id: InvestorIdType,
468    // 合约代码(为空表示查询所有)
469    pub instrument_id: InstrumentIdType,
470    // 交易所代码
471    pub exchange_id: ExchangeIdType,
472    // 投资单元代码
473    pub invest_unit_id: InvestUnitIdType,
474}
475
476impl Default for QryInvestorPositionField {
477    fn default() -> Self {
478        unsafe { std::mem::zeroed() }
479    }
480}
481
482impl QryInvestorPositionField {
483    pub fn new(broker_id: &str, investor_id: &str) -> CtpResult<Self> {
484        let mut req = Self::default();
485        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
486        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
487        Ok(req)
488    }
489
490    pub fn with_instrument_id(mut self, instrument_id: &str) -> CtpResult<Self> {
491        self.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
492        Ok(self)
493    }
494}
495
496// 第一阶段新增结构体
497
498// 查询报单字段
499#[repr(C)]
500#[derive(Debug, Clone)]
501pub struct QryOrderField {
502    // 经纪公司代码
503    pub broker_id: BrokerIdType,
504    // 投资者代码
505    pub investor_id: InvestorIdType,
506    // 合约代码
507    pub instrument_id: InstrumentIdType,
508    // 交易所代码
509    pub exchange_id: ExchangeIdType,
510    // 报单编号
511    pub order_sys_id: OrderSysIdType,
512    // 开始时间
513    pub insert_time_start: TimeType,
514    // 结束时间
515    pub insert_time_end: TimeType,
516    // 投资单元代码
517    pub invest_unit_id: InvestUnitIdType,
518}
519
520impl Default for QryOrderField {
521    fn default() -> Self {
522        unsafe { std::mem::zeroed() }
523    }
524}
525
526impl QryOrderField {
527    pub fn new(broker_id: &str, investor_id: &str) -> CtpResult<Self> {
528        let mut req = Self::default();
529        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
530        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
531        Ok(req)
532    }
533
534    pub fn with_instrument_id(mut self, instrument_id: &str) -> CtpResult<Self> {
535        self.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
536        Ok(self)
537    }
538}
539
540// 查询成交字段
541#[repr(C)]
542#[derive(Debug, Clone)]
543pub struct QryTradeField {
544    // 经纪公司代码
545    pub broker_id: BrokerIdType,
546    // 投资者代码
547    pub investor_id: InvestorIdType,
548    // 合约代码
549    pub instrument_id: InstrumentIdType,
550    // 交易所代码
551    pub exchange_id: ExchangeIdType,
552    // 成交编号
553    pub trade_id: TradeIdType,
554    // 开始时间
555    pub trade_time_start: TimeType,
556    // 结束时间
557    pub trade_time_end: TimeType,
558    // 投资单元代码
559    pub invest_unit_id: InvestUnitIdType,
560}
561
562impl Default for QryTradeField {
563    fn default() -> Self {
564        unsafe { std::mem::zeroed() }
565    }
566}
567
568impl QryTradeField {
569    pub fn new(broker_id: &str, investor_id: &str) -> CtpResult<Self> {
570        let mut req = Self::default();
571        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
572        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
573        Ok(req)
574    }
575
576    pub fn with_instrument_id(mut self, instrument_id: &str) -> CtpResult<Self> {
577        self.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
578        Ok(self)
579    }
580}
581
582// 查询合约字段
583#[repr(C)]
584#[derive(Debug, Clone)]
585pub struct QryInstrumentField {
586    // 合约代码
587    pub instrument_id: InstrumentIdType,
588    // 交易所代码
589    pub exchange_id: ExchangeIdType,
590    // 合约在交易所的代码
591    pub exchange_inst_id: ExchangeInstIdType,
592    // 产品代码
593    pub product_id: InstrumentIdType,
594}
595
596impl Default for QryInstrumentField {
597    fn default() -> Self {
598        unsafe { std::mem::zeroed() }
599    }
600}
601
602impl QryInstrumentField {
603    pub fn new() -> Self {
604        Self::default()
605    }
606
607    pub fn with_instrument_id(mut self, instrument_id: &str) -> CtpResult<Self> {
608        self.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
609        Ok(self)
610    }
611
612    pub fn with_exchange_id(mut self, exchange_id: &str) -> CtpResult<Self> {
613        self.exchange_id = ExchangeIdType::from_utf8_string(exchange_id)?;
614        Ok(self)
615    }
616}
617
618// 报单操作字段(撤单使用)
619#[repr(C)]
620#[derive(Debug, Clone)]
621pub struct InputOrderActionField {
622    // 经纪公司代码
623    pub broker_id: BrokerIdType,
624    // 投资者代码
625    pub investor_id: InvestorIdType,
626    // 报单操作引用
627    pub order_action_ref: OrderActionRefType,
628    // 报单引用
629    pub order_ref: OrderRefType,
630    // 请求编号
631    pub request_id: RequestIdType,
632    // 前置编号
633    pub front_id: FrontIdType,
634    // 会话编号
635    pub session_id: SessionIdType,
636    // 交易所代码
637    pub exchange_id: ExchangeIdType,
638    // 报单编号
639    pub order_sys_id: OrderSysIdType,
640    // 操作标志
641    pub action_flag: ActionFlagType,
642    // 价格
643    pub limit_price: PriceType,
644    // 数量变化
645    pub volume_change: VolumeType,
646    // 用户代码
647    pub user_id: UserIdType,
648    // 合约代码
649    pub instrument_id: InstrumentIdType,
650    // 投资单元代码
651    pub invest_unit_id: InvestUnitIdType,
652    // IP地址
653    pub ip_address: IPAddressType,
654    // Mac地址
655    pub mac_address: MacAddressType,
656}
657
658impl Default for InputOrderActionField {
659    fn default() -> Self {
660        unsafe { std::mem::zeroed() }
661    }
662}
663
664impl InputOrderActionField {
665    pub fn new(
666        broker_id: &str,
667        investor_id: &str,
668        action_flag: u8,
669        order_ref: &str,
670        front_id: i32,
671        session_id: i32,
672        exchange_id: &str,
673        order_sys_id: &str,
674    ) -> CtpResult<Self> {
675        let mut req = Self::default();
676        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
677        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
678        req.action_flag = action_flag;
679        req.order_ref = OrderRefType::from_utf8_string(order_ref)?;
680        req.front_id = front_id;
681        req.session_id = session_id;
682        req.exchange_id = ExchangeIdType::from_utf8_string(exchange_id)?;
683        req.order_sys_id = OrderSysIdType::from_utf8_string(order_sys_id)?;
684        Ok(req)
685    }
686}
687
688// 报单操作回报字段(用于错误回报)
689#[repr(C)]
690#[derive(Debug, Clone)]
691pub struct OrderActionField {
692    // 经纪公司代码
693    pub broker_id: BrokerIdType,
694    // 投资者代码
695    pub investor_id: InvestorIdType,
696    // 报单操作引用
697    pub order_action_ref: OrderActionRefType,
698    // 报单引用
699    pub order_ref: OrderRefType,
700    // 请求编号
701    pub request_id: RequestIdType,
702    // 前置编号
703    pub front_id: FrontIdType,
704    // 会话编号
705    pub session_id: SessionIdType,
706    // 交易所代码
707    pub exchange_id: ExchangeIdType,
708    // 报单编号
709    pub order_sys_id: OrderSysIdType,
710    // 操作标志
711    pub action_flag: ActionFlagType,
712    // 价格
713    pub limit_price: PriceType,
714    // 数量变化
715    pub volume_change: VolumeType,
716    // 操作日期
717    pub action_date: DateType,
718    // 操作时间
719    pub action_time: TimeType,
720    // 交易所交易员代码
721    pub trader_id: TraderIdType,
722    // 安装编号
723    pub install_id: InstallIdType,
724    // 本地报单编号
725    pub order_local_id: OrderLocalIdType,
726    // 操作本地编号
727    pub action_local_id: OrderLocalIdType,
728    // 会员代码
729    pub participant_id: ParticipantIdType,
730    // 客户代码
731    pub client_id: ClientIdType,
732    // 业务单元
733    pub business_unit: BusinessUnitType,
734    // 报单操作状态
735    pub order_action_status: OrderActionStatusType,
736    // 用户代码
737    pub user_id: UserIdType,
738    // 状态信息
739    pub status_msg: ErrorMsgType,
740    // 合约代码
741    pub instrument_id: InstrumentIdType,
742    // 营业部编号
743    pub branch_id: BranchIdType,
744    // 投资单元代码
745    pub invest_unit_id: InvestUnitIdType,
746    // IP地址
747    pub ip_address: IPAddressType,
748    // Mac地址
749    pub mac_address: MacAddressType,
750}
751
752impl Default for OrderActionField {
753    fn default() -> Self {
754        unsafe { std::mem::zeroed() }
755    }
756}
757
758// 第二阶段新增结构体
759
760// 查询合约保证金率字段
761#[repr(C)]
762#[derive(Debug, Clone)]
763pub struct QryInstrumentMarginRateField {
764    // 经纪公司代码
765    pub broker_id: BrokerIdType,
766    // 投资者代码
767    pub investor_id: InvestorIdType,
768    // 合约代码
769    pub instrument_id: InstrumentIdType,
770    // 投机套保标志
771    pub hedge_flag: u8,
772    // 交易所代码
773    pub exchange_id: ExchangeIdType,
774    // 投资单元代码
775    pub invest_unit_id: InvestUnitIdType,
776}
777
778impl Default for QryInstrumentMarginRateField {
779    fn default() -> Self {
780        unsafe { std::mem::zeroed() }
781    }
782}
783
784impl QryInstrumentMarginRateField {
785    pub fn new(broker_id: &str, investor_id: &str, instrument_id: &str) -> CtpResult<Self> {
786        let mut req = Self::default();
787        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
788        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
789        req.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
790        Ok(req)
791    }
792
793    pub fn with_hedge_flag(mut self, hedge_flag: u8) -> Self {
794        self.hedge_flag = hedge_flag;
795        self
796    }
797
798    pub fn with_exchange_id(mut self, exchange_id: &str) -> CtpResult<Self> {
799        self.exchange_id = ExchangeIdType::from_utf8_string(exchange_id)?;
800        Ok(self)
801    }
802}
803
804// 查询合约手续费率字段
805#[repr(C)]
806#[derive(Debug, Clone)]
807pub struct QryInstrumentCommissionRateField {
808    // 经纪公司代码
809    pub broker_id: BrokerIdType,
810    // 投资者代码
811    pub investor_id: InvestorIdType,
812    // 合约代码
813    pub instrument_id: InstrumentIdType,
814    // 交易所代码
815    pub exchange_id: ExchangeIdType,
816    // 投资单元代码
817    pub invest_unit_id: InvestUnitIdType,
818}
819
820impl Default for QryInstrumentCommissionRateField {
821    fn default() -> Self {
822        unsafe { std::mem::zeroed() }
823    }
824}
825
826impl QryInstrumentCommissionRateField {
827    pub fn new(broker_id: &str, investor_id: &str, instrument_id: &str) -> CtpResult<Self> {
828        let mut req = Self::default();
829        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
830        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
831        req.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
832        Ok(req)
833    }
834
835    pub fn with_exchange_id(mut self, exchange_id: &str) -> CtpResult<Self> {
836        self.exchange_id = ExchangeIdType::from_utf8_string(exchange_id)?;
837        Ok(self)
838    }
839}
840
841// 查询交易所字段
842#[repr(C)]
843#[derive(Debug, Clone)]
844pub struct QryExchangeField {
845    // 交易所代码
846    pub exchange_id: ExchangeIdType,
847}
848
849impl Default for QryExchangeField {
850    fn default() -> Self {
851        unsafe { std::mem::zeroed() }
852    }
853}
854
855impl QryExchangeField {
856    pub fn new() -> Self {
857        Self::default()
858    }
859
860    pub fn with_exchange_id(mut self, exchange_id: &str) -> CtpResult<Self> {
861        self.exchange_id = ExchangeIdType::from_utf8_string(exchange_id)?;
862        Ok(self)
863    }
864}
865
866// 查询产品字段
867#[repr(C)]
868#[derive(Debug, Clone)]
869pub struct QryProductField {
870    // 产品代码
871    pub product_id: InstrumentIdType,
872    // 产品类型
873    pub product_class: u8,
874    // 交易所代码
875    pub exchange_id: ExchangeIdType,
876}
877
878impl Default for QryProductField {
879    fn default() -> Self {
880        unsafe { std::mem::zeroed() }
881    }
882}
883
884impl QryProductField {
885    pub fn new() -> Self {
886        Self::default()
887    }
888
889    pub fn with_product_id(mut self, product_id: &str) -> CtpResult<Self> {
890        self.product_id = InstrumentIdType::from_utf8_string(product_id)?;
891        Ok(self)
892    }
893
894    pub fn with_exchange_id(mut self, exchange_id: &str) -> CtpResult<Self> {
895        self.exchange_id = ExchangeIdType::from_utf8_string(exchange_id)?;
896        Ok(self)
897    }
898
899    pub fn with_product_class(mut self, product_class: u8) -> Self {
900        self.product_class = product_class;
901        self
902    }
903}
904
905// 结算信息确认字段
906#[repr(C)]
907#[derive(Debug, Clone)]
908pub struct SettlementInfoConfirmField {
909    // 经纪公司代码
910    pub broker_id: BrokerIdType,
911    // 投资者代码
912    pub investor_id: InvestorIdType,
913    // 确认日期
914    pub confirm_date: [u8; 9],
915    // 确认时间
916    pub confirm_time: [u8; 9],
917    // 结算编号
918    pub settlement_id: i32,
919    // 投资者账户代码
920    pub account_id: AccountIdType,
921    // 币种代码
922    pub currency_id: CurrencyIdType,
923}
924
925impl Default for SettlementInfoConfirmField {
926    fn default() -> Self {
927        unsafe { std::mem::zeroed() }
928    }
929}
930
931impl SettlementInfoConfirmField {
932    pub fn new(broker_id: &str, investor_id: &str) -> CtpResult<Self> {
933        let mut req = Self::default();
934        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
935        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
936        Ok(req)
937    }
938
939    pub fn with_account_id(mut self, account_id: &str) -> CtpResult<Self> {
940        self.account_id = AccountIdType::from_utf8_string(account_id)?;
941        Ok(self)
942    }
943}
944
945// 预埋单字段
946#[repr(C)]
947#[derive(Debug, Clone)]
948pub struct ParkedOrderField {
949    // 经纪公司代码
950    pub broker_id: BrokerIdType,
951    // 投资者代码
952    pub investor_id: InvestorIdType,
953    // 合约代码
954    pub instrument_id: InstrumentIdType,
955    // 报单引用
956    pub order_ref: OrderRefType,
957    // 用户代码
958    pub user_id: UserIdType,
959    // 报单价格条件
960    pub order_price_type: u8,
961    // 买卖方向
962    pub direction: u8,
963    // 组合开平标志
964    pub comb_offset_flag: [u8; 5],
965    // 组合投机套保标志
966    pub comb_hedge_flag: [u8; 5],
967    // 价格
968    pub limit_price: PriceType,
969    // 数量
970    pub volume_total_original: VolumeType,
971    // 有效期类型
972    pub time_condition: u8,
973    // GTD日期
974    pub gtd_date: [u8; 9],
975    // 成交量类型
976    pub volume_condition: u8,
977    // 最小成交量
978    pub min_volume: VolumeType,
979    // 触发条件
980    pub contingent_condition: u8,
981    // 止损价
982    pub stop_price: PriceType,
983    // 强平原因
984    pub force_close_reason: u8,
985    // 自动挂起标志
986    pub is_auto_suspend: i32,
987    // 业务单元
988    pub business_unit: [u8; 21],
989    // 请求编号
990    pub request_id: RequestIdType,
991    // 用户强平标志
992    pub user_force_close: i32,
993    // 交易所代码
994    pub exchange_id: ExchangeIdType,
995    // 预埋报单编号
996    pub parked_order_id: [u8; 13],
997    // 用户类型
998    pub user_type: u8,
999    // 预埋单状态
1000    pub status: u8,
1001    // 错误代码
1002    pub error_id: i32,
1003    // 错误信息
1004    pub error_msg: [u8; 81],
1005    // 互换单标志
1006    pub is_swap_order: i32,
1007    // 投资单元代码
1008    pub invest_unit_id: InvestUnitIdType,
1009    // 资金账号
1010    pub account_id: AccountIdType,
1011    // 币种代码
1012    pub currency_id: CurrencyIdType,
1013    // 客户代码
1014    pub client_id: [u8; 11],
1015    // IP地址
1016    pub ip_address: IPAddressType,
1017    // Mac地址
1018    pub mac_address: MacAddressType,
1019}
1020
1021impl Default for ParkedOrderField {
1022    fn default() -> Self {
1023        unsafe { std::mem::zeroed() }
1024    }
1025}
1026
1027impl ParkedOrderField {
1028    pub fn new(
1029        broker_id: &str,
1030        investor_id: &str,
1031        instrument_id: &str,
1032        order_ref: &str,
1033        direction: u8,
1034        limit_price: f64,
1035        volume: i32,
1036    ) -> CtpResult<Self> {
1037        let mut req = Self::default();
1038        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1039        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1040        req.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
1041        req.order_ref = OrderRefType::from_utf8_string(order_ref)?;
1042        req.direction = direction;
1043        req.limit_price = limit_price;
1044        req.volume_total_original = volume;
1045        Ok(req)
1046    }
1047}
1048
1049// 预埋单操作字段
1050#[repr(C)]
1051#[derive(Debug, Clone)]
1052pub struct ParkedOrderActionField {
1053    // 经纪公司代码
1054    pub broker_id: BrokerIdType,
1055    // 投资者代码
1056    pub investor_id: InvestorIdType,
1057    // 报单操作引用
1058    pub order_action_ref: OrderActionRefType,
1059    // 报单引用
1060    pub order_ref: OrderRefType,
1061    // 请求编号
1062    pub request_id: RequestIdType,
1063    // 前置编号
1064    pub front_id: FrontIdType,
1065    // 会话编号
1066    pub session_id: SessionIdType,
1067    // 交易所代码
1068    pub exchange_id: ExchangeIdType,
1069    // 报单编号
1070    pub order_sys_id: OrderSysIdType,
1071    // 操作标志
1072    pub action_flag: ActionFlagType,
1073    // 价格
1074    pub limit_price: PriceType,
1075    // 数量变化
1076    pub volume_change: VolumeType,
1077    // 用户代码
1078    pub user_id: UserIdType,
1079    // 合约代码
1080    pub instrument_id: InstrumentIdType,
1081    // 预埋撤单单编号
1082    pub parked_order_action_id: [u8; 13],
1083    // 用户类型
1084    pub user_type: u8,
1085    // 预埋撤单状态
1086    pub status: u8,
1087    // 错误代码
1088    pub error_id: i32,
1089    // 错误信息
1090    pub error_msg: [u8; 81],
1091    // 投资单元代码
1092    pub invest_unit_id: InvestUnitIdType,
1093    // IP地址
1094    pub ip_address: IPAddressType,
1095    // Mac地址
1096    pub mac_address: MacAddressType,
1097}
1098
1099impl Default for ParkedOrderActionField {
1100    fn default() -> Self {
1101        unsafe { std::mem::zeroed() }
1102    }
1103}
1104
1105impl ParkedOrderActionField {
1106    pub fn new(
1107        broker_id: &str,
1108        investor_id: &str,
1109        action_flag: u8,
1110        order_ref: &str,
1111        front_id: i32,
1112        session_id: i32,
1113        exchange_id: &str,
1114        order_sys_id: &str,
1115    ) -> CtpResult<Self> {
1116        let mut req = Self::default();
1117        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1118        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1119        req.action_flag = action_flag;
1120        req.order_ref = OrderRefType::from_utf8_string(order_ref)?;
1121        req.front_id = front_id;
1122        req.session_id = session_id;
1123        req.exchange_id = ExchangeIdType::from_utf8_string(exchange_id)?;
1124        req.order_sys_id = OrderSysIdType::from_utf8_string(order_sys_id)?;
1125        Ok(req)
1126    }
1127}
1128
1129// 第二阶段响应字段定义
1130
1131// 合约保证金率字段
1132#[repr(C)]
1133#[derive(Debug, Clone)]
1134pub struct InstrumentMarginRateField {
1135    // 合约代码
1136    pub instrument_id: InstrumentIdType,
1137    // 投资者范围
1138    pub investor_range: u8,
1139    // 经纪公司代码
1140    pub broker_id: BrokerIdType,
1141    // 投资者代码
1142    pub investor_id: InvestorIdType,
1143    // 投机套保标志
1144    pub hedge_flag: u8,
1145    // 多头保证金率
1146    pub long_margin_ratio_by_money: f64,
1147    // 多头保证金费
1148    pub long_margin_ratio_by_volume: f64,
1149    // 空头保证金率
1150    pub short_margin_ratio_by_money: f64,
1151    // 空头保证金费
1152    pub short_margin_ratio_by_volume: f64,
1153    // 是否相对交易所收取
1154    pub is_relative: i32,
1155    // 交易所代码
1156    pub exchange_id: ExchangeIdType,
1157    // 投资单元代码
1158    pub invest_unit_id: InvestUnitIdType,
1159}
1160
1161impl Default for InstrumentMarginRateField {
1162    fn default() -> Self {
1163        unsafe { std::mem::zeroed() }
1164    }
1165}
1166
1167// 合约手续费率字段
1168#[repr(C)]
1169#[derive(Debug, Clone)]
1170pub struct InstrumentCommissionRateField {
1171    // 合约代码
1172    pub instrument_id: InstrumentIdType,
1173    // 投资者范围
1174    pub investor_range: u8,
1175    // 经纪公司代码
1176    pub broker_id: BrokerIdType,
1177    // 投资者代码
1178    pub investor_id: InvestorIdType,
1179    // 开仓手续费率
1180    pub open_ratio_by_money: f64,
1181    // 开仓手续费
1182    pub open_ratio_by_volume: f64,
1183    // 平仓手续费率
1184    pub close_ratio_by_money: f64,
1185    // 平仓手续费
1186    pub close_ratio_by_volume: f64,
1187    // 平今手续费率
1188    pub close_today_ratio_by_money: f64,
1189    // 平今手续费
1190    pub close_today_ratio_by_volume: f64,
1191    // 交易所代码
1192    pub exchange_id: ExchangeIdType,
1193    // 业务类型
1194    pub biz_type: u8,
1195    // 投资单元代码
1196    pub invest_unit_id: InvestUnitIdType,
1197}
1198
1199impl Default for InstrumentCommissionRateField {
1200    fn default() -> Self {
1201        unsafe { std::mem::zeroed() }
1202    }
1203}
1204
1205// 交易所字段
1206#[repr(C)]
1207#[derive(Debug, Clone)]
1208pub struct ExchangeField {
1209    // 交易所代码
1210    pub exchange_id: ExchangeIdType,
1211    // 交易所名称
1212    pub exchange_name: [u8; 61],
1213    // 交易所属性
1214    pub exchange_property: u8,
1215}
1216
1217impl Default for ExchangeField {
1218    fn default() -> Self {
1219        unsafe { std::mem::zeroed() }
1220    }
1221}
1222
1223// 产品字段
1224#[repr(C)]
1225#[derive(Debug, Clone)]
1226pub struct ProductField {
1227    // 产品代码
1228    pub product_id: InstrumentIdType,
1229    // 产品名称
1230    pub product_name: [u8; 21],
1231    // 交易所代码
1232    pub exchange_id: ExchangeIdType,
1233    // 产品类型
1234    pub product_class: u8,
1235    // 合约数量乘数
1236    pub volume_multiple: i32,
1237    // 最小变动价位
1238    pub price_tick: f64,
1239    // 市价单最大下单量
1240    pub max_market_order_volume: i32,
1241    // 市价单最小下单量
1242    pub min_market_order_volume: i32,
1243    // 限价单最大下单量
1244    pub max_limit_order_volume: i32,
1245    // 限价单最小下单量
1246    pub min_limit_order_volume: i32,
1247    // 持仓类型
1248    pub position_type: u8,
1249    // 持仓日期类型
1250    pub position_date_type: u8,
1251    // 平仓处理类型
1252    pub close_deal_type: u8,
1253    // 交易币种类型
1254    pub trade_currency_id: CurrencyIdType,
1255    // 保证金币种类型
1256    pub margin_currency_id: CurrencyIdType,
1257}
1258
1259impl Default for ProductField {
1260    fn default() -> Self {
1261        unsafe { std::mem::zeroed() }
1262    }
1263}
1264
1265// 第三阶段请求和响应字段定义
1266
1267// 执行宣告录入字段
1268#[repr(C)]
1269#[derive(Debug, Clone)]
1270pub struct InputExecOrderField {
1271    pub broker_id: BrokerIdType,
1272    pub investor_id: InvestorIdType,
1273    pub instrument_id: InstrumentIdType,
1274    pub exec_order_ref: OrderRefType,
1275    pub user_id: UserIdType,
1276    pub volume: i32,
1277    pub request_id: i32,
1278    pub business_unit: BusinessUnitType,
1279    pub offset_flag: u8,
1280    pub hedge_flag: u8,
1281    pub action_type: u8,
1282    pub posidir: u8,
1283    pub reserve_position_flag: u8,
1284    pub close_flag: u8,
1285    pub exchange_id: ExchangeIdType,
1286    pub invest_unit_id: InvestUnitIdType,
1287    pub account_id: AccountIdType,
1288    pub currency_id: CurrencyIdType,
1289    pub client_id: ClientIdType,
1290    pub ip_address: [u8; 16],
1291    pub mac_address: [u8; 21],
1292}
1293
1294impl Default for InputExecOrderField {
1295    fn default() -> Self {
1296        unsafe { std::mem::zeroed() }
1297    }
1298}
1299
1300impl InputExecOrderField {
1301    pub fn new(
1302        broker_id: &str,
1303        investor_id: &str,
1304        instrument_id: &str,
1305        exec_order_ref: &str,
1306        user_id: &str,
1307        volume: i32,
1308        action_type: u8,
1309    ) -> CtpResult<Self> {
1310        let mut req = Self::default();
1311        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1312        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1313        req.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
1314        req.exec_order_ref = OrderRefType::from_utf8_string(exec_order_ref)?;
1315        req.user_id = UserIdType::from_utf8_string(user_id)?;
1316        req.volume = volume;
1317        req.action_type = action_type;
1318        Ok(req)
1319    }
1320}
1321
1322// 执行宣告操作字段
1323#[repr(C)]
1324#[derive(Debug, Clone)]
1325pub struct InputExecOrderActionField {
1326    pub broker_id: BrokerIdType,
1327    pub investor_id: InvestorIdType,
1328    pub exec_order_action_ref: OrderRefType,
1329    pub exec_order_ref: OrderRefType,
1330    pub request_id: i32,
1331    pub front_id: i32,
1332    pub session_id: i32,
1333    pub exchange_id: ExchangeIdType,
1334    pub exec_order_sys_id: OrderSysIdType,
1335    pub action_flag: u8,
1336    pub user_id: UserIdType,
1337    pub instrument_id: InstrumentIdType,
1338    pub invest_unit_id: InvestUnitIdType,
1339    pub ip_address: [u8; 16],
1340    pub mac_address: [u8; 21],
1341}
1342
1343impl Default for InputExecOrderActionField {
1344    fn default() -> Self {
1345        unsafe { std::mem::zeroed() }
1346    }
1347}
1348
1349impl InputExecOrderActionField {
1350    pub fn new(
1351        broker_id: &str,
1352        investor_id: &str,
1353        exec_order_action_ref: &str,
1354        exec_order_ref: &str,
1355        user_id: &str,
1356        action_flag: u8,
1357    ) -> CtpResult<Self> {
1358        let mut req = Self::default();
1359        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1360        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1361        req.exec_order_action_ref = OrderRefType::from_utf8_string(exec_order_action_ref)?;
1362        req.exec_order_ref = OrderRefType::from_utf8_string(exec_order_ref)?;
1363        req.user_id = UserIdType::from_utf8_string(user_id)?;
1364        req.action_flag = action_flag;
1365        Ok(req)
1366    }
1367}
1368
1369// 询价录入字段
1370#[repr(C)]
1371#[derive(Debug, Clone)]
1372pub struct InputForQuoteField {
1373    pub broker_id: BrokerIdType,
1374    pub investor_id: InvestorIdType,
1375    pub instrument_id: InstrumentIdType,
1376    pub for_quote_ref: OrderRefType,
1377    pub user_id: UserIdType,
1378    pub exchange_id: ExchangeIdType,
1379    pub invest_unit_id: InvestUnitIdType,
1380    pub ip_address: [u8; 16],
1381    pub mac_address: [u8; 21],
1382}
1383
1384impl Default for InputForQuoteField {
1385    fn default() -> Self {
1386        unsafe { std::mem::zeroed() }
1387    }
1388}
1389
1390impl InputForQuoteField {
1391    pub fn new(
1392        broker_id: &str,
1393        investor_id: &str,
1394        instrument_id: &str,
1395        for_quote_ref: &str,
1396        user_id: &str,
1397    ) -> CtpResult<Self> {
1398        let mut req = Self::default();
1399        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1400        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1401        req.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
1402        req.for_quote_ref = OrderRefType::from_utf8_string(for_quote_ref)?;
1403        req.user_id = UserIdType::from_utf8_string(user_id)?;
1404        Ok(req)
1405    }
1406}
1407
1408// 报价录入字段
1409#[repr(C)]
1410#[derive(Debug, Clone)]
1411pub struct InputQuoteField {
1412    pub broker_id: BrokerIdType,
1413    pub investor_id: InvestorIdType,
1414    pub instrument_id: InstrumentIdType,
1415    pub quote_ref: OrderRefType,
1416    pub user_id: UserIdType,
1417    pub ask_price: f64,
1418    pub bid_price: f64,
1419    pub ask_volume: i32,
1420    pub bid_volume: i32,
1421    pub request_id: i32,
1422    pub business_unit: BusinessUnitType,
1423    pub ask_offset_flag: u8,
1424    pub bid_offset_flag: u8,
1425    pub ask_hedge_flag: u8,
1426    pub bid_hedge_flag: u8,
1427    pub ask_order_ref: OrderRefType,
1428    pub bid_order_ref: OrderRefType,
1429    pub for_quote_sys_id: OrderSysIdType,
1430    pub exchange_id: ExchangeIdType,
1431    pub invest_unit_id: InvestUnitIdType,
1432    pub account_id: AccountIdType,
1433    pub currency_id: CurrencyIdType,
1434    pub client_id: ClientIdType,
1435    pub ip_address: [u8; 16],
1436    pub mac_address: [u8; 21],
1437}
1438
1439impl Default for InputQuoteField {
1440    fn default() -> Self {
1441        unsafe { std::mem::zeroed() }
1442    }
1443}
1444
1445impl InputQuoteField {
1446    pub fn new(
1447        broker_id: &str,
1448        investor_id: &str,
1449        instrument_id: &str,
1450        quote_ref: &str,
1451        user_id: &str,
1452        ask_price: f64,
1453        bid_price: f64,
1454        ask_volume: i32,
1455        bid_volume: i32,
1456    ) -> CtpResult<Self> {
1457        let mut req = Self::default();
1458        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1459        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1460        req.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
1461        req.quote_ref = OrderRefType::from_utf8_string(quote_ref)?;
1462        req.user_id = UserIdType::from_utf8_string(user_id)?;
1463        req.ask_price = ask_price;
1464        req.bid_price = bid_price;
1465        req.ask_volume = ask_volume;
1466        req.bid_volume = bid_volume;
1467        Ok(req)
1468    }
1469}
1470
1471// 报价操作字段
1472#[repr(C)]
1473#[derive(Debug, Clone)]
1474pub struct InputQuoteActionField {
1475    pub broker_id: BrokerIdType,
1476    pub investor_id: InvestorIdType,
1477    pub quote_action_ref: OrderRefType,
1478    pub quote_ref: OrderRefType,
1479    pub request_id: i32,
1480    pub front_id: i32,
1481    pub session_id: i32,
1482    pub exchange_id: ExchangeIdType,
1483    pub quote_sys_id: OrderSysIdType,
1484    pub action_flag: u8,
1485    pub user_id: UserIdType,
1486    pub instrument_id: InstrumentIdType,
1487    pub invest_unit_id: InvestUnitIdType,
1488    pub client_id: ClientIdType,
1489    pub ip_address: [u8; 16],
1490    pub mac_address: [u8; 21],
1491}
1492
1493impl Default for InputQuoteActionField {
1494    fn default() -> Self {
1495        unsafe { std::mem::zeroed() }
1496    }
1497}
1498
1499impl InputQuoteActionField {
1500    pub fn new(
1501        broker_id: &str,
1502        investor_id: &str,
1503        quote_action_ref: &str,
1504        quote_ref: &str,
1505        user_id: &str,
1506        action_flag: u8,
1507    ) -> CtpResult<Self> {
1508        let mut req = Self::default();
1509        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1510        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1511        req.quote_action_ref = OrderRefType::from_utf8_string(quote_action_ref)?;
1512        req.quote_ref = OrderRefType::from_utf8_string(quote_ref)?;
1513        req.user_id = UserIdType::from_utf8_string(user_id)?;
1514        req.action_flag = action_flag;
1515        Ok(req)
1516    }
1517}
1518
1519// 批量报单操作字段
1520#[repr(C)]
1521#[derive(Debug, Clone)]
1522pub struct InputBatchOrderActionField {
1523    pub broker_id: BrokerIdType,
1524    pub investor_id: InvestorIdType,
1525    pub order_action_ref: OrderRefType,
1526    pub request_id: i32,
1527    pub front_id: i32,
1528    pub session_id: i32,
1529    pub exchange_id: ExchangeIdType,
1530    pub user_id: UserIdType,
1531    pub invest_unit_id: InvestUnitIdType,
1532    pub ip_address: [u8; 16],
1533    pub mac_address: [u8; 21],
1534}
1535
1536impl Default for InputBatchOrderActionField {
1537    fn default() -> Self {
1538        unsafe { std::mem::zeroed() }
1539    }
1540}
1541
1542impl InputBatchOrderActionField {
1543    pub fn new(
1544        broker_id: &str,
1545        investor_id: &str,
1546        order_action_ref: &str,
1547        user_id: &str,
1548        exchange_id: &str,
1549    ) -> CtpResult<Self> {
1550        let mut req = Self::default();
1551        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1552        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1553        req.order_action_ref = OrderRefType::from_utf8_string(order_action_ref)?;
1554        req.user_id = UserIdType::from_utf8_string(user_id)?;
1555        req.exchange_id = ExchangeIdType::from_utf8_string(exchange_id)?;
1556        Ok(req)
1557    }
1558}
1559
1560// 删除预埋单字段
1561#[repr(C)]
1562#[derive(Debug, Clone)]
1563pub struct RemoveParkedOrderField {
1564    pub broker_id: BrokerIdType,
1565    pub investor_id: InvestorIdType,
1566    pub parked_order_id: ParkedOrderIdType,
1567    pub invest_unit_id: InvestUnitIdType,
1568}
1569
1570impl Default for RemoveParkedOrderField {
1571    fn default() -> Self {
1572        unsafe { std::mem::zeroed() }
1573    }
1574}
1575
1576impl RemoveParkedOrderField {
1577    pub fn new(broker_id: &str, investor_id: &str, parked_order_id: &str) -> CtpResult<Self> {
1578        let mut req = Self::default();
1579        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1580        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1581        req.parked_order_id = ParkedOrderIdType::from_utf8_string(parked_order_id)?;
1582        Ok(req)
1583    }
1584}
1585
1586// 删除预埋撤单字段
1587#[repr(C)]
1588#[derive(Debug, Clone)]
1589pub struct RemoveParkedOrderActionField {
1590    pub broker_id: BrokerIdType,
1591    pub investor_id: InvestorIdType,
1592    pub parked_order_action_id: ParkedOrderIdType,
1593    pub invest_unit_id: InvestUnitIdType,
1594}
1595
1596impl Default for RemoveParkedOrderActionField {
1597    fn default() -> Self {
1598        unsafe { std::mem::zeroed() }
1599    }
1600}
1601
1602impl RemoveParkedOrderActionField {
1603    pub fn new(
1604        broker_id: &str,
1605        investor_id: &str,
1606        parked_order_action_id: &str,
1607    ) -> CtpResult<Self> {
1608        let mut req = Self::default();
1609        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1610        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1611        req.parked_order_action_id = ParkedOrderIdType::from_utf8_string(parked_order_action_id)?;
1612        Ok(req)
1613    }
1614}
1615
1616// 查询最大报单数量字段
1617#[repr(C)]
1618#[derive(Debug, Clone)]
1619pub struct QryMaxOrderVolumeField {
1620    pub broker_id: BrokerIdType,
1621    pub investor_id: InvestorIdType,
1622    pub instrument_id: InstrumentIdType,
1623    pub direction: u8,
1624    pub offset_flag: u8,
1625    pub hedge_flag: u8,
1626    pub max_volume: i32,
1627    pub exchange_id: ExchangeIdType,
1628    pub invest_unit_id: InvestUnitIdType,
1629}
1630
1631impl Default for QryMaxOrderVolumeField {
1632    fn default() -> Self {
1633        unsafe { std::mem::zeroed() }
1634    }
1635}
1636
1637impl QryMaxOrderVolumeField {
1638    pub fn new(
1639        broker_id: &str,
1640        investor_id: &str,
1641        instrument_id: &str,
1642        direction: u8,
1643    ) -> CtpResult<Self> {
1644        let mut req = Self::default();
1645        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1646        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1647        req.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
1648        req.direction = direction;
1649        Ok(req)
1650    }
1651}
1652
1653// 查询行情字段
1654#[repr(C)]
1655#[derive(Debug, Clone)]
1656pub struct QryDepthMarketDataField {
1657    pub instrument_id: InstrumentIdType,
1658    pub exchange_id: ExchangeIdType,
1659}
1660
1661impl Default for QryDepthMarketDataField {
1662    fn default() -> Self {
1663        unsafe { std::mem::zeroed() }
1664    }
1665}
1666
1667impl QryDepthMarketDataField {
1668    pub fn new() -> Self {
1669        Self::default()
1670    }
1671
1672    pub fn with_instrument_id(mut self, instrument_id: &str) -> CtpResult<Self> {
1673        self.instrument_id = InstrumentIdType::from_utf8_string(instrument_id)?;
1674        Ok(self)
1675    }
1676}
1677
1678// 查询投资者结算结果字段
1679#[repr(C)]
1680#[derive(Debug, Clone)]
1681pub struct QrySettlementInfoField {
1682    pub broker_id: BrokerIdType,
1683    pub investor_id: InvestorIdType,
1684    pub trading_day: TradingDayType,
1685    pub account_id: AccountIdType,
1686    pub currency_id: CurrencyIdType,
1687}
1688
1689impl Default for QrySettlementInfoField {
1690    fn default() -> Self {
1691        unsafe { std::mem::zeroed() }
1692    }
1693}
1694
1695impl QrySettlementInfoField {
1696    pub fn new(broker_id: &str, investor_id: &str) -> CtpResult<Self> {
1697        let mut req = Self::default();
1698        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1699        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1700        Ok(req)
1701    }
1702}
1703
1704// 查询转帐银行字段
1705#[repr(C)]
1706#[derive(Debug, Clone)]
1707pub struct QryTransferBankField {
1708    pub bank_id: BankIdType,
1709    pub bank_brch_id: BankBrchIdType,
1710}
1711
1712impl Default for QryTransferBankField {
1713    fn default() -> Self {
1714        unsafe { std::mem::zeroed() }
1715    }
1716}
1717
1718impl QryTransferBankField {
1719    pub fn new() -> Self {
1720        Self::default()
1721    }
1722}
1723
1724// 查询投资者持仓明细字段
1725#[repr(C)]
1726#[derive(Debug, Clone)]
1727pub struct QryInvestorPositionDetailField {
1728    pub broker_id: BrokerIdType,
1729    pub investor_id: InvestorIdType,
1730    pub instrument_id: InstrumentIdType,
1731    pub exchange_id: ExchangeIdType,
1732    pub invest_unit_id: InvestUnitIdType,
1733}
1734
1735impl Default for QryInvestorPositionDetailField {
1736    fn default() -> Self {
1737        unsafe { std::mem::zeroed() }
1738    }
1739}
1740
1741impl QryInvestorPositionDetailField {
1742    pub fn new(broker_id: &str, investor_id: &str) -> CtpResult<Self> {
1743        let mut req = Self::default();
1744        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1745        req.investor_id = InvestorIdType::from_utf8_string(investor_id)?;
1746        Ok(req)
1747    }
1748}
1749
1750// 查询客户通知字段
1751#[repr(C)]
1752#[derive(Debug, Clone)]
1753pub struct QryNoticeField {
1754    pub broker_id: BrokerIdType,
1755}
1756
1757impl Default for QryNoticeField {
1758    fn default() -> Self {
1759        unsafe { std::mem::zeroed() }
1760    }
1761}
1762
1763impl QryNoticeField {
1764    pub fn new(broker_id: &str) -> CtpResult<Self> {
1765        let mut req = Self::default();
1766        req.broker_id = BrokerIdType::from_utf8_string(broker_id)?;
1767        Ok(req)
1768    }
1769}
1770
1771// 第三阶段响应字段定义
1772
1773// 投资者结算结果字段
1774#[repr(C)]
1775#[derive(Debug, Clone)]
1776pub struct SettlementInfoField {
1777    pub trading_day: TradingDayType,
1778    pub settlement_id: i32,
1779    pub broker_id: BrokerIdType,
1780    pub investor_id: InvestorIdType,
1781    pub sequence_no: i32,
1782    pub content: [u8; 1001],
1783    pub account_id: AccountIdType,
1784    pub currency_id: CurrencyIdType,
1785}
1786
1787impl Default for SettlementInfoField {
1788    fn default() -> Self {
1789        unsafe { std::mem::zeroed() }
1790    }
1791}
1792
1793// 转帐银行字段
1794#[repr(C)]
1795#[derive(Debug, Clone)]
1796pub struct TransferBankField {
1797    pub bank_id: BankIdType,
1798    pub bank_brch_id: BankBrchIdType,
1799    pub bank_name: BankNameType,
1800    pub is_active: i32,
1801}
1802
1803impl Default for TransferBankField {
1804    fn default() -> Self {
1805        unsafe { std::mem::zeroed() }
1806    }
1807}
1808
1809// 投资者持仓明细字段
1810#[repr(C)]
1811#[derive(Debug, Clone)]
1812pub struct InvestorPositionDetailField {
1813    pub instrument_id: InstrumentIdType,
1814    pub broker_id: BrokerIdType,
1815    pub investor_id: InvestorIdType,
1816    pub hedge_flag: u8,
1817    pub direction: u8,
1818    pub open_date: TradingDayType,
1819    pub trade_id: TradeIdType,
1820    pub volume: i32,
1821    pub open_price: f64,
1822    pub trading_day: TradingDayType,
1823    pub settlement_id: i32,
1824    pub trade_type: u8,
1825    pub comb_instrument_id: InstrumentIdType,
1826    pub exchange_id: ExchangeIdType,
1827    pub close_profit_by_date: f64,
1828    pub close_profit_by_trade: f64,
1829    pub position_profit_by_date: f64,
1830    pub position_profit_by_trade: f64,
1831    pub margin: f64,
1832    pub exch_margin: f64,
1833    pub margin_rate_by_money: f64,
1834    pub margin_rate_by_volume: f64,
1835    pub last_settlement_price: f64,
1836    pub settlement_price: f64,
1837    pub close_volume: i32,
1838    pub close_amount: f64,
1839    pub time_first_volume: i32,
1840    pub invest_unit_id: InvestUnitIdType,
1841    pub spec_posidir: u8,
1842}
1843
1844impl Default for InvestorPositionDetailField {
1845    fn default() -> Self {
1846        unsafe { std::mem::zeroed() }
1847    }
1848}
1849
1850// 客户通知字段
1851#[repr(C)]
1852#[derive(Debug, Clone)]
1853pub struct NoticeField {
1854    pub broker_id: BrokerIdType,
1855    pub content: [u8; 501],
1856    pub url_link: [u8; 201],
1857}
1858
1859impl Default for NoticeField {
1860    fn default() -> Self {
1861        unsafe { std::mem::zeroed() }
1862    }
1863}