Streaming FTX Order Updates using Python
In a previous article, I wrote about the steps to stream live data from FTX & Binance using Python.
Streaming live updates of orders (new, cancel, fill), or the fills alone could also be done for FTX, and save you the trouble to query the order status. This article will walk you through the detailed steps.
1. Get FTX API and Secret for Login 2. Streaming Fills 3. Streaming Orders
1. Get FTX API and Secret for Login
First, you need to login to FTX, and from the profile icon on the top right of the website, select “API”:

Scroll down to the API section, and you can choose from the following:

A new set of API Key are Secret will be generated. The values change every time you try to create a new one, and once created, you won’t be able to see the API Secret again. So make sure you store the key and secret and do not share it with anyone!
* The set of key and secret below have been deleted before this piece is published.

2. Streaming Fills
The basic structure is the same as data streaming:
import websocketendpoint = "wss://ftx.com/ws/"def on_open(self, ws):
# todo: send authenticationdef on_message(self, ws, message):
print(message)def on_error(self, ws, error):
print(f"Error: {error}")def on_close(self, close_msg):
print(f"Connection close")ws = websocket.WebSocketApp(endpoint,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever(ping_interval=15)In the case of ticker streaming, channel to subscribe is “ticker”, and on_open function was defined as follows:
import jsondef on_open(self, ws):
data = json.dumps({"op": "subscribe",
"channel": "ticker",
"market": "BTC-PERP"})
ws.send(data)
print("Connected")For channel “fill”, authentication is needed, with the API key and secret generated in the previous step.
As requested in FTX Websocket API, following info is needed for private channels:
key: API keytime: integer current timestamp (in milliseconds)sign: SHA256 HMAC of the following string, using your API secret:<time>websocket_loginsubaccount: (optional) subaccount name
Store the API key and secret as strings:
API_KEY = "7jEZE__dl6qI2bFH4sdemgPbbBEtMmZ_wtzAQ4DL"
API_SECRET = "zHBIsNP2nq-zeoPo9U4zH0XXisXs0nasXLSFO-pJ"Encoding of API secret requires the use of hmac package, install it in Python console: pip install hmac
Finally, the on_open function is defined as follows:
import time
import json
import hmacdef on_open(self, ws): # with authentication
tims_ms = int(time.time() * 1000) # in milliseconds
auth = json.dumps(
{
"op": "login",
"args": {
"key": API_KEY,
"time": tims_ms,
"sign": hmac.new(API_SECRET.encode(),
f"{ts}websocket_login".encode(),
"sha256").hexdigest(),
"subaccount": self.subaccount
}
}
)
ws.send(auth)
data = json.dumps({"op": "subscribe",
"channel": "fills"})
ws.send(data)
print("Fills channel connected")Response will be in the following format, but in strings:

To convert the received message from string to map:
def on_message(self, ws, message):
msg_map = json.loads(message)["data"] # detailed info for fill
data = msg_map["data"] market = data["market"]
side = data["side"] # "buy" or "sell"
price = data["price"]
size = data["size"]
fee = data["fee"]
liquidity = data["liquidity"] # "maker" or "taker"3. Streaming Orders
Orders channel streams updates to your orders across all markets. Orders streamer works exactly the same way as fills streamer, only difference in the program is to change "channel" in on_open() from "fills" to "orders" . Message received will be like:

"type": “limit” or “market”"status": “new” or “closed”
Final Note:
Order and fill streaming are helpful if you have a large number of orders in play and wish to get instant updates, especially in high-frequency algorithm trading. Orders streamer publish about every single update of each order but could take up a lot of resources as well. Hence it’s recommended that you choose the one that better suits your needs and specific use cases.
Thanks for reading, and feel free to comment if you encounter any problems!
Update: New article on order update streaming for Binance!





