Git Product home page Git Product logo

match's Introduction

Introduction

This is a proof of concept for a simple matching engine for a tranding platform. Althought the proof of concept does not cover it but in real life, the overall solution needs to address the following.

  1. Concurrency and Collision Detection
  2. Latency (Could be network, could be speed of execution, could be because of bad garbage collection).
  3. State Management.

To solve collision issue, we deal this as stream computing problem. and split the incoming order stream into two binary heap priority queues.

  1. Sell Queue (With the smallest at the top).
  2. Buy Queue

The Priority Queue, an implementation of binary heap, with smallest item on top is an obvious choice for Sell Queue. Since the complexity for insertion is O(log n) for both, but when it comes to reading, the match is always at the top of the heap.

The best algorithm for chosing the best buy match against a sell order, this is where it gets tricky. For buying match, our first priority is the to get the closest buy order that came first. The binary search tree is binary by definition, i.e. the sort takes place based on one condition. I am implementing a simple array here, with simple filtering.

Note on Real-life Problem

In real life, on an exchange with a large through put, we will have multiple machines doing the matching and sending to processing queue, the collision detection would require matches to be verfied against another lock stream before being notified back to user. Also, there would be concurrency issues. The over state-management would require some heuristics. This is a good problem to solve :).

Getting Started

Pre-requisites

  1. Node v8.11 or higher
  2. Docker

Installation

Depending upon the package manager of choice, i.e. npm, yarn or npx.

<package-manager> install

For me, my go choice is yarn, so

yarn install

Simulation

Yarn

The simulation requires a firebase firestore db. The configuration can be found at

./lib/simulate.ts

just update serviceAccount and const and databaseURL key when intializing.

const serviceAccount = require('./firebase.json');

firebase.initializeApp({
  credential: firebase.credential.cert(serviceAccount),
  databaseURL: 'https://simplex-e7b03.firebaseio.com',
});
docker-compose up -d
yarn simulate

NPM

docker-compose up -d
npm run simulate

Testing

yarn test

Documentation

The documentation is generated automatically using excellent typedoc and can be found in /docs/ folder. To regenerate, run

yarn docs:html

Note on Performance

For the Sell Queue, the order ready to be placed always remain at the top of the queue, so WIN. for Buy Queue, we would be need a multidimensional buy quueue.

The below image is a result of 0.5 ms i.e. 2000 orders per second are being generated, and it does that succesfully.

Imgur

The execution time for BuyQueue would however increase if we have a long enough buy queue. A good solution would be to keep on trimming the queue by maintaing the last known market price and add a padding to price and trim the orders which doesn't feature in our hold bracket. that way, we mantain a small queue for processing at all times. We can think of multiple scenarios to optimize.

match's People

Contributors

debuggerpk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.