记录分钟成交精确时间

This commit is contained in:
boris
2026-08-27 02:47:19 +08:00
parent 8d7bb60c30
commit 6ee1835ca5
5 changed files with 180 additions and 3 deletions
+21
View File
@@ -36,6 +36,9 @@ impl BrokerExecutionReport {
for event in &self.order_events {
event.validate().map_err(BacktestError::Execution)?;
}
for event in &self.fill_events {
event.validate().map_err(BacktestError::Execution)?;
}
Ok(())
}
}
@@ -45,6 +48,8 @@ struct ExecutionLeg {
price: f64,
mark_price: f64,
quantity: u32,
execution_start_timestamp: Option<NaiveDateTime>,
execution_timestamp: Option<NaiveDateTime>,
}
#[derive(Debug, Clone)]
@@ -4084,6 +4089,8 @@ where
price: execution_price,
mark_price: self.snapshot_mark_price(snapshot, OrderSide::Sell),
quantity: fillable_qty,
execution_start_timestamp: None,
execution_timestamp: None,
}],
None,
Vec::new(),
@@ -4227,6 +4234,8 @@ where
decision_date: None,
order_created_date: None,
execution_date: None,
execution_start_timestamp: leg.execution_start_timestamp,
execution_timestamp: leg.execution_timestamp,
order_id: Some(order_id),
symbol: symbol.to_string(),
side: OrderSide::Sell,
@@ -5784,6 +5793,8 @@ where
price: execution_price,
mark_price: self.snapshot_mark_price(snapshot, OrderSide::Buy),
quantity: filled_qty,
execution_start_timestamp: None,
execution_timestamp: None,
}],
None,
Vec::new(),
@@ -5928,6 +5939,8 @@ where
decision_date: None,
order_created_date: None,
execution_date: None,
execution_start_timestamp: leg.execution_start_timestamp,
execution_timestamp: leg.execution_timestamp,
order_id: Some(order_id),
symbol: symbol.to_string(),
side: OrderSide::Buy,
@@ -6812,6 +6825,7 @@ where
let mut filled_qty = 0_u32;
let mut gross_amount = 0.0_f64;
let mut mark_amount = 0.0_f64;
let mut first_timestamp = None;
let mut last_timestamp = None;
let mut legs = Vec::new();
let mut budget_block_reason = None;
@@ -7000,11 +7014,14 @@ where
gross_amount += quote_price * take_qty as f64;
mark_amount += mark_price * take_qty as f64;
filled_qty += take_qty;
first_timestamp.get_or_insert(quote.timestamp);
last_timestamp = Some(quote.timestamp);
legs.push(ExecutionLeg {
price: quote_price,
mark_price,
quantity: take_qty,
execution_start_timestamp: Some(quote.timestamp),
execution_timestamp: Some(quote.timestamp),
});
if consume_depth {
let state = depth_state
@@ -7064,6 +7081,8 @@ where
price: gross_amount / filled_qty as f64,
mark_price: mark_amount / filled_qty as f64,
quantity: filled_qty,
execution_start_timestamp: first_timestamp,
execution_timestamp: last_timestamp,
}]
} else {
legs
@@ -9966,6 +9985,8 @@ mod tests {
assert_eq!(fill.quantity, 200);
assert_eq!(fill.legs.len(), 1);
assert_eq!(fill.legs[0].price, 10.8);
assert_eq!(fill.legs[0].execution_timestamp, Some(quote_timestamp));
assert!(fill.legs[0].execution_timestamp.unwrap() <= decision_time);
assert_eq!(
fill.next_cursor,
quote_timestamp + chrono::Duration::seconds(1)