Skip to main content

This is a simple Python library that provides access to Binance Financial Information eXchange (FIX) SPOT messages using the FIX protocol.

Project description

Binance FIX API Connector in Python

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.

Prerequisites

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.

Example

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:

import time
import os
from pathlib import Path

from binance_fix_connector.fix_connector import create_order_entry_session
from binance_fix_connector.utils import get_api_key, get_private_key

# Credentials
path = config_path = os.path.join(
    Path(__file__).parent.resolve(), "..", "config.ini"
)
API_KEY, PATH_TO_PRIVATE_KEY_PEM_FILE = get_api_key(path)

# FIX URL
FIX_OE_URL = "tcp+tls://fix-oe.testnet.binance.vision:9000"

# Response types
ORD_STATUS = {
    "0": "NEW",
    "1": "PARTIALLY_FILLED",
    "2": "FILLED",
    "4": "CANCELED",
    "6": "PENDING_CANCEL",
    "8": "REJECTED",
    "A": "PENDING_NEW",
    "C": "EXPIRED",
}
ORD_TYPES = {"1": "MARKET", "2": "LIMIT", "3": "STOP", "4": "STOP_LIMIT"}
SIDES = {"1": "BUY", "2": "SELL"}
TIME_IN_FORCE = {
    "1": "GOOD_TILL_CANCEL",
    "3": "IMMEDIATE_OR_CANCEL",
    "4": "FILL_OR_KILL",
}
ORD_REJECT_REASON = {"99": "OTHER"}

# Parameter
INSTRUMENT = "BNBUSDT"

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 ORDER
msg = client_oe.create_fix_message_with_basic_header("D")
msg.append_pair(38, 1)  # ORD QTY
msg.append_pair(40, 2)  # ORD TYPE
msg.append_pair(11, str(time.time_ns()))  # CL ORD ID
msg.append_pair(44, 730)  # PRICE
msg.append_pair(54, 2)  # SIDE
msg.append_pair(55, INSTRUMENT)  # SYMBOL
msg.append_pair(59, 1)  # TIME IN FORCE
client_oe.send_message(msg)


responses = client_oe.retrieve_messages_until(message_type="8")
resp = next(
    (x for x in responses if x.message_type.decode("utf-8") == "8"),
    None,
)
client_oe.logger.info("Parsing response Execution Report (8) for an order LIMIT type.")

cl_ord_id = None if not resp.get(11) else resp.get(11).decode("utf-8")
order_qty = None if not resp.get(38) else resp.get(38).decode("utf-8")
ord_type = None if not resp.get(40) else resp.get(40).decode("utf-8")
side = None if not resp.get(54) else resp.get(54).decode("utf-8")
symbol = None if not resp.get(55) else resp.get(55).decode("utf-8")
price = None if not resp.get(44) else resp.get(44).decode("utf-8")
time_in_force = None if not resp.get(59) else resp.get(59).decode("utf-8")
cum_qty = None if not resp.get(14) else resp.get(14).decode("utf-8")
last_qty = None if not resp.get(32) else resp.get(32).decode("utf-8")
ord_status = None if not resp.get(39) else resp.get(39).decode("utf-8")
ord_rej_reason = None if not resp.get(103) else resp.get(103).decode("utf-8")
error_code = None if not resp.get(25016) else resp.get(25016).decode("utf-8")
text = None if not resp.get(58) else resp.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}")


# LOGOUT
client_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.

Documentation

For more information, have a look at the Binance documentation on Fix API.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

binance_fix_connector-1.0.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

binance_fix_connector-1.0.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file binance_fix_connector-1.0.0.tar.gz.

File metadata

  • Download URL: binance_fix_connector-1.0.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for binance_fix_connector-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f5043bce6ee952bdca1a577e5437602da7e43a685302961a68c55aeb374aa779
MD5 1663c96eb940424276e2bfc13330575e
BLAKE2b-256 30f3b6b84db61826683a05490d001396472c89929747d3363b533aa7f9bb29b2

See more details on using hashes here.

File details

Details for the file binance_fix_connector-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for binance_fix_connector-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39c075a32526d80f6504f68067bfc10a26595fc7098937f386fe029850e9c0d5
MD5 7f1666b0d15927ec1d351450f7bf3c1a
BLAKE2b-256 49fd84da4a71910137a7b6783b0a49b55c0a7bf463f0ef9d77c6fbbb7c16c052

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page