book

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package book is a limit order book for one instrument: price-time priority, partial fills, and cancels.

An incoming order first trades against the opposite side, best price first and, within a price, oldest first. Each trade happens at the RESTING order's price. Whatever is left over rests on its own side at its own limit price, behind every order already resting at that price.

Prices are integers in ticks (the exchange uses hundredths), so no float ever decides which order is better. A Book is not safe for concurrent use; the exchange holds one lock around every call.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadSide  = errors.New("side must be buy or sell")
	ErrBadPrice = errors.New("price must be positive")
	ErrBadQty   = errors.New("quantity must be positive")
	ErrDupID    = errors.New("an order with this ID is already in the book")
)

Errors an order can be refused with before it touches the book.

Functions

This section is empty.

Types

type Book

type Book struct {
	Symbol string
	// contains filtered or unexported fields
}

Book is one instrument's bids and asks.

func New

func New(symbol string) *Book

New returns an empty book.

func (*Book) Cancel

func (b *Book) Cancel(id string) (Order, bool)

Cancel removes the resting order with this ID and returns it as it was (Leaves is what was still open). ok is false when no such order rests, including one that has already filled.

func (*Book) Depth

func (b *Book) Depth(n int) (bids, asks []Level)

Depth returns up to n levels a side, best first.

func (*Book) Get

func (b *Book) Get(id string) (Order, bool)

Get returns a copy of the resting order with this ID.

func (*Book) OwnerOrders

func (b *Book) OwnerOrders(owner string) []Order

OwnerOrders returns copies of every order owner has resting, oldest first.

func (*Book) Resting

func (b *Book) Resting() int

Resting is how many orders rest on both sides.

func (*Book) Submit

func (b *Book) Submit(o Order) (fills []Fill, rested bool, err error)

Submit matches o against the opposite side and rests what is left. It returns the fills in the order they happened and whether a remainder rests.

type Fill

type Fill struct {
	MatchID   uint64
	Price     int64
	Qty       int64
	Aggressor Order
	Resting   Order
}

Fill is one trade between the incoming order and one resting order. Both orders are copies taken just after the trade, so their Leaves are what is open after it.

type Level

type Level struct {
	Price  int64 `json:"price"`
	Qty    int64 `json:"qty"`
	Orders int   `json:"orders"`
}

Level is one price on one side, as a depth view shows it.

type Order

type Order struct {
	ID      string
	Owner   string
	ClOrdID string
	Side    Side
	Price   int64
	Qty     int64
	Leaves  int64
	// Seq is the book's arrival counter, which is what "time" means in
	// price-time priority: a lower Seq at the same price trades first.
	Seq uint64
}

Order is one limit order. Qty is what was asked for and Leaves what is still open; Qty-Leaves has traded.

type Side

type Side byte

Side is the FIX Side(54) value: 1 buy, 2 sell.

const (
	Buy  Side = '1'
	Sell Side = '2'
)

The two sides, with the bytes FIX uses for them.

func (Side) String

func (s Side) String() string

Jump to

Keyboard shortcuts

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