This is a simple Python library that provides access to Binance Financial Information eXchange (FIX) SPOT messages using the FIX protocol. It allows you to perform key operations such as placing orders, canceling orders, and querying current limit usage.
Before using or testing the library, ensure that the necessary dependencies are installed. You can do this by running the following command:
pip install binance-fix-connector
Notes:
- FIX API only support Ed25519 keys. Please refer to this tutorial for setting up an Ed25519 key pair on the mainnet, and this one for the testnet.
- Ensure that your API key has the appropriate Fix API permissions for the Testnet environment before you begin testing.
All the FIX messages can be created with the BinanceFixConnector class. The following example demonstrates how to create a simple order using the FIX API:
importtimefrombinance_fix_connector.fix_connectorimportcreate_order_entry_sessionfrombinance_fix_connector.utilsimportget_api_key, get_private_keyfromconstantsimport (
path,
FIX_OE_URL,
INSTRUMENT,
ORD_REJECT_REASON,
ORD_STATUS,
ORD_TYPES,
SIDES,
TIME_IN_FORCE,
)
# CredentialsAPI_KEY, PATH_TO_PRIVATE_KEY_PEM_FILE=get_api_key(path)
client_oe=create_order_entry_session(
api_key=API_KEY,
private_key=get_private_key(PATH_TO_PRIVATE_KEY_PEM_FILE),
endpoint=FIX_OE_URL,
)
client_oe.retrieve_messages_until(message_type=["A"])
example="This example shows how to place a single order. Order type LIMIT.\nCheck https://github.com/binance/binance-spot-api-docs/blob/master/fix-api.md#newordersingled for additional types."client_oe.logger.info(example)
# PLACING SIMPLE ORDERmsg=client_oe.create_fix_message_with_basic_header("D")
msg.append_pair(38, 1) # ORD QTYmsg.append_pair(40, 2) # ORD TYPEmsg.append_pair(11, str(time.time_ns())) # CL ORD IDmsg.append_pair(44, 730) # PRICEmsg.append_pair(54, 2) # SIDEmsg.append_pair(55, INSTRUMENT) # SYMBOLmsg.append_pair(59, 1) # TIME IN FORCEclient_oe.send_message(msg)
responses=client_oe.retrieve_messages_until(message_type=["8"])
resp=next(
(xforxinresponsesifx.message_type.decode("utf-8") =="8"),
None,
)
client_oe.logger.info("Parsing response Execution Report (8) for an order LIMIT type.")
cl_ord_id=Noneifnotresp.get(11) elseresp.get(11).decode("utf-8")
order_qty=Noneifnotresp.get(38) elseresp.get(38).decode("utf-8")
ord_type=Noneifnotresp.get(40) elseresp.get(40).decode("utf-8")
side=Noneifnotresp.get(54) elseresp.get(54).decode("utf-8")
symbol=Noneifnotresp.get(55) elseresp.get(55).decode("utf-8")
price=Noneifnotresp.get(44) elseresp.get(44).decode("utf-8")
time_in_force=Noneifnotresp.get(59) elseresp.get(59).decode("utf-8")
cum_qty=Noneifnotresp.get(14) elseresp.get(14).decode("utf-8")
last_qty=Noneifnotresp.get(32) elseresp.get(32).decode("utf-8")
ord_status=Noneifnotresp.get(39) elseresp.get(39).decode("utf-8")
ord_rej_reason=Noneifnotresp.get(103) elseresp.get(103).decode("utf-8")
error_code=Noneifnotresp.get(25016) elseresp.get(25016).decode("utf-8")
text=Noneifnotresp.get(58) elseresp.get(58).decode("utf-8")
client_oe.logger.info(f"Client order ID: {cl_ord_id}")
client_oe.logger.info(f"Symbol: {symbol}")
client_oe.logger.info(
f"Order -> Type: {ORD_TYPES.get(ord_type, ord_type)} | Side: {SIDES.get(side, side)} | TimeInForce: {TIME_IN_FORCE.get(time_in_force,time_in_force)}",
)
client_oe.logger.info(
f"Price: {price} | Quantity: {order_qty} | cum qty: {cum_qty} | last qty: {last_qty}"
)
client_oe.logger.info(
f"Status: {ORD_STATUS.get(ord_status,ord_status)} | Msg: {ORD_REJECT_REASON.get(ord_rej_reason,ord_rej_reason)}",
)
client_oe.logger.info(f"Error code: {error_code} | Reason: {text}")
# LOGOUTclient_oe.logger.info("LOGOUT (5)")
client_oe.logout()
client_oe.retrieve_messages_until(message_type=["5"])
client_oe.logger.info(
"Closing the connection with server as we already sent the logout message"
)
client_oe.disconnect()Please look at examples folder to test the examples.
To try the examples, follow the indications written on the examples/config.ini.example file.
For more information, have a look at the Binance documentation on Fix API.
MIT