# How do buy and sell orders work?

How does buying and selling bitcoins work once the order is placed?

I just know that the order-matching engine is used for this purpose, but more information regarding this will be helpful.

New offers and orders are matched respectively with the best order and offer available until they are fulfilled. If there is more than one entry at the same price, the oldest entry will be matched first. If a new entry cannot be matched completely, it will remain in the order book until the remainder is matched or it expires.

See an example below:

## The initial order book

Offers:

``````[Bob] sell 1 BTC at \$1000
[Charlie] sell 2 BTC at \$1001, [Dolph] sell 1 BTC at \$1001
[Emilio] sell 1 BTC at \$1002
``````

Orders:

``````[Fabienne] buy 1 BTC at \$999
[George] buy 1 BTC at \$998
[Harry] buy 1 BTC at \$996, [Ivan] buy 5 BTC at \$996
``````

## Alice wants to buy some Bitcoins.

Alice wants to buy 2 BTC and is willing to pay up to \$2020. She adds an order for 2 BTC at \$1010.

The system will now try to match her order to offers already in the system, of course matching the best offer first.

As seen above, Bob is offering to sell at \$1000 and Charlie offers to sell at \$1001, her order matches with Bob's offer using it up completely. Her order has a second BTC though, which will be matched against Charlie's offer next, because Charlie entered his offer before Dolph, who is offering the same price.

After Alice's order was matched there are the following offers:

``````[Charlie] sell 1 BTC at \$1001, [Dolph] sell 1 BTC at \$1001
[Emilio] sell 1 BTC at \$1002
``````

and the following orders:

``````[Fabienne] buy 1 BTC at \$999
[George] buy 1 BTC at \$998
[Harry] buy 1 BTC at \$996, [Ivan] buy 5 BTC at \$996
``````

Note that Alice's order and Bob's offer are gone, and Charlie's offer is reduced by 1 BTC.

## Afterwards, Jeremy wants to sell some Bitcoins

Now Jeremy wants \$9,970 and has 10 BTC for sale. He creates an offer for 10 BTC at \$997. Currently there are only two orders that will buy at or over that price: His offer matches the orders of Fabienne and George. He gains \$999 and \$998 for two of his BTC, the remaining 8 BTC will stay as an offer.

After Jeremy's offer was partially matched there are the following offers:

``````[Jeremy] sell 8 BTC at \$997
[Charlie] sell 1 BTC at \$1001, [Dolph] sell 1 BTC at \$1001
[Emilio] sell 1 BTC at \$1002
``````

and the following orders:

``````[Harry] buy 1 BTC at \$996, [Ivan] buy 5 BTC at \$996
``````
• Excellent! Just one quick question: Cryptsy applies a fee of 0.2% when buying, and 0.3% when selling. Who pays that fee? In your sample, Alice ended up paying \$2001 for the coins. Is her account charged \$2005.002 , or does Bob receive only \$998 for his sale? Jan 19, 2014 at 3:00
• I think it is the easiest when the fee is applied after the order and offer have been matched. I.e. Alice's \$1000 are matched to Bob's 1 BTC, but Alice ends up receiving `1 BTC * 99.8% = 0.998 BTC`, meanwhile Bob receives `\$1000 * 99.7% = \$997`.
– Murch
Jan 19, 2014 at 16:02