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之前,需要注意:

  1. 从Binance帐户下载Binance API和密钥
  2. 正在生成将用于保护SSL连接的证书。证书certificate.pem可以通过
^{pr2}$

示例

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。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
无法在Netbeans 8.2 JDK8u231中创建java Maven项目(Web应用程序)   java如何以设定的时间间隔生成随机数?   java从socket和inputStream的慢速读取   spring SCORM:Java中基于Web的SCORM播放器   Java将函数传递给方法   java绑定通用服务及其实现和子类型   java如何在运行时从选择列表框中动态选择选项?爪哇硒   java Selenium WebDriver什么是“Selenium客户端和WebDriver语言绑定”   elasticsearch需要elasticsearch高级Java客户端更新ByQueryRequest API帮助   JAVA哈希表查找最大值   WSDL操作中的java soapAction属性为空   java访问封闭类或父类方法的一般方法   eclipse在java中运行带有SeleneTestCase的ANT。lang.NoClassDefFoundError   java Hazelcast不会在节点启动时填充ReplicatedMap   如何在Java中从excel中读取特定行?   html JAVA将本地时间(GMT+8)转换为UTC时间   java将自定义端点添加到Spring数据REST存储库中,并以大摇大摆的方式显示   java计算未来位置