Search code examples
web-crawlerpine-scriptbinancetradingview-api

How to view aggregated liquidations for cryptocurrencies from Binance?


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?


Solution

  • 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.