/go-orderbook

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

Primary LanguageGoMIT LicenseMIT

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