orderbook

package module
v0.0.0-...-30c4c93 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2021 License: MIT Imports: 1 Imported by: 0

README

Order Book

orderbook is a Go library which implements a Limit Order Book for a cryptocurrency exchange.

Disclaimer: This is a pet project and it wasn't used on production. Use it with precautions.

Based on

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NaturalPriceComparator

func NaturalPriceComparator(a, b interface{}) int

NaturalPriceComparator provides a way to compare 2 Price objects using natural order.

func OppositePriceComparator

func OppositePriceComparator(a, b interface{}) int

OppositePriceComparator provides a way to compare 2 Price objects using opposite natural order.

Types

type Instrument

type Instrument string

Instrument describes which markets exists.

var (
	BTC_USD Instrument = "btc-usd"
	BTC_ETH Instrument = "btc-eth"
)

type OrderBook

type OrderBook struct {
	// contains filtered or unexported fields
}

OrderBook represents both bids (BUY) and asks (SELL). For each one it will hold a height balanced binary search tree. The order book is a tree in which the price are keys and the values are volumes.

func NewOrderBook

func NewOrderBook(instrument Instrument) *OrderBook

func (*OrderBook) Add

func (o *OrderBook) Add(side Side, price Price, quantity Volume)

Add adds a BUY or SELL order into the order orderbook according to its price. If the order already exists it will increase the order quantity.

func (*OrderBook) GetAskSize

func (o *OrderBook) GetAskSize(price Price) Volume

GetAskSize returns the ask (SELL) volume size otherwise 0

func (*OrderBook) GetBestAsk

func (o *OrderBook) GetBestAsk() (Price, Volume)

GetBestAsk returns the best (highest) ask and its volume

func (*OrderBook) GetBestBid

func (o *OrderBook) GetBestBid() (Price, Volume)

GetBestBid returns the best (highest) bid and its volume

func (*OrderBook) GetBidSize

func (o *OrderBook) GetBidSize(price Price) Volume

GetBidSize returns the bid (BUY) volume size otherwise 0

func (*OrderBook) Update

func (o *OrderBook) Update(side Side, price Price, quantity Volume)

Update updates a BUY or SELL node (price) volume. If the new volume is higher than 0, then it will be added. If the new volume is lower or equal than zero, the node itself will be removed.

type Price

type Price uint64

Price describes the price of a market order. To facilitate usage we avoid using float. Instead we must store prices as unsigned integers by multiplying it by 100000000.

Example: 1.00000061 should be stored as 100000061

type Side

type Side int

Side is a set of types that defines which side the order was executed.

const (
	BUY Side = 1 + iota
	SELL
)

type Volume

type Volume uint64

Volume describes the bid/ask quantity of a market order. To facilitate usage we avoid using float. Instead we must store prices as unsigned integers by multiplying it by 100000000.

Example: 1.00000061 should be stored as 100000061

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL