I am a little confused about how orders get processed in a limit order book, hope to get a better understanding with this post.
If we start off with an order book
Asks: SELL 2 units at 300 SELL 2 units at 200 SELL 1 units at 100 Bids: BUY 1 units at 50
and a user places a new limit order which causes his account balance to decrease by
$400 (2x200 =
New Order: BUY 2 units at 200
Is the resulting order book supposed to be
Order book #1
Asks: SELL 2 units at 300 SELL 1 units at 200 Bids: BUY 1 units at 50
because his order of quantity
2 first matches with
1 units at 100 then matches
1 units at 200
or should the resulting order book be
Order book #2
Asks: SELL 2 units at 300 SELL 0.5 units at 200 Bids: BUY 1 units at 50
because his order is of
$400, so it first matches with
1 units at 100 for
$100, then the remaining
$300 matches with
1.5 units at 200.
Based on casually trading on crypto exchanges, I think we should end up with Order book #2.
However, based on reading some code for a Limit order book, it appears we should end up with Order book #1. If so, then what happens to the
$400 that was deducted from the trading account when the order of
BUY 2 units at 200 was first placed? Is the unused $100 returned back to the user's account after his order has been processed by the matching engine?