Get the data on the Futures Binance with Python’s web binance flow
==================================================================== == ===================================
As an enthusiastic enthusiast of cryptocurrencies, you are probably not foreign to the world of decentralized and cryptocurrency commercial markets. However, the navigation of the complexities of the Binance’s Websocket API can be discouraging, especially when it comes to extracting specific data for a certain resource.
In this article, we will guide you through the creation of a Python code snippet that uses the Binance Websocket bees to extract user data and the prices of coins from a live flow. This example will demonstrate how to achieve this goal using the popular library requests to make requests http is Panda for data manipulation.
Prerequisites
——-
Before starting, make sure you have:
- An API Binance key (not included in this article)
2
- A basic understanding of the Python programming
The code

——
Here is the code snipper that extracts users’ data and coins prices from a web binance flow flow:
`Python
Import requests
It matters Panda as PD
From WebSocket Import Create_Connection
Set the Binance API key (replace with yours)
Api_key = ‘Your_binance_api_key’
Def Get_user_data (URL):
“” “Get user data from the URL specified” “”
Headers = {‘Authorization’: f’bearer {api_key} ‘}
Response = requests.get (f’https: //api.binance.com/api/v3/fetchopenorders’, params = {‘symbol’: ‘btcusdt’})
Return Response.json ()
Def Get_coin_prices (WS_url, symbol):
“” “Get the prices of the coins from the websocket Binance stream” “”
WS = Create_Connection (WS_URL)
Callback Def (Message):
If the message [‘Type’] == ‘Message’:
Data = json.loads (Message.Decode ())
Prices = []
For the element in the data:
Price = Float (article [‘Data’] [‘Price’])
Prices.Append (price)
Return Pd.Dataframe (Prices) .to_DIT ()
WS.Subscribe (symbol, callback = callback)
WS.Close ()
Get user data and coins prices
WS_URL = ‘WSS: //binance.com/ws/2’
symbol = ‘btcusdt’
User_data = Get_user_data (WS_URL)
Coin_prices = Get_coin_prices (WS_URL, symbol)
Print data extracted
Print (“User data:”)
For the user in User_data:
Print (user [‘id’], user [‘symbol’])
Print (“\ Ncoin Prices:”)
For money, prices in currency_plicges.items ():
PRINT (F “{Coins}: {Prices}”)
`
Explanation
-------
This code consists of two main functions:Get_user_data is Get_Coin_prices. The first function recovers user data from the Binance EPI using the specified URL. Then use a callback function to analyze the received Json data.
The second function creates a binance websocket connection using theCreate_Connectionis from WebSocket-Client ‘function. It subscribers to the specified symbol (in this case, BTCUSDT) and listen to new messages. When a message is received, it extracts relevant data (price) and returns it as a dictionary. Finally, the websocket connection closes.
Tips and variants
———
- Make sure to correctly manage errors in the production code.
- You can change the
GET_USER_DATAto recover other types of data (for example, market orders) function.
- Experiment with different symbols and times to see how they influence the extracted data.
- Take into consideration the use of a more robust errors management approach, such as recording errors or increase in exceptions.
Following this example code, you should be able to correctly recover user data and coins prices from a flow of binance websockets. Happy trading!
