pub mod algo_execution;
pub mod application;
mod spread_group_barrier;
pub use algo_execution::{
AlgoExecutionState, AlgoExecutionStatus, AlgoExecutionStep, AlgoParentOrder,
ALGO_EXECUTION_STATE_SCHEMA_VERSION,
};
pub use application::*;
use qx_control::{cancel_order_target_from_command, order_from_submit_command, ControlCommand};
use qx_core::{DecimalRounding, ExactDecimal, FeeModel};
use qx_guanxing::QuoteTick;
use qx_risk::OrderRiskPosition;
pub use qx_zhenlu::algo::{
plan_twap, plan_vwap, AlgoChildSlice, AlgoExecutionError, VwapVolumeBucket, MAX_ALGO_CHILDREN,
};
use qx_zhenlu::{
OrderGroupStore, PaperVenue, RiskContext, SpreadOrderGroup, SpreadOrderGroupStatus,
SpreadOrderGroupStore, Venue, VenueEvent,
};
pub fn spread_group_barrier(
store: Option<&dyn SpreadOrderGroupStore>,
command: &ControlCommand,
) -> Result<(), String> {
spread_group_barrier::check_spread_group_barrier(store, command)
}
pub struct RiskExecutionContext<'a> {
pub risk: &'a RiskContext,
pub position: &'a OrderRiskPosition,
}
pub struct SubmitOrderWithRiskRequest<'a, V: Venue, P: ExecutionEventPort> {
pub command: &'a ControlCommand,
pub venue: &'a mut V,
pub pipeline: &'a mut P,
pub worker_id: &'a str,
pub now: u64,
pub source_seq: &'a mut u64,
pub context: &'a RiskExecutionContext<'a>,
pub spread_store: Option<&'a dyn SpreadOrderGroupStore>,
}
pub struct PaperSubmitRequest<'a, P: ExecutionEventPort + application::LedgerProbe> {
pub command: &'a ControlCommand,
pub pipeline: &'a mut P,
pub now: u64,
pub risk: Option<RiskContext>,
pub position: Option<OrderRiskPosition>,
pub market_quote: Option<QuoteTick>,
pub fee_model: Box<dyn FeeModel + Send>,
pub allow_synthetic_quote: bool,
pub spread_store: Option<&'a dyn SpreadOrderGroupStore>,
}
pub struct RiskContextPort<'a> {
pub risk: &'a RiskContext,
pub position: &'a OrderRiskPosition,
}
pub struct CanonicalRiskPort<'a> {
pub context: &'a qx_risk::OrderRiskContext,
}
pub struct VenuePortAdapter<V: Venue> {
venue: V,
accept_any_venue: bool,
}
pub struct BorrowedVenuePort<'a, V: Venue + ?Sized> {
venue: &'a mut V,
accept_any_venue: bool,
}
impl<'a, V: Venue + ?Sized> BorrowedVenuePort<'a, V> {
pub fn new(venue: &'a mut V) -> Self {
Self {
venue,
accept_any_venue: false,
}
}
pub fn new_for_any_venue(venue: &'a mut V) -> Self {
Self {
venue,
accept_any_venue: true,
}
}
}
impl<V: Venue> VenuePortAdapter<V> {
pub fn new(venue: V) -> Self {
Self {
venue,
accept_any_venue: false,
}
}
pub fn new_for_any_venue(venue: V) -> Self {
Self {
venue,
accept_any_venue: true,
}
}
pub fn into_inner(self) -> V {
self.venue
}
pub fn venue(&self) -> &V {
&self.venue
}
}
fn venue_events_to_application(events: Vec<VenueEvent>) -> Vec<ExecutionEvent> {
events
.into_iter()
.map(|event| match event {
VenueEvent::Accepted {
client_order_id,
venue_order_id,
..
} => ExecutionEvent::Accepted {
client_order_id,
venue_order_id,
},
VenueEvent::Rejected {
client_order_id,
reason,
..
} => ExecutionEvent::Rejected {
client_order_id,
reason,
},
VenueEvent::Fill(fill) => ExecutionEvent::Fill(fill),
VenueEvent::FeeAdjustment {
account_id,
venue_id,
order_id,
instrument,
fees,
..
} => ExecutionEvent::FeeAdjustment {
account_id,
venue_id,
order_id,
instrument,
fees,
},
VenueEvent::Cancelled {
client_order_id, ..
} => ExecutionEvent::Cancelled { client_order_id },
})
.collect()
}
fn venue_group_submission_to_application(
submission: qx_zhenlu::VenueOrderGroupSubmission,
) -> OrderGroupSubmission {
OrderGroupSubmission {
venue_group_id: submission.venue_group_id,
group_status: submission.group_status,
events: venue_events_to_application(submission.events),
}
}
impl<V: Venue> VenuePort for VenuePortAdapter<V> {
fn venue_id(&self) -> &str {
self.venue.id()
}
fn capabilities(&self) -> qx_zhenlu::venue_capabilities::VenueCapabilities {
*self.venue.capabilities()
}
fn execution_readiness(&self) -> Result<(), String> {
self.venue
.execution_readiness()
.map_err(|error| format!("Venue {} 尚未就绪: {error:?}", self.venue_id()))
}
fn validate_submit_order_group(
&mut self,
group: &qx_core::OrderGroupState,
) -> Result<(), String> {
self.venue
.validate_order_group(group)
.map_err(|error| format!("Venue 订单组预检失败: {error:?}"))
}
fn submit_order_group(
&mut self,
group: &qx_core::OrderGroupState,
ts: u64,
) -> Result<OrderGroupSubmission, String> {
self.venue
.submit_order_group(group, ts)
.map(venue_group_submission_to_application)
.map_err(|error| format!("Venue 订单组提交结果未知: {error:?}"))
}
fn submit_order(
&mut self,
order: qx_core::Order,
ts: u64,
) -> Result<Vec<ExecutionEvent>, String> {
self.validate_submit_capabilities(&order)?;
self.venue
.submit(order, ts)
.map(venue_events_to_application)
.map_err(|error| format!("Venue submit 失败: {error:?}"))
}
fn cancel_order(
&mut self,
client_order_id: u64,
ts: u64,
) -> Result<Vec<ExecutionEvent>, String> {
self.capabilities().validate_cancel()?;
self.venue
.cancel(client_order_id, ts)
.map(venue_events_to_application)
.map_err(|error| format!("Venue cancel 失败: {error:?}"))
}
fn validate_amend_order(
&mut self,
current: &qx_core::Order,
amended: &qx_core::Order,
) -> Result<(), String> {
self.venue
.validate_amendment(current, amended)
.map_err(|error| format!("Venue amend preflight 失败: {error:?}"))
}
fn amend_order(
&mut self,
current: &qx_core::Order,
amended: &qx_core::Order,
ts: u64,
) -> Result<Vec<ExecutionEvent>, String> {
self.venue
.amend(current, amended, ts)
.map(venue_events_to_application)
.map_err(|error| format!("Venue amend 结果未知: {error:?}"))
}
}
impl<V: Venue + ?Sized> VenuePort for BorrowedVenuePort<'_, V> {
fn venue_id(&self) -> &str {
self.venue.id()
}
fn capabilities(&self) -> qx_zhenlu::venue_capabilities::VenueCapabilities {
*self.venue.capabilities()
}
fn execution_readiness(&self) -> Result<(), String> {
self.venue
.execution_readiness()
.map_err(|error| format!("Venue {} 尚未就绪: {error:?}", self.venue_id()))
}
fn validate_submit_order_group(
&mut self,
group: &qx_core::OrderGroupState,
) -> Result<(), String> {
self.venue
.validate_order_group(group)
.map_err(|error| format!("Venue 订单组预检失败: {error:?}"))
}
fn submit_order_group(
&mut self,
group: &qx_core::OrderGroupState,
ts: u64,
) -> Result<OrderGroupSubmission, String> {
self.venue
.submit_order_group(group, ts)
.map(venue_group_submission_to_application)
.map_err(|error| format!("Venue 订单组提交结果未知: {error:?}"))
}
fn submit_order(
&mut self,
order: qx_core::Order,
ts: u64,
) -> Result<Vec<ExecutionEvent>, String> {
self.validate_submit_capabilities(&order)?;
self.venue
.submit(order, ts)
.map(venue_events_to_application)
.map_err(|error| format!("Venue submit 失败: {error:?}"))
}
fn cancel_order(
&mut self,
client_order_id: u64,
ts: u64,
) -> Result<Vec<ExecutionEvent>, String> {
self.capabilities().validate_cancel()?;
self.venue
.cancel(client_order_id, ts)
.map(venue_events_to_application)
.map_err(|error| format!("Venue cancel 失败: {error:?}"))
}
fn validate_amend_order(
&mut self,
current: &qx_core::Order,
amended: &qx_core::Order,
) -> Result<(), String> {
self.venue
.validate_amendment(current, amended)
.map_err(|error| format!("Venue amend preflight 失败: {error:?}"))
}
fn amend_order(
&mut self,
current: &qx_core::Order,
amended: &qx_core::Order,
ts: u64,
) -> Result<Vec<ExecutionEvent>, String> {
self.venue
.amend(current, amended, ts)
.map(venue_events_to_application)
.map_err(|error| format!("Venue amend 结果未知: {error:?}"))
}
}
impl<V: Venue + ?Sized> VenueRouterPort for BorrowedVenuePort<'_, V> {
fn capabilities_for(
&self,
venue_id: &str,
) -> Option<qx_zhenlu::venue_capabilities::VenueCapabilities> {
(self.accept_any_venue
|| venue_id.trim().is_empty()
|| venue_id.eq_ignore_ascii_case(self.venue_id()))
.then(|| self.capabilities())
}
fn execution_readiness_for(&self, venue_id: &str) -> Result<(), String> {
if self.accept_any_venue
|| venue_id.trim().is_empty()
|| venue_id.eq_ignore_ascii_case(self.venue_id())
{
VenuePort::execution_readiness(self)
} else {
Err(format!(
"Venue readiness 路由不匹配: requested={venue_id} configured={}",
self.venue_id()
))
}
}
fn submit_order(
&mut self,
venue_id: &str,
order: qx_core::Order,
ts: u64,
) -> Result<Vec<ExecutionEvent>, String> {
if self.accept_any_venue
|| venue_id.trim().is_empty()
|| venue_id.eq_ignore_ascii_case(self.venue_id())
{
VenuePort::submit_order(self, order, ts)
} else {
Err(format!(
"Venue 路由不匹配: requested={venue_id} configured={}",
self.venue_id()
))
}
}
fn cancel_order(
&mut self,
venue_id: &str,
client_order_id: u64,
ts: u64,
) -> Result<Vec<ExecutionEvent>, String> {
if self.accept_any_venue
|| venue_id.trim().is_empty()
|| venue_id.eq_ignore_ascii_case(self.venue_id())
{
VenuePort::cancel_order(self, client_order_id, ts)
} else {
Err(format!(
"Venue 路由不匹配: requested={venue_id} configured={}",
self.venue_id()
))
}
}
}
impl<V: Venue> VenueRouterPort for VenuePortAdapter<V> {
fn capabilities_for(
&self,
venue_id: &str,
) -> Option<qx_zhenlu::venue_capabilities::VenueCapabilities> {
(self.accept_any_venue
|| venue_id.trim().is_empty()
|| venue_id.eq_ignore_ascii_case(self.venue_id()))
.then(|| self.capabilities())
}
fn execution_readiness_for(&self, venue_id: &str) -> Result<(), String> {
if self.accept_any_venue
|| venue_id.trim().is_empty()
|| venue_id.eq_ignore_ascii_case(self.venue_id())
{
VenuePort::execution_readiness(self)
} else {
Err(format!(
"Venue readiness 路由不匹配: requested={venue_id} configured={}",
self.venue_id()
))
}
}
fn submit_order(
&mut self,
venue_id: &str,
order: qx_core::Order,
ts: u64,
) -> Result<Vec<ExecutionEvent>, String> {
if self.accept_any_venue
|| venue_id.trim().is_empty()
|| venue_id.eq_ignore_ascii_case(self.venue_id())
{
VenuePort::submit_order(self, order, ts)
} else {
Err(format!(
"Venue 路由不匹配: requested={venue_id} configured={}",
self.venue_id()
))
}
}
fn cancel_order(
&mut self,
venue_id: &str,
client_order_id: u64,
ts: u64,
) -> Result<Vec<ExecutionEvent>, String> {
if self.accept_any_venue
|| venue_id.trim().is_empty()
|| venue_id.eq_ignore_ascii_case(self.venue_id())
{
VenuePort::cancel_order(self, client_order_id, ts)
} else {
Err(format!(
"Venue 路由不匹配: requested={venue_id} configured={}",
self.venue_id()
))
}
}
}
impl RiskPort for RiskContextPort<'_> {
fn evaluate_order(&self, order: &qx_core::Order) -> Result<RiskVerdict, String> {
let decision = self.risk.evaluate_order(order, self.position);
if decision.allowed {
Ok(RiskVerdict::Allow)
} else {
Ok(RiskVerdict::Reject {
reason: format!(
"RiskContext {}: {}",
decision.rule_set_version,
decision.violations.join("; ")
),
})
}
}
}
impl RiskPort for CanonicalRiskPort<'_> {
fn evaluate_order(&self, order: &qx_core::Order) -> Result<RiskVerdict, String> {
let decision = qx_risk::RiskEngine::evaluate_order(self.context, order);
if decision.allowed {
Ok(RiskVerdict::Allow)
} else {
Ok(RiskVerdict::Reject {
reason: format!(
"CanonicalRiskEngine {}: {}",
decision.rule_set_version,
decision.violations.join("; ")
),
})
}
}
}
pub struct PortExecutionService<'a, V: VenuePort, P: ExecutionEventPort> {
venue: &'a mut V,
events: &'a mut P,
worker_id: &'a str,
now: u64,
source_seq: &'a mut u64,
registration_correlation: Option<String>,
cancel_operation_id: Option<String>,
instrument_spec: Option<TradingInstrumentSpec>,
spread_store: Option<&'a dyn SpreadOrderGroupStore>,
order_group_store: Option<&'a mut dyn OrderGroupStore>,
submission_group_id: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PortExecutionResult {
pub message: String,
pub event_count: usize,
}
pub type ExecutionGateway<'a, V, P> = PortExecutionService<'a, V, P>;
impl<'a, V: VenuePort, P: ExecutionEventPort> PortExecutionService<'a, V, P> {
pub fn new(
venue: &'a mut V,
events: &'a mut P,
worker_id: &'a str,
now: u64,
source_seq: &'a mut u64,
) -> Self {
Self {
venue,
events,
worker_id,
now,
source_seq,
registration_correlation: None,
cancel_operation_id: None,
instrument_spec: None,
spread_store: None,
order_group_store: None,
submission_group_id: None,
}
}
pub fn with_spread_group_store(mut self, store: &'a dyn SpreadOrderGroupStore) -> Self {
self.spread_store = Some(store);
self
}
pub fn with_order_group_store(mut self, store: &'a mut dyn OrderGroupStore) -> Self {
self.order_group_store = Some(store);
self
}
pub fn with_instrument_spec(mut self, spec: TradingInstrumentSpec) -> Self {
self.instrument_spec = Some(spec);
self
}
pub fn submit_command(
&mut self,
command: &ControlCommand,
) -> Result<PortExecutionResult, String> {
spread_group_barrier(self.spread_store, command)?;
let order = order_from_submit_command(command)
.map_err(|error| format!("SubmitOrder 订单载荷非法: {error:?}"))?;
self.registration_correlation = Some(format!("control:{}", command.command_id));
self.submission_group_id = command.payload.get("spread_group_id").cloned();
let result = self.submit(order);
self.registration_correlation = None;
self.submission_group_id = None;
result
}
pub fn submit_command_with_risk<R: RiskPort>(
&mut self,
command: &ControlCommand,
risk: &R,
) -> Result<PortExecutionResult, String> {
spread_group_barrier(self.spread_store, command)?;
let order = order_from_submit_command(command)
.map_err(|error| format!("SubmitOrder 订单载荷非法: {error:?}"))?;
self.registration_correlation = Some(format!("control:{}", command.command_id));
self.submission_group_id = command.payload.get("spread_group_id").cloned();
let result = self.submit_with_risk(order, risk);
self.registration_correlation = None;
self.submission_group_id = None;
result
}
pub fn submit(&mut self, order: qx_core::Order) -> Result<PortExecutionResult, String> {
self.validate_order(&order)?;
if self.events.orders().iter().any(|existing| {
existing.account_id == order.account_id
&& self.events.has_pending_amendment(existing.client_id)
}) {
return Err("账户存在改单结果未知的订单,必须先对账,禁止新增订单".into());
}
self.venue.validate_submit_capabilities(&order)?;
let existing_order = self
.events
.orders()
.into_iter()
.find(|existing| existing.client_id == order.client_id);
if let Some(existing) = existing_order.as_ref() {
if !existing.same_submission_identity(&order) {
return Err(format!(
"client_order_id {} 已绑定到不同的账户、标的、方向、数量或执行策略,拒绝复用",
order.client_id
));
}
if existing.status.is_terminal()
|| matches!(
existing.status,
qx_core::OrderStatus::Accepted
| qx_core::OrderStatus::Working
| qx_core::OrderStatus::PartiallyFilled
| qx_core::OrderStatus::Filled
)
{
return Ok(PortExecutionResult {
message: format!(
"ALREADY_APPLIED_FROM_EVENT_LOG status={:?}",
existing.status
),
event_count: 0,
});
}
if matches!(
existing.status,
qx_core::OrderStatus::Submitted
| qx_core::OrderStatus::PendingSubmit
| qx_core::OrderStatus::CancelPending
| qx_core::OrderStatus::Unknown
) {
self.append(
ExecutionEvent::ReconcileRequired {
client_order_id: existing.client_id,
},
format!("{}:ambiguous-submit:{}", self.worker_id, existing.client_id),
)?;
return Err(format!(
"订单 {} 状态为 {:?},先决条件不满足:已有远端事实但未确认,或撤单在途,必须先对账,禁止自动补单",
existing.client_id, existing.status
));
}
}
if existing_order.is_none() {
self.venue.execution_readiness()?;
if let Some(group_id) = self.submission_group_id.as_deref() {
let store = self.spread_store.ok_or_else(|| {
format!(
"FAIL_CLOSED: 多腿订单组 {group_id} 未绑定组状态存储,拒绝提交订单 {}",
order.client_id
)
})?;
store.begin_leg_submission(group_id, order.client_id)?;
}
self.events.register_order(
order.clone(),
self.now,
Some(
self.registration_correlation.clone().unwrap_or_else(|| {
format!("{}:submit:{}", self.worker_id, order.client_id)
}),
),
)?;
}
let registered = self
.events
.orders()
.into_iter()
.find(|existing| existing.client_id == order.client_id)
.ok_or_else(|| "订单注册后无法读取订单".to_string())?;
let facts = match self.venue.submit_order(registered, self.now) {
Ok(facts) if facts.is_empty() => {
self.mark_reconcile(order.client_id, "empty-venue-response")?;
return Err("Venue 未返回任何订单事实,结果未知,必须先对账".into());
}
Ok(facts) => facts,
Err(error) => {
self.mark_reconcile(order.client_id, "submit-error")?;
return Err(format!(
"Venue submit 失败/结果未知: {error};订单已标记为待对账"
));
}
};
if let Err(violation) = gate_submit_facts(&order, &facts, self.instrument_spec.as_ref()) {
self.mark_reconcile(order.client_id, violation.reason_tag())?;
return Err(violation.to_string());
}
let event_count = facts.len();
let has_reconcile = facts
.iter()
.any(|fact| matches!(fact, ExecutionEvent::ReconcileRequired { .. }));
let venue_prefix = match self
.registration_correlation
.as_deref()
.filter(|correlation_id| !correlation_id.trim().is_empty())
{
Some(correlation_id) => format!(
"{correlation_id}:venue:{}:{}",
self.venue.venue_id(),
order.client_id
),
None => format!(
"{}:venue:{}:{}",
self.worker_id,
self.venue.venue_id(),
order.client_id
),
};
for fact in facts {
let correlation_id = fact_correlation(&venue_prefix, &fact);
self.append(fact, correlation_id)?;
}
if has_reconcile {
return Err("Venue 返回待对账事实,禁止继续自动提交".into());
}
Ok(PortExecutionResult {
message: format!("SUBMITTED venue_events={event_count}"),
event_count,
})
}
pub fn submit_with_risk<R: RiskPort>(
&mut self,
order: qx_core::Order,
risk: &R,
) -> Result<PortExecutionResult, String> {
self.validate_order(&order)?;
match risk
.evaluate_order(&order)
.map_err(|error| format!("账户级 RiskPort 执行失败: {error}"))?
{
RiskVerdict::Allow => {}
RiskVerdict::Reject { reason } => {
return Err(format!("账户级 RiskPort 拒绝订单: {reason}"))
}
}
self.submit(order)
}
pub fn submit_order_group<R: RiskPort>(
&mut self,
intent: qx_core::OrderGroupIntent,
risk: &R,
) -> Result<PortExecutionResult, String> {
let group = qx_core::OrderGroupState::from_intent(intent)?;
self.venue.validate_submit_order_group(&group)?;
for leg in &group.legs {
self.validate_order(&leg.order)?;
match risk
.evaluate_order(&leg.order)
.map_err(|error| format!("订单组账户级风控执行失败: {error}"))?
{
RiskVerdict::Allow => {}
RiskVerdict::Reject { reason } => {
return Err(format!(
"订单组 {:?} 子腿 {} 被账户级风控拒绝: {reason}",
leg.role, leg.order.client_id
));
}
}
}
self.venue.execution_readiness()?;
if self.order_group_store.is_none() {
return Err("FAIL_CLOSED: 原生订单组提交路径未注入订单组持久化存储".into());
}
let existing_orders = self.events.orders();
if let Some(existing) = group.legs.iter().find_map(|leg| {
existing_orders
.iter()
.find(|order| order.client_id == leg.order.client_id)
}) {
return Err(format!(
"订单组子腿 {} 已在 OMS 中,必须先恢复或对账,禁止重放组提交",
existing.client_id
));
}
{
let store = self
.order_group_store
.as_deref_mut()
.ok_or_else(|| "订单组存储不可用".to_string())?;
store.create(&group)?;
if !store.begin_submission(&group.intent.group_id)? {
return Err(format!(
"订单组 {} 已启动过提交或需对账,禁止重放 Venue 副作用",
group.intent.group_id
));
}
}
for leg in &group.legs {
if let Err(error) = self.events.register_order(
leg.order.clone(),
self.now,
Some(format!(
"{}:group:{}:register:{}",
self.worker_id, group.intent.group_id, leg.order.client_id
)),
) {
if let Some(store) = self.order_group_store.as_deref_mut() {
let _ = store.require_reconciliation(&group.intent.group_id);
}
return Err(format!(
"订单组 {} 子腿 {} 注册失败,订单组已围栏待对账: {error}",
group.intent.group_id, leg.order.client_id
));
}
}
{
let orders = self.events.orders();
let store = self
.order_group_store
.as_deref_mut()
.ok_or_else(|| "订单组存储不可用".to_string())?;
for leg in &group.legs {
let snapshot = orders
.iter()
.find(|order| order.client_id == leg.order.client_id)
.ok_or_else(|| {
format!("已注册订单组子腿 {} 无法从 OMS 读取", leg.order.client_id)
})?;
store.apply_order_snapshot(&group.intent.group_id, snapshot, None)?;
}
}
let submission = match self.venue.submit_order_group(&group, self.now) {
Ok(submission) => submission,
Err(error) => {
self.mark_group_reconcile(&group, "group-submit-unknown")?;
return Err(format!("Venue 订单组提交结果未知,已围栏待对账: {error}"));
}
};
if let Some(venue_group_id) = submission.venue_group_id {
let result = self
.order_group_store
.as_deref_mut()
.ok_or_else(|| "订单组存储不可用".to_string())?
.record_venue_group_id(&group.intent.group_id, venue_group_id);
if let Err(error) = result {
self.mark_group_reconcile(&group, "venue-group-id-conflict")?;
return Err(format!("交易所订单组 ID 无法持久化,已围栏待对账: {error}"));
}
}
let group_status = submission.group_status;
let facts = submission.events;
if let Some(group_status) = group_status {
if group_status == qx_core::OrderGroupStatus::Active && facts.is_empty() {
self.order_group_store
.as_deref_mut()
.ok_or_else(|| "订单组存储不可用".to_string())?
.activate_native_group(&group.intent.group_id)?;
return Ok(PortExecutionResult {
message: format!(
"ORDER_GROUP_ACTIVE group_id={} venue_resource=accepted_without_child_orders",
group.intent.group_id
),
event_count: 0,
});
}
self.mark_group_reconcile(&group, "native-group-state-requires-reconcile")?;
return Err(format!(
"Venue 原生订单组状态 {:?} 尚不能安全归约到子订单,已围栏待对账",
group_status
));
}
let mut per_leg = Vec::with_capacity(group.legs.len());
for leg in &group.legs {
let leg_facts: Vec<_> = facts
.iter()
.filter(|fact| execution_event_client_order_id(fact) == Some(leg.order.client_id))
.cloned()
.collect();
if let Err(error) = validate_submit_events(&leg.order, &leg_facts) {
self.mark_group_reconcile(&group, "invalid-group-facts")?;
return Err(format!(
"Venue 订单组子腿 {} 回报无效,订单组已围栏待对账: {error}",
leg.order.client_id
));
}
if let Err(error) =
gate_submit_facts(&leg.order, &leg_facts, self.instrument_spec.as_ref())
{
self.mark_group_reconcile(&group, error.reason_tag())?;
return Err(error.to_string());
}
per_leg.push((leg.order.client_id, leg_facts));
}
if facts
.iter()
.any(|fact| execution_event_client_order_id(fact).is_none())
{
self.mark_group_reconcile(&group, "unattributed-group-fact")?;
return Err("Venue 订单组返回无法归属子腿的事实,已围栏待对账".into());
}
let event_count = facts.len();
let mut has_reconcile = false;
for (client_order_id, leg_facts) in per_leg {
let venue_prefix = format!(
"{}:venue:{}:{}:group:{}",
self.worker_id,
self.venue.venue_id(),
client_order_id,
group.intent.group_id
);
for fact in leg_facts {
has_reconcile |= matches!(fact, ExecutionEvent::ReconcileRequired { .. });
let correlation_id = fact_correlation(&venue_prefix, &fact);
self.append(fact, correlation_id)?;
}
}
let orders = self.events.orders();
{
let store = self
.order_group_store
.as_deref_mut()
.ok_or_else(|| "订单组存储不可用".to_string())?;
if has_reconcile {
store.require_reconciliation(&group.intent.group_id)?;
}
for leg in &group.legs {
let snapshot = orders
.iter()
.find(|order| order.client_id == leg.order.client_id)
.ok_or_else(|| {
format!("订单组子腿 {} 回报后无法从 OMS 读取", leg.order.client_id)
})?;
let venue_order_id = facts.iter().find_map(|fact| match fact {
ExecutionEvent::Accepted {
client_order_id,
venue_order_id,
} if *client_order_id == leg.order.client_id => Some(venue_order_id.clone()),
_ => None,
});
if has_reconcile {
store.record_order_snapshot_during_reconciliation(
&group.intent.group_id,
snapshot,
venue_order_id,
)?;
} else if store.load(&group.intent.group_id)?.is_some_and(|current| {
current.status == qx_core::OrderGroupStatus::ReconcileRequired
}) {
store.reconcile_order_snapshot(
&group.intent.group_id,
snapshot,
venue_order_id,
)?;
} else {
store.apply_order_snapshot(&group.intent.group_id, snapshot, venue_order_id)?;
}
}
}
if has_reconcile {
return Err("Venue 订单组回报要求对账,已禁止继续自动执行".into());
}
Ok(PortExecutionResult {
message: format!(
"ORDER_GROUP_SUBMITTED group_id={} venue_events={event_count}",
group.intent.group_id
),
event_count,
})
}
fn mark_group_reconcile(
&mut self,
group: &qx_core::OrderGroupState,
reason: &str,
) -> Result<(), String> {
for leg in &group.legs {
self.mark_reconcile(leg.order.client_id, reason)?;
}
let orders = self.events.orders();
let store = self
.order_group_store
.as_deref_mut()
.ok_or_else(|| "订单组存储不可用".to_string())?;
store.require_reconciliation(&group.intent.group_id)?;
for leg in &group.legs {
let snapshot = orders
.iter()
.find(|order| order.client_id == leg.order.client_id)
.ok_or_else(|| {
format!("待对账订单组子腿 {} 无法从 OMS 读取", leg.order.client_id)
})?;
store.record_order_snapshot_during_reconciliation(
&group.intent.group_id,
snapshot,
None,
)?;
}
Ok(())
}
pub fn validate_order(&self, order: &qx_core::Order) -> Result<(), String> {
order
.validate()
.map_err(|error| format!("订单校验失败: {error}"))?;
if let Some(spec) = &self.instrument_spec {
if order.instrument != spec.instrument {
return Err(format!(
"订单 market spec 标的与订单不匹配: order={} spec={}",
order.instrument, spec.instrument
));
}
if let Some(amounts) = order.exact_amounts {
spec.validate_exact_order_precision(amounts.quantity, amounts.limit_price)
.map_err(|error| format!("精确订单不满足 market spec: {error}"))?;
} else {
spec.validate_order(order.qty.raw(), order.limit.map(|price| price.raw()))
.map_err(|error| format!("订单不满足 market spec: {error}"))?;
}
}
Ok(())
}
pub fn submit_with_risk_and_correlation<R: RiskPort>(
&mut self,
order: qx_core::Order,
risk: &R,
correlation_id: impl Into<String>,
) -> Result<PortExecutionResult, String> {
self.registration_correlation = Some(correlation_id.into());
let result = self.submit_with_risk(order, risk);
self.registration_correlation = None;
result
}
pub fn order(&self, client_order_id: u64) -> Option<qx_core::Order> {
self.events
.orders()
.into_iter()
.find(|order| order.client_id == client_order_id)
}
pub fn cancel(&mut self, client_order_id: u64) -> Result<PortExecutionResult, String> {
self.venue.capabilities().validate_cancel()?;
if self.events.has_pending_amendment(client_order_id) {
return Err(format!(
"订单 {client_order_id} 存在改单结果未知的操作,必须先对账后才能撤单"
));
}
let order = self
.order(client_order_id)
.ok_or_else(|| format!("订单不存在: {client_order_id}"))?;
match order.status {
qx_core::OrderStatus::Cancelled => {
return Ok(PortExecutionResult {
message: "ALREADY_CANCELLED".into(),
event_count: 0,
});
}
qx_core::OrderStatus::Filled
| qx_core::OrderStatus::Rejected
| qx_core::OrderStatus::Expired => {
return Err(format!(
"订单 {} 已处于终态 {:?},不能撤销",
client_order_id, order.status
));
}
qx_core::OrderStatus::PendingSubmit => {
return Err(format!(
"订单 {} 尚未提交到交易所,不能撤销",
client_order_id
));
}
qx_core::OrderStatus::CancelPending => {
return Err(format!("订单 {} 已有撤单在途,必须先对账", client_order_id));
}
qx_core::OrderStatus::Unknown => {
return Err(format!(
"订单 {} 状态未知,必须先对账后才能撤单",
client_order_id
));
}
qx_core::OrderStatus::Submitted
| qx_core::OrderStatus::Accepted
| qx_core::OrderStatus::Working
| qx_core::OrderStatus::PartiallyFilled => {}
}
self.venue.execution_readiness()?;
let operation_id = self.cancel_operation_id.clone().unwrap_or_else(|| {
format!("{}:cancel:{}:{}", self.worker_id, client_order_id, self.now)
});
let append_outcome = self.append_with_outcome(
ExecutionEvent::CancelPending { client_order_id },
format!("{operation_id}:pending:{client_order_id}"),
)?;
if append_outcome == EventAppendOutcome::Duplicate {
return Err(format!(
"订单 {} 的撤单意图已由同一操作持久化,禁止重复调用 Venue;必须先对账",
client_order_id
));
}
let facts = match self.venue.cancel_order(client_order_id, self.now) {
Ok(facts) if facts.is_empty() => {
self.mark_reconcile_for_operation(
client_order_id,
"empty-cancel-response",
&operation_id,
)?;
return Err("Venue 未返回取消事实,结果未知,必须先对账".into());
}
Ok(facts) => facts,
Err(error) => {
self.mark_reconcile_for_operation(client_order_id, "cancel-error", &operation_id)?;
return Err(format!(
"Venue cancel 失败/结果未知: {error};订单已标记为待对账"
));
}
};
if let Err(error) = validate_cancel_events(client_order_id, &facts) {
self.mark_reconcile_for_operation(
client_order_id,
"invalid-cancel-response",
&operation_id,
)?;
return Err(format!(
"Venue 返回非法撤单事实,结果未知,必须先对账: {error}"
));
}
let event_count = facts.len();
let has_cancelled = facts.iter().any(|fact| {
matches!(
fact,
ExecutionEvent::Cancelled {
client_order_id: id
} if *id == client_order_id
)
});
let has_reconcile = facts
.iter()
.any(|fact| matches!(fact, ExecutionEvent::ReconcileRequired { .. }));
for fact in facts {
let prefix = format!("{operation_id}:response:{client_order_id}");
let correlation = fact_correlation(&prefix, &fact);
self.append(fact, correlation)?;
}
if has_reconcile {
return Err("Venue 返回撤单待对账事实,禁止重复撤单".into());
}
if !has_cancelled {
return Err(format!(
"订单 {client_order_id} 在撤单竞态中已全部成交,成交事实已入账"
));
}
Ok(PortExecutionResult {
message: format!("CANCELLED venue_events={event_count}"),
event_count,
})
}
pub fn cancel_command(
&mut self,
command: &ControlCommand,
) -> Result<PortExecutionResult, String> {
let target = cancel_order_target_from_command(command)
.map_err(|error| format!("CancelOrder 命令无效: {error:?}"))?;
let order = self
.order(target.client_order_id)
.ok_or_else(|| format!("订单不存在: {}", target.client_order_id))?;
if order.account_id != target.account_id
|| !order
.instrument
.venue
.as_str()
.eq_ignore_ascii_case(&target.venue_id)
|| !self.venue.venue_id().eq_ignore_ascii_case(&target.venue_id)
{
return Err("CancelOrder 路由账户/交易所与 OMS 订单或执行 Venue 不一致".into());
}
self.cancel_operation_id = Some(format!(
"control:cancel:{}:{}",
command.command_id, target.client_order_id
));
let result = self.cancel(target.client_order_id);
self.cancel_operation_id = None;
result
}
pub fn amend_command(
&mut self,
command: &ControlCommand,
) -> Result<PortExecutionResult, String> {
let target = qx_control::amend_order_target_from_command(command)
.map_err(|error| format!("AmendOrder 命令无效: {error:?}"))?;
let current = self
.order(target.client_order_id)
.ok_or_else(|| format!("订单不存在: {}", target.client_order_id))?;
if current.account_id != target.account_id
|| !current
.instrument
.venue
.as_str()
.eq_ignore_ascii_case(&target.venue_id)
|| !self.venue.venue_id().eq_ignore_ascii_case(&target.venue_id)
{
return Err("AmendOrder 路由账户/交易所与 OMS 订单或执行 Venue 不一致".into());
}
if self.events.has_pending_amendment(current.client_id) {
return Err(format!(
"订单 {} 存在改单结果未知的操作,禁止重发;必须先对账",
current.client_id
));
}
if current.filled != target.order.filled
|| current.status != target.order.status
|| current.instrument != target.order.instrument
|| current.side != target.order.side
|| current.account_id != target.order.account_id
|| current.trace != target.order.trace
|| current.policy != target.order.policy
{
return Err("AmendOrder 请求基于过期或身份不一致的订单快照".into());
}
self.venue.validate_amend_order(¤t, &target.order)?;
self.events.refresh_order_state()?;
let refreshed = self
.order(target.client_order_id)
.ok_or_else(|| format!("订单在改单预检期间从 OMS 消失: {}", target.client_order_id))?;
if refreshed != current {
return Err(format!(
"订单 {} 在改单预检期间出现新成交或状态变化,拒绝基于旧快照改单",
target.client_order_id
));
}
let operation_id = format!(
"control:amend:{}:{}",
command.command_id, target.client_order_id
);
let pending = self.append_with_outcome(
ExecutionEvent::OrderAmendmentPending {
operation_id: operation_id.clone(),
order: Box::new(target.order.clone()),
},
format!("{operation_id}:pending:{}", target.client_order_id),
)?;
if pending == EventAppendOutcome::Duplicate {
return Err(format!(
"订单 {} 的改单意图已持久化,禁止重复触达 Venue;必须先对账",
target.client_order_id
));
}
let facts = match self.venue.amend_order(¤t, &target.order, self.now) {
Ok(facts) => facts,
Err(error) => {
self.mark_reconcile_for_operation(
target.client_order_id,
"amend-error",
&operation_id,
)?;
return Err(format!(
"Venue amend 失败/结果未知: {error};订单已标记为待对账"
));
}
};
if facts.iter().any(|fact| match fact {
ExecutionEvent::Fill(fill) => fill.order_id != target.client_order_id,
ExecutionEvent::FillWithSpec { fill, .. } => fill.order_id != target.client_order_id,
ExecutionEvent::FeeAdjustment { order_id, .. } => *order_id != target.client_order_id,
ExecutionEvent::Cancelled { client_order_id }
| ExecutionEvent::CancelPending { client_order_id }
| ExecutionEvent::ReconcileRequired { client_order_id } => {
*client_order_id != target.client_order_id
}
ExecutionEvent::Accepted { .. }
| ExecutionEvent::Rejected { .. }
| ExecutionEvent::OrderAmendmentPending { .. }
| ExecutionEvent::OrderAmended { .. }
| ExecutionEvent::MarketQuote { .. } => true,
}) {
self.mark_reconcile_for_operation(
target.client_order_id,
"invalid-amend-response",
&operation_id,
)?;
return Err("Venue amend 返回了非法或其他订单的事实,结果未知,必须先对账".into());
}
self.append(
ExecutionEvent::OrderAmended {
operation_id: operation_id.clone(),
order: Box::new(target.order.clone()),
},
format!("{operation_id}:confirmed:{}", target.client_order_id),
)?;
let fact_count = facts.len();
let has_reconcile = facts
.iter()
.any(|fact| matches!(fact, ExecutionEvent::ReconcileRequired { .. }));
for fact in facts {
let correlation = fact_correlation(
&format!("{operation_id}:response:{}", target.client_order_id),
&fact,
);
self.append(fact, correlation)?;
}
if has_reconcile {
return Err("改单已确认但成交回报要求对账,账户进入待对账状态".into());
}
Ok(PortExecutionResult {
message: format!("AMENDED venue_events={fact_count}"),
event_count: fact_count + 1,
})
}
fn mark_reconcile(&mut self, client_order_id: u64, reason: &str) -> Result<(), String> {
self.append(
ExecutionEvent::ReconcileRequired { client_order_id },
format!("{}:{reason}:{client_order_id}", self.worker_id),
)
}
fn mark_reconcile_for_operation(
&mut self,
client_order_id: u64,
reason: &str,
operation_id: &str,
) -> Result<(), String> {
self.append(
ExecutionEvent::ReconcileRequired { client_order_id },
format!("{operation_id}:reconcile:{reason}:{client_order_id}"),
)
}
fn append(&mut self, event: ExecutionEvent, correlation_id: String) -> Result<(), String> {
self.append_with_outcome(event, correlation_id).map(|_| ())
}
fn append_with_outcome(
&mut self,
event: ExecutionEvent,
correlation_id: String,
) -> Result<EventAppendOutcome, String> {
*self.source_seq = self.source_seq.saturating_add(1);
let event_ts = match &event {
ExecutionEvent::Fill(fill) | ExecutionEvent::FillWithSpec { fill, .. } => fill.ts,
_ => self.now,
};
let event = match (self.instrument_spec.as_ref(), event) {
(Some(spec), ExecutionEvent::Fill(fill)) => ExecutionEvent::FillWithSpec {
fill,
spec: Box::new(spec.clone()),
},
(_, event) => event,
};
self.events
.append_execution_event_with_outcome(ExecutionEventEnvelope {
event,
event_ts,
receive_ts: self.now,
source_seq: *self.source_seq,
correlation_id,
})
}
}
#[derive(Clone, Debug)]
pub struct HedgeRecoveryOutcome {
pub group: SpreadOrderGroup,
pub attempted: usize,
pub completed: bool,
pub errors: Vec<String>,
}
const HEDGE_RECOVERY_LEASE_MS: u64 = 30_000;
pub trait HedgeOrderValidator {
fn validate(&self, order: &Order) -> Result<(), String>;
fn instrument_spec(&self, _order: &Order) -> Result<Option<TradingInstrumentSpec>, String> {
Ok(None)
}
}
impl<F> HedgeOrderValidator for F
where
F: Fn(&Order) -> Result<(), String>,
{
fn validate(&self, order: &Order) -> Result<(), String> {
self(order)
}
}
pub struct HedgeRecoveryWorker<'a, R: VenueRouterPort, P: ExecutionEventPort> {
router: &'a mut R,
events: &'a mut P,
store: &'a mut dyn SpreadOrderGroupStore,
group: SpreadOrderGroup,
worker_id: &'a str,
claim_owner: String,
now: u64,
source_seq: &'a mut u64,
account_venue_scope: Option<(Option<String>, Option<String>)>,
}
const MAX_HEDGE_ATTEMPTS: u32 = 3;
impl<'a, R: VenueRouterPort, P: ExecutionEventPort> HedgeRecoveryWorker<'a, R, P> {
pub fn new(
group: SpreadOrderGroup,
store: &'a mut dyn SpreadOrderGroupStore,
router: &'a mut R,
events: &'a mut P,
worker_id: &'a str,
now: u64,
source_seq: &'a mut u64,
) -> qx_core::QxResult<Self> {
group.validate_persisted()?;
Ok(Self {
router,
events,
store,
group,
worker_id,
claim_owner: format!("{}:{}:{}", worker_id, std::process::id(), now),
now,
source_seq,
account_venue_scope: None,
})
}
pub fn for_account_venue(mut self, account_id: &str, venue_id: &str) -> Result<Self, String> {
let account_id = account_id.trim();
let venue_id = venue_id.trim();
if account_id.is_empty() || venue_id.is_empty() {
return Err("实盘多腿恢复必须绑定 account_id 与 venue_id".into());
}
self.account_venue_scope = Some((
Some(account_id.to_string()),
Some(venue_id.to_ascii_uppercase()),
));
Ok(self)
}
pub fn for_account(mut self, account_id: &str) -> Result<Self, String> {
let account_id = account_id.trim();
if account_id.is_empty() {
return Err("实盘多腿恢复必须绑定 account_id".into());
}
self.account_venue_scope = Some((Some(account_id.to_string()), None));
Ok(self)
}
fn load_group(&mut self) -> Result<(), String> {
if let Some(saved) = self.store.load(&self.group.group_id)? {
if saved.strategy_id != self.group.strategy_id
|| saved.legs.len() != self.group.legs.len()
|| saved
.legs
.iter()
.zip(&self.group.legs)
.any(|(left, right)| {
left.leg_id != right.leg_id
|| left.venue_id != right.venue_id
|| left.order.account_id != right.order.account_id
|| left.order.instrument != right.order.instrument
})
{
return Err("Hedge 恢复快照与请求的策略、腿或 Venue 路由不一致".into());
}
self.group = saved;
}
Ok(())
}
pub fn execute(self) -> Result<HedgeRecoveryOutcome, String> {
self.execute_with_validator(None)
}
pub fn execute_with_validator(
mut self,
validator: Option<&dyn HedgeOrderValidator>,
) -> Result<HedgeRecoveryOutcome, String> {
self.load_group()?;
match self.group.status {
SpreadOrderGroupStatus::HedgeRequired => {}
SpreadOrderGroupStatus::Hedged => {
return Ok(HedgeRecoveryOutcome {
group: self.group,
attempted: 0,
completed: true,
errors: Vec::new(),
});
}
SpreadOrderGroupStatus::ReconcileRequired => {
return Ok(HedgeRecoveryOutcome {
group: self.group,
attempted: 0,
completed: false,
errors: vec![
"原始多腿订单组存在未知 Venue 状态,必须先完成对账,禁止自动对冲".into(),
],
});
}
status => {
return Ok(HedgeRecoveryOutcome {
group: self.group,
attempted: 0,
completed: false,
errors: vec![format!(
"订单组当前为 {status:?},只有 HedgeRequired 才能执行恢复"
)],
});
}
}
let mut targets = self.group.compensation_targets();
if let Some((account_id, venue_id)) = &self.account_venue_scope {
targets.retain(|target| {
account_id
.as_deref()
.is_none_or(|account_id| target.account_id == account_id)
&& venue_id
.as_deref()
.is_none_or(|venue_id| target.venue_id.eq_ignore_ascii_case(venue_id))
});
}
if targets.is_empty() {
if self.group.all_confirmed_exposure_hedged() {
let Some(claim_token) = self.store.try_claim(
&self.group.group_id,
&self.claim_owner,
self.now,
HEDGE_RECOVERY_LEASE_MS,
)?
else {
return Ok(HedgeRecoveryOutcome {
group: self.group,
attempted: 0,
completed: false,
errors: vec!["订单组正在由其他恢复 owner 处理,跳过本轮".into()],
});
};
if !self.store.verify_claim(
&self.group.group_id,
&self.claim_owner,
claim_token,
self.now,
)? {
return Ok(HedgeRecoveryOutcome {
group: self.group,
attempted: 0,
completed: false,
errors: vec!["多腿恢复 claim 已过期或已被其他 owner 接管".into()],
});
}
self.group
.mark_hedged()
.map_err(|error| format!("标记多腿订单组已对冲失败: {error:?}"))?;
self.store
.save_claimed(&self.group, &self.claim_owner, claim_token, self.now)?;
self.store
.release_claim(&self.group.group_id, &self.claim_owner, claim_token)?;
return Ok(HedgeRecoveryOutcome {
group: self.group,
attempted: 0,
completed: true,
errors: Vec::new(),
});
}
return Ok(HedgeRecoveryOutcome {
group: self.group,
attempted: 0,
completed: false,
errors: Vec::new(),
});
}
let Some(claim_token) = self.store.try_claim(
&self.group.group_id,
&self.claim_owner,
self.now,
HEDGE_RECOVERY_LEASE_MS,
)?
else {
return Ok(HedgeRecoveryOutcome {
group: self.group,
attempted: 0,
completed: false,
errors: vec!["订单组正在由其他恢复 owner 处理,跳过本轮".into()],
});
};
let mut attempted = 0;
let mut errors = Vec::new();
let mut all_filled = true;
for target in targets {
if !self.store.verify_claim(
&self.group.group_id,
&self.claim_owner,
claim_token,
self.now,
)? {
errors.push("多腿恢复 claim 已过期或已被其他 owner 接管".into());
all_filled = false;
break;
}
let source_leg = self
.group
.leg(&target.leg_id)
.map_err(|error| format!("读取补偿来源腿失败: {error:?}"))?;
if source_leg.venue_id.trim().is_empty() {
errors.push(format!(
"补偿腿 {} 缺少 Venue 路由,拒绝使用默认交易所",
target.leg_id
));
all_filled = false;
continue;
}
let client_id = compensation_client_id(&self.group, &target.leg_id, target.attempt);
let existing = self
.events
.orders()
.into_iter()
.find(|order| order.client_id == client_id);
let order = build_compensation_order(&self.group, source_leg, &target, client_id)?;
if let Some(existing) = existing {
if !same_compensation_order(&existing, &order) {
errors.push(format!(
"补偿 client_order_id={} 已被其他订单占用,拒绝覆盖",
client_id
));
self.group.require_reconciliation();
all_filled = false;
continue;
}
if existing.status == OrderStatus::Filled {
if !existing.filled_decimal().is_positive() {
errors.push(format!(
"补偿订单 {} 报告 Filled 但成交数量为零,必须对账",
client_id
));
self.group.require_reconciliation();
all_filled = false;
continue;
}
self.group
.record_compensation_fill_exact(&target.leg_id, existing.filled_decimal())
.map_err(|error| format!("恢复补偿成交进度失败: {error:?}"))?;
if existing
.filled_decimal()
.checked_cmp(target.quantity_decimal())
.is_some_and(|ordering| ordering.is_lt())
{
let next_attempt = self
.group
.advance_compensation_attempt(&target.leg_id)
.map_err(|error| format!("推进补偿尝试序号失败: {error:?}"))?;
if next_attempt >= MAX_HEDGE_ATTEMPTS {
self.group.require_reconciliation();
errors.push(format!(
"补偿腿 {} 已达到 {} 次尝试上限,剩余敞口转人工对账",
target.leg_id, MAX_HEDGE_ATTEMPTS
));
}
}
continue;
}
if matches!(
existing.status,
OrderStatus::PendingSubmit
| OrderStatus::Submitted
| OrderStatus::Accepted
| OrderStatus::Working
| OrderStatus::PartiallyFilled
| OrderStatus::CancelPending
) {
all_filled = false;
continue;
}
if matches!(
existing.status,
OrderStatus::Cancelled | OrderStatus::Expired
) {
if existing.filled_decimal().is_positive() {
self.group
.record_compensation_fill_exact(
&target.leg_id,
existing.filled_decimal(),
)
.map_err(|error| format!("归约终态补偿部分成交失败: {error:?}"))?;
}
let next_attempt = self
.group
.advance_compensation_attempt(&target.leg_id)
.map_err(|error| format!("推进补偿尝试序号失败: {error:?}"))?;
if next_attempt >= MAX_HEDGE_ATTEMPTS {
self.group.require_reconciliation();
errors.push(format!(
"补偿腿 {} 已达到 {} 次尝试上限,剩余敞口转人工对账",
target.leg_id, MAX_HEDGE_ATTEMPTS
));
}
errors.push(format!(
"补偿订单 {} 已终止于 {:?},剩余敞口将在下一恢复轮处理",
client_id, existing.status
));
all_filled = false;
continue;
}
errors.push(format!(
"补偿订单 {} 状态 {:?} 不能安全重试,必须对账",
client_id, existing.status
));
self.group.require_reconciliation();
all_filled = false;
continue;
}
let mut leg_spec: Option<TradingInstrumentSpec> = None;
if let Some(validator) = validator {
if let Err(error) = validator.validate(&order) {
errors.push(format!(
"补偿腿 {} 未通过账户级风控,拒绝自动对冲: {error}",
target.leg_id
));
all_filled = false;
continue;
}
match validator.instrument_spec(&order) {
Ok(spec) => leg_spec = spec,
Err(error) => {
errors.push(format!(
"补偿腿 {} 未解析到产品规格,拒绝自动对冲: {error}",
target.leg_id
));
all_filled = false;
continue;
}
}
}
let Some(capabilities) = self.router.capabilities_for(&source_leg.venue_id) else {
errors.push(format!(
"补偿腿 {} 的 Venue {} 未声明能力或路由不存在,拒绝注册",
target.leg_id, source_leg.venue_id
));
all_filled = false;
continue;
};
if let Err(error) = capabilities.validate_submit(&order, &source_leg.venue_id) {
errors.push(format!("补偿腿 {} 能力校验失败: {error}", target.leg_id));
all_filled = false;
continue;
}
if let Err(error) = self.router.execution_readiness_for(&source_leg.venue_id) {
errors.push(format!("补偿腿 {} Venue 尚未就绪: {error}", target.leg_id));
all_filled = false;
continue;
}
self.events
.register_order(
order.clone(),
self.now,
Some(format!(
"{}:hedge:{}:{}:register",
self.worker_id, self.group.group_id, target.leg_id
)),
)
.map_err(|error| format!("注册补偿订单 {} 失败: {error}", client_id))?;
attempted += 1;
let facts =
match self
.router
.submit_order(&source_leg.venue_id, order.clone(), self.now)
{
Ok(facts) if facts.is_empty() => {
errors.push(format!(
"补偿腿 {} 未返回事实,结果未知,必须对账",
target.leg_id
));
self.group.require_reconciliation();
all_filled = false;
continue;
}
Ok(facts) => facts,
Err(error) => {
errors.push(format!(
"补偿腿 {} 提交失败/结果未知: {error}",
target.leg_id
));
self.group.require_reconciliation();
all_filled = false;
continue;
}
};
if let Err(violation) = gate_submit_facts(&order, &facts, leg_spec.as_ref()) {
errors.push(format!("补偿腿 {} {violation}", target.leg_id));
self.append_fact(
ExecutionEvent::ReconcileRequired {
client_order_id: client_id,
},
format!(
"{}:hedge:{}:{}:{}",
self.worker_id,
self.group.group_id,
target.leg_id,
violation.reason_tag()
),
None,
)?;
self.group.require_reconciliation();
all_filled = false;
continue;
}
let hedge_prefix = format!(
"{}:hedge:{}:{}",
self.worker_id, self.group.group_id, target.leg_id
);
for fact in facts {
let correlation_id = fact_correlation(&hedge_prefix, &fact);
self.append_fact(fact, correlation_id, leg_spec.as_ref())?;
}
let persisted_hedge = self
.events
.orders()
.into_iter()
.find(|candidate| candidate.client_id == client_id);
match persisted_hedge {
Some(hedge)
if hedge.status == OrderStatus::Filled
&& hedge.filled_decimal().is_positive() =>
{
self.group
.record_compensation_fill_exact(&target.leg_id, hedge.filled_decimal())
.map_err(|error| format!("归约补偿成交进度失败: {error:?}"))?;
if hedge
.filled_decimal()
.checked_cmp(target.quantity_decimal())
.is_some_and(|ordering| ordering.is_lt())
{
let next_attempt = self
.group
.advance_compensation_attempt(&target.leg_id)
.map_err(|error| format!("推进补偿尝试序号失败: {error:?}"))?;
if next_attempt >= MAX_HEDGE_ATTEMPTS {
self.group.require_reconciliation();
errors.push(format!(
"补偿腿 {} 已达到 {} 次尝试上限,剩余敞口转人工对账",
target.leg_id, MAX_HEDGE_ATTEMPTS
));
}
}
}
Some(hedge)
if matches!(hedge.status, OrderStatus::Cancelled | OrderStatus::Expired) =>
{
if hedge.filled_decimal().is_positive() {
self.group
.record_compensation_fill_exact(&target.leg_id, hedge.filled_decimal())
.map_err(|error| format!("归约终态补偿部分成交失败: {error:?}"))?;
}
let next_attempt = self
.group
.advance_compensation_attempt(&target.leg_id)
.map_err(|error| format!("推进补偿尝试序号失败: {error:?}"))?;
if next_attempt >= MAX_HEDGE_ATTEMPTS {
self.group.require_reconciliation();
errors.push(format!(
"补偿腿 {} 已达到 {} 次尝试上限,剩余敞口转人工对账",
target.leg_id, MAX_HEDGE_ATTEMPTS
));
}
all_filled = false;
}
Some(hedge)
if matches!(hedge.status, OrderStatus::Rejected | OrderStatus::Unknown) =>
{
self.group.require_reconciliation();
errors.push(format!(
"补偿订单 {} 进入 {:?},必须人工对账",
client_id, hedge.status
));
all_filled = false;
}
_ => all_filled = false,
}
}
if all_filled
&& errors.is_empty()
&& self.group.all_confirmed_exposure_hedged()
&& self.group.status == SpreadOrderGroupStatus::HedgeRequired
{
self.group
.mark_hedged()
.map_err(|error| format!("标记多腿订单组已对冲失败: {error:?}"))?;
}
self.store
.save_claimed(&self.group, &self.claim_owner, claim_token, self.now)?;
self.store
.release_claim(&self.group.group_id, &self.claim_owner, claim_token)?;
let completed =
self.group.status == SpreadOrderGroupStatus::Hedged && all_filled && errors.is_empty();
Ok(HedgeRecoveryOutcome {
group: self.group,
attempted,
completed,
errors,
})
}
fn append_fact(
&mut self,
event: ExecutionEvent,
correlation_id: String,
spec: Option<&TradingInstrumentSpec>,
) -> Result<(), String> {
*self.source_seq = self.source_seq.saturating_add(1);
let event_ts = match &event {
ExecutionEvent::Fill(fill) | ExecutionEvent::FillWithSpec { fill, .. } => fill.ts,
_ => self.now,
};
let event = match (spec, event) {
(Some(spec), ExecutionEvent::Fill(fill)) => ExecutionEvent::FillWithSpec {
fill,
spec: Box::new(spec.clone()),
},
(_, event) => event,
};
self.events.append_execution_event(ExecutionEventEnvelope {
event,
event_ts,
receive_ts: self.now,
source_seq: *self.source_seq,
correlation_id,
})
}
}
fn build_compensation_order(
group: &SpreadOrderGroup,
source_leg: &qx_zhenlu::SpreadOrderLeg,
target: &qx_zhenlu::SpreadCompensationTarget,
client_id: u64,
) -> Result<Order, String> {
let mut policy = source_leg.order.policy.unwrap_or_default();
policy.reduce_only = true;
let exact_amounts = target.qty_exact.map(|quantity| qx_core::ExactOrderAmounts {
quantity,
limit_price: None,
filled: qx_core::ExactDecimal::ZERO,
quote_budget: None,
quote_filled: qx_core::ExactDecimal::ZERO,
market_reference_price: source_leg.order.market_reference_price_decimal(),
conditional: None,
});
let order = Order {
client_id,
instrument: target.instrument.clone(),
side: target.side,
qty: if exact_amounts.is_some() {
qx_core::Quantity::ZERO
} else {
target.qty
},
limit: None,
status: OrderStatus::PendingSubmit,
filled: Quantity::ZERO,
account_id: target.account_id.clone(),
trace: Some(OrderTrace {
run_id: None,
strategy_id: Some(group.strategy_id.clone()),
signal_id: None,
intent_id: None,
rule_version: Some("spread-hedge-v1".into()),
}),
policy: Some(policy),
exact_amounts,
};
order.validate()?;
Ok(order)
}
fn same_compensation_order(left: &Order, right: &Order) -> bool {
left.same_submission_identity(right)
}
fn compensation_client_id(group: &SpreadOrderGroup, leg_id: &str, attempt: u32) -> u64 {
let mut hash = 0xcbf29ce484222325_u64;
let attempt_bytes = attempt.to_le_bytes();
for byte in group
.group_id
.as_bytes()
.iter()
.chain([0_u8].iter())
.chain(leg_id.as_bytes().iter())
.chain([0_u8].iter())
.chain(attempt_bytes.iter())
{
hash ^= u64::from(*byte);
hash = hash.wrapping_mul(0x100000001b3_u64);
}
let mut candidate = hash | (1_u64 << 63);
if candidate == 0 {
candidate = 1_u64 << 63;
}
while group
.legs
.iter()
.any(|leg| leg.order.client_id == candidate)
{
candidate = candidate.wrapping_add(1);
}
candidate
}
enum SubmitFactViolation {
Shape(String),
Precision(qx_core::QxError),
}
impl SubmitFactViolation {
fn reason_tag(&self) -> &'static str {
match self {
Self::Shape(_) => "invalid-submit-response",
Self::Precision(_) => "submit-fill-out-of-spec",
}
}
}
impl std::fmt::Display for SubmitFactViolation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Shape(error) => {
write!(f, "Venue 返回非法下单事实,结果未知,必须先对账: {error}")
}
Self::Precision(error) => write!(
f,
"Venue 提交返回的成交回报违反产品规格,结果未知,必须先对账: {error:?}"
),
}
}
}
fn gate_submit_facts(
order: &Order,
facts: &[ExecutionEvent],
spec: Option<&TradingInstrumentSpec>,
) -> Result<(), SubmitFactViolation> {
validate_submit_events(order, facts).map_err(SubmitFactViolation::Shape)?;
let violation = facts.iter().find_map(|fact| {
let (fill, embedded) = match fact {
ExecutionEvent::Fill(fill) => (fill.as_ref(), None),
ExecutionEvent::FillWithSpec { fill, spec } => (fill.as_ref(), Some(&**spec)),
_ => return None,
};
let spec = embedded.or(spec)?;
validate_fill_against_spec(fill, spec)
});
match violation {
Some(error) => Err(SubmitFactViolation::Precision(error)),
None => Ok(()),
}
}
fn fill_tag(fill: &qx_core::Fill) -> String {
format!(
"fill:{}:{}:{}:{}:{}:{}{}",
fill.order_id,
fill.ts,
fill.qty.raw(),
fill.price.raw(),
fill.fee.raw(),
fill.venue_order_id.clone().unwrap_or_default(),
fill.exact_identity_suffix(),
)
}
fn validate_fill_against_spec(
fill: &qx_core::Fill,
spec: &TradingInstrumentSpec,
) -> Option<qx_core::QxError> {
match fill.exact_amounts {
Some(amounts) => spec
.validate_exact_fill(amounts.quantity, amounts.price)
.err(),
None => spec.validate_fill(fill.qty.raw(), fill.price.raw()).err(),
}
}
fn fact_tag(fact: &ExecutionEvent) -> String {
match fact {
ExecutionEvent::Accepted {
client_order_id, ..
} => format!("accepted:{client_order_id}"),
ExecutionEvent::Rejected {
client_order_id,
reason,
} => {
format!("rejected:{client_order_id}:{reason}")
}
ExecutionEvent::CancelPending { client_order_id } => {
format!("cancel-pending:{client_order_id}")
}
ExecutionEvent::OrderAmendmentPending {
operation_id,
order,
} => format!("amend-pending:{}:{}", order.client_id, operation_id),
ExecutionEvent::OrderAmended {
operation_id,
order,
} => format!("amended:{}:{}", order.client_id, operation_id),
ExecutionEvent::Cancelled { client_order_id } => format!("cancelled:{client_order_id}"),
ExecutionEvent::FeeAdjustment {
account_id,
venue_id,
order_id,
instrument,
fees,
} => {
let mut fees = fees.clone();
fees.sort_by(|left, right| {
left.currency
.as_deref()
.unwrap_or_default()
.cmp(right.currency.as_deref().unwrap_or_default())
.then_with(|| left.amount.raw().cmp(&right.amount.raw()))
});
let identity = fees
.iter()
.map(|fee| {
format!(
"{}={}#{}",
fee.currency.as_deref().unwrap_or_default(),
fee.amount.raw(),
{
let mut trade_ids = fee.source_trade_ids.clone();
trade_ids.sort();
trade_ids.join(";")
}
)
})
.collect::<Vec<_>>()
.join(",");
format!("fee-adjustment:{account_id}:{venue_id}:{order_id}:{instrument}:{identity}")
}
ExecutionEvent::ReconcileRequired { client_order_id } => {
format!("reconcile:{client_order_id}")
}
ExecutionEvent::Fill(fill) | ExecutionEvent::FillWithSpec { fill, .. } => {
fill_tag(fill.as_ref())
}
ExecutionEvent::MarketQuote { instrument, quote } => {
format!("market:{instrument}:{}:{}", quote.source_seq, quote.ts)
}
}
}
fn execution_event_client_order_id(event: &ExecutionEvent) -> Option<u64> {
match event {
ExecutionEvent::Accepted {
client_order_id, ..
}
| ExecutionEvent::Rejected {
client_order_id, ..
}
| ExecutionEvent::CancelPending { client_order_id }
| ExecutionEvent::Cancelled { client_order_id }
| ExecutionEvent::ReconcileRequired { client_order_id } => Some(*client_order_id),
ExecutionEvent::Fill(fill) | ExecutionEvent::FillWithSpec { fill, .. } => {
Some(fill.order_id)
}
ExecutionEvent::FeeAdjustment { order_id, .. } => Some(*order_id),
ExecutionEvent::OrderAmendmentPending { order, .. }
| ExecutionEvent::OrderAmended { order, .. } => Some(order.client_id),
ExecutionEvent::MarketQuote { .. } => None,
}
}
fn fact_correlation(prefix: &str, fact: &ExecutionEvent) -> String {
format!("{prefix}:{}", fact_tag(fact))
}
pub fn ingest_venue_events<P: ExecutionEventPort>(
pipeline: &mut P,
events: impl IntoIterator<Item = VenueEvent>,
worker_id: &str,
receive_ts: u64,
source_seq: &mut u64,
) -> Result<usize, String> {
ingest_venue_events_with_pipeline(pipeline, events, worker_id, receive_ts, source_seq, None)
.map_err(|error| format!("Venue 执行回报事实归约失败: {error}"))
}
pub fn ingest_venue_events_with_spec<P: ExecutionEventPort>(
pipeline: &mut P,
events: impl IntoIterator<Item = VenueEvent>,
worker_id: &str,
receive_ts: u64,
source_seq: &mut u64,
spec: &TradingInstrumentSpec,
) -> Result<usize, String> {
ingest_venue_events_with_pipeline(
pipeline,
events,
worker_id,
receive_ts,
source_seq,
Some(spec),
)
.map_err(|error| format!("带产品规格的 Venue 成交事实归约失败: {error}"))
}
fn ingest_venue_events_with_pipeline<P: ExecutionEventPort>(
pipeline: &mut P,
events: impl IntoIterator<Item = VenueEvent>,
worker_id: &str,
receive_ts: u64,
source_seq: &mut u64,
spec: Option<&TradingInstrumentSpec>,
) -> Result<usize, String> {
let mut event_count = 0_usize;
let mut rejections = Vec::new();
for event in events {
event_count += 1;
*source_seq = source_seq.saturating_add(1);
let (event, event_ts, correlation_id) = match event {
VenueEvent::Accepted {
client_order_id,
venue_order_id,
ts,
} => (
ExecutionEvent::Accepted {
client_order_id,
venue_order_id,
},
ts,
format!("{worker_id}:accepted:{client_order_id}"),
),
VenueEvent::Rejected {
client_order_id,
reason,
ts,
} => (
ExecutionEvent::Rejected {
client_order_id,
reason,
},
ts,
format!("{worker_id}:rejected:{client_order_id}"),
),
VenueEvent::Fill(fill) => {
let fill = *fill;
let event_ts = fill.ts;
let fill_order_id = fill.order_id;
let seq = *source_seq;
let content_correlation = format!("{worker_id}:{}", fill_tag(&fill));
let violation = spec.and_then(|spec| validate_fill_against_spec(&fill, spec));
if let Some(error) = violation {
rejections.push(format!(
"订单 {fill_order_id} 成交回报违反产品规格: {error:?}"
));
(
ExecutionEvent::ReconcileRequired {
client_order_id: fill_order_id,
},
event_ts,
format!("{worker_id}:precision:{fill_order_id}:{seq}"),
)
} else {
let event = match spec {
Some(spec) => ExecutionEvent::FillWithSpec {
fill: Box::new(fill),
spec: Box::new(spec.clone()),
},
None => ExecutionEvent::Fill(Box::new(fill)),
};
(event, event_ts, content_correlation)
}
}
VenueEvent::Cancelled {
client_order_id,
ts,
} => (
ExecutionEvent::Cancelled { client_order_id },
ts,
format!("{worker_id}:cancelled:{client_order_id}"),
),
VenueEvent::FeeAdjustment {
account_id,
venue_id,
order_id,
instrument,
fees,
ts,
} => {
let fact = ExecutionEvent::FeeAdjustment {
account_id,
venue_id,
order_id,
instrument,
fees,
};
let correlation = fact_correlation(worker_id, &fact);
(fact, ts, correlation)
}
};
pipeline.append_execution_event(ExecutionEventEnvelope {
event,
event_ts,
receive_ts,
source_seq: *source_seq,
correlation_id,
})?;
}
if !rejections.is_empty() {
return Err(format!(
"成交回报精度越界,已转为待对账事实: {}",
rejections.join("; ")
));
}
Ok(event_count)
}
pub fn submit_order_with_risk<V: Venue, P: ExecutionEventPort>(
request: SubmitOrderWithRiskRequest<'_, V, P>,
) -> Result<String, String> {
let SubmitOrderWithRiskRequest {
command,
venue,
pipeline,
worker_id,
now,
source_seq,
context,
spread_store,
} = request;
spread_group_barrier(spread_store, command)?;
let mut venue = BorrowedVenuePort::new(venue);
let mut gateway = ExecutionGateway::new(&mut venue, pipeline, worker_id, now, source_seq);
if let Some(store) = spread_store {
gateway = gateway.with_spread_group_store(store);
}
let result = if let Some(spec) = context.risk.instrument_spec.clone() {
gateway
.with_instrument_spec(spec)
.submit_command_with_risk(
command,
&RiskContextPort {
risk: context.risk,
position: context.position,
},
)?
} else {
gateway.submit_command_with_risk(
command,
&RiskContextPort {
risk: context.risk,
position: context.position,
},
)?
};
Ok(result.message)
}
pub struct EventLogReconcilePort<'a, P: EventAppender> {
pipeline: &'a mut P,
worker_id: &'a str,
now: u64,
source_seq: &'a mut u64,
event_ts: u64,
tag: &'static str,
}
impl<'a, P: EventAppender> EventLogReconcilePort<'a, P> {
pub fn new(pipeline: &'a mut P, worker_id: &'a str, now: u64, source_seq: &'a mut u64) -> Self {
Self {
pipeline,
worker_id,
now,
source_seq,
event_ts: now,
tag: "reconcile",
}
}
pub fn at_event_ts(mut self, event_ts: u64) -> Self {
self.event_ts = event_ts;
self
}
pub fn with_tag(mut self, tag: &'static str) -> Self {
self.tag = tag;
self
}
pub fn require_reconcile(&mut self, client_order_id: u64, reason: &str) -> Result<(), String> {
let reason = reason.trim();
if reason.is_empty() {
return Err(format!(
"标记订单 {client_order_id} 待对账失败: 缺少未知结果的原因"
));
}
*self.source_seq = self.source_seq.saturating_add(1);
let source_seq = *self.source_seq;
self.pipeline
.append_execution_event(ExecutionEventEnvelope {
event: ExecutionEvent::ReconcileRequired { client_order_id },
event_ts: self.event_ts,
receive_ts: self.now,
source_seq,
correlation_id: format!("{}:{}:{client_order_id}", self.worker_id, self.tag),
})
.map_err(|error| format!("{error};待对账原因: {reason}"))
}
}
pub fn execute_paper_submit_effect<P: ExecutionEventPort + application::LedgerProbe>(
request: PaperSubmitRequest<'_, P>,
) -> Result<String, String> {
let PaperSubmitRequest {
command,
pipeline,
now,
risk,
position,
market_quote,
fee_model,
allow_synthetic_quote,
spread_store,
} = request;
spread_group_barrier(spread_store, command)?;
let order = order_from_submit_command(command)
.map_err(|error| format!("Paper SubmitOrder 订单载荷非法: {error:?}"))?;
let (risk_context, position_snapshot) = match (risk.as_ref(), position.as_ref()) {
(Some(risk), Some(position)) => (risk, position),
_ => {
return Err(
"FAIL_CLOSED: risk context missing: RiskContext and OrderRiskPosition required"
.into(),
)
}
};
if market_quote.is_none() && !allow_synthetic_quote {
return Err("FAIL_CLOSED: market quote missing: EventLog quote is required".into());
}
let quote = market_quote.unwrap_or_else(|| {
let ask = order.limit.unwrap_or_else(|| Price::from_i64(100));
let bid = Price::from_raw(ask.raw().saturating_sub(qx_core::SCALE));
QuoteTick::new(
now.saturating_add(1),
bid,
Quantity::from_i64(1_000),
ask,
Quantity::from_i64(1_000),
1,
)
});
validate_paper_quote("e)?;
let mut venue = PaperVenue::new("paper", fee_model);
if order.exact_amounts.is_some() {
let spec = risk_context.instrument_spec.clone().ok_or_else(|| {
"FAIL_CLOSED: Paper exact order 缺少冻结 market spec 和结算币种".to_string()
})?;
venue
.set_instrument_spec(spec)
.map_err(|error| format!("Paper 冻结 market spec 配置失败: {error:?}"))?;
}
let mut source_seq = 0_u64;
let risk_port = RiskContextPort {
risk: risk_context,
position: position_snapshot,
};
let submit_result = {
let mut venue_port = BorrowedVenuePort::new(&mut venue);
let mut gateway = ExecutionGateway::new(
&mut venue_port,
pipeline,
"paper-execution",
now,
&mut source_seq,
);
if let Some(store) = spread_store {
gateway = gateway.with_spread_group_store(store);
}
let message = match risk_context.instrument_spec.clone() {
Some(spec) => {
gateway
.with_instrument_spec(spec)
.submit_command_with_risk(command, &risk_port)?
.message
}
None => {
gateway
.submit_command_with_risk(command, &risk_port)?
.message
}
};
message
};
if submit_result.starts_with("ALREADY_APPLIED_FROM_EVENT_LOG") {
return Ok(submit_result);
}
let quote_seq = if quote.source_seq == 0 {
1
} else {
quote.source_seq
};
pipeline.append_execution_event(ExecutionEventEnvelope {
event: ExecutionEvent::MarketQuote {
instrument: order.instrument.clone(),
quote,
},
event_ts: now.saturating_add(1),
receive_ts: now.saturating_add(1),
source_seq: quote_seq,
correlation_id: format!("paper-execution:market:{}:{quote_seq}", order.instrument),
})?;
let fills = venue
.try_on_quote(&order.instrument, quote)
.map_err(|error| format!("Paper 精确撮合失败: {error:?}"))?;
let fill_count = fills.len();
if let Some(spec) = risk_context.instrument_spec.as_ref() {
ingest_venue_events_with_spec(
pipeline,
fills,
"paper-execution",
quote.ts,
&mut source_seq,
spec,
)?;
} else {
ingest_venue_events(
pipeline,
fills,
"paper-execution",
quote.ts,
&mut source_seq,
)?;
}
Ok(format!(
"PAPER_EXECUTED fills={} ledger_entries={}",
fill_count,
pipeline.ledger_entry_count()
))
}
fn validate_paper_quote(quote: &QuoteTick) -> Result<(), String> {
let project = |raw: i128, exact: Option<ExactDecimal>, label: &str| match exact {
Some(value) => {
if !value.is_positive() {
return Err(format!("Paper {label} 精确值必须为正数"));
}
let projected = value
.checked_mul_at_scale(ExactDecimal::ONE, 9, DecimalRounding::TowardZero)
.and_then(|value| value.to_raw(9).ok());
if projected != Some(raw) {
return Err(format!("Paper {label} 精确值与兼容投影不一致"));
}
Ok(value)
}
None if raw > 0 => ExactDecimal::from_parts(raw, 9)
.map_err(|error| format!("Paper {label} 兼容值非法: {error}")),
None => Err(format!("Paper {label} 缺少正数行情")),
};
let bid = project(quote.bid.raw(), quote.bid_exact, "bid")?;
let ask = project(quote.ask.raw(), quote.ask_exact, "ask")?;
if bid.checked_cmp(ask).is_none_or(|ordering| ordering.is_gt()) {
return Err("Paper 行情买卖价交叉或无法比较".into());
}
project(quote.bid_qty.raw(), quote.bid_qty_exact, "bid quantity")?;
project(quote.ask_qty.raw(), quote.ask_qty_exact, "ask quantity")?;
Ok(())
}
#[cfg(test)]
mod tests;