Documentation
¶
Overview ¶
Package orderbook implements a high-performance order book for the DEX VM.
Index ¶
- Variables
- type Order
- type OrderStatus
- type OrderType
- type Orderbook
- func (ob *Orderbook) AddOrder(order *Order) ([]*Trade, error)
- func (ob *Orderbook) CancelOrder(orderID ids.ID) error
- func (ob *Orderbook) GetBestAsk() uint64
- func (ob *Orderbook) GetBestBid() uint64
- func (ob *Orderbook) GetDepth(maxLevels int) (bids, asks []*PriceLevel)
- func (ob *Orderbook) GetMidPrice() uint64
- func (ob *Orderbook) GetOrder(orderID ids.ID) (*Order, error)
- func (ob *Orderbook) GetSpread() uint64
- func (ob *Orderbook) GetStats() (totalVolume, tradeCount uint64, lastTradeTime int64)
- func (ob *Orderbook) Match() []Trade
- func (ob *Orderbook) Symbol() string
- type PriceLevel
- type Side
- type Trade
Constants ¶
This section is empty.
Variables ¶
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) RemainingQuantity ¶
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 Orderbook ¶
type Orderbook struct {
// contains filtered or unexported fields
}
Orderbook maintains the bid and ask sides for a trading pair.
func (*Orderbook) AddOrder ¶
AddOrder adds a new order to the orderbook. Returns executed trades if the order is matched.
func (*Orderbook) CancelOrder ¶
CancelOrder cancels an order by ID.
func (*Orderbook) GetBestAsk ¶
GetBestAsk returns the best (lowest) ask price.
func (*Orderbook) GetBestBid ¶
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 ¶
GetMidPrice returns the mid-market price.
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 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.