Skip to main content

ek_data/
trade.rs

1//! 成交对象(`object.py:199-232`;含迅投 XtTrade 扩展字段)。
2
3use crate::constant::{Direction, Exchange, Offset};
4use crate::time::DtNs;
5
6/// 成交回报数据:一单可多笔。
7#[derive(Debug, Clone, PartialEq, Default)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct TradeData {
10    /// 上游网关名
11    pub gateway_name: String,
12    /// 合约代码
13    pub symbol: String,
14    /// 交易所
15    pub exchange: Exchange,
16    /// 所属委托号(网关内唯一)
17    pub orderid: String,
18    /// 成交号(网关内唯一)
19    pub tradeid: String,
20    /// 方向(缺省 None)
21    pub direction: Option<Direction>,
22    /// 开平(缺省 NONE)
23    pub offset: Offset,
24    /// 成交价
25    pub price: f64,
26    /// 成交量
27    pub volume: f64,
28    /// 时间戳(UTC 纪元纳秒;缺省 None)
29    pub datetime: Option<DtNs>,
30
31    /// 手续费(enkeep 扩展)
32    pub commission: f64,
33    /// 滑点(enkeep 扩展)
34    pub slippage: f64,
35    /// 平仓盈亏(平仓时计算,enkeep 扩展)
36    pub trade_pnl: f64,
37
38    // —— 迅投 XtTrade 扩展(object.py:222-225)——
39    /// 证券名称
40    pub name: String,
41    /// 成交金额
42    pub traded_amount: f64,
43}
44
45impl TradeData {
46    /// `en_symbol`(派生方法;分配一次)。
47    pub fn en_symbol(&self) -> String {
48        let mut buf = String::with_capacity(self.exchange.as_str().len() + 1 + self.symbol.len());
49        self.en_symbol_into(&mut buf);
50        buf
51    }
52
53    /// 零分配版本:追加到复用缓冲。
54    pub fn en_symbol_into(&self, buf: &mut String) {
55        crate::symbol::write_vt_symbol(buf, &self.symbol, self.exchange);
56    }
57
58    /// `en_orderid` = `"{gateway_name}.{orderid}"`(`object.py:230`)。
59    pub fn en_orderid(&self) -> String {
60        format!("{}.{}", self.gateway_name, self.orderid)
61    }
62
63    /// `en_tradeid` = `"{gateway_name}.{tradeid}"`(`object.py:231`)。
64    pub fn en_tradeid(&self) -> String {
65        format!("{}.{}", self.gateway_name, self.tradeid)
66    }
67}