avatarCoucou Camille

Summary

This article provides a detailed guide on how to stream Binance order updates using Python.

Abstract

The article titled "Streaming Binance Order Updates Using Python" offers a comprehensive guide on how to stream order and account updates from Binance. The process involves creating a User Data Stream with a unique listen key, which needs to be generated each time a new connection is established. The guide covers the steps to obtain Binance API and Secret, generate a listen key, establish a websocket connection, and process messages. The article also includes additional notes on the validity of the listen key and the websocket connection.

Bullet points

  • The article provides a detailed guide on streaming order updates from Binance using Python.
  • Unlike market data streaming, order and account updates require the creation of a User Data Stream with a unique listen key.
  • The listen key needs to be generated each time a new connection is established.
  • The base API endpoint for Binance Spot & Margin is https://api.binance.com, while for Binance Futures it is https://fapi.binance.com.
  • The websocket endpoints for Binance Spot & Margin is wss://stream.binance.com:9443, while for Binance Futures it is wss://fstream.binance.com or wss://fstream-auth.binance.com.
  • User Data Streams are accessed at /ws/<listenKey> or /stream?streams=<listenKey>.
  • The listen key is valid for 60 minutes after creation and it is recommended to send a ping about every 30 minutes to keep it alive.
  • A single connection to <stream.binance.com> is only valid for 24 hours.
  • The article includes code examples for generating a listen key, establishing a websocket connection, and processing messages.
  • The message received will be in the following format: {"e": "executionReport", "E": 123456789, "s": "BTCUSDT", "c": "NEW", "S": "BUY", "o": "LIMIT", "f": "GTC", "q": "1.00000000", "p": "0.00000000", "F": "0.00000000", "g": 123456789, "C": "NONE", "x": "NEW", "X": "NEW", "r": "ISO", "i": 123456789, "l": "0.00000000", "z": "0.00000000", "L": "0.00000000", "n": "0", "N": null, "T": 123456789, "t": 123456789, "I": 123456789, "w": true, "m": false, "M": false, "O": 123456789, "Z": "0.00000000", "Y": "0.00000000", "Q": "0.00000000"}.
  • The possible execution types are NEW, CANCELED, TRADE, and EXPIRED.
  • Event type "outboundAccountPosition" is sent whenever an account balance has changed.
  • Event type "balanceUpdate" is sent for any deposit or withdrawals from the account.

Streaming Binance Order Updates Using Python

The detailed steps to stream your order and account update from Binance.

Image by Kanchanara from Unsplash

In previous articles, I wrote about the steps for live market data streaming from crypto exchanges, as well as streaming order & fill updates from FTX.

Streaming from Binance is not so straightforward as FTX, but still feasible.

1. Get Binance API and Secret 2. Streaming Order Updates 2.1. Generate Listen Key 2.2. Websocket Endpoints 2.3. Streaming Connection 2.4. Message Processing 2.5. Additional notes

1. Get Binance API and Secret

You may skip this session if you are familiar with the steps. Just make sure you set it for the intended purpose: spot & margin or futures trading or both.

  1. Log in to your Binance account.
  2. Go to your Profile, and under API Management page, enter your new API key name to “Create”.
  3. Binance will ask for a code from your email and a code from your authentication app. Enter the codes and click “Submit”.
  4. The new API key is now created. Note that the secret key is only visible to you this step and will be hidden afterwards. So remember to securely save it for your future references.
  5. Edit API key permissions.
  6. Save the changes by entering a code from your authentication app.

2. Streaming Order Updates

Unlike the endpoints and channels used for market data streaming, to stream the order and account update, you need to create a User Data Stream with unique listen key, which needs to be generated each time you start a new connection.

2.1. Generate Listen Key

The base API endpoint is different for Spot and Futures:

2.2. Websocket Endpoints

The base websocket endpoints for Spot and Futures:

  • Binance Spot & Margin: wss://stream.binance.com:9443
  • Binance Futures: wss://fstream.binance.com or wss://fstream-auth.binance.com

User Data Streams are accessed at /ws/<listenKey> or /stream?streams=<listenKey>. As an example:

Substitute api_key in line 5 to the API key generated in Step 1.

2.3. Streaming Connection

With the listen_key generated and the websocket endpoints that matches your streaming needs, the websocket connection can be implemented similarly as examples in previous articles. Detailed code as follows:

2.4. Message Processing

Message received will be in the following format. For event type "executionReport" (order updates):

Possible execution types are:

  • NEW
  • CANCELED : order canceled by user
  • TRADE : partial of full fill of the order
  • EXPIRED : order cancelled because of its order type (e.g. IOC or FOK orders, etc)

Event type "ourboundAccountPosition" is sent whenever an account balance has changed:

Event type "balanceUpdate" for any deposit or withdrawals from the account:

Take executionReport as an example to process the fill messages. To convert the received message from string to map, and extract the detailed information on trade done:

2.5. Additional notes

  • A User Data Stream listenKey is valid for 60 minutes after creation. To prevent a timeout and keep it alive, it is recommended to send a ping about every 30 minutes.
  • Doing a PUT on a listenKey will extend its validity for 60 minutes.
  • Doing a DELETE on a listenKey will close the stream and invalidate the listenKey.
  • A single connection to stream.binance.com is only valid for 24 hours; do expect a disconnection at the 24 hour mark.

Thanks for reading, hope the article helps!😄

Cryptocurrency
Binance
Websocket
Recommended from ReadMedium