Skip to main content

Binance asynchronous python client

Project description

binance-aio 0.0.3

Announcement:binance-aio has been replaced by a new library cryptoxlib-aio. cryptoxlib-aio offers the very same functionality as binance-aio but on top it provides access to multiple cryptoexchanges and other (mostly technical) new features. You can keep using binance-aio but please note no new features/bugfixes will be implemented. We recommend to migrate to cryptoxlib-aio.


binance-aio is a Python library providing access to binance crypto exchange. Library implements binance's REST API as well as websockets.

binance-aio is designed as an asynchronous library utilizing modern features of Python and of supporting asynchronous libraries (mainly async websockets and aiohttp).

For changes see CHANGELOG.

Features

  • access to limited binance's REST API (account details, market data, order management, ...) and websockets (account feed, market data feed, orderbook feed, ...). Missing REST calls and websocket streams will be added on request and based on our availability.
  • channels bundled in one or multiple websockets processed in parallel
  • lean architecture setting ground for the future extensions and customizations
  • fully asynchronous design aiming for the best performance

Installation

pip install binance-aio

Prerequisites

Due to dependencies and Python features used by the library please make sure you use Python 3.6 or 3.7.

Before starting using binance-aio, it is necessary to take care of:

  1. downloading your Binance API and SECRET key from your binance account
  2. generating your certificate that will be used to secure SSL connections. Certificate certificate.pem can be generated easily by
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out certificate.pem

Examples

REST API

import asyncio
import pathlib
import logging
import os
from datetime import datetime

from binance.BinanceClient import BinanceClient
from binance.Pair import Pair
from binance.enums import OrderSide, TimeInForce, OrderResponseType

LOG = logging.getLogger("binance")
LOG.setLevel(logging.DEBUG)
LOG.addHandler(logging.StreamHandler())

print(f"Available loggers: {[name for name in logging.root.manager.loggerDict]}\n")

async def account_update(response : dict) -> None:
	print(f"Callback {account_update.__name__}: [{response}]")

async def order_book_update(response : dict) -> None:
	print(f"Callback {order_book_update.__name__}: [{response}]")

async def trade_update(response : dict) -> None:
	local_timestamp_ms = int(datetime.now().timestamp() * 1000)
	server_timestamp_ms = response['E']
	print(f"Trade update timestamp diff [ms]: {local_timestamp_ms - server_timestamp_ms}")

async def orderbook_ticker_update(response : dict) -> None:
	print(f"Callback {orderbook_ticker_update.__name__}: [{response}]")

async def run():
	print("STARTING BINANCE CLIENT\n")

	# to generate a certificate use 'openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out certificate.pem'
	certificate_path = pathlib.Path(__file__).with_name("certificate.pem")

	# to retrieve your API/SEC key go to your binance website, create the keys and store them in APIKEY/SECKEY
	# environment variables
	api_key = os.environ['APIKEY']
	sec_key = os.environ['SECKEY']

	client = BinanceClient(certificate_path, api_key, sec_key)

	# REST api calls
	print("REST API")

	print("\nPing:")
	await client.ping()

	print("\nServer time:")
	await client.get_time()

	print("\nExchange info:")
	await client.get_exchange_info()

	print("\nBest order book ticker:")
	await client.get_best_orderbook_ticker(pair = Pair('ETH', 'BTC'))

	print("\nAccount:")
	await client.get_account(recv_window_ms = 5000)

	print("\nCreate limit order:")
	await client.create_limit_order(Pair("ETH", "BTC"), OrderSide.BUY, "1", "0", time_in_force = TimeInForce.GOOD_TILL_CANCELLED,
	                                new_order_response_type = OrderResponseType.FULL)

	print("\nDelete order:")
	await client.delete_order(pair = Pair('ETH', 'BTC'), order_id = "1")

	await client.close()

if __name__ == "__main__":
	asyncio.run(run())

WEBSOCKETS

import asyncio
import pathlib
import logging
import os
from datetime import datetime

from binance.BinanceClient import BinanceClient
from binance.Pair import Pair
from binance.subscriptions import BestOrderBookTickerSubscription, TradeSubscription, AccountSubscription

