https://www.bitmex.com/app/perpetualContractsGuide#Funding-Rate-Calculations
有时,这些公式计算出的资金费率看起来会高于或低于某些用户的预期。但是,可以使用本文所述的步骤重现这些数字,本文以合约 ONDOUSDT 为例进行说明。页面底部提供了每项计算的 Python 代码片段。
资金费率支付使用上述链接中描述的公式设置:
资金费率 (F) = 溢价指数 (P) + clamp(利率 (I) - 溢价指数 (P), 0.05%, -0.05%)
此公式中的“溢价指数”是指上一个资金费率 8 小时窗口结束时的 .ONDOUSDTPI8H 指数值。这意味着,对于2025 年 1 月 14 日 04:00:00 的资金费用收付,我们需要使用上一个窗口结束时(即 2025 年 1 月 13 日20:00:00)的溢价指数值,该值可以通过以下 API 获取:https://www.bitmex.com/api/v1/trade?symbol=.ONDOUSDTPI8H&count=1&reverse=true
这将返回以下值:
| 时间戳 | 代码 | 方向 | 仓位规模 | 价格 | 价格变动方向 (tickDirection) | 交易类型 (trdType) |
| 2025-01-13T20:00:00.000Z | .ONDOUSDTPI8H | 买入 | 0 | -0.00184 | 价格下跌 (MinusTick) | 参考 (Referential) |
此代码的利率固定为 0.01%(请参阅 https://www.bitmex.com/app/contract/ONDOUSDT),因此我们可以计算此资金费用支付时的资金费用支付为 F = -0.00184 + clamp(0.00185, 0.0005, -0.0005) = -0.00184 + 0.0005 = -0.00134
这与面板中显示的值相符:
8H 值是根据前 8 小时窗口内 .ONDOUSDTPI 指数的算术平均值计算得出的,该平均值以分钟为单位公布,可以从此处的 API 中获取: https://www.bitmex.com/api/v1/trade?symbol=.ONDOUSDTPI&count=500&reverse=false&startTime=2025-01-13T12%3A01%3A00.000Z&endTime=2025-01-13T20%3A00%3A00.000Z
从该输出中取“价格”字段的平均值得出 -0.001839566,四舍五入后与 .ONDOUSDTPI8H 匹配。
我们会根据上方链接中提供的公式,使用工具表中的数据,在每分钟结束时计算每个交易品种的溢价指数: 溢价指数 (P) = (Max(0, 影响买入价 - 标记价格) - Max(0, 标记价格 - 影响卖出价)) / 现货价格+ 标记价格中使用的合理基差. 然后,该值会被插入到指数历史记录中该分钟的溢价指数中。
该代码的产品明细: https://www.bitmex.com/api/v1/instrument?symbol=ONDOUSDT
该产品代码的最新溢价指数值: https://www.bitmex.com/api/v1/trade?symbol=.ONDOUSDTPI&count=1&reverse=true
想复制溢价指数,请从“工具表”记录中提取以下 5 个字段:
- impactBidPrice
- impactAskPrice
- fairPrice
- indicativeSettlePrice
- fundingRate
该分钟的溢价指数值计算如下:
PI = ((max(0, impactBidPrice - fairPrice) - max(0, fairPrice - impactAskPrice)) / indicativeSettlePrice) + fundingRate
每分钟结束时计算出的 PI 值将与该分钟的溢价指数值历史记录相符。但请注意,由于市场活动,计算值在每分钟数据采集之间可能会出现波动——这些值每分钟仅采集一次,因此只有在每分钟结束时才会趋于一致。
为了演示此计算,我们于 UTC 时间 2025 年 1 月 14 日 02:06:00 截取了 ONDOUSDT 数据。此时返回的产品列表(PI 使用上述表达式计算):
| 代码 | impactBidPrice | impactAskPrice | fairPrice | indicativeSettlePrice | fundingRate | PI (计算) |
| ONDOUSDT | 0.541969 | 1.190485 | 1.19192 | 1.1923 | -0.00134 | -0.002543 |
ONDOUSDT PI 数据与以下计算结果相符(见下方“价格”字段):
| 时间戳 | 代码 | 方向 | 大小 | 价格 | 价格变动方向 (tickDirection) | 交易类型 (trdType) |
| 2025-01-14T02:06:00.000Z | .ONDOUSDTPI | Buy | 0 | -0.002543 | 价格下跌 (MinusTick) | 参考 (Referential) |
计算结果完全吻合。
代码示例:
获取 8 小时溢价指数:
import requests
import pandas as pd
symbol = "ONDOUSDT"
start_time = "2025-01-13T12:01:00.000Z"
end_time = "2025-01-13T20:00:00.000Z"
url = f"https://www.bitmex.com/api/v1/trade?symbol=.{symbol}PI8H&count=1&startTime={start_time}&endTime={end_time}"
resp = requests.get(url)
df_pi8h = pd.DataFrame(resp.json())
获取每分钟的溢价指数并取平均值,使其与 8 小时溢价指数相匹配:
url = f"https://www.bitmex.com/api/v1/trade?symbol=.{symbol}PI&count=500&startTime={start_time}&endTime={end_time}"
resp = requests.get(url)
df_pi = pd.DataFrame(resp.json())
pi8h = df_pi["price"].mean()
获取当前产品列表值并计算 PI:
url = f"https://www.bitmex.com/api/v1/instrument?symbol={symbol}"
resp = requests.get(url)
df = pd.DataFrame(resp.json())
df["bid"] = (df["impactBidPrice"] - df["fairPrice"]).clip(lower=0)
df["ask"] = (df["fairPrice"] - df["impactAskPrice"]).clip(lower=0)
df["PI"] = ((df["bid"] - df["ask"]) / df["indicativeSettlePrice"]).fillna(0.0) + df["fundingRate"]
df = df[["impactBidPrice", "impactAskPrice", "fairPrice", "indicativeSettlePrice", "fundingRate", "PI"]]
与 PI 指数的最新值相比:
url = f"https://www.bitmex.com/api/v1/trade?symbol=.{symbol}PI&count=1&reverse=true"
resp = requests.get(url)
df_pi = pd.DataFrame(resp.json())