An unofficial Python wrapper for the Binance exchange REST API v3, also supporting websockets for real-time data streams. It provides both synchronous and asynchronous client implementations to interact with market data, account information, and trading functionalities. The library is actively maintained with frequent releases, currently at version 1.0.36.
pip install python-binanceVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize an `AsyncClient` with API keys from environment variables, fetch account balances, and retrieve a symbol's latest price. It uses `asyncio.run` to execute the asynchronous operations. Remember to replace placeholder API keys with your actual Binance API key and secret, ensuring they are stored securely as environment variables.
Update API calls to use the new 'sapi' endpoints (if directly calling internal methods) and refactor websocket stream handling to use `async with` for `BinanceSocketManager` or `DepthCacheManager`. Refer to the official documentation for updated examples.
Store API keys as environment variables, use a secrets management service, or load them from a secure configuration file. The quickstart example demonstrates using `os.environ.get()`.
Implement proper rate-limiting strategies in your application, such as delays between requests or using a token bucket algorithm. Check Binance's official API documentation for specific rate limits on different endpoints. The `X-MBX-USED-WEIGHT` headers can help monitor usage.
Before placing orders, retrieve exchange information using `client.get_exchange_info()` to determine the correct `stepSize` (for quantity) and `tickSize` (for price) for the specific symbol. Format your order parameters to adhere to these precision rules, typically by rounding down.
Implement robust reconnection logic for all websocket streams. For user data streams, ensure a 'keep-alive' mechanism is in place to periodically send a ping or renew the listen key before it expires.
Do not hardcode `3 * 365` or `1095` for annualization. Query Binance's exchange information or specific funding rate endpoints to determine the actual funding interval for each symbol. If an endpoint only returns non-default intervals, assume 8-hour for symbols not listed.
Synchronize your computer's system clock, preferably using an NTP server; if the issue persists, you can try increasing the `recvWindow` parameter in your client calls (e.g., `client.get_all_orders(symbol='BNBUSDT', recvWindow=60000)`).
Double-check your API key and secret for typos, ensure the API key on Binance has the required permissions (e.g., 'Enable Reading', 'Enable Spot & Margin Trading'), and verify if IP restrictions are enabled and correctly configured for your access IP.
Install the library using `pip install python-binance` and ensure your import statement is `from binance.client import Client` (or `from binance.async_client import AsyncClient` for the async version).
Consult the official `python-binance` documentation for the correct method names (e.g., use `client.create_order_test` for testing orders) and ensure your installed library version supports the method you are trying to call.