Categorygithub.com/danielgatis/go-orderbook
modulepackage
0.0.0-20230808024110-7640955559eb
Repository: https://github.com/danielgatis/go-orderbook.git
Documentation: pkg.go.dev

# README

Go - Orderbook

Go Report Card License MIT Go Doc

The pkg go-orderbook implements a limit order book for high-frequency trading (HFT), as described by WK Selph.

Based on WK Selph's Blogpost: https://goo.gl/KF1SRm

Install

go get -u github.com/danielgatis/go-orderbook

And then import the package in your code:

import "github.com/danielgatis/go-orderbook"

Example

An example described below is one of the use cases.

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"math/rand"
	"time"

	"github.com/danielgatis/go-orderbook"
	"github.com/google/uuid"
	"github.com/shopspring/decimal"
)

func main() {
	book := orderbook.NewOrderBook("BTC/BRL")

	for i := 0; i < 10; i++ {
		rand.Seed(time.Now().UnixNano())
		side := []orderbook.Side{orderbook.Buy, orderbook.Sell}[rand.Intn(2)]

		book.ProcessPostOnlyOrder(uuid.New().String(), uuid.New().String(), side, decimal.NewFromInt(rand.Int63n(1000)), decimal.NewFromInt(rand.Int63n(1000)))
	}

	depth, _ := json.Marshal(book.Depth())
	var buf bytes.Buffer
	json.Indent(&buf, depth, "", "  ")
	fmt.Println(buf.String())
}
❯ go run main.go
{
  "bids": [
    {
      "amount": "392",
      "price": "930"
    },
    {
      "amount": "872",
      "price": "907"
    },
    {
      "amount": "859",
      "price": "790"
    },
    {
      "amount": "643",
      "price": "424"
    },
    {
      "amount": "269",
      "price": "244"
    },
    {
      "amount": "160",
      "price": "83"
    },
    {
      "amount": "74",
      "price": "65"
    }
  ],
  "asks": [
    {
      "amount": "178",
      "price": "705"
    },
    {
      "amount": "253",
      "price": "343"
    },
    {
      "amount": "805",
      "price": "310"
    }
  ]
}

License

Copyright (c) 2020-present Daniel Gatis

Licensed under MIT License

Buy me a coffee

Liked some of my work? Buy me a coffee (or more likely a beer)

Buy Me A Coffee

# Functions

NewDepth creates a new depth.
NewOrder creates a new order.
NewOrderBook creates a new order book.
NewOrderQueue creates a new order queue.
NewOrderSide creates a new order side.
NewPriceLevel creates a new price level.
NewQuote creates a new quote.
NewTrade creates a new trade.
Restore restores a new order book from raw representation.

# Constants

Buy for bids.
Sell for asks.

# Variables

Orderbook erros.
Orderbook erros.
Orderbook erros.
Orderbook erros.
Orderbook erros.
Orderbook erros.

# Structs

Depth represents a order book depth.
Order represents a bid or an ask.
OrderBook represents a order book for a given market symbol.
OrderQueue represents a queue of orders.
OrderSide represents all the prices about bids or asks.
PriceLevel takes a count of how many assets have that price.
Quote represents a quote.
Trade represents a match between a maker order and a taker order.

# Type aliases

A Side of the order.