修复涨跌停最终执行价约束
This commit is contained in:
+116
-43
@@ -2505,7 +2505,7 @@ where
|
||||
merge_partial_fill_reason(partial_fill_reason, fill.unfilled_reason);
|
||||
(fill.quantity, fill.legs)
|
||||
} else {
|
||||
let mut execution_price =
|
||||
let execution_price =
|
||||
self.snapshot_execution_price(snapshot, OrderSide::Sell, Some(fillable_qty));
|
||||
if let Some(reason) =
|
||||
self.execution_limit_rejection_reason(snapshot, OrderSide::Sell, execution_price)
|
||||
@@ -2524,16 +2524,26 @@ where
|
||||
);
|
||||
(0, Vec::new())
|
||||
} else {
|
||||
execution_price =
|
||||
self.execution_price_with_limit_slippage(execution_price, limit_price);
|
||||
(
|
||||
fillable_qty,
|
||||
vec![ExecutionLeg {
|
||||
price: execution_price,
|
||||
mark_price: self.snapshot_mark_price(snapshot, OrderSide::Sell),
|
||||
quantity: fillable_qty,
|
||||
}],
|
||||
)
|
||||
match self.execution_price_with_limit_slippage_or_rejection(
|
||||
snapshot,
|
||||
OrderSide::Sell,
|
||||
execution_price,
|
||||
limit_price,
|
||||
) {
|
||||
Ok(execution_price) => (
|
||||
fillable_qty,
|
||||
vec![ExecutionLeg {
|
||||
price: execution_price,
|
||||
mark_price: self.snapshot_mark_price(snapshot, OrderSide::Sell),
|
||||
quantity: fillable_qty,
|
||||
}],
|
||||
),
|
||||
Err(reason) => {
|
||||
partial_fill_reason =
|
||||
merge_partial_fill_reason(partial_fill_reason, Some(reason));
|
||||
(0, Vec::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
if filled_qty == 0 {
|
||||
@@ -3885,7 +3895,7 @@ where
|
||||
merge_partial_fill_reason(partial_fill_reason, fill.unfilled_reason);
|
||||
(fill.quantity, fill.legs)
|
||||
} else {
|
||||
let mut execution_price =
|
||||
let execution_price =
|
||||
self.snapshot_execution_price(snapshot, OrderSide::Buy, Some(constrained_qty));
|
||||
if let Some(reason) =
|
||||
self.execution_limit_rejection_reason(snapshot, OrderSide::Buy, execution_price)
|
||||
@@ -3904,43 +3914,77 @@ where
|
||||
);
|
||||
(0, Vec::new())
|
||||
} else {
|
||||
execution_price =
|
||||
self.execution_price_with_limit_slippage(execution_price, limit_price);
|
||||
let filled_qty = self.affordable_buy_quantity(
|
||||
date,
|
||||
buy_cash_limit,
|
||||
value_gross_limit,
|
||||
match self.execution_price_with_limit_slippage_or_rejection(
|
||||
snapshot,
|
||||
OrderSide::Buy,
|
||||
execution_price,
|
||||
constrained_qty,
|
||||
self.minimum_order_quantity(data, symbol),
|
||||
self.order_step_size(data, symbol),
|
||||
);
|
||||
if filled_qty > 0 {
|
||||
execution_price =
|
||||
self.snapshot_execution_price(snapshot, OrderSide::Buy, Some(filled_qty));
|
||||
execution_price =
|
||||
self.execution_price_with_limit_slippage(execution_price, limit_price);
|
||||
}
|
||||
if filled_qty < constrained_qty {
|
||||
partial_fill_reason = merge_partial_fill_reason(
|
||||
partial_fill_reason,
|
||||
self.buy_reduction_reason(
|
||||
limit_price,
|
||||
) {
|
||||
Err(reason) => {
|
||||
partial_fill_reason =
|
||||
merge_partial_fill_reason(partial_fill_reason, Some(reason));
|
||||
(0, Vec::new())
|
||||
}
|
||||
Ok(mut execution_price) => {
|
||||
let mut filled_qty = self.affordable_buy_quantity(
|
||||
date,
|
||||
buy_cash_limit,
|
||||
value_gross_limit,
|
||||
execution_price,
|
||||
constrained_qty,
|
||||
filled_qty,
|
||||
),
|
||||
);
|
||||
self.minimum_order_quantity(data, symbol),
|
||||
self.order_step_size(data, symbol),
|
||||
);
|
||||
let mut blocked_by_final_price = false;
|
||||
if filled_qty > 0 {
|
||||
execution_price = self.snapshot_execution_price(
|
||||
snapshot,
|
||||
OrderSide::Buy,
|
||||
Some(filled_qty),
|
||||
);
|
||||
match self.execution_price_with_limit_slippage_or_rejection(
|
||||
snapshot,
|
||||
OrderSide::Buy,
|
||||
execution_price,
|
||||
limit_price,
|
||||
) {
|
||||
Ok(price) => execution_price = price,
|
||||
Err(reason) => {
|
||||
partial_fill_reason = merge_partial_fill_reason(
|
||||
partial_fill_reason,
|
||||
Some(reason),
|
||||
);
|
||||
filled_qty = 0;
|
||||
blocked_by_final_price = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if blocked_by_final_price {
|
||||
(0, Vec::new())
|
||||
} else {
|
||||
if filled_qty < constrained_qty {
|
||||
partial_fill_reason = merge_partial_fill_reason(
|
||||
partial_fill_reason,
|
||||
self.buy_reduction_reason(
|
||||
buy_cash_limit,
|
||||
value_gross_limit,
|
||||
execution_price,
|
||||
constrained_qty,
|
||||
filled_qty,
|
||||
),
|
||||
);
|
||||
}
|
||||
(
|
||||
filled_qty,
|
||||
vec![ExecutionLeg {
|
||||
price: execution_price,
|
||||
mark_price: self.snapshot_mark_price(snapshot, OrderSide::Buy),
|
||||
quantity: filled_qty,
|
||||
}],
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
(
|
||||
filled_qty,
|
||||
vec![ExecutionLeg {
|
||||
price: execution_price,
|
||||
mark_price: self.snapshot_mark_price(snapshot, OrderSide::Buy),
|
||||
quantity: filled_qty,
|
||||
}],
|
||||
)
|
||||
}
|
||||
};
|
||||
if filled_qty == 0 {
|
||||
@@ -4567,6 +4611,21 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn execution_price_with_limit_slippage_or_rejection(
|
||||
&self,
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
side: OrderSide,
|
||||
execution_price: f64,
|
||||
limit_price: Option<f64>,
|
||||
) -> Result<f64, &'static str> {
|
||||
let adjusted = self.execution_price_with_limit_slippage(execution_price, limit_price);
|
||||
if let Some(reason) = self.execution_limit_rejection_reason(snapshot, side, adjusted) {
|
||||
Err(reason)
|
||||
} else {
|
||||
Ok(adjusted)
|
||||
}
|
||||
}
|
||||
|
||||
fn limit_order_can_remain_open(partial_reason: Option<&str>) -> bool {
|
||||
!partial_reason.is_some_and(|reason| {
|
||||
reason.contains("insufficient cash")
|
||||
@@ -4819,6 +4878,14 @@ where
|
||||
}
|
||||
quote_price =
|
||||
self.execution_price_with_limit_slippage(quote_price, limit_price);
|
||||
if let Some(reason) =
|
||||
self.execution_limit_rejection_reason(snapshot, side, quote_price)
|
||||
{
|
||||
execution_block_reason.get_or_insert(reason);
|
||||
execution_block_timestamp = Some(quote.timestamp);
|
||||
take_qty = 0;
|
||||
break;
|
||||
}
|
||||
let candidate_gross = gross_amount + quote_price * take_qty as f64;
|
||||
if gross_limit.is_some_and(|limit| candidate_gross > limit + 1e-6) {
|
||||
budget_block_reason = Some("value budget limit");
|
||||
@@ -4851,6 +4918,12 @@ where
|
||||
quote_price =
|
||||
self.quote_execution_price(snapshot, side, raw_quote_price, Some(take_qty));
|
||||
quote_price = self.execution_price_with_limit_slippage(quote_price, limit_price);
|
||||
if let Some(reason) = self.execution_limit_rejection_reason(snapshot, side, quote_price)
|
||||
{
|
||||
execution_block_reason.get_or_insert(reason);
|
||||
execution_block_timestamp = Some(quote.timestamp);
|
||||
continue;
|
||||
}
|
||||
|
||||
gross_amount += quote_price * take_qty as f64;
|
||||
mark_amount += mark_price * take_qty as f64;
|
||||
|
||||
Reference in New Issue
Block a user