Binance_interface API U本位合约行情

  • Github地址
  • PyTed量化交易研究院

1. API U本位合约行情接口总览

方法解释Path
get_ping测试服务器连通性 PING/fapi/v1/ping
get_time获取服务器时间/fapi/v1/time
get_exchangeInfo获取交易规则和交易对/fapi/v1/exchangeInfo
get_depth深度信息/fapi/v1/depth
get_trades近期成交/fapi/v1/trades
get_historicalTrades查询历史成交(MARKET_DATA)/fapi/v1/historicalTrades
get_aggTrades近期成交(归集)/fapi/v1/aggTrades
get_klinesK线数据/fapi/v1/klines
get_continuousKlines连续合约K线数据/fapi/v1/continuousKlines
get_indexPriceKlines价格指数K线数据/fapi/v1/indexPriceKlines
get_markPriceKlines标记价格K线数据/fapi/v1/markPriceKlines
get_premiumIndexKlines溢价指数K线数据/fapi/v1/premiumIndexKlines
get_premiumIndex最新标记价格和资金费率/fapi/v1/premiumIndex
get_fundingRate查询资金费率历史/fapi/v1/fundingRate
get_fundingInfo查询资金费率信息/fapi/v1/fundingInfo
get_ticker_24hr24hr价格变动情况/fapi/v1/ticker/24hr
get_ticker_price_v1最新价格/fapi/v1/ticker/price
get_ticker_price最新价格V2/fapi/v2/ticker/price
get_ticker_bookTicker当前最优挂单/fapi/v1/ticker/bookTicker
get_openInterest获取未平仓合约数/fapi/v1/openInterest
get_delivery_price季度合约历史结算价/futures/data/delivery-price
get_openInterestHist合约持仓量/futures/data/openInterestHist
get_topLongShortAccountRatio大户账户数多空比/futures/data/topLongShortAccountRatio
get_topLongShortPositionRatio大户持仓量多空比/futures/data/topLongShortPositionRatio
get_globalLongShortAccountRatio多空持仓人数比/futures/data/globalLongShortAccountRatio
get_takerlongshortRatio合约主动买卖量/futures/data/takerlongshortRatio
get_basis基差/futures/data/basis
get_lvtKlines杠杆代币历史净值K线/fapi/v1/lvtKlines
get_indexInfo综合指数交易对信息/fapi/v1/indexInfo
get_assetIndex多资产模式资产汇率指数/fapi/v1/assetIndex
get_constituents查询指数价格成分/fapi/v1/constituents

2. 模型实例化

import datetimefrom binance_interface.api import UMfrom binance_interface.app.utils import eprint
# 转发:需搭建转发服务器,可参考:https://github.com/pyted/binance_resenderproxy_host = Noneum = UM(proxy_host=proxy_host)market = um.market

3. U本位合约实时行情

3.1 当前最优挂单 get_ticker_bookTicker

# 现货全部产品bookTicker_result = market.get_ticker_bookTicker()eprint(bookTicker_result, data_length=5)

输出:

>>> {'code': 200,>>>'data': [{'symbol': 'BTCUSDT',>>>'bidPrice': '40158.10',>>>'bidQty': '1.750',>>>'askPrice': '40158.20',>>>'askQty': '7.586',>>>'...': '......'},>>> {'symbol': 'ETHUSDT',>>>'bidPrice': '2222.27',>>>'bidQty': '5.638',>>>'askPrice': '2222.28',>>>'askQty': '27.227',>>>'...': '......'},>>> {'symbol': 'BCHUSDT',>>>'bidPrice': '235.89',>>>'bidQty': '1.539',>>>'askPrice': '235.90',>>>'askQty': '4.531',>>>'...': '......'},>>> {'symbol': 'XRPUSDT',>>>'bidPrice': '0.5139',>>>'bidQty': '22041.0',>>>'askPrice': '0.5140',>>>'askQty': '101329.3',>>>'...': '......'},>>> {'symbol': 'EOSUSDT',>>>'bidPrice': '0.690',>>>'bidQty': '162800.4',>>>'askPrice': '0.691',>>>'askQty': '116329.8',>>>'...': '......'},>>> '......'],>>>'msg': ''}
# 现货单个交易对bookTicker_result = market.get_ticker_bookTicker(symbol='BTCUSDT')eprint(bookTicker_result)

