calculate_reconciliation_price

Function calculate_reconciliation_price 

Source
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>,
) -> 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 with the correct average price, accounting for the netting simulation logic.

§Returns

Returns Some(Decimal) if a valid reconciliation price can be calculated, None otherwise.

§Notes

The function handles four scenarios:

  1. Position to flat: reconciliation_px = current_avg_px (close at current average)
  2. Flat to position: reconciliation_px = target_avg_px
  3. Position flip (sign change): reconciliation_px = target_avg_px (due to value reset in simulation)
  4. Accumulation/reduction: weighted average formula