Contract
exchange.GetFundings()
获取合约资金费率信息(仅限合约交易)。
语法
python
# 查询所有合约的资金费率
fundings = exchange.GetFundings()
# 查询指定合约的资金费率
funding = exchange.GetFundings("BTC_USDT")参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| symbol | str | 否 | 合约代码(格式,如 "BTC_USDT") |
- 不传参数:返回所有合约的资金费率列表
- 传入合约代码:返回指定合约的资金费率信息
返回值
查询所有合约时
返回一个列表,每个元素包含以下字段:
| 字段 | 类型 | 说明 |
|---|---|---|
| Info | dict | 交易所原始数据 |
| Symbol | str | 合约代码(格式,如 "BTC_USDT") |
| Interval | int | 资金费率结算周期(毫秒) |
| Time | int | 下次结算时间(Unix 时间戳,毫秒) |
| Rate | float | 当前资金费率(浮点数,如 0.0001 表示 0.01%) |
查询指定合约时
返回单个字典,字段同上。
支持的交易所
- ✅ Binance Futures(币安合约)
- ✅ Gate.io Futures(Gate.io 合约)
- ❌ 现货交易所不支持此方法
示例
1. 查询所有合约的资金费率
python
# 获取所有合约的资金费率
fundings = exchange.GetFundings()
Log(f"共 {len(fundings)} 个合约")
for funding in fundings:
# 只显示费率不为0的合约
if funding["Rate"] != 0:
Log(f"合约: {funding['Symbol']}")
Log(f" 资金费率: {funding['Rate'] * 100:.4f}%")
Log(f" 结算周期: {funding['Interval'] / 3600000:.0f} 小时")
# 转换下次结算时间
import datetime
next_time = datetime.datetime.fromtimestamp(funding['Time'] / 1000)
Log(f" 下次结算: {next_time}")2. 查询指定合约的资金费率
python
# 查询 BTC 永续合约
funding = exchange.GetFundings("BTC_USDT")
if funding:
Log(f"合约: {funding['Symbol']}")
Log(f"资金费率: {funding['Rate'] * 100:.4f}%")
Log(f"结算周期: {funding['Interval'] / 3600000:.0f} 小时")
# 计算距离下次结算的时间
import time
current_time = int(time.time() * 1000)
time_to_funding = (funding['Time'] - current_time) / 1000 / 60
Log(f"距离下次结算: {time_to_funding:.0f} 分钟")3. 监控高资金费率合约
python
def monitor_high_funding_rate(threshold=0.001):
"""监控资金费率超过阈值的合约"""
fundings = exchange.GetFundings()
high_rate_contracts = []
for funding in fundings:
if abs(funding["Rate"]) > threshold:
high_rate_contracts.append({
"symbol": funding["Symbol"],
"rate": funding["Rate"] * 100, # 转换为百分比
"time": funding["Time"]
})
# 按费率绝对值排序
high_rate_contracts.sort(key=lambda x: abs(x["rate"]), reverse=True)
Log(f"发现 {len(high_rate_contracts)} 个高资金费率合约(>{threshold*100}%):")
for contract in high_rate_contracts:
direction = "多付空" if contract["rate"] > 0 else "空付多"
Log(f" {contract['symbol']}: {contract['rate']:.4f}% ({direction})")
return high_rate_contracts
# 每小时检查一次
while True:
monitor_high_funding_rate(threshold=0.001) # 0.1%
Sleep(3600000) # 1小时4. 资金费率套利策略
python
def funding_arbitrage_signal():
"""基于资金费率的套利信号"""
# 获取当前持仓
position = exchange.GetPosition()
# 获取当前合约的资金费率
contract_type = exchange.GetContractType()
funding = exchange.GetFundings(contract_type)
if not funding:
return
rate = funding["Rate"]
time_to_funding = (funding["Time"] - int(time.time() * 1000)) / 1000 / 60
Log(f"当前资金费率: {rate * 100:.4f}%")
Log(f"距离下次结算: {time_to_funding:.0f} 分钟")
# 资金费率策略逻辑
if rate > 0.001: # 正费率超过 0.1%
Log("资金费率过高(多方付费),建议做空")
if len(position) == 0 or position[0]["Type"] == 0: # 无持仓或多仓
# 开空仓或平多仓
pass
elif rate < -0.001: # 负费率超过 -0.1%
Log("资金费率过低(空方付费),建议做多")
if len(position) == 0 or position[0]["Type"] == 1: # 无持仓或空仓
# 开多仓或平空仓
pass
else:
Log("资金费率正常,无套利机会")5. 多交易所资金费率对比
python
def compare_funding_rates():
"""对比多个交易所的资金费率"""
# 假设配置了多个交易所
exchanges_list = [exchange, exchange2] # Binance, Gate.io
exchange_names = ["Binance", "Gate.io"]
symbol = "BTC_USDT"
for i, ex in enumerate(exchanges_list):
try:
funding = ex.GetFundings(symbol)
if funding:
Log(f"{exchange_names[i]} {symbol}:")
Log(f" 资金费率: {funding['Rate'] * 100:.4f}%")
import datetime
next_time = datetime.datetime.fromtimestamp(funding['Time'] / 1000)
Log(f" 下次结算: {next_time}")
except Exception as e:
Log(f"{exchange_names[i]} 查询失败: {str(e)}")6. 资金费率预警系统
python
def funding_rate_alert():
"""资金费率预警系统"""
# 设置预警阈值
HIGH_RATE = 0.001 # 0.1%
VERY_HIGH_RATE = 0.005 # 0.5%
fundings = exchange.GetFundings()
for funding in fundings:
rate = funding["Rate"]
abs_rate = abs(rate)
if abs_rate >= VERY_HIGH_RATE:
direction = "多方" if rate > 0 else "空方"
Log(f"⚠️ 极端费率预警: {funding['Symbol']} 资金费率 {rate*100:.4f}% ({direction}付费)")
# 这里可以发送通知、停止策略等
elif abs_rate >= HIGH_RATE:
direction = "多方" if rate > 0 else "空方"
Log(f"📢 高费率提醒: {funding['Symbol']} 资金费率 {rate*100:.4f}% ({direction}付费)")
# 每30分钟检查一次
while True:
funding_rate_alert()
Sleep(1800000) # 30分钟应用场景
1. 资金费率套利
在资金费率极端时进行反向操作,赚取资金费。
2. 成本控制
避免在高费率时持有大额持仓,减少资金费支出。
3. 市场情绪分析
资金费率反映多空情绪,正费率表示市场偏多,负费率表示市场偏空。
4. 跨交易所套利
寻找不同交易所之间的资金费率差异机会。
5. 持仓时机选择
在费率结算前调整持仓,避免不必要的费用。
注意事项
1. 交易所差异
- Binance:每 8 小时结算一次(00:00, 08:00, 16:00 UTC)
- Gate.io:结算周期可能因合约而异
- 不同交易所的费率计算方式可能不同
2. 费率理解
- 正费率(> 0):多方支付给空方(市场偏多)
- 负费率(< 0):空方支付给多方(市场偏空)
- 费率通常在 -0.05% ~ 0.05% 之间
3. 时间精度
Time和Interval均为毫秒时间戳- 建议在结算前几分钟关闭持仓以避免费用
4. 数据更新
- 资金费率会实时变化
- 建议定期查询以获取最新数据
5. 错误处理
python
try:
fundings = exchange.GetFundings()
# 处理数据
except Exception as e:
Log("查询资金费率失败:", str(e))
# 现货交易所会抛出异常6. 性能考虑
python
# 缓存资金费率数据,避免频繁查询
funding_cache = {}
last_update = 0
def get_funding_rate(symbol):
global funding_cache, last_update
current_time = time.time() * 1000
# 每5分钟更新一次
if current_time - last_update > 300000:
fundings = exchange.GetFundings()
for f in fundings:
funding_cache[f["Symbol"]] = f
last_update = current_time
return funding_cache.get(symbol)相关方法
- GetPosition() - 获取持仓信息
- SetContractType() - 设置合约类型
- GetContractType() - 获取合约类型
- SetMarginLevel() - 设置杠杆倍数