-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
aggregate orderbook
github-actions[bot] edited this page Jun 11, 2026
·
2 revisions
import ccxt from '../../js/ccxt.js';
const aggregateOrderBookSide = function (orderbookSide, precision = undefined) {
const result = []
const amounts = {}
for (let i = 0; i < orderbookSide.length; i++) {
const ask = orderbookSide[i]
let price = ask[0]
if (precision !== undefined) {
price = ccxt.decimalToPrecision (price, ccxt.ROUND, precision, ccxt.TICK_SIZE)
}
amounts[price] = (amounts[price] || 0) + ask[1]
}
Object.keys (amounts).forEach (price => {
result.push ([
parseFloat (price),
amounts[price]
])
})
return result
}
const aggregateOrderBook = function (orderbook, precision = undefined) {
let asks = aggregateOrderBookSide(orderbook['asks'], precision)
let bids = aggregateOrderBookSide(orderbook['bids'], precision)
return {
'asks': ccxt.sortBy (asks, 0),
'bids': ccxt.sortBy (bids, 0, true),
'timestamp': orderbook['timestamp'],
'datetime': orderbook['datetime'],
'nonce': orderbook['nonce'],
};
}
;(async () => {
const exchange = new ccxt.coinbasepro()
await exchange.loadMarkets ()
// exchange.verbose = true // uncomment for verbose debug output
// level 2 (default)
const orderbook = await exchange.fetchOrderBook('BTC/USD')
// or level 3
// const orderbook = await exchange.fetchOrderBook('BTC/USD', undefined, { 'level': 3 })
const step = 0.5 // 0.01, 0.1, 0.5, 1.0, 2.5, 5.0, 10.0
console.log (aggregateOrderBook (orderbook, step))
})();(If the page is not being rendered for you, you can refer to the mirror at https://docs.ccxt.com/)
- Install
- Examples
- Manual
- CCXT Pro
- Contributing
- Supported Exchanges
- Exchanges By Country
- API Spec By Method
- FAQ
- Changelog
- Awesome
- API Spec by Exchange
- fetchCurrencies
- alpaca
- apex
- ascendex
- aster
- backpack
- bigone
- binance
- bingx
- bit2c
- bitbank
- bitbns
- bitfinex
- bitflyer
- bitget
- bithumb
- bitmart
- bitmex
- bitopro
- bitrue
- bitso
- bitstamp
- bitteam
- bittrade
- bitvavo
- blockchaincom
- blofin
- btcbox
- btcmarkets
- btcturk
- bullish
- bybit
- bydfi
- cex
- coinbase
- coinbaseexchange
- coinbaseinternational
- coincheck
- coinex
- coinmate
- coinmetro
- coinone
- coinsph
- coinspot
- cryptocom
- cryptomus
- deepcoin
- delta
- deribit
- derive
- digifinex
- dydx
- exmo
- extended
- foxbit
- gate
- gemini
- grvt
- hashkey
- hibachi
- hitbtc
- hollaex
- htx
- hyperliquid
- independentreserve
- indodax
- kraken
- krakenfutures
- kucoin
- fetchBidsAsks
- latoken
- lbank
- lighter
- luno
- mercado
- mexc
- modetrade
- ndax
- novadax
- okx
- onetrading
- p2b
- pacifica
- paradex
- paymium
- phemex
- poloniex
- tokocrypto
- toobit
- upbit
- weex
- whitebit
- woo
- woofipro
- xt
- zaif
- fetchStatus