Live data from Hacker News

Simulating a Market in Ruby

petekeen.net

11–20 of 25 posts

Re: Simulating a Market in Ruby

#11
Limit order books need to need to be very very quick as they usually live in exchanges and process a lot of orders. I've written a few limit order books, I found it difficult to beat this approach.

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.

Re: Simulating a Market in Ruby

#12

Limit order books need to need to be very very quick as they usually live in exchanges and process a lot of orders. I've written a few limit order books, I found it difficult to beat this approach. 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 seem…

Cool, this sounds like a fast approach. The implementation I wrote is very much not optimized at all. I wanted it to be correct-ish and easy to understand.

Re: Simulating a Market in Ruby

#13
First time I've heard of Readme Driven Development, is this a thing?

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!

Re: Simulating a Market in Ruby

#14

Limit order books need to need to be very very quick as they usually live in exchanges and process a lot of orders. I've written a few limit order books, I found it difficult to beat this approach. 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 seem…

I am curious about how you deal with matches that are not exact? > if a value exists at that key you fill the order (or partial fill) If there is an existing sell for 100 and a buy comes in for 101 your hash will miss the 100 entry, do you need to do a scan down the array looking for potential matches?

Re: Simulating a Market in Ruby

#15
Though less helpful if you're trying to simulate an actual market rather than building a new one, another approach to designing a trading market is to use market scoring rules, which can be somewhat simpler conceptually (as least as far as the code goes):

http://blog.oddhead.com/2006/10/30/implementing-hansons-mark...

Re: Simulating a Market in Ruby

#16
We actually have all of our new developers build a simple working stock exchange when they first start at Benzinga.

It really helps those who don't come from a finance background to understand what's actually going on.

Re: Simulating a Market in Ruby

#17

Limit order books need to need to be very very quick as they usually live in exchanges and process a lot of orders. I've written a few limit order books, I found it difficult to beat this approach. 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 seem…

I am curious about how you deal with matches that are not exact? > if a value exists at that key you fill the order (or partial fill) If there is an existing sell for 100 and a buy comes in for 101 your hash will miss the 100 entry, do you need to do a scan down the array looking for potential matches?

You're not going to be able to get away from traversing the order book for every match. Another subtle differentiating factor is the time at which the potential matches entered the order book. Obviously priority is always given to price (better price always taken first) but assuming 2 potential orders came into the book for the same price, the one that got there first will be matched.

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.

Re: Simulating a Market in Ruby

#18

First time I've heard of Readme Driven Development, is this a thing? 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!

http://tom.preston-werner.com/2010/08/23/readme-driven-devel...

It's my understanding that this is the original article.

Re: Simulating a Market in Ruby

#19

Limit order books need to need to be very very quick as they usually live in exchanges and process a lot of orders. I've written a few limit order books, I found it difficult to beat this approach. 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 seem…

> Insertion into the book is then largely O(1) as few people bid/offers are exactly the same price.

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?

Re: Simulating a Market in Ruby

#20
post #17

Earlier quoted context omitted.

I am curious about how you deal with matches that are not exact? > if a value exists at that key you fill the order (or partial fill) If there is an existing sell for 100 and a buy comes in for 101 your hash will miss the 100 entry, do you need to do a scan down the array looking for potential matches?

You're not going to be able to get away from traversing the order book for every match. Another subtle differentiating factor is the time at which the potential matches entered the order book. Obviously priority is always given to price (better price always taken first) but assuming 2 potential orders came into the book for the same price, the one that got there first will be matched. Another thing to think about is…

You can definitely get away with traversals - and they are often required. The simplest method would be to use a sorted list. The head of the list is always the best price, so comparing the best bid against the best offer is linear and can be performed every time the orders are modified. Order insertion is naively O(log n) as the list is sorted, but this can be optimized by keeping a journal of orders and only immediately inserting orders close to the best price. You can then insert from the journal into the order book as time allows or if the best price moves a large enough amount.

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?

Post reply on HN