Simulating a Market in Ruby
petekeen.net
Simulating a Market in Ruby
1–10 of 25 posts
Re: Simulating a Market in Ruby
#2Re: Simulating a Market in Ruby
#3it 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
Re: Simulating a Market in Ruby
#4it 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
#5There 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'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
#7In 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…
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.
Re: Simulating a Market in Ruby
#8Typo 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'.