Skip to content

Commit

Permalink
orderbook: Refactor package structure for simplicity and efficiency (#…
Browse files Browse the repository at this point in the history
…1465)

* initial purge and benchmarks proof before rn overhaul

* rn LinkedList -> Tranche(s) and purge references

* roll out acrost exchanges

* linterino

* rn silly billy label

* linter strikes AAAAAGAIN!

* fix some things

* rm comment

* Add actual comparison from master to branch benchmark for sorting algorithms

* lower case via git mv YAAY!

* drop code

* convert type name

* glorious: nits

---------

Co-authored-by: Ryan O'Hara-Reid <[email protected]>
  • Loading branch information
shazbert and Ryan O'Hara-Reid authored May 14, 2024
1 parent 2a92878 commit 4cd4fb0
Show file tree
Hide file tree
Showing 76 changed files with 1,574 additions and 2,823 deletions.
8 changes: 4 additions & 4 deletions cmd/exchange_template/wrapper_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,17 @@ func ({{.Variable}} *{{.CapitalName}}) UpdateOrderbook(ctx context.Context, pair
return book, err
}

book.Bids = make([]orderbook.Item, len(orderbookNew.Bids))
book.Bids = make([]orderbook.Tranche, len(orderbookNew.Bids))
for x := range orderbookNew.Bids {
book.Bids[x] = orderbook.Item{
book.Bids[x] = orderbook.Tranche{
Amount: orderbookNew.Bids[x].Quantity,
Price: orderbookNew.Bids[x].Price,
}
}

book.Asks = make([]orderbook.Item, len(orderbookNew.Asks))
book.Asks = make([]orderbook.Tranche, len(orderbookNew.Asks))
for x := range orderbookNew.Asks {
book.Asks[x] = orderbook.Item{
book.Asks[x] = orderbook.Tranche{
Amount: orderBookNew.Asks[x].Quantity,
Price: orderBookNew.Asks[x].Price,
}
Expand Down
2 changes: 1 addition & 1 deletion engine/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ func TestGetSpecificOrderbook(t *testing.T) {

base := orderbook.Base{
Pair: currency.NewPair(currency.BTC, currency.USD),
Bids: []orderbook.Item{{Price: 1000, Amount: 1}},
Bids: []orderbook.Tranche{{Price: 1000, Amount: 1}},
Exchange: "Bitstamp",
Asset: asset.Spot,
}
Expand Down
12 changes: 6 additions & 6 deletions engine/rpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3474,13 +3474,13 @@ func TestGetOrderbookMovement(t *testing.T) {
t.Fatal(err)
}

bid := []orderbook.Item{
bid := []orderbook.Tranche{
{Price: 10, Amount: 1},
{Price: 9, Amount: 1},
{Price: 8, Amount: 1},
{Price: 7, Amount: 1},
}
ask := []orderbook.Item{
ask := []orderbook.Tranche{
{Price: 11, Amount: 1},
{Price: 12, Amount: 1},
{Price: 13, Amount: 1},
Expand Down Expand Up @@ -3587,13 +3587,13 @@ func TestGetOrderbookAmountByNominal(t *testing.T) {
t.Fatal(err)
}

bid := []orderbook.Item{
bid := []orderbook.Tranche{
{Price: 10, Amount: 1},
{Price: 9, Amount: 1},
{Price: 8, Amount: 1},
{Price: 7, Amount: 1},
}
ask := []orderbook.Item{
ask := []orderbook.Tranche{
{Price: 11, Amount: 1},
{Price: 12, Amount: 1},
{Price: 13, Amount: 1},
Expand Down Expand Up @@ -3693,13 +3693,13 @@ func TestGetOrderbookAmountByImpact(t *testing.T) {
t.Fatal(err)
}

bid := []orderbook.Item{
bid := []orderbook.Tranche{
{Price: 10, Amount: 1},
{Price: 9, Amount: 1},
{Price: 8, Amount: 1},
{Price: 7, Amount: 1},
}
ask := []orderbook.Item{
ask := []orderbook.Tranche{
{Price: 11, Amount: 1},
{Price: 12, Amount: 1},
{Price: 13, Amount: 1},
Expand Down
8 changes: 4 additions & 4 deletions exchanges/alphapoint/alphapoint_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,17 @@ func (a *Alphapoint) UpdateOrderbook(ctx context.Context, p currency.Pair, asset
return orderBook, err
}

orderBook.Bids = make(orderbook.Items, len(orderbookNew.Bids))
orderBook.Bids = make(orderbook.Tranches, len(orderbookNew.Bids))
for x := range orderbookNew.Bids {
orderBook.Bids[x] = orderbook.Item{
orderBook.Bids[x] = orderbook.Tranche{
Amount: orderbookNew.Bids[x].Quantity,
Price: orderbookNew.Bids[x].Price,
}
}

orderBook.Asks = make(orderbook.Items, len(orderbookNew.Asks))
orderBook.Asks = make(orderbook.Tranches, len(orderbookNew.Asks))
for x := range orderbookNew.Asks {
orderBook.Asks[x] = orderbook.Item{
orderBook.Asks[x] = orderbook.Tranche{
Amount: orderbookNew.Asks[x].Quantity,
Price: orderbookNew.Asks[x].Price,
}
Expand Down
16 changes: 8 additions & 8 deletions exchanges/binance/binance_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,18 +460,18 @@ func (b *Binance) SeedLocalCacheWithBook(p currency.Pair, orderbookNew *OrderBoo
Exchange: b.Name,
LastUpdateID: orderbookNew.LastUpdateID,
VerifyOrderbook: b.CanVerifyOrderbook,
Bids: make(orderbook.Items, len(orderbookNew.Bids)),
Asks: make(orderbook.Items, len(orderbookNew.Asks)),
Bids: make(orderbook.Tranches, len(orderbookNew.Bids)),
Asks: make(orderbook.Tranches, len(orderbookNew.Asks)),
LastUpdated: time.Now(), // Time not provided in REST book.
}
for i := range orderbookNew.Bids {
newOrderBook.Bids[i] = orderbook.Item{
newOrderBook.Bids[i] = orderbook.Tranche{
Amount: orderbookNew.Bids[i].Quantity,
Price: orderbookNew.Bids[i].Price,
}
}
for i := range orderbookNew.Asks {
newOrderBook.Asks[i] = orderbook.Item{
newOrderBook.Asks[i] = orderbook.Tranche{
Amount: orderbookNew.Asks[i].Quantity,
Price: orderbookNew.Asks[i].Price,
}
Expand Down Expand Up @@ -641,16 +641,16 @@ func (b *Binance) unsubscribeFromChan(chans []subscription.Subscription) error {

// ProcessUpdate processes the websocket orderbook update
func (b *Binance) ProcessUpdate(cp currency.Pair, a asset.Item, ws *WebsocketDepthStream) error {
updateBid := make([]orderbook.Item, len(ws.UpdateBids))
updateBid := make([]orderbook.Tranche, len(ws.UpdateBids))
for i := range ws.UpdateBids {
updateBid[i] = orderbook.Item{
updateBid[i] = orderbook.Tranche{
Price: ws.UpdateBids[i][0].Float64(),
Amount: ws.UpdateBids[i][1].Float64(),
}
}
updateAsk := make([]orderbook.Item, len(ws.UpdateAsks))
updateAsk := make([]orderbook.Tranche, len(ws.UpdateAsks))
for i := range ws.UpdateAsks {
updateAsk[i] = orderbook.Item{
updateAsk[i] = orderbook.Tranche{
Price: ws.UpdateAsks[i][0].Float64(),
Amount: ws.UpdateAsks[i][1].Float64(),
}
Expand Down
8 changes: 4 additions & 4 deletions exchanges/binance/binance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,16 +592,16 @@ func (b *Binance) UpdateOrderbook(ctx context.Context, p currency.Pair, assetTyp
return book, err
}

book.Bids = make(orderbook.Items, len(orderbookNew.Bids))
book.Bids = make(orderbook.Tranches, len(orderbookNew.Bids))
for x := range orderbookNew.Bids {
book.Bids[x] = orderbook.Item{
book.Bids[x] = orderbook.Tranche{
Amount: orderbookNew.Bids[x].Quantity,
Price: orderbookNew.Bids[x].Price,
}
}
book.Asks = make(orderbook.Items, len(orderbookNew.Asks))
book.Asks = make(orderbook.Tranches, len(orderbookNew.Asks))
for x := range orderbookNew.Asks {
book.Asks[x] = orderbook.Item{
book.Asks[x] = orderbook.Tranche{
Amount: orderbookNew.Asks[x].Quantity,
Price: orderbookNew.Asks[x].Price,
}
Expand Down
16 changes: 8 additions & 8 deletions exchanges/binanceus/binanceus_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ func (bi *Binanceus) SynchroniseWebsocketOrderbook() {

// ProcessUpdate processes the websocket orderbook update
func (bi *Binanceus) ProcessUpdate(cp currency.Pair, a asset.Item, ws *WebsocketDepthStream) error {
updateBid := make([]orderbook.Item, len(ws.UpdateBids))
updateBid := make([]orderbook.Tranche, len(ws.UpdateBids))
for i := range ws.UpdateBids {
price := ws.UpdateBids[i][0]
p, err := strconv.ParseFloat(price, 64)
Expand All @@ -684,10 +684,10 @@ func (bi *Binanceus) ProcessUpdate(cp currency.Pair, a asset.Item, ws *Websocket
if err != nil {
return err
}
updateBid[i] = orderbook.Item{Price: p, Amount: a}
updateBid[i] = orderbook.Tranche{Price: p, Amount: a}
}

updateAsk := make([]orderbook.Item, len(ws.UpdateAsks))
updateAsk := make([]orderbook.Tranche, len(ws.UpdateAsks))
for i := range ws.UpdateAsks {
price := ws.UpdateAsks[i][0]
p, err := strconv.ParseFloat(price, 64)
Expand All @@ -699,7 +699,7 @@ func (bi *Binanceus) ProcessUpdate(cp currency.Pair, a asset.Item, ws *Websocket
if err != nil {
return err
}
updateAsk[i] = orderbook.Item{Price: p, Amount: a}
updateAsk[i] = orderbook.Tranche{Price: p, Amount: a}
}

return bi.Websocket.Orderbook.Update(&orderbook.Update{
Expand Down Expand Up @@ -844,18 +844,18 @@ func (bi *Binanceus) SeedLocalCacheWithBook(p currency.Pair, orderbookNew *Order
Exchange: bi.Name,
LastUpdateID: orderbookNew.LastUpdateID,
VerifyOrderbook: bi.CanVerifyOrderbook,
Bids: make(orderbook.Items, len(orderbookNew.Bids)),
Asks: make(orderbook.Items, len(orderbookNew.Asks)),
Bids: make(orderbook.Tranches, len(orderbookNew.Bids)),
Asks: make(orderbook.Tranches, len(orderbookNew.Asks)),
LastUpdated: time.Now(), // Time not provided in REST book.
}
for i := range orderbookNew.Bids {
newOrderBook.Bids[i] = orderbook.Item{
newOrderBook.Bids[i] = orderbook.Tranche{
Amount: orderbookNew.Bids[i].Quantity,
Price: orderbookNew.Bids[i].Price,
}
}
for i := range orderbookNew.Asks {
newOrderBook.Asks[i] = orderbook.Item{
newOrderBook.Asks[i] = orderbook.Tranche{
Amount: orderbookNew.Asks[i].Quantity,
Price: orderbookNew.Asks[i].Price,
}
Expand Down
8 changes: 4 additions & 4 deletions exchanges/binanceus/binanceus_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,16 +357,16 @@ func (bi *Binanceus) UpdateOrderbook(ctx context.Context, pair currency.Pair, as
if err != nil {
return book, err
}
book.Bids = make([]orderbook.Item, len(orderbookNew.Bids))
book.Bids = make([]orderbook.Tranche, len(orderbookNew.Bids))
for x := range orderbookNew.Bids {
book.Bids[x] = orderbook.Item{
book.Bids[x] = orderbook.Tranche{
Amount: orderbookNew.Bids[x].Quantity,
Price: orderbookNew.Bids[x].Price,
}
}
book.Asks = make([]orderbook.Item, len(orderbookNew.Asks))
book.Asks = make([]orderbook.Tranche, len(orderbookNew.Asks))
for x := range orderbookNew.Asks {
book.Asks[x] = orderbook.Item{
book.Asks[x] = orderbook.Tranche{
Amount: orderbookNew.Asks[x].Quantity,
Price: orderbookNew.Asks[x].Price,
}
Expand Down
8 changes: 4 additions & 4 deletions exchanges/bitfinex/bitfinex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ func TestGetHistoricTrades(t *testing.T) {
}

var testOb = orderbook.Base{
Asks: []orderbook.Item{
Asks: []orderbook.Tranche{
{Price: 0.05005, Amount: 0.00000500},
{Price: 0.05010, Amount: 0.00000500},
{Price: 0.05015, Amount: 0.00000500},
Expand All @@ -1761,7 +1761,7 @@ var testOb = orderbook.Base{
{Price: 0.05045, Amount: 0.00000500},
{Price: 0.05050, Amount: 0.00000500},
},
Bids: []orderbook.Item{
Bids: []orderbook.Tranche{
{Price: 0.05000, Amount: 0.00000500},
{Price: 0.04995, Amount: 0.00000500},
{Price: 0.04990, Amount: 0.00000500},
Expand All @@ -1783,7 +1783,7 @@ func TestChecksum(t *testing.T) {
}

func TestReOrderbyID(t *testing.T) {
asks := []orderbook.Item{
asks := []orderbook.Tranche{
{ID: 4, Price: 100, Amount: 0.00000500},
{ID: 3, Price: 100, Amount: 0.00000500},
{ID: 2, Price: 100, Amount: 0.00000500},
Expand All @@ -1803,7 +1803,7 @@ func TestReOrderbyID(t *testing.T) {
}
}

bids := []orderbook.Item{
bids := []orderbook.Tranche{
{ID: 4, Price: 100, Amount: 0.00000500},
{ID: 3, Price: 100, Amount: 0.00000500},
{ID: 2, Price: 100, Amount: 0.00000500},
Expand Down
18 changes: 9 additions & 9 deletions exchanges/bitfinex/bitfinex_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,10 +1471,10 @@ func (b *Bitfinex) WsInsertSnapshot(p currency.Pair, assetType asset.Item, books
return errors.New("no orderbooks submitted")
}
var book orderbook.Base
book.Bids = make(orderbook.Items, 0, len(books))
book.Asks = make(orderbook.Items, 0, len(books))
book.Bids = make(orderbook.Tranches, 0, len(books))
book.Asks = make(orderbook.Tranches, 0, len(books))
for i := range books {
item := orderbook.Item{
item := orderbook.Tranche{
ID: books[i].ID,
Amount: books[i].Amount,
Price: books[i].Price,
Expand Down Expand Up @@ -1513,13 +1513,13 @@ func (b *Bitfinex) WsUpdateOrderbook(c *subscription.Subscription, p currency.Pa
orderbookUpdate := orderbook.Update{
Asset: assetType,
Pair: p,
Bids: make([]orderbook.Item, 0, len(book)),
Asks: make([]orderbook.Item, 0, len(book)),
Bids: make([]orderbook.Tranche, 0, len(book)),
Asks: make([]orderbook.Tranche, 0, len(book)),
UpdateTime: time.Now(), // Not included in update
}

for i := range book {
item := orderbook.Item{
item := orderbook.Tranche{
ID: book[i].ID,
Amount: book[i].Amount,
Price: book[i].Price,
Expand Down Expand Up @@ -2088,7 +2088,7 @@ func validateCRC32(book *orderbook.Base, token int) error {
reOrderByID(book.Asks)

// RO precision calculation is based on order ID's and amount values
var bids, asks []orderbook.Item
var bids, asks []orderbook.Tranche
for i := 0; i < 25; i++ {
if i < len(book.Bids) {
bids = append(bids, book.Bids[i])
Expand Down Expand Up @@ -2142,10 +2142,10 @@ func validateCRC32(book *orderbook.Base, token int) error {
// reOrderByID sub sorts orderbook items by its corresponding ID when price
// levels are the same. TODO: Deprecate and shift to buffer level insertion
// based off ascending ID.
func reOrderByID(depth []orderbook.Item) {
func reOrderByID(depth []orderbook.Tranche) {
subSort:
for x := 0; x < len(depth); {
var subset []orderbook.Item
var subset []orderbook.Tranche
// Traverse forward elements
for y := x + 1; y < len(depth); y++ {
if depth[x].Price == depth[y].Price &&
Expand Down
16 changes: 8 additions & 8 deletions exchanges/bitfinex/bitfinex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,36 +403,36 @@ func (b *Bitfinex) UpdateOrderbook(ctx context.Context, p currency.Pair, assetTy
}
if assetType == asset.MarginFunding {
o.IsFundingRate = true
o.Asks = make(orderbook.Items, len(orderbookNew.Asks))
o.Asks = make(orderbook.Tranches, len(orderbookNew.Asks))
for x := range orderbookNew.Asks {
o.Asks[x] = orderbook.Item{
o.Asks[x] = orderbook.Tranche{
ID: orderbookNew.Asks[x].OrderID,
Price: orderbookNew.Asks[x].Rate,
Amount: orderbookNew.Asks[x].Amount,
Period: int64(orderbookNew.Asks[x].Period),
}
}
o.Bids = make(orderbook.Items, len(orderbookNew.Bids))
o.Bids = make(orderbook.Tranches, len(orderbookNew.Bids))
for x := range orderbookNew.Bids {
o.Bids[x] = orderbook.Item{
o.Bids[x] = orderbook.Tranche{
ID: orderbookNew.Bids[x].OrderID,
Price: orderbookNew.Bids[x].Rate,
Amount: orderbookNew.Bids[x].Amount,
Period: int64(orderbookNew.Bids[x].Period),
}
}
} else {
o.Asks = make(orderbook.Items, len(orderbookNew.Asks))
o.Asks = make(orderbook.Tranches, len(orderbookNew.Asks))
for x := range orderbookNew.Asks {
o.Asks[x] = orderbook.Item{
o.Asks[x] = orderbook.Tranche{
ID: orderbookNew.Asks[x].OrderID,
Price: orderbookNew.Asks[x].Price,
Amount: orderbookNew.Asks[x].Amount,
}
}
o.Bids = make(orderbook.Items, len(orderbookNew.Bids))
o.Bids = make(orderbook.Tranches, len(orderbookNew.Bids))
for x := range orderbookNew.Bids {
o.Bids[x] = orderbook.Item{
o.Bids[x] = orderbook.Tranche{
ID: orderbookNew.Bids[x].OrderID,
Price: orderbookNew.Bids[x].Price,
Amount: orderbookNew.Bids[x].Amount,
Expand Down
8 changes: 4 additions & 4 deletions exchanges/bitflyer/bitflyer_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,17 @@ func (b *Bitflyer) UpdateOrderbook(ctx context.Context, p currency.Pair, assetTy
return book, err
}

book.Asks = make(orderbook.Items, len(orderbookNew.Asks))
book.Asks = make(orderbook.Tranches, len(orderbookNew.Asks))
for x := range orderbookNew.Asks {
book.Asks[x] = orderbook.Item{
book.Asks[x] = orderbook.Tranche{
Price: orderbookNew.Asks[x].Price,
Amount: orderbookNew.Asks[x].Size,
}
}

book.Bids = make(orderbook.Items, len(orderbookNew.Bids))
book.Bids = make(orderbook.Tranches, len(orderbookNew.Bids))
for x := range orderbookNew.Bids {
book.Bids[x] = orderbook.Item{
book.Bids[x] = orderbook.Tranche{
Price: orderbookNew.Bids[x].Price,
Amount: orderbookNew.Bids[x].Size,
}
Expand Down
Loading

0 comments on commit 4cd4fb0

Please sign in to comment.