1.5 Binance_interface API 币本位合约行情
Binance_interface API 币本位合约行情
- Github地址
- PyTed量化交易研究院
1. API 币本位合约行情接口总览
方法 | 解释 | Path |
---|---|---|
get_ping | 测试服务器连通性 PING | /dapi/v1/ping |
get_time | 获取服务器时间 | /dapi/v1/time |
get_exchangeInfo | 获取交易规则和交易对 | /dapi/v1/exchangeInfo |
get_depth | 深度信息 | /dapi/v1/depth |
get_trades | 近期成交 | /dapi/v1/trades |
get_historicalTrades | 查询历史成交 (MARKET_DATA) | /dapi/v1/historicalTrades |
get_aggTrades | 近期成交(归集) | /dapi/v1/aggTrades |
get_premiumIndex | 最新现货指数价格和Mark Price | /dapi/v1/premiumIndex |
get_fundingRate | 查询永续合约资金费率历史 | /dapi/v1/fundingRate |
get_fundingInfo | 查询资金费率信息 | /dapi/v1/fundingInfo |
get_klines | K线数据 | /dapi/v1/klines |
get_continuousKlines | 连续合约K线数据 | /dapi/v1/continuousKlines |
get_indexPriceKlines | 价格指数K线数据 | /dapi/v1/indexPriceKlines |
get_markPriceKlines | 标记价格K线数据 | /dapi/v1/markPriceKlines |
get_premiumIndexKlines | 溢价指数K线数据 | /dapi/v1/premiumIndexKlines |
get_ticker_24hr | 24hr价格变动情况 | /dapi/v1/ticker/24hr |
get_ticker_price | 最新价格 | /dapi/v1/ticker/price |
get_ticker_bookTicker | 当前最优挂单 | /dapi/v1/ticker/bookTicker |
get_constituents | 查询指数价格成分 | /dapi/v1/constituents |
get_openInterest | 获取未平仓合约数 | /dapi/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_takerBuySellVol | 合约主动买卖量 | /futures/data/takerBuySellVol |
get_basis | 基差 | /futures/data/basis |
2. 模型实例化
import datetime
from binance_interface.api import CM
from binance_interface.app.utils import eprint
# 转发:需搭建转发服务器,可参考:https://github.com/pyted/binance_resender
proxy_host = None
cm = CM(
proxy_host=proxy_host
)
market = cm.market
3. U本位合约实时行情
3.1 当前最优挂单 get_ticker_bookTicker
# 现货全部产品
bookTicker_result = market.get_ticker_bookTicker()
eprint(bookTicker_result, data_length=5)
输出:
>>> {'code': 200,
>>> 'data': [{'symbol': 'BTCUSD_PERP',
>>> 'pair': 'BTCUSD',
>>> 'bidPrice': '40148.0',
>>> 'bidQty': '620',
>>> 'askPrice': '40148.1',
>>> '...': '......'},
>>> {'symbol': 'ETHUSD_PERP',
>>> 'pair': 'ETHUSD',
>>> 'bidPrice': '2221.81',
>>> 'bidQty': '21727',
>>> 'askPrice': '2221.82',
>>> '...': '......'},
>>> {'symbol': 'LINKUSD_PERP',
>>> 'pair': 'LINKUSD',
>>> 'bidPrice': '13.817',
>>> 'bidQty': '35',
>>> 'askPrice': '13.819',
>>> '...': '......'},
>>> {'symbol': 'BNBUSD_PERP',
>>> 'pair': 'BNBUSD',
>>> 'bidPrice': '293.900',
>>> 'bidQty': '18',
>>> 'askPrice': '293.910',
>>> '...': '......'},
>>> {'symbol': 'TRXUSD_PERP',
>>> 'pair': 'TRXUSD',
>>> 'bidPrice': '0.11355',
>>> 'bidQty': '773',
>>> 'askPrice': '0.11356',
>>> '...': '......'},
>>> '......'],
>>> 'msg': ''}
# 现货单个交易对
bookTicker_result = market.get_ticker_bookTicker(symbol='BTCUSD_PERP')
eprint(bookTicker_result)
输出:
>>> {'code': 200,
>>> 'data': [{'symbol': 'BTCUSD_PERP',
>>> 'pair': 'BTCUSD',
>>> 'bidPrice': '40148.0',
>>> 'bidQty': '624',
>>> 'askPrice': '40148.1',
>>> '...': '......'}],
>>> 'msg': ''}
3.2 最新价格 get_ticker_price
# 现货全部产品
price_result = market.get_ticker_price()
eprint(price_result, data_length=5)
输出:
>>> {'code': 200,
>>> 'data': [{'symbol': 'ETHUSD_240329', 'ps': 'ETHUSD', 'price': '2258.71', 'time': 1706245526966},
>>> {'symbol': 'BCHUSD_PERP', 'ps': 'BCHUSD', 'price': '235.76', 'time': 1706245508581},
>>> {'symbol': 'GMTUSD_PERP', 'ps': 'GMTUSD', 'price': '0.25760', 'time': 1706245403885},
>>> {'symbol': 'LTCUSD_240329', 'ps': 'LTCUSD', 'price': '67.13', 'time': 1706244859254},
>>> {'symbol': 'RUNEUSD_PERP', 'ps': 'RUNEUSD', 'price': '4.1050', 'time': 1706245387936},
>>> '......'],
>>> 'msg': ''}
# 现货单个产品
price_result = market.get_ticker_price(symbol='BTCUSD_PERP')
eprint(price_result)
输出:
>>> {'code': 200, 'data': [{'symbol': 'BTCUSD_PERP', 'ps': 'BTCUSD', 'price': '40148.1', 'time': 1706245538962}], 'msg': ''}
3.3 24hr价格变动情况 get_ticker_24hr
# 现货全部产品
hr24_result = market.get_ticker_24hr()
eprint(hr24_result, data_length=5)
输出:
>>> {'code': 200,
>>> 'data': [{'symbol': 'UNIUSD_PERP',
>>> 'pair': 'UNIUSD',
>>> 'priceChange': '-0.002',
>>> 'priceChangePercent': '-0.035',
>>> 'weightedAvgPrice': '5.75000879',
>>> '...': '......'},
>>> {'symbol': 'THETAUSD_PERP',
>>> 'pair': 'THETAUSD',
>>> 'priceChange': '-0.009',
>>> 'priceChangePercent': '-0.939',
>>> 'weightedAvgPrice': '0.94889503',
>>> '...': '......'},
>>> {'symbol': 'SOLUSD_PERP',
>>> 'pair': 'SOLUSD',
>>> 'priceChange': '0.582',
>>> 'priceChangePercent': '0.668',
>>> 'weightedAvgPrice': '87.18539016',
>>> '...': '......'},
>>> {'symbol': 'AAVEUSD_PERP',
>>> 'pair': 'AAVEUSD',
>>> 'priceChange': '0.470',
>>> 'priceChangePercent': '0.533',
>>> 'weightedAvgPrice': '88.24177045',
>>> '...': '......'},
>>> {'symbol': 'GMTUSD_PERP',
>>> 'pair': 'GMTUSD',
>>> 'priceChange': '0.00240',
>>> 'priceChangePercent': '0.940',
>>> 'weightedAvgPrice': '0.25543639',
>>> '...': '......'},
>>> '......'],
>>> 'msg': ''}
# 现货单个产品
hr24_result = market.get_ticker_24hr(symbol='BTCUSD_PERP')
eprint(hr24_result)
输出:
>>> {'code': 200,
>>> 'data': [{'symbol': 'BTCUSD_PERP',
>>> 'pair': 'BTCUSD',
>>> 'priceChange': '279.5',
>>> 'priceChangePercent': '0.701',
>>> 'weightedAvgPrice': '39927.54529431',
>>> '...': '......'}],
>>> '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='BTCUSD_PERP', interval='1m', endTime=endTime, limit=2)
eprint(kline_result)
输出:
>>> {'code': 200,
>>> 'data': [[1672502640000, '16580.3', '16580.3', '16580.3', '16580.3', '......'],
>>> [1672502700000, '16580.3', '16580.3', '16580.3', '16580.3', '......']],
>>> 'msg': ''}
# 仅给定起始和终止时间
kline_result = market.get_klines(
symbol='BTCUSD_PERP',
interval='1m',
startTime=startTime, # 包括startTime
endTime=endTime, # 包括endTime
)
eprint(kline_result)
输出:
>>> {'code': 200,
>>> 'data': [[1672502400000, '16578.1', '16580.3', '16578.0', '16580.3', '......'],
>>> [1672502460000, '16580.3', '16580.3', '16580.3', '16580.3', '......'],
>>> [1672502520000, '16580.4', '16580.4', '16580.4', '16580.4', '......'],
>>> [1672502580000, '16580.3', '16580.3', '16580.3', '16580.3', '......'],
>>> [1672502640000, '16580.3', '16580.3', '16580.3', '16580.3', '......'],
>>> [1672502700000, '16580.3', '16580.3', '16580.3', '16580.3', '......']],
>>> 'msg': ''}
5. 深度信息 get_depth
depth_result = market.get_depth(symbol='BTCUSD_PERP', limit=5)
eprint(depth_result)
输出:
>>> {'code': 200,
>>> 'data': {'lastUpdateId': 844374293002,
>>> 'E': 1706245546602,
>>> 'T': 1706245546590,
>>> 'symbol': 'BTCUSD_PERP',
>>> 'pair': 'BTCUSD',
>>> 'bids': [['40148.0', '655'], ['40147.9', '1'], ['40147.8', '228'], ['40146.8', '1'], ['40146.7', '1']],
>>> 'asks': [['40148.1', '14131'], ['40148.2', '17'], ['40148.3', '4'], ['40148.5', '234'], ['40148.6', '54']]},
>>> 'msg': ''}
6. 成交查询
6.1 近期成交列表 get_trades
trades_result = market.get_trades(symbol='BTCUSD_PERP', limit=5)
eprint(trades_result)
输出:
>>> {'code': 200,
>>> 'data': [{'id': 720241178,
>>> 'price': '40148.0',
>>> 'qty': '26',
>>> 'baseQty': '0.06476039',
>>> 'time': 1706245546325,
>>> '...': '......'},
>>> {'id': 720241179,
>>> 'price': '40148.0',
>>> 'qty': '116',
>>> 'baseQty': '0.28893096',
>>> 'time': 1706245546763,
>>> '...': '......'},
>>> {'id': 720241180,
>>> 'price': '40148.0',
>>> 'qty': '5',
>>> 'baseQty': '0.01245392',
>>> 'time': 1706245546763,
>>> '...': '......'},
>>> {'id': 720241181,
>>> 'price': '40148.1',
>>> 'qty': '1',
>>> 'baseQty': '0.00249078',
>>> 'time': 1706245546963,
>>> '...': '......'},
>>> {'id': 720241182,
>>> 'price': '40148.1',
>>> 'qty': '11',
>>> 'baseQty': '0.02739856',
>>> 'time': 1706245546964,
>>> '...': '......'}],
>>> 'msg': ''}
6.2 查询历史成交 get_historicalTrades
historicalTrades_result = market.get_historicalTrades(symbol='BTCUSD_PERP', limit=5) # 这个接口需要key
eprint(historicalTrades_result)
输出:
>>> {'code': 200,
>>> 'data': [{'id': 720241182,
>>> 'price': '40148.1',
>>> 'qty': '11',
>>> 'baseQty': '0.02739856',
>>> 'time': 1706245546964,
>>> '...': '......'},
>>> {'id': 720241183,
>>> 'price': '40148.0',
>>> 'qty': '1',
>>> 'baseQty': '0.00249078',
>>> 'time': 1706245548286,
>>> '...': '......'},
>>> {'id': 720241184,
>>> 'price': '40148.0',
>>> 'qty': '1',
>>> 'baseQty': '0.00249078',
>>> 'time': 1706245548286,
>>> '...': '......'},
>>> {'id': 720241185,
>>> 'price': '40148.1',
>>> 'qty': '1',
>>> 'baseQty': '0.00249078',
>>> 'time': 1706245548962,
>>> '...': '......'},
>>> {'id': 720241186,
>>> 'price': '40148.1',
>>> 'qty': '11',
>>> 'baseQty': '0.02739856',
>>> 'time': 1706245548962,
>>> '...': '......'}],
>>> 'msg': ''}
6.3 近期成交(归集) get_aggTrades
aggTrades_result = market.get_aggTrades(symbol='BTCUSD_PERP', limit=5)
eprint(aggTrades_result)
输出:
>>> {'code': 200,
>>> 'data': [{'a': 293255475, 'p': '40148.0', 'q': '44', 'f': 720241176, 'l': 720241178, '...': '......'},
>>> {'a': 293255476, 'p': '40148.0', 'q': '121', 'f': 720241179, 'l': 720241180, '...': '......'},
>>> {'a': 293255477, 'p': '40148.1', 'q': '12', 'f': 720241181, 'l': 720241182, '...': '......'},
>>> {'a': 293255478, 'p': '40148.0', 'q': '2', 'f': 720241183, 'l': 720241184, '...': '......'},
>>> {'a': 293255479, 'p': '40148.1', 'q': '12', 'f': 720241185, 'l': 720241186, '...': '......'}],
>>> 'msg': ''}
7. 交易规范信息 get_exchangeInfo
exchangeInfo_result = market.get_exchangeInfo()
eprint(exchangeInfo_result)
输出:
>>> {'code': 200,
>>> 'data': {'timezone': 'UTC',
>>> 'serverTime': 1706241611122,
>>> 'rateLimits': [{'rateLimitType': 'REQUEST_WEIGHT', 'interval': 'MINUTE', 'intervalNum': 1, 'limit': 2400},
>>> {'rateLimitType': 'ORDERS', 'interval': 'MINUTE', 'intervalNum': 1, 'limit': 1200}],
>>> 'exchangeFilters': [],
>>> 'symbols': [{'symbol': 'BTCUSD_PERP',
>>> 'pair': 'BTCUSD',
>>> 'contractType': 'PERPETUAL',
>>> 'deliveryDate': 4133404800000,
>>> 'onboardDate': 1597042800000,
>>> '...': '......'},
>>> {'symbol': 'BTCUSD_240329',
>>> 'pair': 'BTCUSD',
>>> 'contractType': 'CURRENT_QUARTER',
>>> 'deliveryDate': 1711699200000,
>>> 'onboardDate': 1695974400000,
>>> '...': '......'},
>>> {'symbol': 'BTCUSD_240628',
>>> 'pair': 'BTCUSD',
>>> 'contractType': 'NEXT_QUARTER',
>>> 'deliveryDate': 1719561600000,
>>> 'onboardDate': 1703836800000,
>>> '...': '......'},
>>> {'symbol': 'ETHUSD_PERP',
>>> 'pair': 'ETHUSD',
>>> 'contractType': 'PERPETUAL',
>>> 'deliveryDate': 4133404800000,
>>> 'onboardDate': 1597734000000,
>>> '...': '......'},
>>> {'symbol': 'ETHUSD_240329',
>>> 'pair': 'ETHUSD',
>>> 'contractType': 'CURRENT_QUARTER',
>>> 'deliveryDate': 1711699200000,
>>> 'onboardDate': 1695974400000,
>>> '...': '......'},
>>> '......']},
>>> 'msg': ''}