LOG = logging.getLogger("binance")
LOG.setLevel(logging.DEBUG)
LOG.addHandler(logging.StreamHandler())

print(f"Available loggers: {[name for name in logging.root.manager.loggerDict]}\n")

async def account_update(response : dict) -> None:
	print(f"Callback {account_update.__name__}: [{response}]")

async def order_book_update(response : dict) -> None:
	print(f"Callback {order_book_update.__name__}: [{response}]")

async def trade_update(response : dict) -> None:
	local_timestamp_ms = int(datetime.now().timestamp() * 1000)
	server_timestamp_ms = response['E']
	print(f"Trade update timestamp diff [ms]: {local_timestamp_ms - server_timestamp_ms}")

async def orderbook_ticker_update(response : dict) -> None:
	print(f"Callback {orderbook_ticker_update.__name__}: [{response}]")

async def run():
	print("STARTING BINANCE CLIENT\n")

	# to generate a certificate use 'openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out certificate.pem'
	certificate_path = pathlib.Path(__file__).with_name("certificate.pem")

	# to retrieve your API/SEC key go to your binance website, create the keys and store them in API_KEY/SEC_KEY
	# environment variables
	api_key = os.environ['APIKEY']
	sec_key = os.environ['SECKEY']

	client = BinanceClient(certificate_path, api_key, sec_key)

	# Websockets
	print("\nWEBSOCKETS\n")

	print("\nCreate listen key:")
	listen_key = await client.get_listen_key()

	# Bundle several subscriptions into a single websocket
	client.compose_subscriptions([
		BestOrderBookTickerSubscription(callbacks = [orderbook_ticker_update]),
		TradeSubscription(pair = Pair('ETH', 'BTC'), callbacks = [trade_update])
	])

	# Bundle another subscriptions into a separate websocket
	print(listen_key)
	client.compose_subscriptions([
		AccountSubscription(client, callbacks = [account_update])
	])

	# Execute all websockets asynchronously
	await client.start_subscriptions()

	await client.close()

if __name__ == "__main__":
	asyncio.run(run())

All examples can be found in client-example/client.py in the GitHub repository.

Support

If you like the library and you feel like you want to support its further development, enhancements and bugfixing, then it will be of great help and most appreciated if you:

  • file bugs, proposals, pull requests, ...
  • spread the word
  • donate an arbitrary tip
    • BTC: 15JUgVq3YFoPedEj5wgQgvkZRx5HQoKJC4
    • ETH: 0xf29304b6af5831030ba99aceb290a3a2129b993d
    • ADA: DdzFFzCqrhshyLV3wktXFvConETEr9mCfrMo9V3dYz4pz6yNq9PjJusfnn4kzWKQR91pWecEbKoHodPaJzgBHdV2AKeDSfR4sckrEk79
    • XRP: rhVWrjB9EGDeK4zuJ1x2KXSjjSpsDQSaU6 + tag 599790141

Contact

If you feel you want to get in touch, then please

  • preferably use Github Issues, or
  • send an e-mail to

Affiliation

In case you are interested in an automated trading bot, check out our other project creten.

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-aio-0.0.3.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

binance_aio-0.0.3-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file binance-aio-0.0.3.tar.gz.

File metadata

  • Download URL: binance-aio-0.0.3.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for binance-aio-0.0.3.tar.gz
Algorithm Hash digest
SHA256 81592564cbaf538b8e464e8ce7043e90d38b27cd4dbc10d6a97bc5cc3c0610a3
MD5 2cb9b046ef30de95f8a020060e06a562
BLAKE2b-256 e1b2557c3885ea15df1298cad57f5e9a802fa66603dbd564cc4b5cd96e2cf994

See more details on using hashes here.

File details

Details for the file binance_aio-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: binance_aio-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for binance_aio-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f266a6737b51b32037fc1d09c0b50b879b5f9dcd5febd8b141bb49a94bc22c1d
MD5 984a8f806ac11f33b2686a9416de935b
BLAKE2b-256 4f5f30e74cc1fdc2f3a401b63c76be2c8932b9e8db9162a3c773cf35008fcdbd

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