orderbook: change Base struct name to Book (#1914)

* orderbook: change Base struct name to Snapshot

* linter: fix

* Update exchanges/exchange.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/orderbook/depth.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/orderbook/orderbook_types.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Snapshot -> Book

* Tranche(s) -> Level(s)

* Tranche(s) -> Level(s)

* rm tranche ref

* linter: fix

* linter: rides again

* update tests

* Update exchange/websocket/buffer/buffer.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update backtester/eventhandlers/exchange/slippage/slippage.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchange/websocket/buffer/buffer.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchange/websocket/buffer/buffer.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/orderbook/orderbook_types.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/orderbook/orderbook_types.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* fixup tests

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/orderbook/orderbook_types.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/orderbook/orderbook_types.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* gk: nits and rm stuff that is not needed

* Update exchanges/orderbook/orderbook_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* gk: nits

---------

Co-authored-by: shazbert <ryan.oharareid@thrasher.io>
Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2025-06-16 17:09:25 +10:00
committed by GitHub
parent fd021d364a
commit 2958e64afe
93 changed files with 1259 additions and 1391 deletions

View File

@@ -19,7 +19,7 @@ import (
)
var (
itemArray = [][]orderbook.Tranche{
itemArray = [][]orderbook.Level{
{{Price: 1000, Amount: 1, ID: 1000}},
{{Price: 2000, Amount: 1, ID: 2000}},
{{Price: 3000, Amount: 1, ID: 3000}},
@@ -37,11 +37,11 @@ func getExclusivePair() (currency.Pair, error) {
return currency.NewPairFromStrings(currency.BTC.String(), currency.USDT.String()+strconv.FormatInt(offset.IncrementAndGet(), 10))
}
func createSnapshot(pair currency.Pair, bookVerifiy ...bool) (holder *Orderbook, asks, bids orderbook.Tranches, err error) {
asks = orderbook.Tranches{{Price: 4000, Amount: 1, ID: 6}}
bids = orderbook.Tranches{{Price: 4000, Amount: 1, ID: 6}}
func createSnapshot(pair currency.Pair, bookVerifiy ...bool) (holder *Orderbook, asks, bids orderbook.Levels, err error) {
asks = orderbook.Levels{{Price: 4000, Amount: 1, ID: 6}}
bids = orderbook.Levels{{Price: 4000, Amount: 1, ID: 6}}
book := &orderbook.Base{
book := &orderbook.Book{
Exchange: exchangeName,
Asks: asks,
Bids: bids,
@@ -70,14 +70,14 @@ func createSnapshot(pair currency.Pair, bookVerifiy ...bool) (holder *Orderbook,
return holder, asks, bids, err
}
func bidAskGenerator() []orderbook.Tranche {
response := make([]orderbook.Tranche, 100)
func bidAskGenerator() []orderbook.Level {
response := make([]orderbook.Level, 100)
for i := range 100 {
price := float64(rand.Intn(1000)) //nolint:gosec // no need to import crypo/rand for testing
if price == 0 {
price = 1
}
response[i] = orderbook.Tranche{
response[i] = orderbook.Level{
Amount: float64(rand.Intn(10)), //nolint:gosec // no need to import crypo/rand for testing
Price: price,
ID: int64(i),
@@ -352,7 +352,7 @@ func TestInsertWithIDs(t *testing.T) {
err = holder.Update(&orderbook.Update{
UpdateTime: time.Now(),
Asset: asset.Spot,
Asks: []orderbook.Tranche{{Price: 999999}},
Asks: []orderbook.Level{{Price: 999999}},
Pair: cp,
})
require.NoError(t, err)
@@ -437,11 +437,11 @@ func TestOrderbookLastUpdateID(t *testing.T) {
assert.Equal(t, 1000., itemArray[0][0].Price)
holder.checksum = func(*orderbook.Base, uint32) error { return errors.New("testerino") }
holder.checksum = func(*orderbook.Book, uint32) error { return errors.New("testerino") }
// this update invalidates the book
err = holder.Update(&orderbook.Update{
Asks: []orderbook.Tranche{{Price: 999999}},
Asks: []orderbook.Level{{Price: 999999}},
Pair: cp,
UpdateID: -1,
Asset: asset.Spot,
@@ -455,7 +455,7 @@ func TestOrderbookLastUpdateID(t *testing.T) {
holder, _, _, err = createSnapshot(cp)
require.NoError(t, err)
holder.checksum = func(*orderbook.Base, uint32) error { return nil }
holder.checksum = func(*orderbook.Book, uint32) error { return nil }
holder.updateIDProgression = true
for i := range itemArray {
@@ -472,7 +472,7 @@ func TestOrderbookLastUpdateID(t *testing.T) {
// out of order
err = holder.Update(&orderbook.Update{
Asks: []orderbook.Tranche{{Price: 999999}},
Asks: []orderbook.Level{{Price: 999999}},
Pair: cp,
UpdateID: 1,
Asset: asset.Spot,
@@ -491,8 +491,8 @@ func TestRunUpdateWithoutSnapshot(t *testing.T) {
require.NoError(t, err)
var holder Orderbook
asks := []orderbook.Tranche{{Price: 4000, Amount: 1, ID: 8}}
bids := []orderbook.Tranche{{Price: 5999, Amount: 1, ID: 8}, {Price: 4000, Amount: 1, ID: 9}}
asks := []orderbook.Level{{Price: 4000, Amount: 1, ID: 8}}
bids := []orderbook.Level{{Price: 5999, Amount: 1, ID: 8}, {Price: 4000, Amount: 1, ID: 9}}
holder.exchangeName = exchangeName
err = holder.Update(&orderbook.Update{
Bids: bids,
@@ -513,8 +513,8 @@ func TestRunUpdateWithoutAnyUpdates(t *testing.T) {
var obl Orderbook
obl.exchangeName = exchangeName
err = obl.Update(&orderbook.Update{
Bids: []orderbook.Tranche{},
Asks: []orderbook.Tranche{},
Bids: []orderbook.Level{},
Asks: []orderbook.Level{},
Pair: cp,
UpdateTime: time.Now(),
Asset: asset.Spot,
@@ -531,7 +531,7 @@ func TestRunSnapshotWithNoData(t *testing.T) {
var obl Orderbook
obl.ob = make(map[key.PairAsset]*orderbookHolder)
obl.dataHandler = make(chan any, 1)
var snapShot1 orderbook.Base
var snapShot1 orderbook.Book
snapShot1.Asset = asset.Spot
snapShot1.Pair = cp
snapShot1.Exchange = "test"
@@ -549,10 +549,10 @@ func TestLoadSnapshot(t *testing.T) {
var obl Orderbook
obl.dataHandler = make(chan any, 100)
obl.ob = make(map[key.PairAsset]*orderbookHolder)
var snapShot1 orderbook.Base
var snapShot1 orderbook.Book
snapShot1.Exchange = "SnapshotWithOverride"
asks := []orderbook.Tranche{{Price: 4000, Amount: 1, ID: 8}}
bids := []orderbook.Tranche{{Price: 4000, Amount: 1, ID: 9}}
asks := []orderbook.Level{{Price: 4000, Amount: 1, ID: 8}}
bids := []orderbook.Level{{Price: 4000, Amount: 1, ID: 9}}
snapShot1.Asks = asks
snapShot1.Bids = bids
snapShot1.Asset = asset.Spot
@@ -584,9 +584,9 @@ func TestInsertingSnapShots(t *testing.T) {
var holder Orderbook
holder.dataHandler = make(chan any, 100)
holder.ob = make(map[key.PairAsset]*orderbookHolder)
var snapShot1 orderbook.Base
var snapShot1 orderbook.Book
snapShot1.Exchange = "WSORDERBOOKTEST1"
asks := []orderbook.Tranche{
asks := []orderbook.Level{
{Price: 6000, Amount: 1, ID: 1},
{Price: 6001, Amount: 0.5, ID: 2},
{Price: 6002, Amount: 2, ID: 3},
@@ -600,7 +600,7 @@ func TestInsertingSnapShots(t *testing.T) {
{Price: 6010, Amount: 7, ID: 11},
}
bids := []orderbook.Tranche{
bids := []orderbook.Level{
{Price: 5999, Amount: 1, ID: 12},
{Price: 5998, Amount: 0.5, ID: 13},
{Price: 5997, Amount: 2, ID: 14},
@@ -621,9 +621,9 @@ func TestInsertingSnapShots(t *testing.T) {
snapShot1.LastUpdated = time.Now()
require.NoError(t, holder.LoadSnapshot(&snapShot1))
var snapShot2 orderbook.Base
var snapShot2 orderbook.Book
snapShot2.Exchange = "WSORDERBOOKTEST2"
asks = []orderbook.Tranche{
asks = []orderbook.Level{
{Price: 51, Amount: 1, ID: 1},
{Price: 52, Amount: 0.5, ID: 2},
{Price: 53, Amount: 2, ID: 3},
@@ -637,7 +637,7 @@ func TestInsertingSnapShots(t *testing.T) {
{Price: 60, Amount: 7, ID: 11},
}
bids = []orderbook.Tranche{
bids = []orderbook.Level{
{Price: 49, Amount: 1, ID: 12},
{Price: 48, Amount: 0.5, ID: 13},
{Price: 47, Amount: 2, ID: 14},
@@ -662,9 +662,9 @@ func TestInsertingSnapShots(t *testing.T) {
snapShot2.LastUpdated = time.Now()
require.NoError(t, holder.LoadSnapshot(&snapShot2))
var snapShot3 orderbook.Base
var snapShot3 orderbook.Book
snapShot3.Exchange = "WSORDERBOOKTEST3"
asks = []orderbook.Tranche{
asks = []orderbook.Level{
{Price: 511, Amount: 1, ID: 1},
{Price: 52, Amount: 0.5, ID: 2},
{Price: 53, Amount: 2, ID: 3},
@@ -678,7 +678,7 @@ func TestInsertingSnapShots(t *testing.T) {
{Price: 60, Amount: 7, ID: 11},
}
bids = []orderbook.Tranche{
bids = []orderbook.Level{
{Price: 49, Amount: 1, ID: 12},
{Price: 48, Amount: 0.5, ID: 13},
{Price: 47, Amount: 2, ID: 14},
@@ -844,10 +844,10 @@ func TestEnsureMultipleUpdatesViaPrice(t *testing.T) {
assert.LessOrEqual(t, 3, askLen)
}
func deploySliceOrdered(size int) orderbook.Tranches {
items := make([]orderbook.Tranche, size)
func deploySliceOrdered(size int) orderbook.Levels {
items := make([]orderbook.Level, size)
for i := range size {
items[i] = orderbook.Tranche{Amount: 1, Price: rand.Float64() + float64(i), ID: rand.Int63()} //nolint:gosec // Not needed for tests
items[i] = orderbook.Level{Amount: 1, Price: rand.Float64() + float64(i), ID: rand.Int63()} //nolint:gosec // Not needed for tests
}
return items
}
@@ -878,7 +878,7 @@ func TestUpdateByIDAndAction(t *testing.T) {
err = holder.updateByIDAndAction(&orderbook.Update{
Action: orderbook.Amend,
Bids: []orderbook.Tranche{{Price: 100, ID: 6969}},
Bids: []orderbook.Level{{Price: 100, ID: 6969}},
})
require.ErrorIs(t, err, errAmendFailure)
@@ -888,8 +888,8 @@ func TestUpdateByIDAndAction(t *testing.T) {
// append to slice
err = holder.updateByIDAndAction(&orderbook.Update{
Action: orderbook.UpdateInsert,
Bids: []orderbook.Tranche{{Price: 0, ID: 1337, Amount: 1}},
Asks: []orderbook.Tranche{{Price: 100, ID: 1337, Amount: 1}},
Bids: []orderbook.Level{{Price: 0, ID: 1337, Amount: 1}},
Asks: []orderbook.Level{{Price: 100, ID: 1337, Amount: 1}},
UpdateTime: time.Now(),
})
require.NoError(t, err)
@@ -902,8 +902,8 @@ func TestUpdateByIDAndAction(t *testing.T) {
// Change amount
err = holder.updateByIDAndAction(&orderbook.Update{
Action: orderbook.UpdateInsert,
Bids: []orderbook.Tranche{{Price: 0, ID: 1337, Amount: 100}},
Asks: []orderbook.Tranche{{Price: 100, ID: 1337, Amount: 100}},
Bids: []orderbook.Level{{Price: 0, ID: 1337, Amount: 100}},
Asks: []orderbook.Level{{Price: 100, ID: 1337, Amount: 100}},
UpdateTime: time.Now(),
})
require.NoError(t, err)
@@ -916,8 +916,8 @@ func TestUpdateByIDAndAction(t *testing.T) {
// Change price level
err = holder.updateByIDAndAction(&orderbook.Update{
Action: orderbook.UpdateInsert,
Bids: []orderbook.Tranche{{Price: 100, ID: 1337, Amount: 99}},
Asks: []orderbook.Tranche{{Price: 0, ID: 1337, Amount: 99}},
Bids: []orderbook.Level{{Price: 100, ID: 1337, Amount: 99}},
Asks: []orderbook.Level{{Price: 0, ID: 1337, Amount: 99}},
UpdateTime: time.Now(),
})
require.NoError(t, err)
@@ -935,7 +935,7 @@ func TestUpdateByIDAndAction(t *testing.T) {
// Delete - not found
err = holder.updateByIDAndAction(&orderbook.Update{
Action: orderbook.Delete,
Asks: []orderbook.Tranche{{Price: 0, ID: 1337, Amount: 99}},
Asks: []orderbook.Level{{Price: 0, ID: 1337, Amount: 99}},
})
require.ErrorIs(t, err, errDeleteFailure)
@@ -945,7 +945,7 @@ func TestUpdateByIDAndAction(t *testing.T) {
// Delete - found
err = holder.updateByIDAndAction(&orderbook.Update{
Action: orderbook.Delete,
Asks: []orderbook.Tranche{asks[0]},
Asks: []orderbook.Level{asks[0]},
UpdateTime: time.Now(),
})
require.NoError(t, err)
@@ -957,7 +957,7 @@ func TestUpdateByIDAndAction(t *testing.T) {
// Apply update
err = holder.updateByIDAndAction(&orderbook.Update{
Action: orderbook.Amend,
Asks: []orderbook.Tranche{{ID: 123456}},
Asks: []orderbook.Level{{ID: 123456}},
})
require.ErrorIs(t, err, errAmendFailure)
@@ -974,7 +974,7 @@ func TestUpdateByIDAndAction(t *testing.T) {
err = holder.updateByIDAndAction(&orderbook.Update{
Action: orderbook.Amend,
Asks: []orderbook.Tranche{update},
Asks: []orderbook.Level{update},
UpdateTime: time.Now(),
})
require.NoError(t, err)
@@ -993,10 +993,10 @@ func TestFlushOrderbook(t *testing.T) {
err = w.Setup(&config.Exchange{Name: "test"}, &Config{}, make(chan any, 2))
require.NoError(t, err)
var snapShot1 orderbook.Base
var snapShot1 orderbook.Book
snapShot1.Exchange = "Snapshooooot"
asks := []orderbook.Tranche{{Price: 4000, Amount: 1, ID: 8}}
bids := []orderbook.Tranche{{Price: 4000, Amount: 1, ID: 9}}
asks := []orderbook.Level{{Price: 4000, Amount: 1, ID: 8}}
bids := []orderbook.Level{{Price: 4000, Amount: 1, ID: 9}}
snapShot1.Asks = asks
snapShot1.Bids = bids
snapShot1.Asset = asset.Spot