fastme - Fast extensible matching engine Go implementation.

Related tags

Financial fastme
Overview

fastme (fast golang matching engine) library

Go Report Card

Design

The library solves the task of recalculating the exchange order book. A classic matching engine consists of limit orders (orders) for buying or selling (exchanging) some commodity for another commodity, usually currency.

The distinctive feature of the library is that:

  • No predetermined price levels
  • There is no restriction on the types of numeric values used
  • When working within integers, performance is achieved up to 500k transactions per second
  • Contains all necessary dependencies
  • There is support for write-off of commissions like maker and taker for working on cryptocurrency assets

Description of interfaces and data types

All mathematical recalculation operations of the order book are designed to work in the system of business logic, so the engine itself works with the input interfaces and notifies the business logic about the events that occurred during the exchange of goods. BL decides what to do with this data (e.g., write off funds from the corresponding wallet). For the correct operation of the exchange it is necessary to implement the interfaces described in the file ```echange.go``.

Asset

type Asset string

Describes the name of some exchange asset. Matching engine notifies the business logic of the event with the product and Asset type, not string, for clarity.

Volume

type Volume struct {
	Price    Value
	Quantity Value
}

When making a transaction or closing a position, the matching engine also notifies about the volume of the transaction of Volume type. It contains the value of the amount and quantity of the exchange asset.

Value

// Value calculates math operations
type Value interface {
	// Add is an "+" operation
	Add(Value) Value

	// Sub is an "-" operation
	Sub(Value) Value

	// Mul is an "*" operation
	Mul(Value) Value

	// Cmp returns 1 if self > given, -1 if self < given and 0 if self == given
	Cmp(Value) int

	// Sign returns 1 if Value > 0, -1 if Value < 0 and 0 if Value == 0
	Sign() int

	// Hash returns any string representation of the Value
	Hash() string
}

For recalculation and storage of numbers in the order of the order book it is necessary to implement certain mathematical operations. To dwell on a certain type of data (eg int.Big) is obviously inconvenient because of the subsequent severe restriction on the related type. Therefore, the type Value has appeared, in which you can wrap this interface (example in the file ```engine_test.go``).

Add, Sub и Mul

Must return a new object, not a changed current one.

Cmp и Sign

Must return the sign of a number to be able to compare the two numbers, as indicated in the help.

Hash

Does not have to return the hash number. It is enough to return a unique string for a specific single number. In most cases, the conversion of a number to a string will do. This function is used to store price levels and add orders to the queue at a specific price. This interface is enough to implement all the mathematics of exchange.

Wallet

type Wallet interface {
	// Balance returns current wallet balance for given asset
	Balance(context.Context, Asset) Value

	// UpdateBalance calls by matching engine to update wallet balance
	UpdateBalance(context.Context, Asset, Value)

	// InOrder returns amount of asset in order
	InOrder(context.Context, Asset) Value

	// UpdateInOrder calls by matching engine to inform about freezed amount in order
	UpdateInOrder(context.Context, Asset, Value)
}

In the course of the transaction, the matching engine uses the interface Wallet in order to correctly transfer or write off funds in exchange wallets. In a wallet there is an obligatory balance Balance, which contains the current balance of a wallet, and also InOrder balance, which denotes the quantity of an asset in an order book.

Functions are called by the engine when calculating the request. Context is passed through from the functions of placing and canceling an order, so it is possible to place, for example, database record objects.

Functions marked as optional may be plugged and do not affect the operation of the engine, but are useful for taking into account the number of assets in the order book when calculating the exchange business logic.

Balance и InOrder

Must return the current balance of the wallet for further recalculation operations.

UpdateBalance и UpdateInOrder

Must update the balance of the wallet when the transaction.

Order

type Order interface {
	// ID returns any unique string for order
	ID() string

	// Owner returns wallet to debit or credit asset on exchange process
	Owner() Wallet

	// Sell returns true if order for selling, true otherwise
	Sell() bool

	// Price retuns order price
	Price() Value

	// Quantity returns current order quantity
	Quantity() Value

	// UpdateQuantity calls by matching engine to set new order quantity
	UpdateQuantity(Value)
}

