A lightweight and efficient order book implementation in Go. I developed this project while learning Go, so it may include some unconventional or non-idiomatic uses of the language. It supports limit and market orders on both sides (buy/sell). The project uses a custom fixed-point number module for precise arithmetic, though it is still a work in progress.
Clone the repository and install dependencies:
git clone https://github.com/0xarash/order-book.git
cd order-book
go mod tidyHere’s a minimal example of how to create and use the order book:
package main
import (
"fmt"
"github.com/0xarash/order-book/book"
"github.com/0xarash/order-book/util/fixed"
)
func main() {
ob := book.NewOrderBook()
// Add a limit buy order
ob.Add(book.LimitOrder, &book.AddParam{
Price: fixed.New(100.0, 2),
Quantity: fixed.New(5.0, 2),
OrderId: 1,
Side: book.BuySide,
})
// Add a limit sell order
trades := ob.Add(book.LimitOrder, &book.AddParam{
Price: fixed.New(99.0, 2),
Quantity: fixed.New(3.0, 2),
OrderId: 2,
Side: book.SellSide,
})
// Print resulting trades
for _, trade := range trades {
fmt.Printf("Trade executed: %+v\n", trade)
}
}