国库券收益率转贴现率

国库券收益率转贴现率 货币市场收益率 / 债券等价收益率 → 贴现率
根据国库券收益率(货币市场收益率或债券等价收益率)、结算日与到期日,换算对应的贴现率。
格式 yyyy-mm-dd
格式 yyyy-mm-dd
计算结果
点击「计算」查看输出
code 计算逻辑代码
以下为「国库券收益率转贴现率」QuantLib 代码示例。

# 依赖版本:
# python==3.9
# QuantLib==1.43
import traceback
from datetime import datetime
import QuantLib as ql
from fastapi import Query


# ============================================================
# 工具函数
# ============================================================


def _parse_date(s: str) -> ql.Date:
    d = datetime.strptime(s.strip(), "%Y-%m-%d")
    return ql.Date(d.day, d.month, d.year)


# ============================================================
# 核心计算函数
# ============================================================
def qjquant_calu_tbillyield2disc(
    yield_val: float = Query(...),
    settle_date: str = Query(...),
    maturity_date: str = Query(...),
    type: int = Query(1),          # 1=money market, 2=bond-equivalent
):
    try:
        # ---------- 参数校验 ----------
        if yield_val < 0:
            return {"ok": False, "msg": "收益率不能为负"}
        if type not in (1, 2):
            return {"ok": False, "msg": f"不支持的 Type: {type},仅支持 1/2"}

        settle_qd   = _parse_date(settle_date)
        maturity_qd = _parse_date(maturity_date)

        if settle_qd >= maturity_qd:
            return {"ok": False, "msg": "结算日必须早于到期日"}

        # ---------- 实际天数 ----------
        days = ql.Actual360().dayCount(settle_qd, maturity_qd)
        if days <= 0:
            return {"ok": False, "msg": "到期天数异常"}

        FACE = 100.0

        # ---------- 由收益率求价格 ----------
        if type == 1:
            # 货币市场收益率,Actual/360
            price = FACE / (1.0 + yield_val * days / 360.0)
        else:
            # 债券等价收益率,Actual/365
            if days <= 182:
                # 短期:Price = 100 / (1 + BEY × days/365)
                price = FACE / (1.0 + yield_val * days / 365.0)
            else:
                # 长期 T-Bill BEY 价格公式(与 tbillyield 的反向一致)
                x = days / 365.0
                # Price = 100 / [(1 + BEY/2)(1 + BEY/2 × (2x - 1))]
                price = FACE / ((1.0 + yield_val / 2.0) *
                                (1.0 + yield_val / 2.0 * (2.0 * x - 1.0)))

        if price <= 0:
            return {"ok": False, "msg": "收益率过高导致价格非正"}

        # ---------- 由价格求贴现率(Actual/360) ----------
        discount = (1.0 - price / FACE) * 360.0 / days

        return {
            "ok": True,
            "discount": round(discount, 4),
        }

    except ValueError as ve:
        traceback.print_exc()
        return {"ok": False, "msg": f"参数错误: {ve}"}
    except Exception as e:
        traceback.print_exc()
        return {"ok": False, "msg": f"未知错误: {e}"}


# ============================================================
# 可复制运行的示例
# ============================================================
if __name__ == "__main__":
    print("=" * 60)
    print("国库券收益率转贴现率 tbillyield2disc 示例")
    print("=" * 60)

    # 示例 1:货币市场收益率(type=1),短期 180 天
    r1 = qjquant_calu_tbillyield2disc(
        yield_val=0.0497,
        settle_date="2002-10-01",
        maturity_date="2003-03-31",
        type=1,
    )
    print("\n[示例1] type=1 货币市场收益率 5%(短期 180 天)")
    print("  结果:", r1)

    # 示例 2:债券等价收益率(type=2),短期 180 天
    r2 = qjquant_calu_tbillyield2disc(
        yield_val=0.05,
        settle_date="2024-01-02",
        maturity_date="2024-07-01",
        type=2,
    )
    print("\n[示例2] type=2 债券等价收益率 5%(短期 180 天)")
    print("  结果:", r2)

    # 示例 3:债券等价收益率(type=2),长期 364 天
    r3 = qjquant_calu_tbillyield2disc(
        yield_val=0.05,
        settle_date="2024-01-02",
        maturity_date="2024-12-31",
        type=2,
    )
    print("\n[示例3] type=2 债券等价收益率 5%(长期 364 天)")
    print("  结果:", r3)