pub fn calculate_reconciliation_price(
current_position_qty: Decimal,
current_position_avg_px: Option<Decimal>,
target_position_qty: Decimal,
target_position_avg_px: Option<Decimal>,
target_avg_px_reconciliation: AvgPxReconciliation,
target_avg_px_precision: Option<u8>,
) -> Option<Decimal>Expand description
Calculate the price needed for a reconciliation order to achieve target position.
This is a pure function that calculates what price a fill would need to have to move from the current position state to the target position state. By default, a reduction uses the reported average price but cannot change the remaining position’s entry average.
A same-side reduction or accumulation instead keeps the current average as its price when the
target average is [AvgPxReconciliation::OpeningOnly], or when the target average carries a
precision and already matches the current average, so neither a different averaging method
nor venue rounding fabricates a fill price. This override takes precedence over scenarios 4
and 5 below.
§Returns
Returns Some(Decimal) if a valid reconciliation price can be calculated, None otherwise.
§Notes
The function handles five scenarios:
- Position to flat:
reconciliation_px=current_avg_px(close at current average) - Flat to position:
reconciliation_px=target_avg_px - Position flip (sign change):
reconciliation_px=target_avg_px(due to value reset in simulation) - Reduction: target average price by default (remaining entry average is unchanged)
- Accumulation: weighted average formula by default