Files
gocryptotrader/exchanges/orderbook/orderbook_test.go
Ryan O'Hara-Reid eb0571cc9b exchange: binance orderbook fix (#599)
* port orderbook binance management from draft singular asset (spot) processing add additional updates to buffer management

* integrate port

* shifted burden of proof to exchange and remove repairing techniques that obfuscate issues and could caause artifacts

* WIP

* Update exchanges, update tests, update configuration so we can default off on buffer util.

* Add buffer enabled switching to all exchanges and some that are missing, default to off.

* lbtc set not aggregate books

* Addr linter issues

* EOD wip

* optimization and bug fix pass

* clean before test and benchmarking

* add testing/benchmarks to sorting/reversing functions, dropped pointer to slice as we aren't changing slice len or cap

* Add tests and removed ptr for main book as we just ammend amount

* addr exchange test issues

* ci issues

* addr glorious issues

* Addr MCB nits, fixed funding rate book for bitfinex and fixed potential panic on nil book return

* addr linter issues

* updated mistakes

* Fix more tests

* revert bypass

* Addr mcb nits

* fix zero price bug caused by exchange. Filted out bid result rather then unsubscribing. Updated orderbook to L2 so there is no aggregation.

* Allow for zero bid and ask books to be loaded and warn if found.

* remove authentication subscription conflicts as they do not have a channel ID return

* WIP - Batching outbound requests for kraken as they do not give you the partial if you subscribe to do many things.

* finalised outbound request for kraken

* filter zero value due to invalid returned data from exchange, add in max subscription amount and increased outbound batch limit

* expand to max allowed book length & fix issue where they were sending a zero length ask side when we sent a depth of zero

* Updated function comments and added in more realistic book sizing for sort cases

* change map ordering

* amalgamate maps in buffer

* Rm ln

* fix kraken linter issues

* add in buffer initialisation

* increase timout by 30seconds

* Coinbene: Add websocket orderbook length check.

* Engine: Improve switch statement for orderbook summary dissplay.

* Binance: Added tests, remove deadlock

* Exchanges: Change orderbook field -> IsFundingRate

* Orderbook Buffer: Added method to orderbookHolder

* Kraken: removed superfluous integer for sleep

* Bitmex: fixed error return

* cmd/gctcli: force 8 decimal place usage for orderbook streaming

* Kraken: Add checksum and fix bug where we were dropping returned data which was causing artifacts

* Kraken: As per orderbook documentation added in maxdepth field to update to filter depth that goes beyond current scope

* Bitfinex: Tracking down bug on margin-funding, added sequence and checksum validation websocket config on connect (WIP)

* Bitfinex: Complete implementation of checksum

* Bitfinex: Fix funding book insertion and checksum - Dropped updates and deleting items not on book are continuously occuring from stream

* Bitfinex: Fix linter issues

* Bitfinex: Fix even more linter issues.

* Bitmex: Populate orderbook base identification fields to be passed back when error occurrs

* OkGroup: Populate orderbook base identification fields to be passed back when error occurrs

* BTSE: Change string check to 'connect success' to capture multiple user successful strings

* Bitfinex: Updated handling of funding tickers

* Bitfinex: Fix undocumented alignment bug for funding rates

* Bitfinex: Updated error return with more information

* Bitfinex: Change REST fetching to Raw book to keep it in line with websocket implementation. Fix woopsy.

* Localbitcoins: Had to impose a rate limiter to stop errors, fixed return for easier error identification.

* Exchanges: Update failing tests

* LocalBitcoins: Addr nit and bumped time by 1 second for fetching books

* Kraken: Dynamically scale precision based on str return for checksum calculations

* Kraken: Add pair and asset type to validateCRC32 error reponse

* BTSE: Filter out zero amount orderbook price levels in websocket return

* Exchanges: Update orderbook functions to return orderbook base to differentiate errors.

* BTSE: Fix spelling

* Bitmex: Fix error return string

* BTSE: Add orderbook filtering function

* Coinbene: Change wording

* BTSE: Add test for filtering

* Binance: Addr nits, added in variables for buffers and worker amounts and fixed error log messages

