|
August 19, 2011, 03:06:07 PM |
|
Trying to work out in my head how an exchange works, specifically how the database tables are designed and what happens internally in the database when someone places an order.
Maybe there's open-source code out there already, but it's kind of fun to do this kind of math/problem solving with just pen and paper (or notepad.exe!)
So, let's say the db has the following orders:
bid, 2.00000000, 10.70000000 bid, 1.00000000, 10.80000000 ask, 2.00000000, 11.00000000 ask, 3.00000000, 11.20000000 <- let's call this order X
Now what happens when these hypothetical orders are placed:
bid, 3.00000000, 10.70000000 check the lowest ask -> 11. This bid is less than the lowest ask, so no trade occurs, the order it simply gets added to the db.
bid, 3.00000000, 11.50000000 check the lowest ask -> 11. This bid is above the lowest ask, so check how many coins are available at that lowest price -> 2. This only fills part of the order, so check the next lowest ask -> 11.2. Check how many coins are available at that price -> 3. This is more than enough to fill the order, so the order is complete, however this ask also has 2 btc left to fill.
Question: In this scenario, when order X is only partially filled, would it be best to mark the row as complete and add a new entry with the remaining amount, or just edit the row to show the new amount?
eg.
insert method: db before: ask, 3.00000000, 11.20000000 db after: ask, 3.00000000, 11.20000000, complete ask, 2.00000000, 11.20000000
or edit method: db before: ask, 3.00000000, 11.20000000 db after: ask, 2.00000000, 11.20000000
|