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, ACTIVE_STATUSES};
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        crate::symbol::vt_symbol(&self.symbol, self.exchange)
53    }
54
55    /// `en_orderid` = `"{gateway_name}.{orderid}"`(`object.py:180`)。
56    pub fn en_orderid(&self) -> String {
57        format!("{}.{}", self.gateway_name, self.orderid)
58    }
59
60    /// 订单是否活跃(`object.py:182`;判定集合 [`ACTIVE_STATUSES`])。
61    pub fn is_active(&self) -> bool {
62        ACTIVE_STATUSES.contains(&self.status)
63    }
64
65    /// 由订单直接生成撤单请求(`object.py:188`)。
66    pub fn create_cancel_request(&self) -> CancelRequest {
67        CancelRequest {
68            orderid: self.orderid.clone(),
69            symbol: self.symbol.clone(),
70            exchange: self.exchange,
71        }
72    }
73}