Binance异步python客户端
binance-aio的Python项目详细描述
二进制aio 0.0.3
公告:binance-aio
已被新库^{cryptoxlib-aio
提供了与binance-aio
相同的功能,但最重要的是它提供了对多个密码交换和其他(主要是技术)新功能的访问。您可以继续使用binance-aio
,但请注意,不会实现新的功能/错误修复。建议迁移到cryptoxlib-aio
。在
binance-aio
是一个Python库,提供对binance crypto exchange的访问。这个库实现了binance的restapi和websockets。在
binance-aio
被设计成一个异步库,它利用了Python的现代特性和支持异步库(主要是async websockets和{a6})。在
有关更改,请参见CHANGELOG。在
特点
- 访问有限的binance的restapi(帐户详细信息、市场数据、订单管理等)和websockets(account feed、market data feed、orderbook feed…)。丢失的REST调用和websocket流将根据请求和我们的可用性添加。在
- 捆绑在一个或多个并行处理的WebSocket中的通道
- 精益架构为未来的扩展和定制奠定了基础
- 以最佳性能为目标的全异步设计
安装
pip install binance-aio
先决条件
由于库使用的依赖关系和Python特性,请确保使用Python 3.6
或{
在开始使用binance-aio
之前,需要注意:
- 从Binance帐户下载Binance API和密钥
- 正在生成将用于保护SSL连接的证书。证书
certificate.pem
可以通过
示例
REST API
importasyncioimportpathlibimportloggingimportosfromdatetimeimportdatetimefrombinance.BinanceClientimportBinanceClientfrombinance.PairimportPairfrombinance.enumsimportOrderSide,TimeInForce,OrderResponseTypeLOG=logging.getLogger("binance")LOG.setLevel(logging.DEBUG)LOG.addHandler(logging.StreamHandler())print(f"Available loggers: {[namefornameinlogging.root.manager.loggerDict]}\n")asyncdefaccount_update(response:dict)->None:print(f"Callback {account_update.__name__}: [{response}]")asyncdeforder_book_update(response:dict)->None:print(f"Callback {order_book_update.__name__}: [{response}]")asyncdeftrade_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}")asyncdeforderbook_ticker_update(response:dict)->None:print(f"Callback {orderbook_ticker_update.__name__}: [{response}]")asyncdefrun():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 variablesapi_key=os.environ['APIKEY']sec_key=os.environ['SECKEY']client=BinanceClient(certificate_path,api_key,sec_key)# REST api callsprint("REST API")print("\nPing:")awaitclient.ping()print("\nServer time:")awaitclient.get_time()print("\nExchange info:")awaitclient.get_exchange_info()print("\nBest order book ticker:")awaitclient.get_best_orderbook_ticker(pair=Pair('ETH','BTC'))print("\nAccount:")awaitclient.get_account(recv_window_ms=5000)print("\nCreate limit order:")awaitclient.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:")awaitclient.delete_order(pair=Pair('ETH','BTC'),order_id="1")awaitclient.close()if__name__=="__main__":asyncio.run(run())
网络插座
importasyncioimportpathlibimportloggingimportosfromdatetimeimportdatetimefrombinance.BinanceClientimportBinanceClientfrombinance.PairimportPairfrombinance.subscriptionsimportBestOrderBookTickerSubscription,TradeSubscription,AccountSubscriptionLOG=logging.getLogger("binance")LOG.setLevel(logging.DEBUG)LOG.addHandler(logging.StreamHandler())print(f"Available loggers: {[namefornameinlogging.root.manager.loggerDict]}\n")asyncdefaccount_update(response:dict)->None:print(f"Callback {account_update.__name__}: [{response}]")asyncdeforder_book_update(response:dict)->None:print(f"Callback {order_book_update.__name__}: [{response}]")asyncdeftrade_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}")asyncdeforderbook_ticker_update(response:dict)->None:print(f"Callback {orderbook_ticker_update.__name__}: [{response}]")asyncdefrun():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 variablesapi_key=os.environ['APIKEY']sec_key=os.environ['SECKEY']client=BinanceClient(certificate_path,api_key,sec_key)# Websocketsprint("\nWEBSOCKETS\n")print("\nCreate listen key:")listen_key=awaitclient.get_listen_key()# Bundle several subscriptions into a single websocketclient.compose_subscriptions([BestOrderBookTickerSubscription(callbacks=[orderbook_ticker_update]),TradeSubscription(pair=Pair('ETH','BTC'),callbacks=[trade_update])])# Bundle another subscriptions into a separate websocketprint(listen_key)client.compose_subscriptions([AccountSubscription(client,callbacks=[account_update])])# Execute all websockets asynchronouslyawaitclient.start_subscriptions()awaitclient.close()if__name__=="__main__":asyncio.run(run())
所有示例都可以在GitHub存储库的client-example/client.py
中找到。在
支持
如果您喜欢这个库,并且希望支持它的进一步开发、增强和错误修复,那么它将是非常有帮助的,如果您:
- 文件错误,建议,拉请求。。。在
- 传播信息
- 任意捐小费
- BTC:15jugvq3yfopedj5wgqgvkzrx5hqokjc4
- 以太网:0xf2930b6af5831030ba99aceb290a3a129b993d
- ADA:DDZFFZCqrhHylv3WKTXFVConeter9MCFRMO9V3DYZ4PZ6YNQ9PJJUSFN4KZWKQR91PWECEBKOHDPAJZGBHDV2AKDSFR4SCKREK79
- XRP:rhVWrjB9EGDeK4zuJ1x2KXSjjSpsDQSaU6+标签599790141
触点
如果你想联系你,那就请
- 最好使用Github问题,或者
- 发送电子邮件至
隶属关系
如果您对自动交易机器人感兴趣,请查看我们的另一个项目creten。在
- 项目
标签: