Align board-specific stock order sizing
This commit is contained in:
@@ -33,7 +33,8 @@ struct TargetConstraint {
|
||||
max_target_qty: u32,
|
||||
provisional_target_qty: u32,
|
||||
price: f64,
|
||||
round_lot: u32,
|
||||
minimum_order_quantity: u32,
|
||||
order_step_size: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -420,7 +421,11 @@ where
|
||||
let raw_qty = ((equity * weight) / price).floor() as u32;
|
||||
desired_targets.insert(
|
||||
symbol.clone(),
|
||||
self.round_buy_quantity(raw_qty, self.round_lot(data, symbol)),
|
||||
self.round_buy_quantity(
|
||||
raw_qty,
|
||||
self.minimum_order_quantity(data, symbol),
|
||||
self.order_step_size(data, symbol),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -443,14 +448,16 @@ where
|
||||
symbol: symbol.clone(),
|
||||
field: price_field_name(self.execution_price_field),
|
||||
})?;
|
||||
let round_lot = self.round_lot(data, &symbol);
|
||||
let minimum_order_quantity = self.minimum_order_quantity(data, &symbol);
|
||||
let order_step_size = self.order_step_size(data, &symbol);
|
||||
let min_target_qty = self.minimum_target_quantity(
|
||||
date,
|
||||
portfolio,
|
||||
data,
|
||||
&symbol,
|
||||
current_qty,
|
||||
round_lot,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
);
|
||||
let max_target_qty = self.maximum_target_quantity(
|
||||
date,
|
||||
@@ -458,7 +465,8 @@ where
|
||||
data,
|
||||
&symbol,
|
||||
current_qty,
|
||||
round_lot,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
);
|
||||
let provisional_target_qty = desired_qty.clamp(min_target_qty, max_target_qty);
|
||||
if current_qty > provisional_target_qty {
|
||||
@@ -475,7 +483,8 @@ where
|
||||
max_target_qty,
|
||||
provisional_target_qty,
|
||||
price,
|
||||
round_lot,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -490,7 +499,8 @@ where
|
||||
None,
|
||||
constraint.price,
|
||||
desired_additional,
|
||||
constraint.round_lot,
|
||||
constraint.minimum_order_quantity,
|
||||
constraint.order_step_size,
|
||||
);
|
||||
target_qty = (constraint.current_qty + affordable_additional)
|
||||
.clamp(constraint.min_target_qty, constraint.max_target_qty);
|
||||
@@ -518,7 +528,8 @@ where
|
||||
data: &DataSet,
|
||||
symbol: &str,
|
||||
current_qty: u32,
|
||||
round_lot: u32,
|
||||
minimum_order_quantity: u32,
|
||||
order_step_size: u32,
|
||||
) -> u32 {
|
||||
if current_qty == 0 {
|
||||
return 0;
|
||||
@@ -547,7 +558,8 @@ where
|
||||
snapshot,
|
||||
OrderSide::Sell,
|
||||
sellable.min(current_qty),
|
||||
round_lot,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
0,
|
||||
sellable >= current_qty,
|
||||
) {
|
||||
@@ -564,7 +576,8 @@ where
|
||||
data: &DataSet,
|
||||
symbol: &str,
|
||||
current_qty: u32,
|
||||
round_lot: u32,
|
||||
minimum_order_quantity: u32,
|
||||
order_step_size: u32,
|
||||
) -> u32 {
|
||||
let Ok(snapshot) = data.require_market(date, symbol) else {
|
||||
return current_qty;
|
||||
@@ -582,7 +595,8 @@ where
|
||||
snapshot,
|
||||
OrderSide::Buy,
|
||||
u32::MAX,
|
||||
round_lot,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
0,
|
||||
false,
|
||||
) {
|
||||
@@ -660,7 +674,8 @@ where
|
||||
snapshot,
|
||||
OrderSide::Sell,
|
||||
requested_qty.min(sellable),
|
||||
self.round_lot(data, symbol),
|
||||
self.minimum_order_quantity(data, symbol),
|
||||
self.order_step_size(data, symbol),
|
||||
*intraday_turnover.get(symbol).unwrap_or(&0),
|
||||
requested_qty >= position.quantity && sellable >= position.quantity,
|
||||
);
|
||||
@@ -701,6 +716,8 @@ where
|
||||
data,
|
||||
filled_qty,
|
||||
self.round_lot(data, symbol),
|
||||
self.minimum_order_quantity(data, symbol),
|
||||
self.order_step_size(data, symbol),
|
||||
filled_qty >= position.quantity,
|
||||
execution_cursors,
|
||||
None,
|
||||
@@ -810,7 +827,8 @@ where
|
||||
let current_value = price * current_qty as f64;
|
||||
let target_qty = self.round_buy_quantity(
|
||||
((target_value.max(0.0)) / price).floor() as u32,
|
||||
self.round_lot(data, symbol),
|
||||
self.minimum_order_quantity(data, symbol),
|
||||
self.order_step_size(data, symbol),
|
||||
);
|
||||
|
||||
if current_qty > target_qty {
|
||||
@@ -884,9 +902,14 @@ where
|
||||
})?;
|
||||
if value > 0.0 {
|
||||
let round_lot = self.round_lot(data, symbol);
|
||||
let minimum_order_quantity = self.minimum_order_quantity(data, symbol);
|
||||
let order_step_size = self.order_step_size(data, symbol);
|
||||
let price = self.sizing_price(snapshot);
|
||||
let snapshot_requested_qty =
|
||||
self.round_buy_quantity(((value.abs()) / price).floor() as u32, round_lot);
|
||||
let snapshot_requested_qty = self.round_buy_quantity(
|
||||
((value.abs()) / price).floor() as u32,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
);
|
||||
let requested_qty = self.maybe_expand_periodic_value_buy_quantity(
|
||||
date,
|
||||
portfolio,
|
||||
@@ -916,7 +939,8 @@ where
|
||||
let price = self.sizing_price(snapshot);
|
||||
let requested_qty = self.round_buy_quantity(
|
||||
((value.abs()) / price).floor() as u32,
|
||||
self.round_lot(data, symbol),
|
||||
self.minimum_order_quantity(data, symbol),
|
||||
self.order_step_size(data, symbol),
|
||||
);
|
||||
self.process_sell(
|
||||
date,
|
||||
@@ -986,7 +1010,8 @@ where
|
||||
snapshot,
|
||||
OrderSide::Buy,
|
||||
requested_qty,
|
||||
self.round_lot(data, symbol),
|
||||
self.minimum_order_quantity(data, symbol),
|
||||
self.order_step_size(data, symbol),
|
||||
*intraday_turnover.get(symbol).unwrap_or(&0),
|
||||
false,
|
||||
);
|
||||
@@ -1014,6 +1039,8 @@ where
|
||||
data,
|
||||
constrained_qty,
|
||||
self.round_lot(data, symbol),
|
||||
self.minimum_order_quantity(data, symbol),
|
||||
self.order_step_size(data, symbol),
|
||||
false,
|
||||
execution_cursors,
|
||||
None,
|
||||
@@ -1034,7 +1061,8 @@ where
|
||||
value_budget.map(|budget| budget + 400.0),
|
||||
execution_price,
|
||||
constrained_qty,
|
||||
self.round_lot(data, symbol),
|
||||
self.minimum_order_quantity(data, symbol),
|
||||
self.order_step_size(data, symbol),
|
||||
);
|
||||
(filled_qty, execution_price)
|
||||
};
|
||||
@@ -1149,9 +1177,47 @@ where
|
||||
.unwrap_or(self.board_lot_size.max(1))
|
||||
}
|
||||
|
||||
fn round_buy_quantity(&self, quantity: u32, round_lot: u32) -> u32 {
|
||||
let lot = round_lot.max(1);
|
||||
(quantity / lot) * lot
|
||||
fn minimum_order_quantity(&self, data: &DataSet, symbol: &str) -> u32 {
|
||||
data.instruments()
|
||||
.get(symbol)
|
||||
.map(|instrument| instrument.minimum_order_quantity())
|
||||
.unwrap_or(self.board_lot_size.max(1))
|
||||
}
|
||||
|
||||
fn order_step_size(&self, data: &DataSet, symbol: &str) -> u32 {
|
||||
data.instruments()
|
||||
.get(symbol)
|
||||
.map(|instrument| instrument.order_step_size())
|
||||
.unwrap_or(self.board_lot_size.max(1))
|
||||
}
|
||||
|
||||
fn round_buy_quantity(
|
||||
&self,
|
||||
quantity: u32,
|
||||
minimum_order_quantity: u32,
|
||||
order_step_size: u32,
|
||||
) -> u32 {
|
||||
let step = order_step_size.max(1);
|
||||
let normalized = (quantity / step) * step;
|
||||
if normalized < minimum_order_quantity.max(1) {
|
||||
0
|
||||
} else {
|
||||
normalized
|
||||
}
|
||||
}
|
||||
|
||||
fn decrement_order_quantity(
|
||||
&self,
|
||||
quantity: u32,
|
||||
minimum_order_quantity: u32,
|
||||
order_step_size: u32,
|
||||
) -> u32 {
|
||||
let minimum = minimum_order_quantity.max(1);
|
||||
if quantity <= minimum {
|
||||
return 0;
|
||||
}
|
||||
let next = quantity.saturating_sub(order_step_size.max(1));
|
||||
if next < minimum { 0 } else { next }
|
||||
}
|
||||
|
||||
fn affordable_buy_quantity(
|
||||
@@ -1161,21 +1227,27 @@ where
|
||||
gross_limit: Option<f64>,
|
||||
price: f64,
|
||||
requested_qty: u32,
|
||||
round_lot: u32,
|
||||
minimum_order_quantity: u32,
|
||||
order_step_size: u32,
|
||||
) -> u32 {
|
||||
let lot = round_lot.max(1);
|
||||
let mut quantity = self.round_buy_quantity(requested_qty, lot);
|
||||
let mut quantity =
|
||||
self.round_buy_quantity(requested_qty, minimum_order_quantity, order_step_size);
|
||||
while quantity > 0 {
|
||||
let gross = price * quantity as f64;
|
||||
if gross_limit.is_some_and(|limit| gross > limit + 1e-6) {
|
||||
quantity = quantity.saturating_sub(lot);
|
||||
quantity = self.decrement_order_quantity(
|
||||
quantity,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
let cost = self.cost_model.calculate(date, OrderSide::Buy, gross);
|
||||
if gross + cost.total() <= cash + 1e-6 {
|
||||
return quantity;
|
||||
}
|
||||
quantity = quantity.saturating_sub(lot);
|
||||
quantity =
|
||||
self.decrement_order_quantity(quantity, minimum_order_quantity, order_step_size);
|
||||
}
|
||||
0
|
||||
}
|
||||
@@ -1185,7 +1257,8 @@ where
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
side: OrderSide,
|
||||
requested_qty: u32,
|
||||
round_lot: u32,
|
||||
minimum_order_quantity: u32,
|
||||
order_step_size: u32,
|
||||
consumed_turnover: u32,
|
||||
allow_odd_lot_sell: bool,
|
||||
) -> Result<u32, String> {
|
||||
@@ -1198,8 +1271,6 @@ where
|
||||
}
|
||||
|
||||
let mut max_fill = requested_qty;
|
||||
let lot = round_lot.max(1);
|
||||
|
||||
if self.liquidity_limit {
|
||||
let top_level_liquidity = match side {
|
||||
OrderSide::Buy => snapshot.liquidity_for_buy(),
|
||||
@@ -1212,7 +1283,11 @@ where
|
||||
let top_level_limit = if side == OrderSide::Sell && allow_odd_lot_sell {
|
||||
top_level_liquidity
|
||||
} else {
|
||||
self.round_buy_quantity(top_level_liquidity, lot)
|
||||
self.round_buy_quantity(
|
||||
top_level_liquidity,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
)
|
||||
};
|
||||
max_fill = max_fill.min(top_level_limit);
|
||||
}
|
||||
@@ -1226,7 +1301,7 @@ where
|
||||
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)
|
||||
self.round_buy_quantity(raw_limit as u32, minimum_order_quantity, order_step_size)
|
||||
};
|
||||
if volume_limited == 0 {
|
||||
return Err("tick volume limit".to_string());
|
||||
@@ -1246,6 +1321,8 @@ where
|
||||
data: &DataSet,
|
||||
requested_qty: u32,
|
||||
round_lot: u32,
|
||||
minimum_order_quantity: u32,
|
||||
order_step_size: u32,
|
||||
allow_odd_lot_sell: bool,
|
||||
_execution_cursors: &mut BTreeMap<String, NaiveDateTime>,
|
||||
_global_execution_cursor: Option<NaiveDateTime>,
|
||||
@@ -1265,7 +1342,8 @@ where
|
||||
gross_limit,
|
||||
execution_price,
|
||||
requested_qty,
|
||||
round_lot,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
),
|
||||
OrderSide::Sell => requested_qty,
|
||||
};
|
||||
@@ -1295,6 +1373,8 @@ where
|
||||
start_cursor,
|
||||
requested_qty,
|
||||
round_lot,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
allow_odd_lot_sell,
|
||||
cash_limit,
|
||||
gross_limit,
|
||||
@@ -1313,6 +1393,8 @@ where
|
||||
start_cursor: Option<NaiveDateTime>,
|
||||
requested_qty: u32,
|
||||
round_lot: u32,
|
||||
minimum_order_quantity: u32,
|
||||
order_step_size: u32,
|
||||
allow_odd_lot_sell: bool,
|
||||
cash_limit: Option<f64>,
|
||||
gross_limit: Option<f64>,
|
||||
@@ -1357,7 +1439,8 @@ where
|
||||
}
|
||||
let mut take_qty = remaining_qty.min(available_qty);
|
||||
if !(side == OrderSide::Sell && allow_odd_lot_sell && take_qty == remaining_qty) {
|
||||
take_qty = self.round_buy_quantity(take_qty, lot);
|
||||
take_qty =
|
||||
self.round_buy_quantity(take_qty, minimum_order_quantity, order_step_size);
|
||||
}
|
||||
if take_qty == 0 {
|
||||
continue;
|
||||
@@ -1367,13 +1450,21 @@ where
|
||||
while take_qty > 0 {
|
||||
let candidate_gross = gross_amount + quote_price * take_qty as f64;
|
||||
if gross_limit.is_some_and(|limit| candidate_gross > limit + 1e-6) {
|
||||
take_qty = take_qty.saturating_sub(lot);
|
||||
take_qty = self.decrement_order_quantity(
|
||||
take_qty,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if candidate_gross <= cash + 1e-6 {
|
||||
break;
|
||||
}
|
||||
take_qty = take_qty.saturating_sub(lot);
|
||||
take_qty = self.decrement_order_quantity(
|
||||
take_qty,
|
||||
minimum_order_quantity,
|
||||
order_step_size,
|
||||
);
|
||||
}
|
||||
if take_qty == 0 {
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user