Get total numeber of coins listed via websoket with Python.

Hello everyone,
I need to have the total number of coins listed on the spot market always updated and know when a new one adds in real time. Using the REST API I implemented this function

def CoinsListed():
    number_of_coins = len(client.get_all_tickers())
    new_num_of_coins = 0
    while new_num_of_coins <= number_of_coins:
        new_num_of_coins = len(client.get_all_tickers())
        
        time.sleep(0.05)

But if I wanted to do the same by websoket, how could I do?

Thanks!

This is good solution to get symbols with ticker info. If you like get them from websocket, please check the same method from websocket api:

https://binance-docs.github.io/apidocs/websocket_api/en/#24hr-ticker-price-change-statistics

Hope this will be helpful.

1 Like

Thank you! Maybe I understood how to implement it.

  1. Create an empty dict called symbols
  2. Fill them once script loaded
  3. Create a trade or tick stream
  4. If symbol is not in your dict, add it!

You are going to have all of them in your dict, right at their first trade!

TIP:


# create this in your init
symbols: dict = {}

# now fill this using 

# in your Trade stream function

# get the symbol
s = data['s']

# is already in the list?
if s not in symbols:
    # OMG we have a new listing!
    symbols[s] = {}
symbols[s]['p'] = data['p']

Hope this helps

1 Like

Thanks!