欢迎来到Python-Binance v1.0.29
这是Binance Exchange REST API V3的非正式Python包装纸。
如果您来这里寻找购买加密货币的二元交易所,那就去这里。如果您想自动化与Binance Stick的互动。
这个项目由
请确保您的Python-Binance版本为V.1.0.20或更高。不再推荐以前的版本,因为某些端点已被弃用。
- 源代码
- https://g*ithub*.*com/sammchardy/python-binance
- 文档
- https://python-binance.r*e*ad*thedocs.io/en/latest/
- 社区电报聊天
- https://t.me*/py*t*hon_binance
- 公告频道
- https://t.me*/py*t*hon_binance_annuncements
- 示例包括异步
- https://g*ithub*.*com/sammchardy/python-binance/tree/master/examples
- 异步基础知识
- 了解二元订单过滤器
确保您经常更新,并检查ChangElog的新功能和错误修复。
您总是欢迎您的贡献,建议和修复!不要犹豫,打开GitHub问题或在我们的电报聊天中与我们联系
特征
- 实施所有一般,市场数据和帐户端点。
- 异步实现
- 测试网支持现场,期货和香草选项
- 简单处理身份验证包括RSA和EDDSA键
- 无需自己生成时间戳,包装器为您做
- 默认发送的recvWindow
- 响应例外处理
- 可自定义的HTTP标头
- Websocket通过重新连接和多路复用连接处理
- 在Websockets上进行CRUD,通过Websockets创建/获取/编辑,以最小延迟。
- 符号深度缓存
- 历史Kline/Candle提取功能
- 撤回功能
- 存款地址
- 保证金交易
- 期货交易
- Porfolio保证金交易
- 香草选项
- 代理支持(休息和WS)
- Orjson支持更快的JSON解析
- 支持其他域(.us,.jp等)
- 支持礼品卡API
升级到v1.0.0+
破裂的变化包括从WAPI到SAPI端点的迁移,这些端点与Binance Doc中详细介绍的钱包端点有关
另一个打破变化是用于Websocket流和已转换为使用异步上下文经理的Depth Cache Manager。请参阅下面的“异步”部分中的示例,或查看Websockets和Depth缓存文档。
快速开始
用binance注册帐户。
生成一个API密钥并分配相关权限。
如果您使用的是来自美国,日本或其他TLD的交易所,则在创建客户端时确保通过tld ='us'。
使用现场,香草选项或Futures TestNet Pass testnet =创建客户端时。
pip install python-binance 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 = '' ,
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" )
# create order through websockets
order_ws = client . ws_create_order ( symbol = "LTCUSDT" , side = "BUY" , type = "MARKET" , quantity = 0.1 )
# get account using custom headers
account = client . get_account ( headers = { 'MyCustomKey' : 'MyCustomValue' })
# 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 ()有关更多信息,请查看文档。
异步示例
阅读异步基础知识以获取更多信息。
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 = await client . get_historical_klines ( "ETHBTC" , Client . KLINE_INTERVAL_30MINUTE , "1 Dec, 2017" , "1 Jan, 2018" )
# fetch weekly klines since it listed
klines = await client . get_historical_klines ( "NEOBTC" , Client . KLINE_INTERVAL_1WEEK , "1 Jan, 2017" )
# create order through websockets
order_ws = await client . ws_create_order ( symbol = "LTCUSDT" , side = "BUY" , type = "MARKET" , quantity = 0.1 )
# 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 ())该图书馆符合MIT许可证,这意味着任何开发人员都可以在其上构建商业和OpenSource软件绝对免费,但是在没有保证的情况下以您自己的风险使用它。
Orjson支持
Python-Binance还支持Orjson解析JSON,因为它比内置图书馆快得多。使用Websocket时,这一点尤其重要,因为某些交换返回需要尽快解析和派遣的大消息。
但是,默认情况下未启用Orjson,因为每个Python解释器都不支持它。如果您想选择加入,则只需要在本地环境上安装它(PIP安装ORJSON)即可。 Python-Binance将检测安装并自动拾取。
星历史
联系我们
进行业务查询:info@ccxt.trade
其他交流
- 查看CCXT,并通过统一交易API进行100多个加密货币交易所。
- 如果您使用Kucoin,请查看我的Python-Kucoin库。
通过命令行克隆项目: