1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Using Quantlib to calculate the Zspread of a treasury bond is returning a type...

Discussão em 'Python' iniciado por Stack, Outubro 7, 2024.

  1. Stack

    Stack Membro Participativo

    This is the relevant part of my code:

    def calculate_z_spread_ql(market_price, coupon_rate, maturity_date, yield_curve_handle, issue_date, leg):
    """
    Calculate Z-spread using QuantLib with specified calendar and day count.
    """
    calendar = ql.UnitedStates(ql.UnitedStates.GovernmentBond)
    settlement_days = 2
    face_value = 100
    day_count = ql.ActualActual(ql.ActualActual.Bond)


    #maturity_ql_date = ql.Date(maturity_date.day, maturity_date.month, maturity_date.year)
    tenor = ql.Period(ql.Semiannual)

    coupons = [coupon_rate]


    schedule = ql.Schedule(issue_date, maturity_date, tenor, calendar,
    ql.Unadjusted, ql.Unadjusted, ql.DateGeneration.Backward, False)
    bond = ql.FixedRateBond(settlement_days, face_value, schedule, coupons, day_count)
    bond.setPricingEngine(ql.DiscountingBondEngine(yield_curve_handle))

    settlement_date = ql.Settings.instance().evaluationDate

    includeSettlementDateFlows = True

    #Error here, not sure why, all parameters are set up properly

    z_spread = ql.CashFlows.zSpread(
    leg, # Cash flows of the bond
    market_price, # Market price of the bond
    yield_curve_handle, # Yield curve handle
    day_count, # Day count convention
    ql.Compounded, # Compounding method (optional, default: Simple)
    ql.Semiannual, # Frequency of compounding (optional, default: Annual)
    includeSettlementDateFlows, # Include settlement date flows (optional, default: False)
    settlement_date
    ) * 10000 # Convert to basis points

    return z_spread

    ################################################ Main script ######################################################################

    isin = 'US912810TX63' # Example 30yr ISIN

    #global evaluation date is set to today
    today = ql.Date().todaysDate()
    ql.Settings.instance().evaluationDate = today

    market_price, coupon_rate, maturity_date, issue_date, leg = fetch_bond_details_and_leg(isin)

    maturities, yields = fetch_current_SOFR_yields()


    yield_curve_handle = setup_quantlib_yield_curve(maturities, yields)

    #This is for debugging only, curve set properly
    print('################### cf in leg #######################')
    for cf in leg: # Directly iterate over the leg to print details of each cash flow
    print(f"Cash flow date: {cf.date()}, amount: {cf.amount()}")

    print('################### maturities #######################')
    print(maturities)
    print('################### yields #######################')
    print(yields)
    print('################# yield_curve_handle #########################')
    print_curve_details(yield_curve_handle)

    print('################# z-spread calculations #########################')

    z_spread = calculate_z_spread_ql(market_price, coupon_rate, maturity_date, yield_curve_handle, issue_date, leg)


    What am I misssing?

    I printed all the relevant inputs to the quantlib zpsread function as shown above and still something's not working in either the type or number of arguments, yet I think I set everything up correctly. ChatGPT not figuring it out either.

    Here's the output with errors:

    ################### cf in leg #######################
    Cash flow date: August 15th, 2024, amount: 2.124999999999999
    Cash flow date: February 18th, 2025, amount: 2.124999999999999
    Cash flow date: August 15th, 2025, amount: 2.124999999999999
    Cash flow date: February 17th, 2026, amount: 2.124999999999999
    Cash flow date: August 17th, 2026, amount: 2.124999999999999
    Cash flow date: February 16th, 2027, amount: 2.124999999999999
    Cash flow date: August 16th, 2027, amount: 2.124999999999999
    Cash flow date: February 15th, 2028, amount: 2.124999999999999
    Cash flow date: August 15th, 2028, amount: 2.124999999999999
    Cash flow date: February 15th, 2029, amount: 2.124999999999999
    Cash flow date: August 15th, 2029, amount: 2.124999999999999
    Cash flow date: February 15th, 2030, amount: 2.124999999999999
    Cash flow date: August 15th, 2030, amount: 2.124999999999999
    Cash flow date: February 18th, 2031, amount: 2.124999999999999
    Cash flow date: August 15th, 2031, amount: 2.124999999999999
    Cash flow date: February 17th, 2032, amount: 2.124999999999999
    Cash flow date: August 16th, 2032, amount: 2.124999999999999
    Cash flow date: February 15th, 2033, amount: 2.124999999999999
    Cash flow date: August 15th, 2033, amount: 2.124999999999999
    Cash flow date: February 15th, 2034, amount: 2.124999999999999
    Cash flow date: August 15th, 2034, amount: 2.124999999999999
    Cash flow date: February 15th, 2035, amount: 2.124999999999999
    Cash flow date: August 15th, 2035, amount: 2.124999999999999
    Cash flow date: February 15th, 2036, amount: 2.124999999999999
    Cash flow date: August 15th, 2036, amount: 2.124999999999999
    Cash flow date: February 17th, 2037, amount: 2.124999999999999
    Cash flow date: August 17th, 2037, amount: 2.124999999999999
    Cash flow date: February 16th, 2038, amount: 2.124999999999999
    Cash flow date: August 16th, 2038, amount: 2.124999999999999
    Cash flow date: February 15th, 2039, amount: 2.124999999999999
    Cash flow date: August 15th, 2039, amount: 2.124999999999999
    Cash flow date: February 15th, 2040, amount: 2.124999999999999
    Cash flow date: August 15th, 2040, amount: 2.124999999999999
    Cash flow date: February 15th, 2041, amount: 2.124999999999999
    Cash flow date: August 15th, 2041, amount: 2.124999999999999
    Cash flow date: February 18th, 2042, amount: 2.124999999999999
    Cash flow date: August 15th, 2042, amount: 2.124999999999999
    Cash flow date: February 17th, 2043, amount: 2.124999999999999
    Cash flow date: August 17th, 2043, amount: 2.124999999999999
    Cash flow date: February 16th, 2044, amount: 2.124999999999999
    Cash flow date: August 15th, 2044, amount: 2.124999999999999
    Cash flow date: February 15th, 2045, amount: 2.124999999999999
    Cash flow date: August 15th, 2045, amount: 2.124999999999999
    Cash flow date: February 15th, 2046, amount: 2.124999999999999
    Cash flow date: August 15th, 2046, amount: 2.124999999999999
    Cash flow date: February 15th, 2047, amount: 2.124999999999999
    Cash flow date: August 15th, 2047, amount: 2.124999999999999
    Cash flow date: February 18th, 2048, amount: 2.124999999999999
    Cash flow date: August 17th, 2048, amount: 2.124999999999999
    Cash flow date: February 16th, 2049, amount: 2.124999999999999
    Cash flow date: August 16th, 2049, amount: 2.124999999999999
    Cash flow date: February 15th, 2050, amount: 2.124999999999999
    Cash flow date: August 15th, 2050, amount: 2.124999999999999
    Cash flow date: February 15th, 2051, amount: 2.124999999999999
    Cash flow date: August 15th, 2051, amount: 2.124999999999999
    Cash flow date: February 15th, 2052, amount: 2.124999999999999
    Cash flow date: August 15th, 2052, amount: 2.124999999999999
    Cash flow date: February 18th, 2053, amount: 2.124999999999999
    Cash flow date: August 15th, 2053, amount: 2.124999999999999
    Cash flow date: February 17th, 2054, amount: 2.124999999999999
    Cash flow date: February 17th, 2054, amount: 100.0
    ################### maturities #######################
    [1, 2, 3, 5, 7, 10, 20, 30]
    ################### yields #######################
    [0.050895, 0.046245, 0.043525, 0.040827, 0.039832, 0.03928, 0.038740000000000004, 0.036802]
    ################# yield_curve_handle #########################
    Reference Date: March 17th, 2025
    Year 1: Zero Rate = 0.0462, Discount Factor = 0.9548
    Year 2: Zero Rate = 0.0435, Discount Factor = 0.9166
    Year 3: Zero Rate = 0.0422, Discount Factor = 0.8811
    Year 4: Zero Rate = 0.0408, Discount Factor = 0.8493
    Year 5: Zero Rate = 0.0403, Discount Factor = 0.8174
    Year 6: Zero Rate = 0.0398, Discount Factor = 0.7874
    Year 7: Zero Rate = 0.0396, Discount Factor = 0.7576
    Year 8: Zero Rate = 0.0395, Discount Factor = 0.7293
    Year 9: Zero Rate = 0.0393, Discount Factor = 0.7022
    Year 10: Zero Rate = 0.0392, Discount Factor = 0.6755
    Year 11: Zero Rate = 0.0392, Discount Factor = 0.6499
    Year 12: Zero Rate = 0.0391, Discount Factor = 0.6254
    Year 13: Zero Rate = 0.0391, Discount Factor = 0.6018
    Year 14: Zero Rate = 0.0390, Discount Factor = 0.5792
    Year 15: Zero Rate = 0.0390, Discount Factor = 0.5575
    Year 16: Zero Rate = 0.0389, Discount Factor = 0.5366
    Year 17: Zero Rate = 0.0388, Discount Factor = 0.5166
    Year 18: Zero Rate = 0.0388, Discount Factor = 0.4974
    Year 19: Zero Rate = 0.0387, Discount Factor = 0.4790
    Year 20: Zero Rate = 0.0385, Discount Factor = 0.4626
    Year 21: Zero Rate = 0.0384, Discount Factor = 0.4469
    Year 22: Zero Rate = 0.0382, Discount Factor = 0.4319
    Year 23: Zero Rate = 0.0380, Discount Factor = 0.4176
    Year 24: Zero Rate = 0.0378, Discount Factor = 0.4039
    Year 25: Zero Rate = 0.0376, Discount Factor = 0.3909
    Year 26: Zero Rate = 0.0374, Discount Factor = 0.3783
    Year 27: Zero Rate = 0.0372, Discount Factor = 0.3664
    Year 28: Zero Rate = 0.0370, Discount Factor = 0.3549
    Year 29: Zero Rate = 0.0368, Discount Factor = 0.3440
    ################# z-spread calculations #########################
    Traceback (most recent call last):
    File "H:\Python_KS\ZSpreadTest_QuantLib_Test.py", line 188, in <module>
    z_spread = calculate_z_spread_ql(market_price, coupon_rate, maturity_date, yield_curve_handle, issue_date, leg)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "H:\Python_KS\ZSpreadTest_QuantLib_Test.py", line 143, in calculate_z_spread_ql
    z_spread = ql.CashFlows.zSpread(
    ^^^^^^^^^^^^^^^^^^^^^
    File "C:\ProgramData\anaconda3\envs\Khalil_Python\Lib\site-packages\QuantLib\QuantLib.py", line 15020, in zSpread
    return _QuantLib.CashFlows_zSpread(*args)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: Wrong number or type of arguments for overloaded function 'CashFlows_zSpread'.
    Possible C/C++ prototypes are:
    CashFlows::zSpread(Leg const &,Real,ext::shared_ptr< YieldTermStructure > const &,DayCounter const &,Compounding,Frequency,bool,Date,Date,Real,Size,Rate)
    CashFlows::zSpread(Leg const &,Real,ext::shared_ptr< YieldTermStructure > const &,DayCounter const &,Compounding,Frequency,bool,Date,Date,Real,Size)
    CashFlows::zSpread(Leg const &,Real,ext::shared_ptr< YieldTermStructure > const &,DayCounter const &,Compounding,Frequency,bool,Date,Date,Real)
    CashFlows::zSpread(Leg const &,Real,ext::shared_ptr< YieldTermStructure > const &,DayCounter const &,Compounding,Frequency,bool,Date,Date)
    CashFlows::zSpread(Leg const &,Real,ext::shared_ptr< YieldTermStructure > const &,DayCounter const &,Compounding,Frequency,bool,Date)
    CashFlows::zSpread(Leg const &,Real,ext::shared_ptr< YieldTermStructure > const &,DayCounter const &,Compounding,Frequency,bool)

    Continue reading...

Compartilhe esta Página