Help us shape the future of Go! Take the 2026 Go Developer Survey

orderbook

package
v1.22.28 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package orderbook implements a high-performance order book for the DEX VM.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInsufficientLiquidity = errors.New("insufficient liquidity")
	ErrOrderNotFound         = errors.New("order not found")
	ErrInvalidPrice          = errors.New("invalid price")
	ErrInvalidQuantity       = errors.New("invalid quantity")
	ErrOrderExpired          = errors.New("order expired")
	ErrSelfTrade             = errors.New("self-trade not allowed")
)

Functions

This section is empty.

Types

type Order

type Order struct {
	ID          ids.ID      `json:"id"`
	Owner       ids.ShortID `json:"owner"`
	Symbol      string      `json:"symbol"`
	Side        Side        `json:"side"`
	Type        OrderType   `json:"type"`
	Price       uint64      `json:"price"`     // Price in quote asset (scaled by 1e18)
	Quantity    uint64      `json:"quantity"`  // Quantity in base asset (scaled by 1e18)
	FilledQty   uint64      `json:"filledQty"` // Already filled quantity
	StopPrice   uint64      `json:"stopPrice"` // For stop orders
	Status      OrderStatus `json:"status"`
	CreatedAt   int64       `json:"createdAt"`   // Unix timestamp in nanoseconds
	ExpiresAt   int64       `json:"expiresAt"`   // Unix timestamp in nanoseconds
	PostOnly    bool        `json:"postOnly"`    // Only add liquidity
	ReduceOnly  bool        `json:"reduceOnly"`  // Only reduce position
	TimeInForce string      `json:"timeInForce"` // GTC, IOC, FOK
}

Order represents a trading order in the orderbook.

func (*Order) IsActive

func (o *Order) IsActive() bool

IsActive returns true if the order can still be filled.

func (*Order) RemainingQuantity

func (o *Order) RemainingQuantity() uint64

RemainingQuantity returns the unfilled quantity.

type OrderStatus

type OrderStatus uint8

OrderStatus represents the status of an order.

const (
	StatusOpen OrderStatus = iota
	StatusPartiallyFilled
	StatusFilled
	StatusCancelled
	StatusExpired
)

func (OrderStatus) String

func (s OrderStatus) String() string

type OrderType

type OrderType uint8

OrderType represents the type of order.

const (
	Limit OrderType = iota
	Market
	StopLoss
	TakeProfit
	StopLimit
)

func (OrderType) String

func (t OrderType) String() string

type Orderbook

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

Orderbook maintains the bid and ask sides for a trading pair.

func New

func New(symbol string) *Orderbook

New creates a new orderbook for the given symbol.

func (*Orderbook) AddOrder

func (ob *Orderbook) AddOrder(order *Order) ([]*Trade, error)

AddOrder adds a new order to the orderbook. Returns executed trades if the order is matched.

func (*Orderbook) CancelOrder

func (ob *Orderbook) CancelOrder(orderID ids.ID) error

CancelOrder cancels an order by ID.

func (*Orderbook) GetBestAsk

func (ob *Orderbook) GetBestAsk() uint64

GetBestAsk returns the best (lowest) ask price.

func (*Orderbook) GetBestBid

func (ob *Orderbook) GetBestBid() uint64

GetBestBid returns the best (highest) bid price.

func (*Orderbook) GetDepth

func (ob *Orderbook) GetDepth(maxLevels int) (bids, asks []*PriceLevel)

GetDepth returns the orderbook depth up to maxLevels.

func (*Orderbook) GetMidPrice

func (ob *Orderbook) GetMidPrice() uint64

GetMidPrice returns the mid-market price.

func (*Orderbook) GetOrder

func (ob *Orderbook) GetOrder(orderID ids.ID) (*Order, error)

GetOrder returns an order by ID.

func (*Orderbook) GetSpread

func (ob *Orderbook) GetSpread() uint64

GetSpread returns the bid-ask spread.

func (*Orderbook) GetStats

func (ob *Orderbook) GetStats() (totalVolume, tradeCount uint64, lastTradeTime int64)

GetStats returns orderbook statistics.

func (*Orderbook) Match

func (ob *Orderbook) Match() []Trade

Match runs the matching engine and returns all trades from crossed orders. This is called per-block for deterministic matching. It processes all resting orders that can cross with each other.

func (*Orderbook) Symbol

func (ob *Orderbook) Symbol() string

Symbol returns the trading pair symbol.

type PriceLevel

type PriceLevel struct {
	Price    uint64   `json:"price"`
	Quantity uint64   `json:"quantity"`
	Orders   []*Order `json:"-"` // Orders at this level
}

PriceLevel represents a price level in the orderbook with aggregated quantity.

type Side

type Side uint8

Side represents the order side (buy or sell).

const (
	Buy Side = iota
	Sell
)

func (Side) String

func (s Side) String() string

type Trade

type Trade struct {
	ID          ids.ID      `json:"id"`
	Symbol      string      `json:"symbol"`
	MakerOrder  ids.ID      `json:"makerOrder"`
	TakerOrder  ids.ID      `json:"takerOrder"`
	Maker       ids.ShortID `json:"maker"`
	Taker       ids.ShortID `json:"taker"`
	Side        Side        `json:"side"`        // Taker's side
	Price       uint64      `json:"price"`       // Execution price
	Quantity    uint64      `json:"quantity"`    // Filled quantity
	MakerFee    uint64      `json:"makerFee"`    // Fee paid by maker
	TakerFee    uint64      `json:"takerFee"`    // Fee paid by taker
	Timestamp   int64       `json:"timestamp"`   // Execution timestamp
	BlockNumber uint64      `json:"blockNumber"` // Block where trade occurred
}

Trade represents a filled trade between two orders.

Jump to

Keyboard shortcuts

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