Skip to main content

map_ws_execution

Function map_ws_execution 

pub fn map_ws_execution(
    msg: &OrderMessage,
    now: Instant,
    before: Option<Cumulative>,
) -> Option<Execution>
Expand description

A fill from the orders/events/ws WebSocket. Only messages that carry a fill: lastQuantity > 0 and an executionId, and not a corrected report (executionType 13, which restates an earlier message). The order’s cid is read as crate::map::order::message_cid says.

The price of the fill. before is the running total of the same order from the previous message the connector saw (cumulative_of). With it the price is exact:

  • the totals after this message minus before give the quantity and the value of this fill alone, and the price is value divided by quantity. A first fill of an order (before is None and executedQuantity equals lastQuantity) is the same with before of zero. Two fills at different prices give two different prices, not their average.
  • When the totals do not fit (a message was missed, so the growth of executedQuantity is not lastQuantity, or the message has no totals), the price is the best the message says: averagePrice of the order so far, else the price of the order. That is an average over earlier fills too, so it can be off by the difference between the fills. The price of every trade is in the history (ExecutionSource::executions), which is the way to settle it.