Skip to main content

ek_data/
order.rs

1//! 委托对象(`object.py:152-196`;含迅投 XtOrder 扩展字段)。
2
3use crate::cancel_request::CancelRequest;
4use crate::constant::{Direction, Exchange, Offset, OrderType, Status};
5use crate::time::DtNs;
6
7/// 订单最新状态跟踪数据。
8#[derive(Debug, Clone, PartialEq, Default)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct OrderData {
11    /// 上游网关名
12    pub gateway_name: String,
13    /// 合约代码
14    pub symbol: String,
15    /// 交易所
16    pub exchange: Exchange,
17    /// 委托号(网关内唯一)
18    pub orderid: String,
19
20    /// 委托类型(Python `type`;Rust 改名避关键字,serde 线上字段名还原为 `"type"`)
21    #[cfg_attr(feature = "serde", serde(rename = "type"))]
22    pub order_type: OrderType,
23    /// 方向(缺省 None)
24    pub direction: Option<Direction>,
25    /// 开平(缺省 NONE)
26    pub offset: Offset,
27    /// 委托价
28    pub price: f64,
29    /// 委托总量
30    pub volume: f64,
31    /// 已成交量
32    pub traded: f64,
33    /// 状态(缺省 SUBMITTING)
34    pub status: Status,
35    /// 时间戳(UTC 纪元纳秒;缺省 None)
36    pub datetime: Option<DtNs>,
37    /// 策略/来源备注
38    pub reference: String,
39
40    // —— 迅投 XtOrder 扩展(object.py:172-175)——
41    /// 证券名称
42    pub name: String,
43    /// 委托状态描述(废单原因)
44    pub status_msg: String,
45    /// 成交均价
46    pub traded_price: f64,
47}
48
49impl OrderData {
50    /// `en_symbol`(派生方法;分配一次)。
51    pub fn en_symbol(&self) -> String {
52        let mut buf = String::with_capacity(self.exchange.as_str().len() + 1 + self.symbol.len());
53        self.en_symbol_into(&mut buf);
54        buf
55    }
56
57    /// 零分配版本:追加到复用缓冲。
58    pub fn en_symbol_into(&self, buf: &mut String) {
59        crate::symbol::write_vt_symbol(buf, &self.symbol, self.exchange);
60    }
61
62    /// `en_orderid` = `"{gateway_name}.{orderid}"`(`object.py:180`)。
63    pub fn en_orderid(&self) -> String {
64        format!("{}.{}", self.gateway_name, self.orderid)
65    }
66
67    /// 订单是否活跃(`object.py:182`;真源为 [`Status::is_active`])。
68    pub fn is_active(&self) -> bool {
69        self.status.is_active()
70    }
71
72    /// 由订单直接生成撤单请求(`object.py:188`)。
73    pub fn create_cancel_request(&self) -> CancelRequest {
74        CancelRequest {
75            orderid: self.orderid.clone(),
76            symbol: self.symbol.clone(),
77            exchange: self.exchange,
78        }
79    }
80}