使用CCXT库从Bybit获取期货列表时遇参数错误及空列表问题
解决CCXT获取Bybit期货合约列表的问题
先升级CCXT到最新版本
旧版本CCXT可能存在方法参数或接口适配问题,先执行升级:
pip install --upgrade ccxt
正确的初始化与调用方式
Bybit的期货合约分为**线性(linear)和反向(inverse)**两种类型,fetch_future_markets方法必须指定类型参数才能返回数据。以下是完整示例:
import ccxt # 初始化Bybit exchange,无需API密钥即可获取公共市场数据 exchange = ccxt.bybit({ 'options': { 'defaultType': 'future', # 设置默认市场类型为期货 }, }) # 获取线性期货合约列表 linear_futures = exchange.fetch_future_markets({'type': 'linear'}) # 获取反向期货合约列表 inverse_futures = exchange.fetch_future_markets({'type': 'inverse'}) # 合并所有期货合约 all_future_markets = linear_futures + inverse_futures # 验证结果:打印前5个合约的交易对 for market in all_future_markets[:5]: print(f"合约交易对: {market['symbol']}, 类型: {market['type']}")
针对你遇到的问题的具体说明
- 调用
fetch_future_markets()提示缺少必填参数params:Bybit的该方法要求指定合约类型(linear/inverse),必须通过params传入,比如fetch_future_markets({'type': 'linear'})。 - 直接赋值
exchange.fetch_future_markets得到空列表:你这是获取了方法对象本身,不是执行方法的结果,必须加括号()调用方法并传入参数才能得到合约列表。 - 设置
defaultType为future后仍无法解决:这个配置是让exchange默认操作期货市场,但fetch_future_markets方法本身仍需要指定具体的合约类型参数,二者不冲突但需要配合使用。
另一种过滤方式
如果你不想分开调用,也可以先获取所有市场,再过滤出期货类型的合约:
all_markets = exchange.fetch_markets() future_markets = [market for market in all_markets if market['type'] == 'future']
内容的提问来源于stack exchange,提问作者Geeko
相关产品推荐
相关产品推荐

