Ethereum: Python Binance – How to get the last PRICES traded in futures with websocket?

Ethereum: Trading Latest Futures Prices on Binance WebSocket

As a developer working with Ethereum and the Binance API, getting real-time price data is essential for building effective trading strategies. In this article, we will explain how to get the latest futures prices using the Binance WebSocket API.

Prerequisites

  • You have created a Binance account and obtained an API key.
  • You have installed the required libraries: python-binance and websocket-client.

Step-by-step guide

  • Initialize Binance Socket Manager

import binancesocket as bss

BinanceWS = bss.BinanceSocketManager(binance_info=binance_info)

`

Replace binance_info with your actual Binance API key and secret.

  • Establish a connection to the WebSocket server

    Ethereum: Python Binance - How to get the last PRICES traded in futures with websocket?

conn_key = 'your_api_key_here'

conn_secret = 'your_api_secret_here'

bss.connect(wss='wss://api.binance.com/truehost', key=conn_key, secret=conn_secret)

Replace ‘wss’ with your Binance WebSocket server URL.

  • Subscribe to the futures market

symbol = 'BTCUSD'

subscribes = bss.Subscribed(symbol=symbol)

subscribes.subscribe()

This will subscribe to the Bitcoin USD futures market on the Binance exchange.

  • Get the latest prices for trading

latest_prices = subscribes.get_all_tickers()

for ticking with latest prices:

print(f"{ticker.ymbol} last price: {ticker.price}")

Remember that we are only interested in futures whose columns end in “_USD” (e.g. “BTCUSDT”, “ETHUSDT”, etc.). If you want to get all futures, uncomment this line:

all_tickers = subscribes.get_all_tickers()

ticker in all_tickers:

print(f"{ticker.ymbol} last price: {ticker.price}")

Sample Output

BTCUSDT last price: 40000.0

ETHUDT last price: 42000.0

...

  • Close the subscription and disconnect from the WebSocket server

subscribes.close()

bss.disconnect()

Remember to close the subscription when you are done using it to avoid unnecessary API requests.

After following these steps, you should get the latest futures prices on the Binance WebSocket server. Happy trading!

Leave a Reply

Your email address will not be published. Required fields are marked *