match

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2023 License: GPL-3.0 Imports: 9 Imported by: 0

README

matching-engine

a matching-engine for crypto exchange

Feature

  1. order type (market, limit, ioc, post_only, fok)
  2. engine supports multiple markets
  3. high-speed. (all in memory)
  4. query order book depth

Get Started

 publishTrader := NewMemoryPublishTrader() // save trade into memory, if you want to pulish the trade to MQ, you can implement the interface
 engine := NewMatchingEngine(publishTrader)

 // market1
 market1 := "BTC-USDT"
 order1 := &Order{
  ID:       "order1",
  MarketID: market1,
  Type:     Limit,
  Side:     Buy,
  Price:    decimal.NewFromInt(100),
  Size:     decimal.NewFromInt(2),
 }

 _, err := suite.engine.AddOrder(order1)

Benchmark

Please refer to doc

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInsufficientLiquidity = errors.New("there is not enough depth to fill the order")
	ErrInvalidParam          = errors.New("the param is invalid")
	ErrInternal              = errors.New("internal server error")
	ErrTimeout               = errors.New("timeout")
)

Functions

func NewBuyerQueue

func NewBuyerQueue() *queue

func NewSellerQueue

func NewSellerQueue() *queue

Types

type Depth added in v0.5.0

type Depth struct {
	Asks []*DepthItem
	Bids []*DepthItem
}

type DepthItem added in v0.5.0

type DepthItem struct {
	ID    uint32
	Price decimal.Decimal
	Size  decimal.Decimal
}

type DiscardPublishTrader added in v0.6.0

type DiscardPublishTrader struct {
}

func NewDiscardPublishTrader added in v0.6.0

func NewDiscardPublishTrader() *DiscardPublishTrader

func (*DiscardPublishTrader) PublishTrades added in v0.6.0

func (p *DiscardPublishTrader) PublishTrades(trades ...*Trade)

type MatchingEngine

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

func NewMatchingEngine

func NewMatchingEngine(publishTrader PublishTrader) *MatchingEngine

func (*MatchingEngine) AddOrder added in v0.5.1

func (engine *MatchingEngine) AddOrder(ctx context.Context, order *Order) error

func (*MatchingEngine) CancelOrder

func (engine *MatchingEngine) CancelOrder(ctx context.Context, marketID string, orderID string) error

func (*MatchingEngine) OrderBook added in v0.5.0

func (engine *MatchingEngine) OrderBook(marketID string) *OrderBook

type MemoryPublishTrader added in v0.6.0

type MemoryPublishTrader struct {
	Trades []*Trade
	// contains filtered or unexported fields
}

func NewMemoryPublishTrader added in v0.6.0

func NewMemoryPublishTrader() *MemoryPublishTrader

func (*MemoryPublishTrader) Count added in v0.6.0

func (m *MemoryPublishTrader) Count() int

func (*MemoryPublishTrader) Get added in v0.6.0

func (m *MemoryPublishTrader) Get(index int) *Trade

func (*MemoryPublishTrader) PublishTrades added in v0.6.0

func (m *MemoryPublishTrader) PublishTrades(trades ...*Trade)

type Message added in v0.5.1

type Message struct {
	Action  string
	Payload any
	Resp    chan *Response
}

type Order

type Order struct {
	ID        string          `json:"id"`
	MarketID  string          `json:"market_id"`
	Side      Side            `json:"side"`
	Price     decimal.Decimal `json:"price"`
	Size      decimal.Decimal `json:"size"`
	Type      OrderType       `json:"type"`
	UserID    int64           `json:"user_id"`
	CreatedAt time.Time       `json:"created_at"`
}

type OrderBook

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

OrderBook type

func NewOrderBook

func NewOrderBook(publishTrader PublishTrader) *OrderBook

func (*OrderBook) AddOrder added in v0.5.0

func (book *OrderBook) AddOrder(ctx context.Context, order *Order) error

func (*OrderBook) CancelOrder

func (book *OrderBook) CancelOrder(ctx context.Context, id string) error

func (*OrderBook) Depth added in v0.5.0

func (book *OrderBook) Depth(limit uint32) (*Depth, error)

func (*OrderBook) Start added in v0.5.0

func (book *OrderBook) Start() error

type OrderBookUpdateEvent

type OrderBookUpdateEvent struct {
	Bids []*UpdateEvent
	Asks []*UpdateEvent
	Time time.Time
}

type OrderType

type OrderType string
const (
	Market   OrderType = "market"
	Limit    OrderType = "limit"
	FOK      OrderType = "fok"       // 全部成交或立即取消
	IOC      OrderType = "ioc"       // 立即成交并取消剩余
	PostOnly OrderType = "post_only" // be maker order only
	Cancel   OrderType = "cancel"    // the order has been canceled
)

type PublishTrader added in v0.6.0

type PublishTrader interface {
	PublishTrades(...*Trade)
}

type Response added in v0.5.1

type Response struct {
	Error error
	Data  any
}

type Side

type Side int8
const (
	Buy  Side = 1
	Sell Side = 2
)

type Trade

type Trade struct {
	ID             string          `json:"id"`
	MarketID       string          `json:"market_id"`
	TakerOrderID   string          `json:"taker_order_id"`
	TakerOrderSide Side            `json:"taker_order_side"`
	TakerOrderType OrderType       `json:"taker_order_type"`
	TakerUserID    int64           `json:"taker_user_id"`
	MakerOrderID   string          `json:"maker_order_id"`
	MakerUserID    int64           `json:"maker_user_id"`
	Price          decimal.Decimal `json:"price"`
	Size           decimal.Decimal `json:"size"`
	IsCancel       bool            `json:"is_cancel"`
	CreatedAt      time.Time       `json:"created_at"`
}

type UpdateEvent

type UpdateEvent struct {
	Price string `json:"price"`
	Size  string `json:"size"`
}

Jump to

Keyboard shortcuts

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