Skip to main content

Binance REST API python implementation

Project description

Note

I am working on a python3 version with async support to remove legacy dependencies and their related issues.

I would appreciate if you could try out the feature/asyncio branch and give your feedback.

https://img.shields.io/pypi/v/python-binance.svg https://img.shields.io/pypi/l/python-binance.svg https://img.shields.io/travis/sammchardy/python-binance.svg https://img.shields.io/coveralls/sammchardy/python-binance.svg https://img.shields.io/pypi/wheel/python-binance.svg https://img.shields.io/pypi/pyversions/python-binance.svg

This is an unofficial Python wrapper for the Binance exchange REST API v1/3. I am in no way affiliated with Binance, use at your own risk.

If you came here looking for the Binance exchange to purchase cryptocurrencies, then go here. If you want to automate interactions with Binance stick around.

If you’re interested in Binance’s new DEX Binance Chain see my python-binance-chain library

Source code

https://github.com/sammchardy/python-binance

Documentation

https://python-binance.readthedocs.io/en/latest/

Binance API Telegram

https://t.me/binance_api_english

Blog with examples

https://sammchardy.github.io

Make sure you update often and check the Changelog for new features and bug fixes.

Features

  • Implementation of all General, Market Data and Account endpoints.

  • Simple handling of authentication

  • 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

  • Support other domains (.us, .jp, etc)

Quick Start

Register an account with Binance.

Generate an API Key and assign relevant permissions.

pip install python-binance-am11yfork
from binance.client import Client
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, BinanceWithdrawException
try:
    result = client.withdraw(
        asset='ETH',
        address='<eth_address>',
        amount=100)
except BinanceAPIException as e:
    print(e)
except BinanceWithdrawException 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(asset='ETH')

# get a deposit address for BTC
address = client.get_deposit_address(asset='BTC')

# start aggregated trade websocket for BNBBTC
def process_message(msg):
    print("message type: {}".format(msg['e']))
    print(msg)
    # do something

from binance.websockets import BinanceSocketManager
bm = BinanceSocketManager(client)
bm.start_aggtrade_socket('BNBBTC', process_message)
bm.start()

# 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")

For more check out the documentation.

Other Exchanges

If you use Binance Chain check out my python-binance-chain library.

If you use Kucoin check out my python-kucoin library.

If you use IDEX check out my python-idex library.

https://analytics-pixel.appspot.com/UA-111417213-1/github/python-binance?pixel&useReferer

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

python-binance-am11yfork-0.7.9.tar.gz (66.9 kB view details)

Uploaded Source

Built Distribution

python_binance_am11yfork-0.7.9-py2.py3-none-any.whl (68.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file python-binance-am11yfork-0.7.9.tar.gz.

File metadata

  • Download URL: python-binance-am11yfork-0.7.9.tar.gz
  • Upload date:
  • Size: 66.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for python-binance-am11yfork-0.7.9.tar.gz
Algorithm Hash digest
SHA256 5d8df1f5895421166c4a10dbf8367dd9971c358ab328e1f4c555e843b03437ee
MD5 ea312266e006bcbeadd8654941c686cb
BLAKE2b-256 903f9a0438e39ba46cbcb0039e9d1cad32c590b6bdb04ea232dd68e2ad2d2072

See more details on using hashes here.

File details

Details for the file python_binance_am11yfork-0.7.9-py2.py3-none-any.whl.

File metadata

  • Download URL: python_binance_am11yfork-0.7.9-py2.py3-none-any.whl
  • Upload date:
  • Size: 68.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for python_binance_am11yfork-0.7.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c73be6b9cdaa498a51cadc947747fd57a92a51abc44364a82d5e55d5c3a9be7a
MD5 75f239749d467f07f2ecb8df446af404
BLAKE2b-256 4dcaa10047992a81351b92de3c48a3d99579305fe9e7035ba3e0e8480eb4897f

See more details on using hashes here.

Supported by

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