7

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?

alper
  • 2,919
  • 9
  • 53
  • 102

2 Answers2

7

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/

rumpypumpydumpy
  • 3,435
  • 2
  • 5
  • 10
  • That is the question I was asking, how/where can I obtain the data array information related to data streams for liquidations. – alper Jul 10 '21 at 12:10
  • Updated the answer with data source information – rumpypumpydumpy Jul 11 '21 at 01:49
  • www.cryptometer.io/api-doc/ seems not free . I was hoping to find a free way for this. – alper Jul 11 '21 at 10:51
  • No, data from cryptometer.io isn't free, but probably the quickest and easiest way. As far as I know there is no free aggregate sources. You will have to construct your own from separately pulling the data from each exchange if you don't want to pay for it. – rumpypumpydumpy Jul 11 '21 at 11:40
  • That's the questions I am looking for, like how can I pul the data from each exchange like Binance? If I can able to pull, its easy to construct the data. – alper Jul 11 '21 at 13:22
  • As I already explained, you will have to create your own process, which is most easily done in either js or python that makes use of each exchange's API in order to get the data you're after. There's at least one py package - cryptofeed (and probably others and for js if you prefer) that will simplify the task. The cryptofeed github even has an example of retrieving liquidations data and there are plenty of other projects on github that can show you how to access the APIs of exchanges if you prefer to go that route. – rumpypumpydumpy Jul 11 '21 at 14:24
  • I couldn't find and liquidation example related to binance at cryptofeed. Any simple code piece to pull the liquidation data from binance is the solution I was looking for. – alper Jul 13 '21 at 17:23
  • It isn't simple! If it was easy you wouldn't have data providers that are able to charge a fee. Especially if you aren't familiar with py, pandas, working with CSVs etc etc it will be a significant project and learning curve to undertake.The github repo has an example for handling liquidations feeds. It's for bitmex, but you would use it the same as for binance futures except using `BinanceFutures` as the exchange https://github.com/bmoscon/cryptofeed/blob/master/examples/demo_liquidations.py – rumpypumpydumpy Jul 14 '21 at 00:46
  • Ah the trading robots use that information to liquidate traders, so it will be nice to protect traders from those or at least allow them to access the same data. I will look into the links you have sent, in future if I am able to make it will let you know. – alper Jul 14 '21 at 01:09
4

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.

alper
  • 2,919
  • 9
  • 53
  • 102