• Stars
    star
    396
  • Rank 106,388 (Top 3 %)
  • Language
    Go
  • Created almost 12 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple financial trading matching engine. Built to learn more about how they work.

matching_engine

===============

A simple financial trading matching engine. Built to learn more about how they work.

This system is not a complete implementation of a full matching engine system. It contains a basic set of internal components for a matching engine but doesn't include any networking components which would be required to actually use it in production.

msg

The msg package defines the basic messages which are processed by a matching engine. The system was designed to be entirely driven by these messages, so we find both the expected buy/sell limit-order type messages as well as new-trader and shutdown messages which change the state of the matching engine without actually engaging it trading activity.

The rationale behind this choice was that the state of a matching engine should be determined exclusively by the serise of messages which have passed through it. This allows us to perfectly reproduce any matching engine state simply by replaying these messages.

There is some initial work done on a binary serialisation format. This was intended to be used for storing messages in logs, and for sending over the network. I don't think the approach taken here was very well conceived, and I would probably use an efficient encoding library if I was to work on this now.

The maker.go file is interesting in that it generates random sets of buy/sell messages. While the code-base has many traditional run-code/assert-outcome style unit tests, we also employed a lot of system-invariant style tests with randomly generated test data.

matcher/pqueue

This is a priority queue implementation custom built to support the matching engine. This is definitely the most complex piece of code in the system. The pqueue is a pair of linked red-black trees. The two trees share the nodes. Specifically an OrderNode is a member of two red-black trees, one tree is ordered by price and the other is ordered by the id of the order. A feature of having a single order object linked to two trees is that when we remove an OrderNode from one tree we can remove it from the other, without having to perform a search through the tree to find it.

The red-black tree is an internal detail the publicly exposed type is the pqueue.MatchQueues this is a trio of red-black trees, one for buys and one for sells and one for all orders sorted by guid. This exposes the operations needed to submit a buy, or sell order as well as cancel an existing order (using its guid).

The testing approach used here is primarily invariant based testing. We perform operations on the rb-trees and then test to ensure that the structural invariants of the trees has not been violated. In the development of these trees I first started with a body of hand-written operations, followed with assertions on the result. Even after writing a very large number of unit tests I was not able to find any bugs. However, when I built the randomly generated invariant based testing I was able to find a number of subtle bugs and fix them

If I did this now I would have kept the 'normal' style unit tests as well as the more complex invariant style tests. Although invariant testing was more valuable in finding bugs, the 'normal' style of unit tests are easy to read and are a useful form of documentation of the expected behaviour of the rb-trees.

matcher

The matcher implements an actual matching engine. This uses a pqueue.MatchQueues to manage incoming orders. As each new order comes in an attempt is made to match the order, buy or sell, and the resulting matches are written to the output. Cancelling orders is supported, as-is shutting down the order book.

coordinator

This package is designed to allow us to wrap a matcher.M with an input and output queue. There are two implementations available, one which uses a Go channel and one which uses an imported high performance queue. The queue imported is from another project I authored which can be found at github.com/fmstephe/flib.

I would not use this approach if I was building this system again today. I think that the choice to make the matcher.M struct embed the coordinator.AppMsgHelper interface is unnecessarily complicated.

More Repositories

1

flib

A set of experimental extensions to the go standard library
Go
29
star
2

location_server

A location server which can tell clients about other nearby users
Go
14
star
3

queues

Collection of experimental concurrent queues
7
star
4

Tankwars

Yet another Tankwars clone in a javascript
JavaScript
7
star
5

lfqueue

Lock free concurrent queue, based on the java implementation ConcurrentLinkedQueue
Go
6
star
6

Erlang-Sat-Solver

A novel distributed Sat Solver written in Erlang.
Erlang
5
star
7

unsafeutil

A small utility for converting unsafely between string and []byte without allocations in Golang
Go
4
star
8

priority_queues

A collection of orphaned priority queues from my matching_engine project
Go
3
star
9

simpleid

A very simple concurrent id generater
Go
3
star
10

Masyu-Puzzle-Solver

A parallel implementation of a Masyu Solver in Java
Java
2
star
11

fatomic

Additional atomic methods in asm for Golang. Complementing the sync/atomic package
Go
2
star
12

P2P-Work-Sharing-Library

A helpful library for implementing masterless P2P work sharing (aka work stealing) networks primarily aimed at depth first search.
Java
2
star
13

Limited-Concurrent-QuadTree

An implementation of a quad tree allowing concurrent writes
Java
2
star
14

P2P-Distributed-Sat-Solver

A novel peer to peer approach to combinatorial search, demonstrated with a Sat Solver in Java.
Java
1
star
15

network_programming_with_go

Some worked examples from the excellent guide to networking in go (http://jan.newmarch.name/go/)
Go
1
star
16

HTML5-particle-simulator

A very simple particle simulator in HTML5
HTML
1
star
17

Go-Environment-Setup

A set of scripts for setting up development environment for Go
Shell
1
star
18

install_java_mvn

Simple scripts for a basic installation of java and maven on linux (Ubuntu)
Shell
1
star