=================================
Welcome to python-binance v1.0.22
=================================
.. image:: https://img.shields.io/pypi/v/python-binance.svg
:target: https://pypi.python.org/pypi/python-binance
.. image:: https://img.shields.io/pypi/l/python-binance.svg
:target: https://pypi.python.org/pypi/python-binance
.. image:: https://img.shields.io/coveralls/sammchardy/python-binance.svg
:target: https://coveralls.io/github/sammchardy/python-binance
.. image:: https://img.shields.io/pypi/wheel/python-binance.svg
:target: https://pypi.python.org/pypi/python-binance
.. image:: https://img.shields.io/pypi/pyversions/python-binance.svg
:target: https://pypi.python.org/pypi/python-binance
.. image:: https://img.shields.io/badge/Telegram-Join%20Us-blue?logo=Telegram
:target: https://t.me/python_binance
This is an unofficial Python wrapper for the `Binance exchange REST API v3 <https://binance-docs.github.io/apidocs/spot/en>`_.
If you came here looking for the `Binance exchange <https://accounts.binance.com/register?ref=PGDFCE46>`_ to purchase cryptocurrencies, then `go here <https://accounts.binance.com/register?ref=PGDFCE46>`_.
If you want to automate interactions with Binance stick around.
.. |ico1| image:: https://avatars.githubusercontent.com/u/31901609?s=48&v=4
:target: https://github.com/ccxt/ccxt
:height: 3ex
:align: middle
**This project is powered by** |ico1|
Source code
https://github.com/sammchardy/python-binance
Documentation
https://python-binance.readthedocs.io/en/latest/
Community Telegram Chat
https://t.me/python_binance
Announcements Channel
https://t.me/python_binance_announcements
Examples including async
https://github.com/sammchardy/python-binance/tree/master/examples
- `Async basics for Binance <https://sammchardy.github.io/binance/2021/05/01/async-binance-basics.html>`_
- `Understanding Binance Order Filters <https://sammchardy.github.io/binance/2021/05/03/binance-order-filters.html>`_
Make sure you update often and check the `Changelog <https://python-binance.readthedocs.io/en/latest/changelog.html>`_ for new features and bug fixes.
Your contributions, suggestions, and fixes are always welcome! Don't hesitate to open a GitHub issue or reach out to us on our Telegram chat
Features
--------
- Implementation of all General, Market Data and Account endpoints.
- Asyncio implementation
- Testnet support for Spot, Futures and Vanilla Options
- Simple handling of authentication include RSA and EDDSA keys
- No need to generate timestamps yourself, the wrapper does it for you
- Response exception handling
- Websocket handling with reconnection and multiplexed connections
- Symbol Depth Cache
- Historical Kline/Candle fetching function
- Withdraw functionality
- Deposit addresses
- Margin Trading
- Futures Trading
- Porfolio Margin Trading
- Vanilla Options
- Proxy support
- Support other domains (.us, .jp, etc)
Upgrading to v1.0.0+
--------------------
The breaking changes include the migration from wapi to sapi endpoints which related to the
wallet endpoints detailed in the `Binance Docs <https://binance-docs.github.io/apidocs/spot/en/#wallet-endpoints>`_
The other breaking change is for websocket streams and the Depth Cache Manager which have been
converted to use Asynchronous Context Managers. See examples in the Async section below or view the
`websockets <https://python-binance.readthedocs.io/en/latest/websockets.html>`_ and
`depth cache <https://python-binance.readthedocs.io/en/latest/depth_cache.html>`_ docs.
Quick Start
-----------
`Register an account with Binance <https://accounts.binance.com/register?ref=PGDFCE46>`_.
`Generate an API Key <https://www.binance.com/en/my/settings/api-management>`_ and assign relevant permissions.
If you are using an exchange from the US, Japan or other TLD then make sure pass `tld='us'` when creating the
client.
To use the `Spot <https://testnet.binance.vision/>`_, `Vanilla Options <https://testnet.binanceops.com/>`_ , or `Futures <https://testnet.binancefuture.com>`_ Testnet
pass `testnet=True` when creating the client.
.. code:: bash
pip install python-binance
.. code:: python
from binance import Client, ThreadedWebsocketManager, ThreadedDepthCacheManager
client = Client(api_key, api_secret)
# get market depth
depth = client.get_order_book(symbol='BNBBTC')
# place a test market buy order, to place an actual order use the create_order function
order = client.create_test_order(
symbol='BNBBTC',
side=Client.SIDE_BUY,
type=Client.ORDER_TYPE_MARKET,
quantity=100)
# get all symbol prices
prices = client.get_all_tickers()
# withdraw 100 ETH
# check docs for assumptions around withdrawals
from binance.exceptions import BinanceAPIException
try:
result = client.withdraw(
asset='ETH',
address='<eth_address>',
amount=100)
except BinanceAPIException as e:
print(e)
else:
print("Success")
# fetch list of withdrawals
withdraws = client.get_withdraw_history()
# fetch list of ETH withdrawals
eth_withdraws = client.get_withdraw_history(coin='ETH')
# get a deposit address for BTC
address = client.get_deposit_address(coin='BTC')
# get historical kline data from any date range
# fetch 1 minute klines for the last day up until now
klines = client.get_historical_klines("BNBBTC", Client.KLINE_INTERVAL_1MINUTE, "1 day ago UTC")
# fetch 30 minute klines for the last month of 2017
klines = client.get_historical_klines("ETHBTC", Client.KLINE_INTERVAL_30MINUTE, "1 Dec, 2017", "1 Jan, 2018")
# fetch weekly klines since it listed
klines = client.get_historical_klines("NEOBTC", Client.KLINE_INTERVAL_1WEEK, "1 Jan, 2017")
# socket manager using threads
twm = ThreadedWebsocketManager()
twm.start()
# depth cache manager using threads
dcm = ThreadedDepthCacheManager()
dcm.start()
def handle_socket_message(msg):
print(f"message type: {msg['e']}")
print(msg)
def handle_dcm_message(depth_cache):
print(f"symbol {depth_cache.symbol}")
print("top 5 bids")
print(depth_cache.get_bids()[:5])
print("top 5 asks")
print(depth_cache.get_asks()[:5])
print("last update time {}".format(depth_cache.update_time))
twm.start_kline_socket(callback=handle_socket_message, symbol='BNBBTC')
dcm.start_depth_cache(callback=handle_dcm_message, symbol='ETHBTC')
# replace with a current options symbol
options_symbol = 'BTC-241227-41000-C'
dcm.start_options_depth_cache(callback=handle_dcm_message, symbol=options_symbol)
# join the threaded managers to the main thread
twm.join()
dcm.join()
For more `check out the documentation <https://python-binance.readthedocs.io/en/latest/>`_.
Async Example
-------------
Read `Async basics for Binance <https://sammchardy.github.io/binance/2021/05/01/async-binance-basics.html>`_
for more information.
.. code:: python
import asyncio
import json
from binance import AsyncClient, DepthCacheManager, BinanceSocketManager
async def main():
# initialise the client
client = await AsyncClient.create()
# run some simple requests
print(json.dumps(await client.get_exchange_info(), indent=2))
print(json.dumps(await client.get_symbol_ticker(symbol="BTCUSDT"), indent=2))
# initialise websocket factory manager
bsm = BinanceSocketManager(client)
# create listener using async with
# this will exit and close the connection after 5 messages
async with bsm.trade_socket('ETHBTC') as ts:
for _ in range(5):
res = await ts.recv()
print(f'recv {res}')
# get historical kline data from any date range
# fetch 1 minute klines for the last day up until now
klines = client.get_historical_klines("BNBBTC", AsyncClient.KLINE_INTERVAL_1MINUTE, "1 day ago UTC")
# use generator to fetch 1 minute klines for the last day up until now
async for kline in await client.get_historical_klines_generator("BNBBTC", AsyncClient.KLINE_INTERVAL_1MINUTE, "1 day ago UTC"):
print(kline)
# fetch 30 minute klines for the last month of 2017
klines = client.get_historical_klines("ETHBTC", Client.KLINE_INTERVAL_30MINUTE, "1 Dec, 2017", "1 Jan, 2018")
# fetch weekly klines since it listed
klines = client.get_historical_klines("NEOBTC", Client.KLINE_INTERVAL_1WEEK, "1 Jan, 2017")
# setup an async context the Depth Cache and exit after 5 messages
async with DepthCacheManager(client, symbol='ETHBTC') as dcm_socket:
for _ in range(5):
depth_cache = await dcm_socket.recv()
print(f"symbol {depth_cache.symbol} updated:{depth_cache.update_time}")
print("Top 5 asks:")
print(depth_cache.get_asks()[:5])
print("Top 5 bids:")
print(depth_cache.get_bids()[:5])
# Vanilla options Depth Cache works the same, update the symbol to a current one
options_symbol = 'BTC-241227-41000-C'
async with OptionsDepthCacheManager(client, symbol=options_symbol) as dcm_socket:
for _ in range(5):
depth_cache = await dcm_socket.recv()
count += 1
print(f"symbol {depth_cache.symbol} updated:{depth_cache.update_time}")
print("Top 5 asks:")
print(depth_cache.get_asks()[:5])
print("Top 5 bids:")
print(depth_cache.get_bids()[:5])
await client.close_connection()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
The library is under `MIT license`, that means it's absolutely free for any developer to build commercial and opensource software on top of it, but use it at your own risk with no warranties, as is.
Star history
------------
.. image:: https://api.star-history.com/svg?repos=sammchardy/python-binance&type=Date
:target: https://api.star-history.com/svg?repos=sammchardy/python-binance&type=Date
Contact Us
----------
For business inquiries: `info@ccxt.trade`
Other Exchanges
---------------
- Check out `CCXT <https://github.com/ccxt/ccxt>`_ for more than 100 crypto exchanges with a unified trading API.
- If you use `Kucoin <https://www.kucoin.com/ucenter/signup?rcode=E5wkqe>`_ check out my `python-kucoin <https://github.com/sammchardy/python-kucoin>`_ library.
Raw data
{
"_id": null,
"home_page": "https://github.com/sammchardy/python-binance",
"name": "python-binance",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "binance exchange rest api bitcoin ethereum btc eth neo",
"author": "Sam McHardy",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/c8/13/1f77c69015190a08e6b2f42e01b99c121fa32b808a45d6ac46a56ee5811d/python-binance-1.0.22.tar.gz",
"platform": null,
"description": "=================================\nWelcome to python-binance v1.0.22\n=================================\n\n.. image:: https://img.shields.io/pypi/v/python-binance.svg\n :target: https://pypi.python.org/pypi/python-binance\n\n.. image:: https://img.shields.io/pypi/l/python-binance.svg\n :target: https://pypi.python.org/pypi/python-binance\n\n.. image:: https://img.shields.io/coveralls/sammchardy/python-binance.svg\n :target: https://coveralls.io/github/sammchardy/python-binance\n\n.. image:: https://img.shields.io/pypi/wheel/python-binance.svg\n :target: https://pypi.python.org/pypi/python-binance\n\n.. image:: https://img.shields.io/pypi/pyversions/python-binance.svg\n :target: https://pypi.python.org/pypi/python-binance\n\n.. image:: https://img.shields.io/badge/Telegram-Join%20Us-blue?logo=Telegram\n :target: https://t.me/python_binance\n\n\nThis is an unofficial Python wrapper for the `Binance exchange REST API v3 <https://binance-docs.github.io/apidocs/spot/en>`_.\n\nIf you came here looking for the `Binance exchange <https://accounts.binance.com/register?ref=PGDFCE46>`_ to purchase cryptocurrencies, then `go here <https://accounts.binance.com/register?ref=PGDFCE46>`_.\nIf you want to automate interactions with Binance stick around.\n\n.. |ico1| image:: https://avatars.githubusercontent.com/u/31901609?s=48&v=4\n :target: https://github.com/ccxt/ccxt\n :height: 3ex\n :align: middle\n\n**This project is powered by** |ico1|\n\nSource code\n https://github.com/sammchardy/python-binance\n\nDocumentation\n https://python-binance.readthedocs.io/en/latest/\n\nCommunity Telegram Chat\n https://t.me/python_binance\n\nAnnouncements Channel\n https://t.me/python_binance_announcements\n\nExamples including async\n https://github.com/sammchardy/python-binance/tree/master/examples\n\n- `Async basics for Binance <https://sammchardy.github.io/binance/2021/05/01/async-binance-basics.html>`_\n- `Understanding Binance Order Filters <https://sammchardy.github.io/binance/2021/05/03/binance-order-filters.html>`_\n\nMake sure you update often and check the `Changelog <https://python-binance.readthedocs.io/en/latest/changelog.html>`_ for new features and bug fixes.\n\nYour contributions, suggestions, and fixes are always welcome! Don't hesitate to open a GitHub issue or reach out to us on our Telegram chat\n\nFeatures\n--------\n\n- Implementation of all General, Market Data and Account endpoints.\n- Asyncio implementation\n- Testnet support for Spot, Futures and Vanilla Options\n- Simple handling of authentication include RSA and EDDSA keys\n- No need to generate timestamps yourself, the wrapper does it for you\n- Response exception handling\n- Websocket handling with reconnection and multiplexed connections\n- Symbol Depth Cache\n- Historical Kline/Candle fetching function\n- Withdraw functionality\n- Deposit addresses\n- Margin Trading\n- Futures Trading\n- Porfolio Margin Trading\n- Vanilla Options\n- Proxy support\n- Support other domains (.us, .jp, etc)\n\nUpgrading to v1.0.0+\n--------------------\n\nThe breaking changes include the migration from wapi to sapi endpoints which related to the\nwallet endpoints detailed in the `Binance Docs <https://binance-docs.github.io/apidocs/spot/en/#wallet-endpoints>`_\n\nThe other breaking change is for websocket streams and the Depth Cache Manager which have been\nconverted to use Asynchronous Context Managers. See examples in the Async section below or view the\n`websockets <https://python-binance.readthedocs.io/en/latest/websockets.html>`_ and\n`depth cache <https://python-binance.readthedocs.io/en/latest/depth_cache.html>`_ docs.\n\nQuick Start\n-----------\n\n`Register an account with Binance <https://accounts.binance.com/register?ref=PGDFCE46>`_.\n\n`Generate an API Key <https://www.binance.com/en/my/settings/api-management>`_ and assign relevant permissions.\n\nIf you are using an exchange from the US, Japan or other TLD then make sure pass `tld='us'` when creating the\nclient.\n\nTo use the `Spot <https://testnet.binance.vision/>`_, `Vanilla Options <https://testnet.binanceops.com/>`_ , or `Futures <https://testnet.binancefuture.com>`_ Testnet\npass `testnet=True` when creating the client.\n\n\n.. code:: bash\n\n pip install python-binance\n\n\n.. code:: python\n\n from binance import Client, ThreadedWebsocketManager, ThreadedDepthCacheManager\n client = Client(api_key, api_secret)\n\n # get market depth\n depth = client.get_order_book(symbol='BNBBTC')\n\n # place a test market buy order, to place an actual order use the create_order function\n order = client.create_test_order(\n symbol='BNBBTC',\n side=Client.SIDE_BUY,\n type=Client.ORDER_TYPE_MARKET,\n quantity=100)\n\n # get all symbol prices\n prices = client.get_all_tickers()\n\n # withdraw 100 ETH\n # check docs for assumptions around withdrawals\n from binance.exceptions import BinanceAPIException\n try:\n result = client.withdraw(\n asset='ETH',\n address='<eth_address>',\n amount=100)\n except BinanceAPIException as e:\n print(e)\n else:\n print(\"Success\")\n\n # fetch list of withdrawals\n withdraws = client.get_withdraw_history()\n\n # fetch list of ETH withdrawals\n eth_withdraws = client.get_withdraw_history(coin='ETH')\n\n # get a deposit address for BTC\n address = client.get_deposit_address(coin='BTC')\n\n # get historical kline data from any date range\n\n # fetch 1 minute klines for the last day up until now\n klines = client.get_historical_klines(\"BNBBTC\", Client.KLINE_INTERVAL_1MINUTE, \"1 day ago UTC\")\n\n # fetch 30 minute klines for the last month of 2017\n klines = client.get_historical_klines(\"ETHBTC\", Client.KLINE_INTERVAL_30MINUTE, \"1 Dec, 2017\", \"1 Jan, 2018\")\n\n # fetch weekly klines since it listed\n klines = client.get_historical_klines(\"NEOBTC\", Client.KLINE_INTERVAL_1WEEK, \"1 Jan, 2017\")\n\n # socket manager using threads\n twm = ThreadedWebsocketManager()\n twm.start()\n\n # depth cache manager using threads\n dcm = ThreadedDepthCacheManager()\n dcm.start()\n\n def handle_socket_message(msg):\n print(f\"message type: {msg['e']}\")\n print(msg)\n\n def handle_dcm_message(depth_cache):\n print(f\"symbol {depth_cache.symbol}\")\n print(\"top 5 bids\")\n print(depth_cache.get_bids()[:5])\n print(\"top 5 asks\")\n print(depth_cache.get_asks()[:5])\n print(\"last update time {}\".format(depth_cache.update_time))\n\n twm.start_kline_socket(callback=handle_socket_message, symbol='BNBBTC')\n\n dcm.start_depth_cache(callback=handle_dcm_message, symbol='ETHBTC')\n\n # replace with a current options symbol\n options_symbol = 'BTC-241227-41000-C'\n dcm.start_options_depth_cache(callback=handle_dcm_message, symbol=options_symbol)\n\n # join the threaded managers to the main thread\n twm.join()\n dcm.join()\n\nFor more `check out the documentation <https://python-binance.readthedocs.io/en/latest/>`_.\n\nAsync Example\n-------------\n\nRead `Async basics for Binance <https://sammchardy.github.io/binance/2021/05/01/async-binance-basics.html>`_\nfor more information.\n\n.. code:: python\n\n import asyncio\n import json\n\n from binance import AsyncClient, DepthCacheManager, BinanceSocketManager\n\n async def main():\n\n # initialise the client\n client = await AsyncClient.create()\n\n # run some simple requests\n print(json.dumps(await client.get_exchange_info(), indent=2))\n\n print(json.dumps(await client.get_symbol_ticker(symbol=\"BTCUSDT\"), indent=2))\n\n # initialise websocket factory manager\n bsm = BinanceSocketManager(client)\n\n # create listener using async with\n # this will exit and close the connection after 5 messages\n async with bsm.trade_socket('ETHBTC') as ts:\n for _ in range(5):\n res = await ts.recv()\n print(f'recv {res}')\n\n # get historical kline data from any date range\n\n # fetch 1 minute klines for the last day up until now\n klines = client.get_historical_klines(\"BNBBTC\", AsyncClient.KLINE_INTERVAL_1MINUTE, \"1 day ago UTC\")\n\n # use generator to fetch 1 minute klines for the last day up until now\n async for kline in await client.get_historical_klines_generator(\"BNBBTC\", AsyncClient.KLINE_INTERVAL_1MINUTE, \"1 day ago UTC\"):\n print(kline)\n\n # fetch 30 minute klines for the last month of 2017\n klines = client.get_historical_klines(\"ETHBTC\", Client.KLINE_INTERVAL_30MINUTE, \"1 Dec, 2017\", \"1 Jan, 2018\")\n\n # fetch weekly klines since it listed\n klines = client.get_historical_klines(\"NEOBTC\", Client.KLINE_INTERVAL_1WEEK, \"1 Jan, 2017\")\n\n # setup an async context the Depth Cache and exit after 5 messages\n async with DepthCacheManager(client, symbol='ETHBTC') as dcm_socket:\n for _ in range(5):\n depth_cache = await dcm_socket.recv()\n print(f\"symbol {depth_cache.symbol} updated:{depth_cache.update_time}\")\n print(\"Top 5 asks:\")\n print(depth_cache.get_asks()[:5])\n print(\"Top 5 bids:\")\n print(depth_cache.get_bids()[:5])\n\n # Vanilla options Depth Cache works the same, update the symbol to a current one\n options_symbol = 'BTC-241227-41000-C'\n async with OptionsDepthCacheManager(client, symbol=options_symbol) as dcm_socket:\n for _ in range(5):\n depth_cache = await dcm_socket.recv()\n count += 1\n print(f\"symbol {depth_cache.symbol} updated:{depth_cache.update_time}\")\n print(\"Top 5 asks:\")\n print(depth_cache.get_asks()[:5])\n print(\"Top 5 bids:\")\n print(depth_cache.get_bids()[:5])\n\n await client.close_connection()\n\n if __name__ == \"__main__\":\n\n loop = asyncio.get_event_loop()\n loop.run_until_complete(main())\n\n\nThe library is under `MIT license`, that means it's absolutely free for any developer to build commercial and opensource software on top of it, but use it at your own risk with no warranties, as is.\n\n\nStar history\n------------\n\n.. image:: https://api.star-history.com/svg?repos=sammchardy/python-binance&type=Date\n :target: https://api.star-history.com/svg?repos=sammchardy/python-binance&type=Date\n\nContact Us\n----------\n\nFor business inquiries: `info@ccxt.trade`\n\nOther Exchanges\n---------------\n- Check out `CCXT <https://github.com/ccxt/ccxt>`_ for more than 100 crypto exchanges with a unified trading API.\n- If you use `Kucoin <https://www.kucoin.com/ucenter/signup?rcode=E5wkqe>`_ check out my `python-kucoin <https://github.com/sammchardy/python-kucoin>`_ library.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Binance REST API python implementation",
"version": "1.0.22",
"project_urls": {
"Homepage": "https://github.com/sammchardy/python-binance"
},
"split_keywords": [
"binance",
"exchange",
"rest",
"api",
"bitcoin",
"ethereum",
"btc",
"eth",
"neo"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "dba44c96a51a8ef4ec6fba16c43688076ec0aec7b296ef2d7b89e109f70d468d",
"md5": "3eb9861dc0e15e9a66a14dabbe379db7",
"sha256": "5a179a8827c231556bb80de502857b3c83c19e158a9f1e2841d081b7b3549957"
},
"downloads": -1,
"filename": "python_binance-1.0.22-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "3eb9861dc0e15e9a66a14dabbe379db7",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 81445,
"upload_time": "2024-10-29T16:38:27",
"upload_time_iso_8601": "2024-10-29T16:38:27.978462Z",
"url": "https://files.pythonhosted.org/packages/db/a4/4c96a51a8ef4ec6fba16c43688076ec0aec7b296ef2d7b89e109f70d468d/python_binance-1.0.22-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c8131f77c69015190a08e6b2f42e01b99c121fa32b808a45d6ac46a56ee5811d",
"md5": "215960dfc79c81d1fb2b213600c38fb0",
"sha256": "33c0dead72363d1e7c7982d5c568f93abbb942d6e76712a1084806b5481df422"
},
"downloads": -1,
"filename": "python-binance-1.0.22.tar.gz",
"has_sig": false,
"md5_digest": "215960dfc79c81d1fb2b213600c38fb0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 89434,
"upload_time": "2024-10-29T16:38:29",
"upload_time_iso_8601": "2024-10-29T16:38:29.781756Z",
"url": "https://files.pythonhosted.org/packages/c8/13/1f77c69015190a08e6b2f42e01b99c121fa32b808a45d6ac46a56ee5811d/python-binance-1.0.22.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-29 16:38:29",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "sammchardy",
"github_project": "python-binance",
"travis_ci": true,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "aiohttp",
"specs": []
},
{
"name": "dateparser",
"specs": []
},
{
"name": "pycryptodome",
"specs": []
},
{
"name": "requests",
"specs": []
},
{
"name": "websockets",
"specs": []
}
],
"tox": true,
"lcname": "python-binance"
}