Post
Topic
Board Trading Discussion
Topic OP
Writing an exchange can be hard
by
aes1
on 26/04/2013, 10:52:56 UTC
Consider this case (ignoring transaction fees for a second):

There are sell orders (1)  for 1 BTC @ 150 USD, and another (2) for 1 BTC @ 160 USD... Now someone wants to buy both of them. So he figures that it's a good idea to put in a buy order (3) for 2 BTC @ 155 USD. What happens?

If the exchange wants to be nasty, it can combine orders (1) and (3), buy the bitcoin from the seller at 150 USD and sell it to buyer at 155 USD. Exchange makes a profit of 5 USD and both the seller and the buyer got what they wanted (sort of). The leftover of the buy order (1 BTC @ 155 USD) is left on the order book.

If the exchange wants to be nice, it can combine orders (1) and (3), but this time decide that whoever comes later gets the better price. So the buyer gets 1 BTC at 150 USD, and the order of 1 BTC @ 155 USD is again left on the order book. This has the additional problem that the prices are indeterministic: if (1) is placed slightly after (3), the trade takes place at 155 USD - if not, it takes place at 150 USD.

So another solution is to execute the two best orders at the price halfway between bid and ask price. So the trade would take place at 152.5 USD.  

But none of the solutions gets the buyer what he originally wanted, i.e. 2 BTC at the average price of 155 USD.

So the "perfect" solution is to write a filling algorithm that goes through the sell orders starting from the most affordable, consuming bitcoins until the average price becomes 155 USD. (Yeah, he might've just put in a market order for 2 BTC, but what if he wanted to be *sure* that he'd get them at @155 or better?)

Or is it? Do you people actually care about this stuff?

(Because, damn it, implementing these kind of "perfect" things can get complex really quickly. Especially when you consider all of the different cases: usually the volumes between paired orders do not balance up, so you have to split orders into two; now there's three different ways how buy/sell orders are executed partially, then there's fees and balance reservations to consider, and then there are rounding errors... after a while you start to realize that it's actually quite a bit of work to make an actually working exchange. I wonder what all of those exchanges did that went belly up...)