obm

package module
v0.0.0-...-dc616df Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 7 Imported by: 0

README

Orderbook manager with Golang

go-obm is orderbook manager. set, update, sort, get best price and more.

Installation

$ go get -u github.com/go-numb/go-obm

Usage

package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
    // Setup
	o := New("BTC-PERP")
	o.SetCap(20, 20)

    // input(server response...)
    // dummy data
	l := 10
	asks := make([]Book, l)
	bids := make([]Book, l)

	s := rand.NewSource(time.Now().UnixNano())
	r := rand.New(s)

	for i := range asks {
		asks[i] = Book{
			Price: r.Float64(),
			Size:  r.NormFloat64() * 10,
		}
		time.Sleep(time.Millisecond)
	}

	for i := range bids {
		bids[i] = Book{
			Price: r.Float64(),
			Size:  r.NormFloat64() * 10,
		}
		time.Sleep(time.Millisecond)
	}

	o.Update(asks, bids)


    // use struct
	fmt.Println(o.Asks.Get(10), "\n", o.Bids.Get(10))

	fmt.Println(o.Best())

	fmt.Printf("ask: %d, bid: %d, %#v\n", len(o.Asks.Books), len(o.Bids.Books), o)

// Print out

// updated time: 0.000000 s

// asks------------------------
// 0 - 0.999126:12.623239
// 1 - 0.998673:11.969490
// 2 - 0.995374:12.411968
// 3 - 0.993730:19.209979
// 4 - 0.993432:1.188730
// 5 - 0.993338:10.510313
// 6 - 0.993020:17.003518
// 7 - 0.988681:10.699286
// 8 - 0.986657:6.555007
// 9 - 0.983770:17.040381 
// bids------------------------
// 0 - 0.158852:2.346508
// 1 - 0.157248:4.807470
// 2 - 0.154350:7.245589
// 3 - 0.150005:2.467844
// 4 - 0.148196:1.627250
// 5 - 0.146792:13.557464
// 6 - 0.144692:4.510992
// 7 - 0.134729:2.586043
// 8 - 0.133824:2.608753
// 9 - 0.132424:3.714124

// get time: 0.000000 s

// bestask{0.765535581376781 8.375973510078225}, bestbid{0.15885186778959806 2.346508140219099}
// ask: 10, bid: 10, &obm.Orderbook{Mutex:sync.Mutex{state:0, sema:0x0}, Symbol:"BTC-PERP", Bids:(*obm.Books)(0xc000074750), Asks:(*obm.Books)(0xc000074780), UpdatedAt:time.Date(2022, time.April, 5, 20, 15, 59, 532576600, time.Local)}
// exec time: 0.009937 s
// PASS

	// limit order placement ratio of price range 
	r := 0.01
	lob := o.LOB(r)
	for i := 0; i < len(lob.Bids); i++ {
		fmt.Printf("%.0f - %f - %f - %f%%\n", lob.Bids[i].Price, lob.Bids[i].Size, lob.Bids[i].AccSize, lob.Bids[i].AccRatio*100)
	}

	// bid[996] - 5233998.000000 - 0.050000
	
	// price, size, accumulation of bids[A], ratio:[A]/accumulation of all
	// 5234465 - 0.010000 - 0.010000 - 0.031682%
	// 5234468 - 0.012767 - 0.022767 - 0.072130%
	// 5234522 - 0.012779 - 0.035546 - 0.112616%
	// 5234530 - 0.010000 - 0.045546 - 0.144298%
	// 5234539 - 0.015000 - 0.060546 - 0.191820%

}

Author

@_numbP

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Book

type Book struct {
	Price float64
	Size  float64
}

type Books

type Books struct {
	Books []Book
	// contains filtered or unexported fields
}

func (*Books) Each

func (p *Books) Each(fn func(key, val float64))

func (*Books) Get

func (p *Books) Get(depth int) *Books

Get depth default:10

func (*Books) Len

func (p *Books) Len() int

func (*Books) Less

func (p *Books) Less(i, j int) bool

func (*Books) Put

func (p *Books) Put(book Book)

func (*Books) Size

func (p *Books) Size() int

func (*Books) String

func (p *Books) String() string

func (*Books) Swap

func (p *Books) Swap(i, j int)

type LimitOrderBook

type LimitOrderBook struct {
	Book
	AccSize  float64
	AccRatio float64
}

type LimitOrderPlacement

type LimitOrderPlacement struct {
	Accumulation float64
	AccAsksRatio float64
	AccBidsRatio float64

	Asks []LimitOrderBook
	Bids []LimitOrderBook
}

type Orderbook

type Orderbook struct {
	sync.Mutex

	Symbol    string
	Bids      *Books
	Asks      *Books
	UpdatedAt time.Time
}

func New

func New(symbol string) *Orderbook

func (*Orderbook) Best

func (p *Orderbook) Best() (ask, bid Book)

func (*Orderbook) GetCap

func (p *Orderbook) GetCap() (askcap, bidcap int)

func (*Orderbook) GetMax

func (p *Orderbook) GetMax() (askmax, bidmax any)

func (*Orderbook) GetMin

func (p *Orderbook) GetMin() (askmin, bidmin any)

func (*Orderbook) LOB

func (p *Orderbook) LOB(rangeRatio float64) *LimitOrderPlacement

LOB 範囲板(cap)の総量とOrderPlacePriceの累計量と割合を算出

func (*Orderbook) SetCap

func (p *Orderbook) SetCap(askcap, bidcap int) *Orderbook

SetCap is Determine the upper and lower limits of length stored in Map

func (*Orderbook) Update

func (p *Orderbook) Update(asks, bids []Book)

func (*Orderbook) Wall

func (p *Orderbook) Wall() (ask, bid Book)

Wall search Big board In the setting cap range Search by Price near Mid

type Remover

type Remover int
const (
	MAX Remover = iota
	MIN
)

Jump to

Keyboard shortcuts

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