输出:

>>> {'code': 200,>>>'data': {'symbol': 'BTCUSDT',>>> 'bidPrice': '40148.30',>>> 'bidQty': '0.808',>>> 'askPrice': '40148.40',>>> 'askQty': '5.244',>>> 'time': 1706245394375,>>> 'lastUpdateId': 3888877129825},>>>'msg': ''}

3.2 最新价格 get_ticker_price

# 现货全部产品price_result = market.get_ticker_price()eprint(price_result, data_length=5)

输出:

>>> {'code': 200,>>>'data': [{'symbol': 'NEARUSDT', 'price': '2.7580', 'time': 1706245389313},>>> {'symbol': '1000SATSUSDT', 'price': '0.0004072', 'time': 1706245399913},>>> {'symbol': 'UMAUSDT', 'price': '5.321000', 'time': 1706245400559},>>> {'symbol': 'GALAUSDT', 'price': '0.02223', 'time': 1706245396551},>>> {'symbol': 'MINAUSDT', 'price': '1.0101000', 'time': 1706245396476},>>> '......'],>>>'msg': ''}
# 现货单个产品price_result = market.get_ticker_price(symbol='BTCUSDT')eprint(price_result)

输出:

>>> {'code': 200, 'data': {'symbol': 'BTCUSDT', 'price': '40146.70', 'time': 1706245405158}, 'msg': ''}

3.3 24hr价格变动情况 get_ticker_24hr

# 现货全部产品hr24_result = market.get_ticker_24hr()eprint(hr24_result, data_length=5)

输出:

>>> {'code': 200,>>>'data': [{'symbol': 'ATAUSDT',>>>'priceChange': '0.0020',>>>'priceChangePercent': '2.389',>>>'weightedAvgPrice': '0.0843',>>>'lastPrice': '0.0857',>>>'...': '......'},>>> {'symbol': 'XMRUSDT',>>>'priceChange': '6.70',>>>'priceChangePercent': '4.404',>>>'weightedAvgPrice': '156.27',>>>'lastPrice': '158.82',>>>'...': '......'},>>> {'symbol': 'BLUEBIRDUSDT',>>>'priceChange': '0.10800',>>>'priceChangePercent': '1.381',>>>'weightedAvgPrice': '7.88532',>>>'lastPrice': '7.92700',>>>'...': '......'},>>> {'symbol': 'POLYXUSDT',>>>'priceChange': '0.0028000',>>>'priceChangePercent': '1.808',>>>'weightedAvgPrice': '0.1557668',>>>'lastPrice': '0.1577000',>>>'...': '......'},>>> {'symbol': 'ATOMUSDT',>>>'priceChange': '0.142',>>>'priceChangePercent': '1.546',>>>'weightedAvgPrice': '9.205',>>>'lastPrice': '9.327',>>>'...': '......'},>>> '......'],>>>'msg': ''}
# 现货单个产品hr24_result = market.get_ticker_24hr(symbol='BTCUSDT')eprint(hr24_result)

输出:

>>> {'code': 200,>>>'data': {'symbol': 'BTCUSDT',>>> 'priceChange': '253.80',>>> 'priceChangePercent': '0.636',>>> 'weightedAvgPrice': '39934.69',>>> 'lastPrice': '40148.20',>>> 'lastQty': '0.022',>>> 'openPrice': '39894.40',>>> 'highPrice': '40300.00',>>> 'lowPrice': '39535.30',>>> 'volume': '209588.044',>>> 'quoteVolume': '8369834245.01',>>> 'openTime': 1706158980000,>>> 'closeTime': 1706245408978,>>> 'firstId': 4527594884,>>> 'lastId': 4530284646,>>> 'count': 2689755},>>>'msg': ''}

4. 历史K线 get_klines

# 测试时间startDatetime = '2023-01-01 00:00:00'endDatetime = '2023-01-01 00:05:00'startTime = int(datetime.datetime.strptime(startDatetime, '%Y-%m-%d %H:%M:%S').timestamp() * 1000)endTime = int(datetime.datetime.strptime(endDatetime, '%Y-%m-%d %H:%M:%S').timestamp() * 1000)print(startDatetime, startTime)print(endDatetime, endTime)

