Simulating a Market in Ruby
petekeen.net
petekeen.net
There is no market in the world that I know of that works that way. In the real world if you submit a marketable buy order (limit price > best offered price) the match will happen at the best offered price which is $10.00 in your example. If it clears out all the shares available at that price level it will continue to match at the next best price until the order is fully matched or the matching sweeps up to the order limit price.
Limit orders are the fundemental building block of a market. There are other order types but they're always built using one or more limit orders.
This is not technically true either. A market order by definition has no limit price.
I'm glad to see this article on HN. For you next step you might think about how you can make money by placing orders on both sides of the market (making a market).
Thanks, this makes a lot more sense. I'll fix the article.
> This is not technically true either. A market order by definition has no limit price.
I figured that a market order was implemented as a limit order with a bid over the ask, but it makes sense that real world markets would have a special case order type for it.
Deal with prices as an int not a float. This important as the price can then form the key of a hash table. Size of the hash table should be the limits of price in your book (including decimal places you support). This seems like a lot of memory, however the structures we're dealing with are small. Insertion into the book is then largely O(1) as few people bid/offers are exactly the same price.
Bid and offer can share the same hash table as they will be at opposite ends of the table (apart from orders that cross).
We can preallocate all active orders and form a link list of nodes inside the order book. Never allocate any memory on the fly as it is very expensive.
Order { Order *next; int price; int size; int side; }
This will allow fast insertion/removal and will keep a lot of the data in the level 2 cache. Of course you need to keep the list big enough to support all active orders.
Processing an order would involve working out if it's a buy or sell, then hashing the price, if a value exists at that key you fill the order (or partial fill).
Finally use taskset or something similar to make sure the OS puts your process on a dedicated hardware thread.
Another thing to think about is that some firms also don't always want price improvement for an asset. Typically these firms are custodians of other peoples assets and need to trade with equal and offsetting amounts between multiple accounts. If they get price improvement then they end up buying or selling a different quantity which then leaves them responsible for explaining the difference. A lot of times it's just more trouble than its worth.
There are a lot of other techniques too, but you definitely can't just let people buy or sell a different quantity. At least on any formal market I know of?
This is very much not true in my experience. Automated trades of any sort often hit the same prices, and trading strategies such as Iceberg which put many, many small orders will nearly always overrun with existing orders.
Which markets were you building this order book for?
Assuming I read you right, I think I would characterize this more as a pigeonhole sort[1] than a hash table.
Which makes sense: the goal here is a sorted collection. And, if you have a known range of values, it's hard to beat a pigeonhole sort for that.
http://blog.oddhead.com/2006/10/30/implementing-hansons-mark...
It really helps those who don't come from a finance background to understand what's actually going on.
I always lambast myself for continually rewriting the Readme as I go. Part of me sees it as a time drain, or at the very least an admission I've not done sufficient high level strategising upfront.
I do keep doing it though!
It's my understanding that this is the original article.
Plus, you ensured that there was a manual written. That might not happen otherwise, or it might be done in a rush because of the next project.
It was a different time, but I always found it to be an interesting anecdote.
'Andrew would like to buy 10 shares of TSLA and is willing to pay $9.95 per share'
but the order book shows '10 @ $9.99' under 'Sell'.