mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-07 23:16:53 +00:00
orderbook: consolidate slice array types to orderbook package (#1992)
* orderbook: consolidate slice array types to orderbook package * Update exchanges/bybit/bybit_types.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * linter: fix and add test * cranktakular: nits * cranktakular: nits * Update exchanges/orderbook/orderbook_types.go Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com> * Update exchanges/gateio/gateio_test.go Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com> * gk: nits consolidation * gk: rm unifySpotOrderbook func * gk: nit but different * linter: fix * gk: nits * glorious: nits * Update exchanges/binance/binance.go Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io> * Update exchanges/binance/binance_cfutures.go Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io> * Update exchanges/binanceus/binanceus.go Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io> * thrasher-:nits * thrasher-: more nit --------- Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com> Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
@@ -148,22 +148,7 @@ func (e *Exchange) GetOrderbook(ctx context.Context, marketID string, level int6
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ob := &Orderbook{
|
||||
MarketID: resp.MarketID,
|
||||
SnapshotID: resp.SnapshotID,
|
||||
Bids: make([]OBData, len(resp.Bids)),
|
||||
Asks: make([]OBData, len(resp.Asks)),
|
||||
}
|
||||
|
||||
for x := range resp.Asks {
|
||||
ob.Asks[x].Price = resp.Asks[x][0].Float64()
|
||||
ob.Asks[x].Volume = resp.Asks[x][1].Float64()
|
||||
}
|
||||
for x := range resp.Bids {
|
||||
ob.Bids[x].Price = resp.Bids[x][0].Float64()
|
||||
ob.Bids[x].Volume = resp.Bids[x][1].Float64()
|
||||
}
|
||||
return ob, nil
|
||||
return &Orderbook{MarketID: resp.MarketID, SnapshotID: resp.SnapshotID, Bids: resp.Bids.Levels(), Asks: resp.Asks.Levels()}, nil
|
||||
}
|
||||
|
||||
// GetMarketCandles gets candles for specified currency pair
|
||||
@@ -227,16 +212,8 @@ func (e *Exchange) GetMultipleOrderbooks(ctx context.Context, marketIDs []string
|
||||
orderbooks[i] = Orderbook{
|
||||
MarketID: resp[i].MarketID,
|
||||
SnapshotID: resp[i].SnapshotID,
|
||||
Asks: make([]OBData, len(resp[i].Asks)),
|
||||
Bids: make([]OBData, len(resp[i].Bids)),
|
||||
}
|
||||
for j := range resp[i].Asks {
|
||||
orderbooks[i].Asks[j].Price = resp[i].Asks[j][0].Float64()
|
||||
orderbooks[i].Asks[j].Volume = resp[i].Asks[j][1].Float64()
|
||||
}
|
||||
for j := range resp[i].Bids {
|
||||
orderbooks[i].Bids[j].Price = resp[i].Bids[j][0].Float64()
|
||||
orderbooks[i].Bids[j].Volume = resp[i].Bids[j][1].Float64()
|
||||
Asks: resp[i].Asks.Levels(),
|
||||
Bids: resp[i].Bids.Levels(),
|
||||
}
|
||||
}
|
||||
return orderbooks, nil
|
||||
|
||||
@@ -46,24 +46,18 @@ type Trade struct {
|
||||
|
||||
// tempOrderbook stores orderbook data
|
||||
type tempOrderbook struct {
|
||||
MarketID currency.Pair `json:"marketId"`
|
||||
SnapshotID int64 `json:"snapshotId"`
|
||||
Asks [][2]types.Number `json:"asks"`
|
||||
Bids [][2]types.Number `json:"bids"`
|
||||
}
|
||||
|
||||
// OBData stores orderbook data
|
||||
type OBData struct {
|
||||
Price float64
|
||||
Volume float64
|
||||
MarketID currency.Pair `json:"marketId"`
|
||||
SnapshotID int64 `json:"snapshotId"`
|
||||
Asks orderbook.LevelsArrayPriceAmount `json:"asks"`
|
||||
Bids orderbook.LevelsArrayPriceAmount `json:"bids"`
|
||||
}
|
||||
|
||||
// Orderbook holds current orderbook information returned from the exchange
|
||||
type Orderbook struct {
|
||||
MarketID currency.Pair
|
||||
SnapshotID int64
|
||||
Asks []OBData
|
||||
Bids []OBData
|
||||
Asks []orderbook.Level
|
||||
Bids []orderbook.Level
|
||||
}
|
||||
|
||||
// MarketCandle stores candle data for a given pair
|
||||
|
||||
@@ -257,43 +257,28 @@ func (e *Exchange) UpdateOrderbook(ctx context.Context, p currency.Pair, assetTy
|
||||
return nil, err
|
||||
}
|
||||
|
||||
book := &orderbook.Book{
|
||||
fPair, err := e.FormatExchangeCurrency(p, assetType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Retrieve level one book which is the top 50 ask and bids, this is not
|
||||
// cached.
|
||||
resp, err := e.GetOrderbook(ctx, fPair.String(), 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ob := &orderbook.Book{
|
||||
Exchange: e.Name,
|
||||
Pair: p,
|
||||
Asset: assetType,
|
||||
PriceDuplication: true,
|
||||
ValidateOrderbook: e.ValidateOrderbook,
|
||||
Asks: resp.Asks,
|
||||
Bids: resp.Bids,
|
||||
}
|
||||
|
||||
fPair, err := e.FormatExchangeCurrency(p, assetType)
|
||||
if err != nil {
|
||||
return book, err
|
||||
}
|
||||
// Retrieve level one book which is the top 50 ask and bids, this is not
|
||||
// cached.
|
||||
tempResp, err := e.GetOrderbook(ctx, fPair.String(), 1)
|
||||
if err != nil {
|
||||
return book, err
|
||||
}
|
||||
|
||||
book.Bids = make(orderbook.Levels, len(tempResp.Bids))
|
||||
for x := range tempResp.Bids {
|
||||
book.Bids[x] = orderbook.Level{
|
||||
Amount: tempResp.Bids[x].Volume,
|
||||
Price: tempResp.Bids[x].Price,
|
||||
}
|
||||
}
|
||||
|
||||
book.Asks = make(orderbook.Levels, len(tempResp.Asks))
|
||||
for y := range tempResp.Asks {
|
||||
book.Asks[y] = orderbook.Level{
|
||||
Amount: tempResp.Asks[y].Volume,
|
||||
Price: tempResp.Asks[y].Price,
|
||||
}
|
||||
}
|
||||
err = book.Process()
|
||||
if err != nil {
|
||||
return book, err
|
||||
if err := ob.Process(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return orderbook.Get(e.Name, p, assetType)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user