* GolangCI: Remove excess 0

* Binance: Reduces double ups on asset and pair in errors

* Binance: Fix error checking
2021-01-04 17:19:55 +11:00

749 lines
17 KiB
Go

package orderbook
import (
"errors"
"log"
"math/rand"
"os"
"strconv"
"sync"
"testing"
"time"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/dispatch"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
)
func TestMain(m *testing.M) {
err := dispatch.Start(1, dispatch.DefaultJobsLimit)
if err != nil {
log.Fatal(err)
}
cpyMux = service.mux
os.Exit(m.Run())
}
var cpyMux *dispatch.Mux
func TestSubscribeOrderbook(t *testing.T) {
_, err := SubscribeOrderbook("", currency.Pair{}, asset.Item(""))
if err == nil {
t.Error("error cannot be nil")
}
p := currency.NewPair(currency.BTC, currency.USD)
b := Base{
Pair: p,
AssetType: asset.Spot,
}
err = b.Process()
if err == nil {
t.Error("error cannot be nil")
}
b.ExchangeName = "SubscribeOBTest"
b.Bids = []Item{{Price: 100, Amount: 1}, {Price: 99, Amount: 1}}
err = b.Process()
if err != nil {
t.Error("process error", err)
}
_, err = SubscribeOrderbook("SubscribeOBTest", p, asset.Spot)
if err != nil {
t.Error(err)
}
// process redundant update
err = b.Process()
if err != nil {
t.Error("process error", err)
}
}
func TestUpdateBooks(t *testing.T) {
p := currency.NewPair(currency.BTC, currency.USD)
b := Base{
Pair: p,
AssetType: asset.Spot,
ExchangeName: "UpdateTest",
}
service.mux = nil
err := service.Update(&b)
if err == nil {
t.Error("error cannot be nil")
}
b.Pair.Base = currency.CYC
err = service.Update(&b)
if err == nil {
t.Error("error cannot be nil")
}
b.Pair.Quote = currency.ENAU
err = service.Update(&b)
if err == nil {
t.Error("error cannot be nil")
}
b.AssetType = "unicorns"
err = service.Update(&b)
if err == nil {
t.Error("error cannot be nil")
}
service.mux = cpyMux
}
func TestSubscribeToExchangeOrderbooks(t *testing.T) {
_, err := SubscribeToExchangeOrderbooks("")
if err == nil {
t.Error("error cannot be nil")
}
p := currency.NewPair(currency.BTC, currency.USD)
b := Base{
Pair: p,
AssetType: asset.Spot,
ExchangeName: "SubscribeToExchangeOrderbooks",
Bids: []Item{{Price: 100, Amount: 1}, {Price: 99, Amount: 1}},
}
err = b.Process()
if err != nil {
t.Error(err)
}
_, err = SubscribeToExchangeOrderbooks("SubscribeToExchangeOrderbooks")
if err != nil {
t.Error(err)
}
}
func TestVerify(t *testing.T) {
t.Parallel()
b := Base{
ExchangeName: "TestExchange",
AssetType: asset.Spot,
Pair: currency.NewPair(currency.BTC, currency.USD),
}
err := b.Verify()
if err != nil {
t.Fatalf("expecting %v error but received %v", nil, err)
}
b.Asks = []Item{{ID: 1337, Price: 99, Amount: 1}, {ID: 1337, Price: 100, Amount: 1}}
err = b.Verify()
if err == nil || !errors.Is(err, errIDDuplication) {
t.Fatalf("expecting %s error but received %v", errIDDuplication, err)
}
b.Asks = []Item{{Price: 100, Amount: 1}, {Price: 100, Amount: 1}}
err = b.Verify()
if err == nil || !errors.Is(err, errDuplication) {
t.Fatalf("expecting %s error but received %v", errDuplication, err)
}
b.Asks = []Item{{Price: 100, Amount: 1}, {Price: 99, Amount: 1}}
b.IsFundingRate = true
err = b.Verify()
if err == nil || !errors.Is(err, errPeriodUnset) {
t.Fatalf("expecting %s error but received %v", errPeriodUnset, err)
}
b.IsFundingRate = false
err = b.Verify()
if err == nil || !errors.Is(err, errOutOfOrder) {
t.Fatalf("expecting %s error but received %v", errOutOfOrder, err)
}
b.Asks = []Item{{Price: 100, Amount: 1}, {Price: 100, Amount: 0}}
err = b.Verify()
if err == nil || !errors.Is(err, errAmountInvalid) {
t.Fatalf("expecting %s error but received %v", errAmountInvalid, err)
}
b.Asks = []Item{{Price: 100, Amount: 1}, {Price: 0, Amount: 100}}
err = b.Verify()
if err == nil || !errors.Is(err, errPriceNotSet) {
t.Fatalf("expecting %s error but received %v", errPriceNotSet, err)
}
b.Bids = []Item{{ID: 1337, Price: 100, Amount: 1}, {ID: 1337, Price: 99, Amount: 1}}
err = b.Verify()
if err == nil || !errors.Is(err, errIDDuplication) {
t.Fatalf("expecting %s error but received %v", errIDDuplication, err)
}
b.Bids = []Item{{Price: 100, Amount: 1}, {Price: 100, Amount: 1}}
err = b.Verify()
if err == nil || !errors.Is(err, errDuplication) {
t.Fatalf("expecting %s error but received %v", errDuplication, err)
}
b.Bids = []Item{{Price: 99, Amount: 1}, {Price: 100, Amount: 1}}
b.IsFundingRate = true
err = b.Verify()
if err == nil || !errors.Is(err, errPeriodUnset) {
t.Fatalf("expecting %s error but received %v", errPeriodUnset, err)
}
b.IsFundingRate = false
err = b.Verify()
if err == nil || !errors.Is(err, errOutOfOrder) {
t.Fatalf("expecting %s error but received %v", errOutOfOrder, err)
}
b.Bids = []Item{{Price: 100, Amount: 1}, {Price: 100, Amount: 0}}
err = b.Verify()
if err == nil || !errors.Is(err, errAmountInvalid) {
t.Fatalf("expecting %s error but received %v", errAmountInvalid, err)
}
b.Bids = []Item{{Price: 100, Amount: 1}, {Price: 0, Amount: 100}}
err = b.Verify()
if err == nil || !errors.Is(err, errPriceNotSet) {
t.Fatalf("expecting %s error but received %v", errPriceNotSet, err)
}
}
func TestCalculateTotalBids(t *testing.T) {
t.Parallel()
curr, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
base := Base{
Pair: curr,
Bids: []Item{{Price: 100, Amount: 10}},
LastUpdated: time.Now(),
}
a, b := base.TotalBidsAmount()
if a != 10 && b != 1000 {
t.Fatal("TestCalculateTotalBids expected a = 10 and b = 1000")
}
}
func TestCalculateTotaAsks(t *testing.T) {
t.Parallel()
curr, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
base := Base{
Pair: curr,
Asks: []Item{{Price: 100, Amount: 10}},
}
a, b := base.TotalAsksAmount()
if a != 10 && b != 1000 {
t.Fatal("TestCalculateTotalAsks expected a = 10 and b = 1000")
}
}
func TestUpdate(t *testing.T) {
t.Parallel()
curr, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
timeNow := time.Now()
base := Base{
Pair: curr,
Asks: []Item{{Price: 100, Amount: 10}},
Bids: []Item{{Price: 200, Amount: 10}},
LastUpdated: timeNow,
}
asks := []Item{{Price: 200, Amount: 101}}
bids := []Item{{Price: 201, Amount: 100}}
time.Sleep(time.Millisecond * 50)
base.Update(bids, asks)
if !base.LastUpdated.After(timeNow) {
t.Fatal("TestUpdate expected LastUpdated to be greater then original time")
}
a, b := base.TotalAsksAmount()
if a != 100 && b != 20200 {
t.Fatal("TestUpdate expected a = 100 and b = 20100")
}
a, b = base.TotalBidsAmount()
if a != 100 && b != 20100 {
t.Fatal("TestUpdate expected a = 100 and b = 20100")
}
}
func TestGetOrderbook(t *testing.T) {
c, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
base := &Base{
Pair: c,
Asks: []Item{{Price: 100, Amount: 10}},
Bids: []Item{{Price: 200, Amount: 10}},
ExchangeName: "Exchange",
AssetType: asset.Spot,
}
err = base.Process()
if err != nil {
t.Fatal(err)
}
result, err := Get("Exchange", c, asset.Spot)
if err != nil {
t.Fatalf("TestGetOrderbook failed to get orderbook. Error %s",
err)
}
if !result.Pair.Equal(c) {
t.Fatal("TestGetOrderbook failed. Mismatched pairs")
}
_, err = Get("nonexistent", c, asset.Spot)
if err == nil {
t.Fatal("TestGetOrderbook retrieved non-existent orderbook")
}
c.Base = currency.NewCode("blah")
_, err = Get("Exchange", c, asset.Spot)
if err == nil {
t.Fatal("TestGetOrderbook retrieved non-existent orderbook using invalid first currency")
}
newCurrency, err := currency.NewPairFromStrings("BTC", "AUD")
if err != nil {
t.Fatal(err)
}
_, err = Get("Exchange", newCurrency, asset.Spot)
if err == nil {
t.Fatal("TestGetOrderbook retrieved non-existent orderbook using invalid second currency")
}
base.Pair = newCurrency
err = base.Process()
if err != nil {
t.Error(err)
}
_, err = Get("Exchange", newCurrency, "meowCats")
if err == nil {
t.Error("error cannot be nil")
}
}
func TestCreateNewOrderbook(t *testing.T) {
c, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
base := &Base{
Pair: c,
Asks: []Item{{Price: 100, Amount: 10}},
Bids: []Item{{Price: 200, Amount: 10}},
ExchangeName: "testCreateNewOrderbook",
AssetType: asset.Spot,
}
err = base.Process()
if err != nil {
t.Fatal(err)
}
result, err := Get("testCreateNewOrderbook", c, asset.Spot)
if err != nil {
t.Fatal("TestCreateNewOrderbook failed to create new orderbook", err)
}
if !result.Pair.Equal(c) {
t.Fatal("TestCreateNewOrderbook result pair is incorrect")
}
a, b := result.TotalAsksAmount()
if a != 10 && b != 1000 {
t.Fatal("TestCreateNewOrderbook CalculateTotalAsks value is incorrect")
}
a, b = result.TotalBidsAmount()
if a != 10 && b != 2000 {
t.Fatal("TestCreateNewOrderbook CalculateTotalBids value is incorrect")
}
}
func TestProcessOrderbook(t *testing.T) {
c, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
base := Base{
Asks: []Item{{Price: 100, Amount: 10}},
Bids: []Item{{Price: 200, Amount: 10}},
ExchangeName: "ProcessOrderbook",
}
// test for empty pair
base.Pair = currency.Pair{}
err = base.Process()
if err == nil {
t.Error("empty pair should throw an err")
}
// test for empty asset type
base.Pair = c
err = base.Process()
if err == nil {
t.Error("empty asset type should throw an err")
}
// now process a valid orderbook
base.AssetType = asset.Spot
err = base.Process()
if err != nil {
t.Error("unexpcted result: ", err)
}
result, err := Get("ProcessOrderbook", c, asset.Spot)
if err != nil {
t.Fatal("TestProcessOrderbook failed to create new orderbook")
}
if !result.Pair.Equal(c) {
t.Fatal("TestProcessOrderbook result pair is incorrect")
}
// now test for processing a pair with a different quote currency
c, err = currency.NewPairFromStrings("BTC", "GBP")
if err != nil {
t.Fatal(err)
}
base.Pair = c
err = base.Process()
if err != nil {
t.Error("Process() error", err)
}
result, err = Get("ProcessOrderbook", c, asset.Spot)
if err != nil {
t.Fatal("TestProcessOrderbook failed to retrieve new orderbook")
}
if !result.Pair.Equal(c) {
t.Fatal("TestProcessOrderbook result pair is incorrect")
}
// now test for processing a pair which has a different base currency
c, err = currency.NewPairFromStrings("LTC", "GBP")
if err != nil {
t.Fatal(err)
}
base.Pair = c
err = base.Process()
if err != nil {
t.Error("Process() error", err)
}
result, err = Get("ProcessOrderbook", c, asset.Spot)
if err != nil {
t.Fatal("TestProcessOrderbook failed to retrieve new orderbook")
}
if !result.Pair.Equal(c) {
t.Fatal("TestProcessOrderbook result pair is incorrect")
}
base.Asks = []Item{{Price: 200, Amount: 200}}
base.AssetType = "monthly"
err = base.Process()
if err != nil {
t.Error("Process() error", err)
}
result, err = Get("ProcessOrderbook", c, "monthly")
if err != nil {
t.Fatal("TestProcessOrderbook failed to retrieve new orderbook")
}
a, b := result.TotalAsksAmount()
if a != 200 && b != 40000 {
t.Fatal("TestProcessOrderbook CalculateTotalsAsks incorrect values")
}
base.Bids = []Item{{Price: 420, Amount: 200}}
base.ExchangeName = "Blah"
base.AssetType = "quarterly"
err = base.Process()
if err != nil {
t.Error("Process() error", err)
}
_, err = Get("Blah", c, "quarterly")
if err != nil {
t.Fatal("TestProcessOrderbook failed to create new orderbook")
}
if a != 200 && b != 84000 {
t.Fatal("TestProcessOrderbook CalculateTotalsBids incorrect values")
}
type quick struct {
Name string
P currency.Pair
Bids []Item
Asks []Item
}
var testArray []quick
_ = rand.NewSource(time.Now().Unix())
var wg sync.WaitGroup
var m sync.Mutex
var catastrophicFailure bool
for i := 0; i < 500; i++ {
if catastrophicFailure {
break
}
wg.Add(1)
go func() {
newName := "Exchange" + strconv.FormatInt(rand.Int63(), 10) // nolint:gosec // no need to import crypo/rand for testing
newPairs := currency.NewPair(currency.NewCode("BTC"+strconv.FormatInt(rand.Int63(), 10)),
currency.NewCode("USD"+strconv.FormatInt(rand.Int63(), 10))) // nolint:gosec // no need to import crypo/rand for testing
asks := []Item{{Price: rand.Float64(), Amount: rand.Float64()}} // nolint:gosec // no need to import crypo/rand for testing
bids := []Item{{Price: rand.Float64(), Amount: rand.Float64()}} // nolint:gosec // no need to import crypo/rand for testing
base := &Base{
Pair: newPairs,
Asks: asks,
Bids: bids,
ExchangeName: newName,
AssetType: asset.Spot,
}
m.Lock()
err = base.Process()
if err != nil {
t.Error(err)
catastrophicFailure = true
return
}
testArray = append(testArray, quick{Name: newName, P: newPairs, Bids: bids, Asks: asks})
m.Unlock()
wg.Done()
}()
}
if catastrophicFailure {
t.Fatal("Process() error", err)
}
wg.Wait()
for _, test := range testArray {
wg.Add(1)
fatalErr := false
go func(test quick) {
result, err := Get(test.Name, test.P, asset.Spot)
if err != nil {
fatalErr = true
return
}
if result.Asks[0] != test.Asks[0] {
t.Error("TestProcessOrderbook failed bad values")
}
if result.Bids[0] != test.Bids[0] {
t.Error("TestProcessOrderbook failed bad values")
}
wg.Done()
}(test)
if fatalErr {
t.Fatal("TestProcessOrderbook failed to retrieve new orderbook")
}
}
wg.Wait()
}
func deployUnorderedSlice() []Item {
var items []Item
rand.Seed(time.Now().UnixNano())
for i := 0; i < 1000; i++ {
items = append(items, Item{Amount: 1, Price: rand.Float64(), ID: rand.Int63()}) // nolint:gosec // Not needed in tests
}
return items
}
func TestSorting(t *testing.T) {
var b Base
b.Asks = deployUnorderedSlice()
err := b.Verify()
if err == nil {
t.Fatal("error cannot be nil")
}
SortAsks(nil)
SortAsks(b.Asks)
err = b.Verify()
if err != nil {
t.Fatal(err)
}
b.Bids = deployUnorderedSlice()
err = b.Verify()
if err == nil {
t.Fatal("error cannot be nil")
}
SortBids(nil)
SortBids(b.Bids)
err = b.Verify()
if err != nil {
t.Fatal(err)
}
}
func deploySliceOrdered() []Item {
rand.Seed(time.Now().UnixNano())
var items []Item
for i := 0; i < 1000; i++ {
items = append(items, Item{Amount: 1, Price: float64(i + 1), ID: rand.Int63()}) // nolint:gosec // Not needed in tests
}
return items
}
func TestReverse(t *testing.T) {
var b Base
length := 1000
b.Bids = deploySliceOrdered()
if len(b.Bids) != length {
t.Fatal("incorrect length")
}
err := b.Verify()
if err == nil {
t.Fatal("error cannot be nil")
}
Reverse(nil)
Reverse(b.Bids)
err = b.Verify()
if err != nil {
t.Fatal(err)
}
b.Asks = append(b.Bids[:0:0], b.Bids...) // nolint:gocritic // Short hand
err = b.Verify()
if err == nil {
t.Fatal("error cannot be nil")
}
Reverse(b.Asks)
err = b.Verify()
if err != nil {
t.Fatal(err)
}
}
// 705985 1856 ns/op 0 B/op 0 allocs/op
func BenchmarkReverse(b *testing.B) {
length := 1000
s := deploySliceOrdered()
if len(s) != length {
b.Fatal("incorrect length")
}
for i := 0; i < b.N; i++ {
Reverse(s)
}
}
// 20209 56385 ns/op 49189 B/op 2 allocs/op
func BenchmarkSortAsksDecending(b *testing.B) {
s := deploySliceOrdered()
for i := 0; i < b.N; i++ {
ts := append(s[:0:0], s...)
SortAsks(ts)
}
}
// 14924 79199 ns/op 49206 B/op 3 allocs/op
func BenchmarkSortBidsAscending(b *testing.B) {
s := deploySliceOrdered()
Reverse(s)
for i := 0; i < b.N; i++ {
ts := append(s[:0:0], s...)
SortBids(ts)
}
}
// 9842 133761 ns/op 49194 B/op 2 allocs/op
func BenchmarkSortAsksStandard(b *testing.B) {
s := deployUnorderedSlice()
for i := 0; i < b.N; i++ {
ts := append(s[:0:0], s...)
SortAsks(ts)
}
}
// 7058 155057 ns/op 49214 B/op 3 allocs/op
func BenchmarkSortBidsStandard(b *testing.B) {
s := deployUnorderedSlice()
for i := 0; i < b.N; i++ {
ts := append(s[:0:0], s...)
SortBids(ts)
}
}
// 20565 57001 ns/op 49188 B/op 2 allocs/op
func BenchmarkSortAsksAscending(b *testing.B) {
s := deploySliceOrdered()
for i := 0; i < b.N; i++ {
ts := append(s[:0:0], s...)
SortAsks(ts)
}
}
// 12565 97257 ns/op 49208 B/op 3 allocs/op
func BenchmarkSortBidsDescending(b *testing.B) {
s := deploySliceOrdered()
Reverse(s)
for i := 0; i < b.N; i++ {
ts := append(s[:0:0], s...)
SortBids(ts)
}
}
// 923154 1169 ns/op 4096 B/op 1 allocs/op
func BenchmarkDuplicatingSlice(b *testing.B) {
s := deploySliceOrdered()
for i := 0; i < b.N; i++ {
_ = append(s[:0:0], s...)
}
}
// 705922 1546 ns/op 4096 B/op 1 allocs/op
func BenchmarkCopySlice(b *testing.B) {
s := deploySliceOrdered()
for i := 0; i < b.N; i++ {
cpy := make([]Item, len(s))
copy(cpy, s)
}
}