stex2-blog

Sound the opening foghorn

2021-07-29 / exchange,server,python / Sam

Feeling only slightly guilty, I made a start on stex2 without Tom last week. Having spent a bit of time chatting about the possibilities with Tom the week before, I was curious as to how a stock exchange actually works in the first place. What actually happens on an exchange? Annoyingly, most literature on the topic is aimed at non-technical prospective day-traders; focussing more on the terminology and market tips. All was not lost, as I found an excellent resource: Technical Specifications and User Guides from the London Stock Exchange group themselves. These documents are heavy on the detail and will come in handy later, but really all I wanted to know was what happens to buys and sells on an exchange?

After a bit of Googling and refining my terminology, I found the question I was trying to ask was “how are buy orders matched to sell orders”. The keyword of “matching” led me to discover that an OMS (order matching system) is the key part of a system handling trades, and that the Price-Time Priority matching algorithm is one of the most common.

I implemented a basic procedural Python program to play with order matching. I won’t walkthrough it here, but to highlight:

I had always thought of a stock exchange as a magic mystery box, but the rules that drive basic trade are quite simple. Of course, a real exchange handles a lot more complexity than what is implemented here - the LSE documents describe a whole host of order types, rules and auction types that we’ll have to wrap our heads around later - but I was really surprised at how easy it was to get this working. Clearly, the hard work isn’t going to be on the business logic side, but writing code that’ll stand up to tens of thousands of orders a second, in a reliable way. This is going to be fun.