Live liquidations monitor for top cryptocurrency exchanges
Live liquidations data is public and can be accessed via exchanges WebSocket APIs, however, it can be quite tricky for people to access this data stream for multiple exchanges at once due to different data formats and quirks how that data is provided.
Goal of this post is to build something similar to @WhaleBotRektd and @BXRekt Twitter bots, just without twitter integration part (boring…).
We’ll use open source tardis-dev
lib here, so most of the hard work is handled for us — real-time WS connections management, data normalization, consolidate data streaming from multiple exchanges etc.
If you’re not comfortable with Node.js, see tardis-machine
that provides the same functionality as locally runnable server you can host on your own and connect to it from any language that supports WebSocket and HTTP protocols.
Full disclosure, both libs are written and maintained by me.
Let’s install tardis-dev
...
npm install tardis-dev
… and start with the simplest example possible where we’ll only stream live BitMEX liquidations for XBTUSD
and ETHUSD
instruments. You can try out code below live on RunKit or run it locally.
OK, this is all nice, handles BitMEX WS API connection management (subscribing to live data, automatic dropped connections restarts etc.) and provides async iterable iterator that we feed into for await...of
, but still it’s single exchange only and message doesn’t really look like @WhaleBotRektd message. Let’s fix that.
In the example below we’ll combine top crypto exchanges (BitMEX, FTX, Binance, Huobi etc.) live liquidations data streams into one and finally format the message properly — normalize their liquidations amounts to USD as some exchanges contracts are linear, some inverse, some have different contract multipliers/face values (for example each Huobi Swap contract is $100), detect liquidations position (long or short) based on liquidations side and format prices and amounts with currency formatter.
You can try out code below live on RunKit or run it locally.
We used combine
helper function that allows us combine multiple async iterables into one, which we can then again feed into for await...of
loop.
Additionally timeoutIntervalMS
has been set to keep WebSocket connection alive for longer even if there’s no data received for a while as liquidations do not happen that often. Here’s TypeScript version for TS fans.
Enjoy!