输出:

>>> 2023-01-01 00:00:00 1672502400000>>> 2023-01-01 00:05:00 1672502700000
# 仅给定终止时间kline_result = market.get_klines(symbol='BTCUSDT', interval='1m', endTime=endTime, limit=2)eprint(kline_result)

输出:

>>> {'code': 200,>>>'data': [[1672502640000, '16587.90', '16588.00', '16587.70', '16587.70', '......'],>>> [1672502700000, '16587.70', '16587.70', '16586.90', '16587.00', '......']],>>>'msg': ''}
# 仅给定起始和终止时间kline_result = market.get_klines(symbol='BTCUSDT',interval='1m',startTime=startTime,# 包括startTimeendTime=endTime,# 包括endTime)eprint(kline_result)

输出:

>>> {'code': 200,>>>'data': [[1672502400000, '16586.10', '16591.20', '16586.00', '16591.10', '......'],>>> [1672502460000, '16591.10', '16591.50', '16590.80', '16590.90', '......'],>>> [1672502520000, '16590.80', '16591.70', '16590.80', '16591.70', '......'],>>> [1672502580000, '16591.60', '16591.70', '16587.90', '16587.90', '......'],>>> [1672502640000, '16587.90', '16588.00', '16587.70', '16587.70', '......'],>>> [1672502700000, '16587.70', '16587.70', '16586.90', '16587.00', '......']],>>>'msg': ''}

5. 深度信息 get_depth

depth_result = market.get_depth(symbol='BTCUSDT', limit=5)eprint(depth_result)

输出:

>>> {'code': 200,>>>'data': {'lastUpdateId': 3888878453883,>>> 'E': 1706245426397,>>> 'T': 1706245426390,>>> 'bids': [['40152.50', '0.131'],>>>['40152.40', '0.008'],>>>['40152.20', '0.005'],>>>['40152.10', '0.013'],>>>['40151.90', '0.038']],>>> 'asks': [['40152.60', '3.099'],>>>['40152.70', '0.008'],>>>['40152.80', '0.008'],>>>['40152.90', '0.005'],>>>['40153.00', '0.005']]},>>>'msg': ''}

6. 成交查询

6.1 近期成交列表 get_trades

trades_result = market.get_trades(symbol='BTCUSDT', limit=5)eprint(trades_result)

输出:

>>> {'code': 200,>>>'data': [{'id': 4530285100,>>>'price': '40151.90',>>>'qty': '0.003',>>>'quoteQty': '120.45',>>>'time': 1706245427482,>>>'...': '......'},>>> {'id': 4530285101,>>>'price': '40151.70',>>>'qty': '0.003',>>>'quoteQty': '120.45',>>>'time': 1706245427482,>>>'...': '......'},>>> {'id': 4530285102,>>>'price': '40151.60',>>>'qty': '0.005',>>>'quoteQty': '200.75',>>>'time': 1706245427482,>>>'...': '......'},>>> {'id': 4530285103,>>>'price': '40151.50',>>>'qty': '0.002',>>>'quoteQty': '80.30',>>>'time': 1706245427482,>>>'...': '......'},>>> {'id': 4530285104,>>>'price': '40151.20',>>>'qty': '0.026',>>>'quoteQty': '1043.93',>>>'time': 1706245428266,>>>'...': '......'}],>>>'msg': ''}

6.2 查询历史成交 get_historicalTrades

historicalTrades_result = market.get_historicalTrades(symbol='BTCUSDT', limit=5)# 这个接口需要keyeprint(historicalTrades_result)

输出:

