Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

orderbook: Refactor package structure for simplicity and efficiency #1465

Merged
merged 18 commits into from
May 14, 2024
8 changes: 4 additions & 4 deletions cmd/exchange_template/wrapper_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,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 @@ -3461,13 +3461,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 @@ -3574,13 +3574,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 @@ -3680,13 +3680,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 @@ -222,17 +222,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 @@ -493,18 +493,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 @@ -652,7 +652,7 @@ func (b *Binance) Unsubscribe(channelsToUnsubscribe []subscription.Subscription)

// 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 {
price, ok := ws.UpdateBids[i][0].(string)
if !ok {
Expand All @@ -670,10 +670,10 @@ func (b *Binance) ProcessUpdate(cp currency.Pair, a asset.Item, ws *WebsocketDep
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, ok := ws.UpdateAsks[i][0].(string)
if !ok {
Expand All @@ -691,7 +691,7 @@ func (b *Binance) ProcessUpdate(cp currency.Pair, a asset.Item, ws *WebsocketDep
if err != nil {
return err
}
updateAsk[i] = orderbook.Item{Price: p, Amount: a}
updateAsk[i] = orderbook.Tranche{Price: p, Amount: a}
}

return b.Websocket.Orderbook.Update(&orderbook.Update{
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 @@ -615,16 +615,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 @@ -379,16 +379,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 @@ -1759,7 +1759,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 @@ -1771,7 +1771,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 @@ -1793,7 +1793,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 @@ -1813,7 +1813,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 @@ -426,36 +426,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