Live data from Hacker News

Simulating a Market in Ruby

petekeen.net

1–10 of 25 posts

Re: Simulating a Market in Ruby

#3
post #2

it seems like MinHeap and MaxHead from the algorithms gem would do the same thing as your red-black tree and are built to hold ascending and descending keys

That would work but a heap isn't a totally ordered data structure so if we were to display the order book it may look weird with things slightly out of place. An RB tree guarantees the order.

Re: Simulating a Market in Ruby

#4
post #3
post #2

it seems like MinHeap and MaxHead from the algorithms gem would do the same thing as your red-black tree and are built to hold ascending and descending keys

That would work but a heap isn't a totally ordered data structure so if we were to display the order book it may look weird with things slightly out of place. An RB tree guarantees the order.

Gotcha!

Re: Simulating a Market in Ruby

#5
In our example, if Fred didn't look at the book and decides to put in a buy for $10.05, he'll get his 10 shares at $10.05 and Pete would make out like a bandit and the price would briefly shoot up to $10.05.

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).

Re: Simulating a Market in Ruby

#6
Typo in the third paragraph after 'Basic Concepts'?

'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'.

Re: Simulating a Market in Ruby

#7
post #5

In our example, if Fred didn't look at the book and decides to put in a buy for $10.05, he'll get his 10 shares at $10.05 and Pete would make out like a bandit and the price would briefly shoot up to $10.05. 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…

> 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.

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.

Post reply on HN