Developing Matching Engine to Simulate Effects Algorithmic Trading Strategies on Financial Markets

Rob Graumans
Nov 3 · 6 min read

Last weekend, I was bored and looking for a challenge. I decided to try and develop a trade matching engine. For those who don’t know what a matching engine is, a brief explanation will follow. If you do know what a matching engine is, please skip to the “Simulations” section.

So: what is a matching engine? Well, if you want to buy shares ING (for example), you send a buy-order to a financial market (“I want to buy 10 shares ING for $25”). Now the matching engine sees your order coming in, and starts looking if there is someone willing to sell shares ING for $25 (or less). If there is, a transaction will occur. If not, or if your orders matches only partially (= you can only buy 6 shares for $25), your remaining order will appear in the “orderbook” on the buy-side (think an excel-sheet with two columns, buy and sell. You can also see figure below). Now everyone sees there is someone willing to buy some amount of shares ING for $25.

In practice, your buy-order might match multiple sell orders (someone willing to sell at $20, someone at $24, etc.). If that is true, you will trade first with the person willing to sell for the lowest price. If after that you still have remaining shares you want to buy, you will go to the next order. This will repeat until you have no remaining shares to buy or there are no remaining sell-orders matching the price at which you are willing to buy. The remaining order will appear in the orderbook. The opposite goes if you want to sell shares.

You can download the code here https://github.com/Bobbakov/orderbook . It is a “simple” matching engine because you can only send in one order-type (limit-orders) and there is only one market. I will add features later.

Simulations

This is all well and good, but what can you do with a matching engine? Well, now we have a way to match incoming orders (and initiate transactions), we can simulate a financial market!

Let’s say we send in 10,000 orders consecutively, some being buy-orders, some being sell-orders (chosen at random). They all have a price between 1 and 100 (at random), and a quantity between 1 and 10 (at random). What happens?

You can see what happens visually, by showing the orderbook after each order send in.

Type:

# Import module
from orderbook_module.py import *
# Initialize market
a = market()
# Start market. We want to see the orderbook and transactions between
# consecutive orders sent. We set speed to 1.
a.orderGenerator(10000, sleeptime = 1, mute_orderbook = False, mute_transactions = False)

The market starts generating orders, and you will see:

Note: the 4th column is price, and the 5th quantity. We call this guy “Bert”.

Seeing an orderbook change over time might be interesting for those fascinated by the micro-structure of financial markets (like I am), but if you are not, you can also just plot the price development over time:

a.plot()

So these are the prices of transactions occurring by randomly sending in 10,000 orders consecutively. In blue you see the “price”, in green and red the “volatility”.

Let’s add a market maker algorithm

In practice, not everyone sends in orders at random (or at least I hope so).

In practice there might also be a market maker, for example, always willing to buy or sell at some price.

What would happen if we would add a market maker to the market? Let’s check it out! Our market maker has a “dime-algorithm”, which always checks if he is the best bid (highest price to buy in the orderbook) and best offer (lowest price to sell in the orerbook). If he is, good. If not, improve the best bid/offer by 1 euros.

# Initialize new market
b = market()
# Generate 10000 random orders and (possible) market maker orders,
# one after the other
b.orderGenerator(10000, dime_algo = True)
# Plot the resultb.plot()

What happens?

It is clear: the volatility (= price swings) decreases (as you would expect). This can be seen even more clearly, by simulating a market that starts out sending in random orders, then introducing the combination of random orders and the dime algorithm:

So we have learned something. We can deduce that in a random market, introducing a market maker/dime algorithm decreases volatility. If you want to be truly sure, would want to run the simulation many more times, and see if you get the same result.

Simulation allows for stress-testing algorithms

You can use a similar set up to test the effects of other algorithmic trading strategies on the financial markets. For example: a trend-following algorithm, news-algorithm, etc.

Why would you want to do that? Different trading strategies might have different effects on the market (in terms of volatility, spread, etc.). It might be difficult — for both regulators and market parties — to evaluate the benefits/risks associated with particular algorithmic trading strategies.

That might (at least in part) be because different trading algorithms might have different effects in different market environments. Using simulations you can control for the environment, hence comparing algorithms using similar “input”. That way you are comparing apples with apples. Simulations are an excellent way to do so.

Simulation allows for “stress-testing” trading algorithms. You can see how they will behave in different market conditions. In our example, we assumed the market was random. But what if the market is “scared”? Willing to sell exponentially if the price goes down? You can measure the effects of any algorithm in such a market. A market maker might still decrease volatility. But a trend-following algorithm? We will see (in another article).

You can also introduce multiple markets, and algorithms trading between markets. I will do so in a future article.

It gets especially interesting if you add multiple trading algorithms in the mix. What happens if you — for example — have two dime algorithms? Or two trend following algorithms? One might be “good” (measured according some criteria), but the other not so.

Reinforcement learning

We are talking about rule-based algorithms. For example: an algorithm that sends in a buy order if he isn’t best bid. An algorithm send a market buy-order if the price-trend is up. This is interesting, but we can add a level of complexity.

What if we don’t tell an algorithm what to do except that it has to maximize profits? We can train it, via reinforcement learning, to figure out by itself how it’s going to do that. What happens if we add such an algorithm to the mix? Might we see — for example — that algorithms learn forms of order book manipulation, just because it turns out to be profitable? And in what kind of environments would that happen? We will see in the future.

I am curious what you guys think of all of this.

Rob Graumans

Written by

Philosopher, eager to learn and editor @Elsevier.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade