hftorderbook

package module
v0.0.0-...-0e4fc5c Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 3 Imported by: 0

README

Build Status

go-hft-orderbook

Golang implementation of a Limit Order Book (LOB) for high frequency trading in crypto exchanges. Inspired by this article.

Operations

  • Add – O(log M) for the first order at a limit, O(1) for all others
  • Cancel – O(1)
  • GetBestBid/Offer – O(1)
  • GetVolumeAtLimit – O(1)

Performance

  • Random generated insertion with limited number of price levels (10K levels) on average MacBook Pro: ~200ns/op or ~5M op/s

TODO

  • Object pool (Done)
  • Real data for benchmarks

Radicle URN

rad:git:hwd1yregaqj5mrp5dgc3gyuu6exszg84zr71su8j1f7t6oe9czxee1zzyzr

Documentation

Index

Constants

View Source
const MaxLimitsNum int = 10000

maximum limits per orderbook side to pre-allocate memory

Variables

This section is empty.

Functions

func NewBST

func NewBST() bst

func NewIndexMinPQ

func NewIndexMinPQ(size int) indexMinPQ

func NewMinPQ

func NewMinPQ(size int) minPQ

func NewOrdersQueue

func NewOrdersQueue() ordersQueue

func NewRedBlackBST

func NewRedBlackBST() redBlackBST

Types

type LimitOrder

type LimitOrder struct {
	Price float64
	// contains filtered or unexported fields
}

Limit price orders combined as a FIFO queue

func NewLimitOrder

func NewLimitOrder(price float64) LimitOrder

func (*LimitOrder) AddVolume

func (lo *LimitOrder) AddVolume(volumeToAdd float64)

func (*LimitOrder) Clear

func (this *LimitOrder) Clear()

func (*LimitOrder) Delete

func (this *LimitOrder) Delete(o *Order)

func (*LimitOrder) Dequeue

func (this *LimitOrder) Dequeue() *Order

func (*LimitOrder) Enqueue

func (this *LimitOrder) Enqueue(o *Order)

func (*LimitOrder) EnqueueBulk

func (lo *LimitOrder) EnqueueBulk(orders []*Order)

func (*LimitOrder) Peek

func (this *LimitOrder) Peek(index int) *Order

func (*LimitOrder) Size

func (this *LimitOrder) Size() int

func (*LimitOrder) SubtractVolume

func (lo *LimitOrder) SubtractVolume(volumeToSubtract decimal.Decimal)

func (*LimitOrder) TotalVolume

func (this *LimitOrder) TotalVolume() float64

type Order

type Order struct {
	Id       string
	UserId   string
	Volume   float64
	Next     *Order
	Prev     *Order
	Limit    *LimitOrder
	BidOrAsk bool
}

Single Order in an order book, as a node in a LimitOrder FIFO queue

type Orderbook

type Orderbook struct {
	Bids *redBlackBST
	Asks *redBlackBST
	// contains filtered or unexported fields
}

func NewOrderbook

func NewOrderbook() Orderbook

func (*Orderbook) ALength

func (this *Orderbook) ALength() int

func (*Orderbook) Add

func (this *Orderbook) Add(price float64, o *Order)

func (*Orderbook) AddBulk

func (ob *Orderbook) AddBulk(price float64, orders []*Order, isBid bool)

func (*Orderbook) BLength

func (this *Orderbook) BLength() int

func (*Orderbook) Cancel

func (this *Orderbook) Cancel(o *Order)

func (*Orderbook) ClearAskLimit

func (this *Orderbook) ClearAskLimit(price float64)

func (*Orderbook) ClearBidLimit

func (this *Orderbook) ClearBidLimit(price float64)

func (*Orderbook) DeleteAskLimit

func (this *Orderbook) DeleteAskLimit(price float64)

func (*Orderbook) DeleteBidLimit

func (this *Orderbook) DeleteBidLimit(price float64)

func (*Orderbook) GetBestBid

func (this *Orderbook) GetBestBid() float64

func (*Orderbook) GetBestOffer

func (this *Orderbook) GetBestOffer() float64

func (*Orderbook) GetVolumeAtAskLimit

func (this *Orderbook) GetVolumeAtAskLimit(price float64) float64

func (*Orderbook) GetVolumeAtBidLimit

func (this *Orderbook) GetVolumeAtBidLimit(price float64) float64

Jump to

Keyboard shortcuts

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