The main unit of exchange is an order. Limit requests are stored in the RAM of the matching engine and are deleted from it as soon as the specified quantity is completed. For engine operation it is necessary to implement the specified methods.

ID

Must return the unique identifier of the order. In case of repetition, the request will be rejected.

Owner

Must return wallet, which owns this order. This method can be implemented in various ways, for example, through the wallet manager when creating an order. Or keep a pointer to the wallet in the request and return it.

Sell

Returns true if the order for sale.

Price

Returns the desired limit value at which you are willing to buy or sell the asset. If the value is zero, the order will be fully executed at the current market price (market order) if there are sufficient funds in your wallet to buy or sell.

Quantity

Must return the remaining amount in the unfulfilled order.

UpdateQuantity

It is called by the engine when recalculating and executing a transaction, notifying the business logic about the current amount of the asset remaining.

FeeHandler

type FeeHandler interface {
	// HandleFeeMaker calls by  matching engine and provide data to correct output value for fee processing
	HandleFeeMaker(context.Context, Order, Asset, Value) (out Value)

	// HandleFeeTaker calls by  matching engine and provide data to correct output value for fee processing
	HandleFeeTaker(context.Context, Order, Asset, Value) (out Value)
}

Called at the time of the transaction in order to write off the commission. In the current version, the commission is written off by processing. For example, 1 unit of goods has been sold and 0.9 units will be credited to the balance due to the specifics of the engine. This parameter is optional. The handler is divided into Maker - market maker. That is a limit order. And Taker - a market order (or a market part of a limit order).

HandleFeeMaker, HandleFeeTaker

The context of a constraint, information on a certain asset transaction is received at the entrance. The function should return inValue - feeValue to out value.

EventListener

type EventListener interface {
	OnIncomingOrderPartial(context.Context, Order, Volume)
	OnIncomingOrderDone(context.Context, Order, Volume)
	OnIncomingOrderPlaced(context.Context, Order)

	OnExistingOrderPartial(context.Context, Order, Volume)
	OnExistingOrderDone(context.Context, Order, Volume)
	OnExistingOrderCanceled(context.Context, Order)

	OnBalanceChanged(context.Context, Wallet, Asset, Value)
	OnInOrderChanged(context.Context, Wallet, Asset, Value)
}

The interface takes part in the operations of placing and withdrawing an order. It is used to notify the higher business logic about all events that occurred during order processing. There are 2 order types - incoming and existing. Existing order is a limit order (with it's balance), which is located in the order book.

OnIncomingOrderPartial

Called at partial execution of an incoming order.

OnIncomingOrderDone

Called when an incoming order is completed.

OnIncomingOrderPlaced

Called when the incoming order (or the balance of partial execution) has entered the queue of the matching engine.

OnExistingOrderPartial

Called at partial limit order execution.

OnExistingOrderDone

Called when the limit order is completed.

OnExistingOrderCanceled

Called when the limit order is canceled.

OnBalanceChanged

Called when the balance on your wallet changes as a result of processing the order. Notifies the recipient about the new balance.

OnInOrderChanged

Called in case of change of balance in orders on your wallet as a result of processing the order. Notifies the recipient of the new value of the amount of asset in the order book.

Functionality

The main functions of processing incoming orders have been implemented to manage the order book.

func NewEngine(base, quote Asset) *Engine

Initializes the matching engine. Allocates memory for data.

func (e *Engine) SetFeeHandler(h FeeHandler)

Sets the fee handler.

func (e *Engine) PlaceOrder(ctx context.Context, listener EventListener, o Order) (err error)

Processes incoming order. Recalculates the state of the order book. Returns the following errors in case of impossibility to process the order:

var (
	ErrInvalidQuantity      = errors.New("Quantity could not be less or equal zero")
	ErrInvalidPrice         = errors.New("Price could not be less zero")
	ErrInsufficientQuantity = errors.New("Insufficient quantity to calculate market price")
	ErrInsufficientFunds    = errors.New("Insufficient funds to process order")
	ErrOrderExists          = errors.New("Order with given ID already exists")
	ErrOrderNotFound        = errors.New("Order with given ID not found")
)

func (e *Engine) CancelOrder(ctx context.Context, listener EventListener, o Order)

Removes the specified order from the order book.

func (e *Engine) PushOrder(ctx context.Context, o Order)

Puts the order in a queue without applying mathematical recalculation. It is used to restore the glass from the database.

func (e *Engine) Quantity(sell bool, priceLim Value) Value

Returns the volume of the asset to a certain price.

func (e *Engine) Price(sell bool, quantity Value) (Value, error)

Returns the market value for the specified amount of asset.

func (e *Engine) Spread() (bestAsk, bestBid Value)

Returns the value of the price spread in the order book. nil if there are no bids in one or two directions.

func (e *Engine) FindOrder(id string) (Order, error)

Returns an order by its identifier or error ErrOrderNotFound

func (e *Engine) Orders() (orders []Order)

Returns the list of limit orders that are in the order book.

func (e *Engine) OrderBook(iter func(asks bool, price, volume Value, len int))

Iterates price levels by returning information about price, order volume and queue length.

Work algorithm

The engine structure contains a linked list of orders and a search tree for the optimal price to insert or place.

Owner
LLC Newity
LLC Newity
Go implementation of Fowler's Money pattern

Money GoMoney provides ability to work with monetary value using a currency's smallest unit. This package provides basic and precise Money operations

Raymond 1.1k Jun 22, 2022
Matching Engine for Limit Order Book in Golang

Go orderbook Improved matching engine written in Go (Golang) Features Standard price-time priority Supports both market and limit orders Supports orde

Karlson Lee 272 Jun 26, 2022
A distributed fault-tolerant order book matching engine

Go - Between A distributed fault-tolerant order book matching engine. Features Limit orders Market orders Order book depth Calculate market price for

Daniel Gatis 1 Dec 24, 2021
An extremely fast Go (golang) HTTP router that supports regular expression route matching. Comes with full support for building RESTful APIs.

ozzo-routing You may consider using go-rest-api to jumpstart your new RESTful applications with ozzo-routing. Description ozzo-routing is a Go package

Ozzo Framework 438 Jun 15, 2022
Antch, a fast, powerful and extensible web crawling & scraping framework for Go

Antch Antch, inspired by Scrapy. If you're familiar with scrapy, you can quickly get started. Antch is a fast, powerful and extensible web crawling &

null 233 Jun 4, 2022
:rotating_light: Is a lightweight, fast and extensible zero allocation HTTP router for Go used to create customizable frameworks.

LARS LARS is a fast radix-tree based, zero allocation, HTTP router for Go. view examples. If looking for a more pure Go solution, be sure to check out

Go Playgound 387 Jun 11, 2022
Fast, multithreaded, modular and extensible DHCP server written in Go

coredhcp Fast, multithreaded, modular and extensible DHCP server written in Go This is still a work-in-progress Example configuration In CoreDHCP almo

CoreDHCP 742 Jun 21, 2022
Fast extensible file name sanitizer that works in Windows/Linux

Sanity Sanity is a fast and easily extensible file name (and in fact any other string) sanitizer. Usage Built-in rule set Sanity provides a sensible d

null 2 Jun 8, 2022
A highly extensible Git implementation in pure Go.

go-git is a highly extensible git implementation library written in pure Go. It can be used to manipulate git repositories at low level (plumbing) or

go-git 3.6k Jun 29, 2022
Multi-String Pattern Matching Algorithm Using TrieHashNode

Multi-String Pattern Matching algorithm. This implementation is inspired from Aho-Corasick algorithm Getting Started modelA = mspm.NewModel("mspm_mode

Sujit Shakya 17 Jan 23, 2022
Quick and easy expression matching for JSON schemas used in requests and responses

schema schema makes it easier to check if map/array structures match a certain schema. Great for testing JSON API's or validating the format of incomi

Jaap Groeneveld 17 May 12, 2022
agrep-like fuzzy matching, but made faster using Golang and precomputation.

goagrep There are situations where you want to take the user's input and match a primary key in a database. But, immediately a problem is introduced:

Zack 41 Nov 30, 2021
String-matching in Golang using the Knuth–Morris–Pratt algorithm (KMP)

gokmp String-matching in Golang using the Knuth–Morris–Pratt algorithm (KMP). Disclaimer This library was written as part of my Master's Thesis and sh

Patrick-Ranjit D. Madsen 38 Dec 16, 2021
Go library that provides fuzzy string matching optimized for filenames and code symbols in the style of Sublime Text, VSCode, IntelliJ IDEA et al.

Go library that provides fuzzy string matching optimized for filenames and code symbols in the style of Sublime Text, VSCode, IntelliJ IDEA et al. This library is external dependency-free. It only depends on the Go standard library.

Sahil Muthoo 1.1k Jun 26, 2022
efficient string matching in Golang via the aho-corasick algorithm.

aho-corasick Efficient string matching in Golang via the aho-corasick algorithm. x20 faster than https://github.com/cloudflare/ahocorasick and x3 fast

Petar Dambovaliev 27 Jun 9, 2022
WhatsApp Bot to auto join groups with subjects matching whitelist.

WhatsApp AutoJoin Bot Premise: My Uni uses WhatsApp for most unofficial communication during coronavirus "study from home". Naturally students share h

Anchit Bajaj 4 May 11, 2022
This is a "simple" game server. Main functionalities are matching and establishing a connection between players

Game Server This is a "simple" game server. Main functionalities are matching and establishing a connection between players How to Run? run the server

eco 7 Feb 6, 2022
radix: a go radix tree with nearest matching

radix implements a radix tree. The package only provides a single Tree implementation, optimized for sparse nodes.

Ahmed W. 6 Feb 6, 2022
Act is a task runner and supervisor with some great features like act name matching, subacts, etc. We use this in nosebit workspaces.

Act Act is a task runner and supervisor tool written in Go which aims to provide the following features: process supervision in a project level allow

Nosebit 6 May 8, 2022
Print lines matching a pattern in repositories using GitHub API

gh-grep Print lines matching a pattern in repositories using GitHub API Usage $ gh grep func.*schema.Schema --include=**/*.go --owner k1LoW --repo tbl

Ken’ichiro Oyama 119 Jun 25, 2022
Trade Matching / Transaction System Load Testing Solution

Load Generation System for Trade Matching Systems Operation Users select one of the following options from thew Test Management Portal: Generate a new

Traiano Welcome 1 Feb 25, 2022
A simple go tool, that calculates the allocated resources from all nodes matching the label selector.

kube-allocated-resources This is a simple go tool, that calculates the allocated resources from all nodes matching the label selector. Build Build on

Yusuf Kör 1 Jan 12, 2022
Json-match - Command line util for matching values in a JSON input

json-match Match JSON input by specifying key and value > json-match -json '{\"p

Trond Boksasp 0 Jan 12, 2022
watch for file changes (matching a suffix whitelist) in a directory tree and run a command when they change

watchspawn what is it? Watches for file creates and writes in and below the current directory and when any of them (matching a suffix list) change, ru

John Slee 0 Jan 16, 2022
Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

koanf (pronounced conf; a play on the Japanese Koan) is a library for reading configuration from different sources in different formats in Go applicat

Kailash Nadh 1k Jun 26, 2022
Pluggable, extensible virtual file system for Go

vfs Package vfs provides a pluggable, extensible, and opinionated set of file system functionality for Go across a number of file system types such as

C2FO 174 Jun 17, 2022
Gomol is a library for structured, multiple-output logging for Go with extensible logging outputs

gomol Gomol (Go Multi-Output Logger) is an MIT-licensed structured logging library for Go. Gomol grew from a desire to have a structured logging libra

Kristin Davidson 18 Dec 20, 2021
An easy to use, extensible health check library for Go applications.

Try browsing the code on Sourcegraph! Go Health Check An easy to use, extensible health check library for Go applications. Table of Contents Example M

Claudemiro 431 May 31, 2022
An simple, easily extensible and concurrent health-check library for Go services

Healthcheck A simple and extensible RESTful Healthcheck API implementation for Go services. Health provides an http.Handlefunc for use as a healthchec

Ether Labs 221 May 24, 2022