A trade matching engine matches up order bids and offers to generate trades. Matching engines allocate trades among competing bids and offers at the same price.
This call will calculate the "cost" to the trader should the be closed out (based on current position). The actual position should be passed, along with a Side
. If the trader holds a long position, the call should be:
closeOutPNL := matchingEngine.GetClosePNL(position.Size(), types.Side_Sell)
Internally, the matching engine will iterate over the order book (buy/sell side depending on the second argument). The "cheapest" orders will be used first. This means that, for the buy side, the price levels are traversed backwards (the levels are tracked in descending order). The sell side is stored in ascending order, and is traversed as-is.
This call has no effect on the order status (i.e. it won't change the Remaining
field of the orders).