>>> {'code': 200,>>>'data': [{'id': 4530285131,>>>'price': '40151.30',>>>'qty': '0.004',>>>'quoteQty': '160.60',>>>'time': 1706245430957,>>>'...': '......'},>>> {'id': 4530285132,>>>'price': '40151.30',>>>'qty': '0.003',>>>'quoteQty': '120.45',>>>'time': 1706245431264,>>>'...': '......'},>>> {'id': 4530285133,>>>'price': '40151.20',>>>'qty': '0.007',>>>'quoteQty': '281.05',>>>'time': 1706245431698,>>>'...': '......'},>>> {'id': 4530285134,>>>'price': '40151.20',>>>'qty': '0.006',>>>'quoteQty': '240.90',>>>'time': 1706245431698,>>>'...': '......'},>>> {'id': 4530285135,>>>'price': '40151.20',>>>'qty': '0.110',>>>'quoteQty': '4416.63',>>>'time': 1706245431698,>>>'...': '......'}],>>>'msg': ''}

6.3 近期成交(归集) get_aggTrades

aggTrades_result = market.get_aggTrades(symbol='BTCUSDT', limit=5)eprint(aggTrades_result)

输出:

>>> {'code': 200,>>>'data': [{'a': 2000008269, 'p': '40151.20', 'q': '0.003', 'f': 4530285152, 'l': 4530285152, '...': '......'},>>> {'a': 2000008270, 'p': '40151.20', 'q': '0.422', 'f': 4530285153, 'l': 4530285176, '...': '......'},>>> {'a': 2000008271, 'p': '40151.10', 'q': '0.008', 'f': 4530285177, 'l': 4530285178, '...': '......'},>>> {'a': 2000008272, 'p': '40151.00', 'q': '0.005', 'f': 4530285179, 'l': 4530285179, '...': '......'},>>> {'a': 2000008273, 'p': '40150.90', 'q': '0.005', 'f': 4530285180, 'l': 4530285180, '...': '......'}],>>>'msg': ''}

7. 交易规范信息 get_exchangeInfo

exchangeInfo_result = market.get_exchangeInfo()eprint(exchangeInfo_result)

输出:

>>> {'code': 200,>>>'data': {'timezone': 'UTC',>>> 'serverTime': 1706241611125,>>> 'futuresType': 'U_MARGINED',>>> 'rateLimits': [{'rateLimitType': 'REQUEST_WEIGHT', 'interval': 'MINUTE', 'intervalNum': 1, 'limit': 2400},>>>{'rateLimitType': 'ORDERS', 'interval': 'MINUTE', 'intervalNum': 1, 'limit': 1200},>>>{'rateLimitType': 'ORDERS', 'interval': 'SECOND', 'intervalNum': 10, 'limit': 300}],>>> 'exchangeFilters': [],>>> 'assets': [{'asset': 'USDT', 'marginAvailable': True, 'autoAssetExchange': '-10000'},>>>{'asset': 'BTC', 'marginAvailable': True, 'autoAssetExchange': '-0.10000000'},>>>{'asset': 'BNB', 'marginAvailable': True, 'autoAssetExchange': '-10'},>>>{'asset': 'ETH', 'marginAvailable': True, 'autoAssetExchange': '-5'},>>>{'asset': 'XRP', 'marginAvailable': True, 'autoAssetExchange': '0'},>>>'......'],>>> 'symbols': [{'symbol': 'BTCUSDT',>>>'pair': 'BTCUSDT',>>>'contractType': 'PERPETUAL',>>>'deliveryDate': 4133404800000,>>>'onboardDate': 1569398400000,>>>'...': '......'},>>> {'symbol': 'ETHUSDT',>>>'pair': 'ETHUSDT',>>>'contractType': 'PERPETUAL',>>>'deliveryDate': 4133404800000,>>>'onboardDate': 1569398400000,>>>'...': '......'},>>> {'symbol': 'BCHUSDT',>>>'pair': 'BCHUSDT',>>>'contractType': 'PERPETUAL',>>>'deliveryDate': 4133404800000,>>>'onboardDate': 1569398400000,>>>'...': '......'},>>> {'symbol': 'XRPUSDT',>>>'pair': 'XRPUSDT',>>>'contractType': 'PERPETUAL',>>>'deliveryDate': 4133404800000,>>>'onboardDate': 1569398400000,>>>'...': '......'},>>> {'symbol': 'EOSUSDT',>>>'pair': 'EOSUSDT',>>>'contractType': 'PERPETUAL',>>>'deliveryDate': 4133404800000,>>>'onboardDate': 1569398400000,>>>'...': '......'},>>> '......']},>>>'msg': ''}