python binance

2025-12-11 0 544

欢迎来到Python-Binance v1.0.29

这是Binance Exchange REST API V3的非正式Python包装纸。

如果您来这里寻找购买加密货币的二元交易所,那就去这里。如果您想自动化与Binance Stick的互动。

这个项目由

确保您Python-Binance版本V.1.0.20或更高。不再推荐以前的版本,因为某些端点已被弃用。

源代码
https://g*ithub*.*com/sammchardy/pythonbinance
文档
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 = \'<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库。

下载源码

通过命令行克隆项目:

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 python binance https://www.zuozi.net/34403.html

whitelist
上一篇: whitelist
persepolis
下一篇: persepolis
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务