{"id":13633131,"url":"https://github.com/tiagosiebler/OrderBooks","last_synced_at":"2025-04-18T10:34:14.629Z","repository":{"id":41321506,"uuid":"279839705","full_name":"tiagosiebler/orderbooks","owner":"tiagosiebler","description":"Simple utility classes to handle orderbook snapshot \u0026 delta events in node.js, with examples for Bybit \u0026 Binance.","archived":false,"fork":false,"pushed_at":"2025-04-04T13:51:36.000Z","size":361,"stargazers_count":143,"open_issues_count":1,"forks_count":35,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-09T08:04:06.566Z","etag":null,"topics":["binance","bitmex","bots","bybit","crypto","cryptocurrency","cryptocurrency-exchanges","nodejs","orderbook","orderbooks","trading","websockets"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tiagosiebler.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["tiagosiebler"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2020-07-15T10:37:13.000Z","updated_at":"2025-04-04T14:17:28.000Z","dependencies_parsed_at":"2024-01-17T17:19:16.970Z","dependency_job_id":"40d49540-fbf7-4136-a1fd-eeae06064a7d","html_url":"https://github.com/tiagosiebler/orderbooks","commit_stats":{"total_commits":42,"total_committers":5,"mean_commits":8.4,"dds":0.5,"last_synced_commit":"f2345d8128f8e1c3616556eb44c32db5f43c8aaf"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiagosiebler%2Forderbooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiagosiebler%2Forderbooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiagosiebler%2Forderbooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiagosiebler%2Forderbooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tiagosiebler","download_url":"https://codeload.github.com/tiagosiebler/orderbooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249479054,"owners_count":21279187,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binance","bitmex","bots","bybit","crypto","cryptocurrency","cryptocurrency-exchanges","nodejs","orderbook","orderbooks","trading","websockets"],"created_at":"2024-08-01T23:00:28.434Z","updated_at":"2025-04-18T10:34:14.324Z","avatar_url":"https://github.com/tiagosiebler.png","language":"TypeScript","funding_links":["https://github.com/sponsors/tiagosiebler"],"categories":["Miscellaneous tools"],"sub_categories":[],"readme":"# OrderBooks Store [![npm version](https://img.shields.io/npm/v/orderbooks.svg)][1] [![npm size](https://img.shields.io/bundlephobia/min/orderbooks.svg)][1] [![npm downloads](https://img.shields.io/npm/dt/orderbooks.svg)][1]\n\n[![CodeFactor](https://www.codefactor.io/repository/github/tiagosiebler/orderbooks/badge)](https://www.codefactor.io/repository/github/tiagosiebler/orderbooks)\n\n[1]: https://www.npmjs.com/package/orderbooks\n\nA minimal set of utilities for handling orderbook snapshots and delta updates, with bybit examples.\n\n## Issues \u0026 Discussion\n\n- Issues? Check the [issues tab](https://github.com/tiagosiebler/orderbooks/issues).\n- Discuss \u0026 collaborate with other node devs? Join our [Node.js Algo Traders](https://t.me/nodetraders) engineering community on telegram.\n\n## Documentation\n\n- [TSDoc Documentation (generated using typedoc via npm module)](https://tsdocs.dev/docs/orderbooks)\n\n## Related projects\n\nCheck out my related projects:\n\n- Try my connectors:\n  - [binance](https://www.npmjs.com/package/binance)\n  - [bybit-api](https://www.npmjs.com/package/bybit-api)\n  - [okx-api](https://www.npmjs.com/package/okx-api)\n  - [bitget-api](https://www.npmjs.com/package/bitget-api)\n  - [ftx-api](https://www.npmjs.com/package/ftx-api)\n- Try my misc utilities:\n  - [orderbooks](https://www.npmjs.com/package/orderbooks)\n- Check out my examples:\n  - [awesome-crypto-examples](https://github.com/tiagosiebler/awesome-crypto-examples)\n\n## Contributions \u0026 Thanks\n\nIf you found this project interesting or useful, [sponsor me](https://github.com/sponsors/tiagosiebler) on github, create accounts with my referral links:\n\n- [Bybit](https://www.bybit.com/en-US/register?affiliate_id=9410\u0026language=en-US\u0026group_id=0\u0026group_type=1)\n- [Binance](https://www.binance.com/en/register?ref=20983262)\n\nFor more ways to give thanks \u0026 support my efforts, visit [Contributions \u0026 Thanks](https://github.com/tiagosiebler/awesome-crypto-examples/wiki/Contributions-\u0026-Thanks)!\n\n## Project Contributions\n\nContributions are very welcome, I will review any incoming pull requests. See the issues tab for todo items.\n\n## Features\n\n- Handle snapshot and delta orderbook events.\n- Track multiple symbol orderbooks.\n- Easily access best bid/ask prices.\n- Conveniently access the difference between the best bid and ask prices, with the spread represented in basis point units.\n- Easily keep orderbook depth trimmed to max depth.\n- Tiny module with 0 external dependencies.\n\n## Installation\n\n```\nnpm install -save orderbooks\n```\n\n## Usage\n\n### Tracking\n\n- Import books store \u0026 level\n\n```javascript\nconst { OrderBooksStore, OrderBookLevel } = require('orderbooks');\n```\n\n- Create instance of orderbooks store, to store multiple order books for a broker\n\n```javascript\n// all options are optional\nconst options = {\n  // output traces on any events sent handled by book\n  traceLog: true,\n\n  // check current timestamp \u003e last timestamp, else deny processing event\n  checkTimestamps: false,\n\n  // max size of orderbook (e.g 50 == 25 bids \u0026 25 asks). Defaults to 250.\n  maxDepth: 50,\n};\n\nconst OrderBooks = new OrderBooksStore(options);\n```\n\n- Feed snapshot and delta updates into OrderBooks.handle() methods.\n\n## Examples\n\nSee the [./samples/](./samples/) folder for more.\n\n### Real Example - Binance\n\nSee [./samples/binance.ts](./samples/binance.ts)\n\n### Real Example - Bybit\n\n- Import modules\n- Prepare OrderBooks store instance\n- Connect to OrderBooks websockets\n- Map event properties to expected key:value pairs\n- Feed mapped snapshot and delta events into OrderBooks.handle() methods\n\nSee [./samples/bybit.ts](./samples/bybit.ts)\n\nExample output with `print()` calls to output book state to console:\n\n```\n---------- BTCUSD ask:bid 9240:9239.5 \u0026 spread: 0.01%\n┌─────────┬──────────┬────────┬────────┬─────────┐\n│ (index) │  symbol  │ price  │  side  │   qty   │\n├─────────┼──────────┼────────┼────────┼─────────┤\n│    0    │ 'BTCUSD' │  9252  │ 'Sell' │ 132623  │\n│    1    │ 'BTCUSD' │ 9251.5 │ 'Sell' │  82221  │\n│    2    │ 'BTCUSD' │  9251  │ 'Sell' │  34974  │\n│    3    │ 'BTCUSD' │ 9250.5 │ 'Sell' │  12842  │\n│    4    │ 'BTCUSD' │  9250  │ 'Sell' │ 550687  │\n│    5    │ 'BTCUSD' │ 9249.5 │ 'Sell' │  63371  │\n│    6    │ 'BTCUSD' │  9249  │ 'Sell' │ 200127  │\n│    7    │ 'BTCUSD' │ 9248.5 │ 'Sell' │ 129099  │\n│    8    │ 'BTCUSD' │  9248  │ 'Sell' │ 209061  │\n│    9    │ 'BTCUSD' │ 9247.5 │ 'Sell' │  30722  │\n│   10    │ 'BTCUSD' │  9247  │ 'Sell' │ 165469  │\n│   11    │ 'BTCUSD' │ 9246.5 │ 'Sell' │  97780  │\n│   12    │ 'BTCUSD' │  9246  │ 'Sell' │  95342  │\n│   13    │ 'BTCUSD' │ 9245.5 │ 'Sell' │  41319  │\n│   14    │ 'BTCUSD' │  9245  │ 'Sell' │ 227242  │\n│   15    │ 'BTCUSD' │ 9244.5 │ 'Sell' │ 167586  │\n│   16    │ 'BTCUSD' │  9244  │ 'Sell' │ 237029  │\n│   17    │ 'BTCUSD' │ 9243.5 │ 'Sell' │ 103426  │\n│   18    │ 'BTCUSD' │  9243  │ 'Sell' │ 126357  │\n│   19    │ 'BTCUSD' │ 9242.5 │ 'Sell' │ 165034  │\n│   20    │ 'BTCUSD' │  9242  │ 'Sell' │ 264286  │\n│   21    │ 'BTCUSD' │ 9241.5 │ 'Sell' │ 261200  │\n│   22    │ 'BTCUSD' │  9241  │ 'Sell' │ 233533  │\n│   23    │ 'BTCUSD' │ 9240.5 │ 'Sell' │ 399512  │\n│   24    │ 'BTCUSD' │  9240  │ 'Sell' │ 1397987 │\n│   25    │ 'BTCUSD' │ 9239.5 │ 'Buy'  │  1132   │\n│   26    │ 'BTCUSD' │  9239  │ 'Buy'  │ 234214  │\n│   27    │ 'BTCUSD' │ 9238.5 │ 'Buy'  │  58320  │\n│   28    │ 'BTCUSD' │  9238  │ 'Buy'  │  17094  │\n│   29    │ 'BTCUSD' │ 9237.5 │ 'Buy'  │  50980  │\n│   30    │ 'BTCUSD' │  9237  │ 'Buy'  │  13449  │\n│   31    │ 'BTCUSD' │ 9236.5 │ 'Buy'  │  2608   │\n│   32    │ 'BTCUSD' │  9236  │ 'Buy'  │  53742  │\n│   33    │ 'BTCUSD' │ 9235.5 │ 'Buy'  │ 106681  │\n│   34    │ 'BTCUSD' │  9235  │ 'Buy'  │  48653  │\n│   35    │ 'BTCUSD' │ 9234.5 │ 'Buy'  │  76188  │\n│   36    │ 'BTCUSD' │  9234  │ 'Buy'  │ 215664  │\n│   37    │ 'BTCUSD' │ 9233.5 │ 'Buy'  │ 169265  │\n│   38    │ 'BTCUSD' │  9233  │ 'Buy'  │  30296  │\n│   39    │ 'BTCUSD' │ 9232.5 │ 'Buy'  │ 196676  │\n│   40    │ 'BTCUSD' │  9232  │ 'Buy'  │  82840  │\n│   41    │ 'BTCUSD' │ 9231.5 │ 'Buy'  │ 105854  │\n│   42    │ 'BTCUSD' │  9231  │ 'Buy'  │  1671   │\n│   43    │ 'BTCUSD' │ 9230.5 │ 'Buy'  │  25909  │\n│   44    │ 'BTCUSD' │  9230  │ 'Buy'  │ 146198  │\n│   45    │ 'BTCUSD' │ 9229.5 │ 'Buy'  │  95941  │\n│   46    │ 'BTCUSD' │  9229  │ 'Buy'  │  61212  │\n│   47    │ 'BTCUSD' │ 9228.5 │ 'Buy'  │  76966  │\n│   48    │ 'BTCUSD' │  9228  │ 'Buy'  │  93996  │\n│   49    │ 'BTCUSD' │ 9227.5 │ 'Buy'  │  44058  │\n└─────────┴──────────┴────────┴────────┴─────────┘\n```\n\n## Accessing State\n\nAccess orderbook state using the OrderBooksStore.\n\n```javascript\nconst btcOrderBook = OrderBooks.getBook('BTCUSD');\n\n// Get an array dump of the current orderbook state (similar to what you see on exchange websites)\nconst btcOrderBookState = btcOrderBook.getBookState();\nconsole.log('Current book state: ', JSON.stringify(btcOrderBookState));\n\nconst bestBid = btcOrderBook.getBestBid();\n// bestBid = 9239.5\n\nconst secondBestBid = btcOrderBook.getBestBid(1);\n// secondBestBid = 9239\n\nconst bestAsk = btcOrderBook.getBestAsk();\n// bestAsk = 9040\n\nconst secondBestAsk = btcOrderBook.getBestAsk(1);\n// secondBestAsk = 9040.5\n\nconst currentSpread = btcORderBook.getSpreadPercent();\n// currentSpread = 0.01\n```\n\n## Utility Methods\n\nThe following ultity methods are exposed for each book:\n\n```javascript\nconst btcOrderBook = OrderBooks.getBook('BTCUSD');\n\n// console.log current orderbook state\nbtcOrderBook.print();\n\n// clear current orderbook to free memory\nbtcOrderBook.reset();\n```\n\n## Star History\n\n[![Star History Chart](https://api.star-history.com/svg?repos=tiagosiebler/ftx-api,tiagosiebler/bybit-api,tiagosiebler/binance,tiagosiebler/orderbooks,tiagosiebler/okx-api,tiagosiebler/awesome-crypto-examples\u0026type=Date)](https://star-history.com/#tiagosiebler/ftx-api\u0026tiagosiebler/bybit-api\u0026tiagosiebler/binance\u0026tiagosiebler/orderbooks\u0026tiagosiebler/okx-api\u0026tiagosiebler/awesome-crypto-examples\u0026Date)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiagosiebler%2FOrderBooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftiagosiebler%2FOrderBooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiagosiebler%2FOrderBooks/lists"}