Allow odd-lot full liquidation sells

This commit is contained in:
boris
2026-04-22 23:05:34 -07:00
parent 34e16520fa
commit 7a67e815d7
2 changed files with 138 additions and 8 deletions

View File

@@ -549,6 +549,7 @@ where
sellable.min(current_qty),
round_lot,
0,
sellable >= current_qty,
) {
Ok(quantity) => quantity.min(sellable).min(current_qty),
Err(_) => 0,
@@ -577,11 +578,17 @@ where
if !rule.allowed {
return current_qty;
}
let additional_limit =
match self.market_fillable_quantity(snapshot, OrderSide::Buy, u32::MAX, round_lot, 0) {
Ok(quantity) => quantity,
Err(_) => 0,
};
let additional_limit = match self.market_fillable_quantity(
snapshot,
OrderSide::Buy,
u32::MAX,
round_lot,
0,
false,
) {
Ok(quantity) => quantity,
Err(_) => 0,
};
current_qty.saturating_add(additional_limit)
}
@@ -655,6 +662,7 @@ where
requested_qty.min(sellable),
self.round_lot(data, symbol),
*intraday_turnover.get(symbol).unwrap_or(&0),
requested_qty >= position.quantity && sellable >= position.quantity,
);
let filled_qty = match market_limited_qty {
Ok(quantity) => quantity.min(sellable),
@@ -693,6 +701,7 @@ where
data,
filled_qty,
self.round_lot(data, symbol),
filled_qty >= position.quantity,
execution_cursors,
None,
None,
@@ -979,6 +988,7 @@ where
requested_qty,
self.round_lot(data, symbol),
*intraday_turnover.get(symbol).unwrap_or(&0),
false,
);
let constrained_qty = match market_limited_qty {
Ok(quantity) => quantity,
@@ -1004,6 +1014,7 @@ where
data,
constrained_qty,
self.round_lot(data, symbol),
false,
execution_cursors,
None,
Some(portfolio.cash()),
@@ -1176,6 +1187,7 @@ where
requested_qty: u32,
round_lot: u32,
consumed_turnover: u32,
allow_odd_lot_sell: bool,
) -> Result<u32, String> {
if requested_qty == 0 {
return Ok(0);
@@ -1197,7 +1209,12 @@ where
if top_level_liquidity == 0 {
return Err("no quote liquidity".to_string());
}
max_fill = max_fill.min(self.round_buy_quantity(top_level_liquidity, lot));
let top_level_limit = if side == OrderSide::Sell && allow_odd_lot_sell {
top_level_liquidity
} else {
self.round_buy_quantity(top_level_liquidity, lot)
};
max_fill = max_fill.min(top_level_limit);
}
if self.volume_limit {
@@ -1206,7 +1223,11 @@ where
if raw_limit <= 0 {
return Err("tick volume limit".to_string());
}
let volume_limited = self.round_buy_quantity(raw_limit as u32, lot);
let volume_limited = if side == OrderSide::Sell && allow_odd_lot_sell {
raw_limit as u32
} else {
self.round_buy_quantity(raw_limit as u32, lot)
};
if volume_limited == 0 {
return Err("tick volume limit".to_string());
}
@@ -1225,6 +1246,7 @@ where
data: &DataSet,
requested_qty: u32,
round_lot: u32,
allow_odd_lot_sell: bool,
_execution_cursors: &mut BTreeMap<String, NaiveDateTime>,
_global_execution_cursor: Option<NaiveDateTime>,
cash_limit: Option<f64>,
@@ -1273,6 +1295,7 @@ where
start_cursor,
requested_qty,
round_lot,
allow_odd_lot_sell,
cash_limit,
gross_limit,
) {
@@ -1290,6 +1313,7 @@ where
start_cursor: Option<NaiveDateTime>,
requested_qty: u32,
round_lot: u32,
allow_odd_lot_sell: bool,
cash_limit: Option<f64>,
gross_limit: Option<f64>,
) -> Option<ExecutionFill> {
@@ -1332,7 +1356,9 @@ where
break;
}
let mut take_qty = remaining_qty.min(available_qty);
take_qty = self.round_buy_quantity(take_qty, lot);
if !(side == OrderSide::Sell && allow_odd_lot_sell && take_qty == remaining_qty) {
take_qty = self.round_buy_quantity(take_qty, lot);
}
if take_qty == 0 {
continue;
}