Suppose that the user's balance is debited before his order is sent to the matching engine. The debiting is done to ensure that the user has sufficient balance and that he is not submitting orders that he ultimately cannot afford.
For limit orders, the amount to be debited is trivial to estimate.
Debit(Limit buy) = price * qty (quote asset)
Debit(Limit sell) = qty (base asset)
For market sell orders, the amount to be debited is also trivially estimated:
Debit(Market sell) = qty (base asset)
How about market buy orders, however?
The price is not known before the order hits the book, so you cannot do
Debit(Market buy) = price * qty (quote asset)
You could do last price instead:
Debit(Market buy) = last price * qty (quote asset)
And then, once you know the actual price at execution, debit or credit an additional amount to make up the difference.
But what if last price < actual price
?
Then the user's balance will potentially become negative.
Of course you could require a buffer (e.g. 150% of the last price) to account for this but there is no guarantee that this buffer will be sufficient for preventing negative balance.
Am I missing something obvious here?