How to view aggregated liquidations for cryptocurrencies from Binance?

Viewed 3389

On these sites (https://coinalyze.net/ethereum-classic/liquidations/, BTC/USDT), I am able to add following indications into grpah [Liquidations, Long Liquidations, Short Liquidations, Aggregated Liquidations COIN-margined Contracts, Aggregated Liquidations STABLECOIN-margined Contracts].

Aggregated liquidations = liquidations of coin-margined contracts + liquidations of stablecoin-margined contracts converted to USD. For the moment only BTC/USD and BTC/USDT contracts are included. See the indicator options, you can select/deselect individual contracts.


=> The main question is to how to obtain data streams for liquidations in cryptocurrencies, if possible from Tradingview or exchanges like Binance.

I have tried to add Aggregated liquidations or just Liquidations in to my graphs on https://www.tradingview.com for cryptocurrencies under Futures. I was not able to find its pine-script code or its built-in indicator, so I believe that data is private and was dead end for me.

Is it possible to obtain data streams for liquidations in cryptocurrencies from exchanges like Binance or others? or add Aggregated liquidations into graphs on TradingView for cryptocurrencies?

2 Answers

The short answer is no, it's not possible because Tradingview doesn't provide that level of data. Sites like coinalyze are using the Tradingview plugin and providing their own data streams for liquidations.

To create the equivalent on the Trandingview platform itself there is something of a workaround but it is less than ideal. It won't be live data and you will have to manually update the data array yourself. You will also have to source the liquidation data yourself.

You will have to take note of the timestamp of the first data entry and parse the liquidation data into a set of comma separated values.

From there you can "import" it into a script using array.from()

start_timestamp = timestamp(2021, 7, 9, 0, 0, 0)

var float[] a_longLiqs = array.from(17, 13458.4, 87453.56, 2345.23 , 23457.983, 353, .... etc)

var int index = na
var bool started = false
float longLiqs = na

if time == start_timestamp
    started := true
    index := 0
else if time > start_timestamp
    index += 1

if started and index < array.size(a_longLiqs)
    longLiqs := array.get(a_longLiqs, index)

plot(longLiqs)

At this point you've effectively converted the array into a time series variable longLiqs which you can work with the same as any other, such as close, volume, etc. However you only get new data when you add it to the array manually.

To obtain the aggregate data itself is a bit of a process. You have to make use of the exchange's APIs.

For example : https://www.bitmex.com/api/explorer/#/Liquidation

https://bybit-exchange.github.io/docs/inverse/#t-query_liqrecords

There are quite a number of existing projects on github in both js and python, I suggest you start there rather than reinvent the wheel. For example the cryptofeed py package might be a good place to start as it seems to support pulling the liquidation data over multiple exchanges.

https://pypi.org/project/cryptofeed/

https://github.com/bmoscon/cryptofeed/blob/master/docs/callbacks.md

Once you have the data you'll have to aggregate it yourself and parse it like I mentioned above to be able to insert it into a pine array.

Alternatively there are paid data providers that may make it a bit easier if you're ok with paying for the data. You may still need to aggregate it and parse it, but you'll only be dealing with one API rather than having to manage it from every exchange.

Here's one I've found that seems to provide aggregate liquidation data : https://www.cryptometer.io/api-doc/

I have started from here:

You have to install following packages: pip install websocket-client websocket.


#!/usr/bin/env python3

import websocket


class Liq:
    def __init__(self):
        self.socket = "wss://fstream.binance.com/ws/!forceOrder@arr"
        self.ws = websocket.WebSocketApp(self.socket, on_message=self.on_message, on_close=self.on_close)
        self.symbol: str = ""
        self.order_quantity = 0
        self.event_time: int = 0
        self.average_price: float = 0.0
        self.side = ""
        self.price: float = 0.0
        self.order_last_filled_quantity = 0.0
        self.order_filled_accumulated_quantity = 0
        self.order_trade_time = 0

    def print_result(self):
        amount = int(self.order_quantity * self.average_price)
        print(f"==> symbol={self.symbol}")
        print(f"==> side={self.side} | ", end="")
        if self.side == "BUY":
            print("shorts liquadated")
        else:
            print("longs liquadated")

        print(f"==> order_quantity={self.order_quantity}")
        print(f"==> event_time={self.event_time}")
        print(f"==> order_last_filled_quantity={self.order_last_filled_quantity}")
        print(f"==> order_filled_accumulated_quantity={self.order_filled_accumulated_quantity}")
        print(f"==> order_trade_time={self.order_trade_time}")
        print(f"==> price={self.price}")
        print(f"==> average_price={self.average_price}")
        print(f"==> liq_amount={amount}")
        print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")

    def on_message(self, ws, message):
        """Fetch liquidation Order Streams.

        __ https://binance-docs.github.io/apidocs/futures/en/#liquidation-order-streams
        """
        for item in message.split(","):
            item = item.replace("}", "").replace("{", "").replace('"', "").replace("o:s:", "s:")
            if "forceOrder" not in item:
                _item = item.split(":")
                if _item[0] == "E":
                    self.event_time = int(_item[1])
                elif _item[0] == "s":
                    self.symbol = _item[1]
                elif _item[0] == "S":
                    self.side = _item[1]
                elif _item[0] == "q":
                    self.order_quantity = float(_item[1])
                elif _item[0] == "p":
                    self.price = _item[1]
                elif _item[0] == "ap":
                    self.average_price = float(_item[1])
                elif _item[0] == "l":
                    self.order_last_filled_quantity = _item[1]
                elif _item[0] == "z":
                    self.order_filled_accumulated_quantity = _item[1]
                elif _item[0] == "T":
                    self.order_trade_time = _item[1]

        self.print_result()


    def on_close(self):
        print("closed")


liq = Liq()
liq.ws.run_forever()

Example output, all liquadated pairs will be printed:

==> symbol=KEEPUSDT
==> side=SELL | longs liquadated
==> order_quantity=4705.0
==> event_time=1634474643639
==> order_last_filled_quantity=278
==> order_filled_accumulated_quantity=4705
==> order_trade_time=1634474643630
==> price=0.9877
==> average_price=1.0
==> liq_amount=4705
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
==> symbol=FTMUSDT
==> side=BUY | shorts liquadated
==> order_quantity=1458.0
==> event_time=1634474896201
==> order_last_filled_quantity=1240
==> order_filled_accumulated_quantity=1458
==> order_trade_time=1634474896197
==> price=2.257972
==> average_price=2.236581
==> liq_amount=3260

Afterwards you can store this results in a database like mongadb.

Related