pythonquantlib

Python QuantLib cannot bootstrap - wrong number or type of arguments


I have been trying to bootstrap par yields from South African IRS with QuantLib. I am new to the library and been having problems running the code below:

import QuantLib as ql
import pandas as pd

calendar = ql.SouthAfrica()

day_count = ql.Actual365Fixed()
settlementDays = 0
today = ql.Date(28, 2, 2023)
spot = ql.SouthAfrica().advance(today, 0, ql.Days)


index = ql.Jibar(ql.Period(3, ql.Months))

term = [i for i in range(10)]
rates = [0.0795, 0.0808,0.0828,0.0851, 0.0873, 0.08925, 0.09085, 0.0921, 0.0941, 0.09535]
curve = pd.DataFrame(list(zip(term, rates)), columns = ["Term", "Rates"])

deposit_helpers = ql.DepositRateHelper(ql.QuoteHandle(ql.SimpleQuote(curve["Rates"][0])),
                                       ql.Period(3, ql.Months),
                                       settlementDays,
                                       calendar,
                                       ql.Unadjusted,
                                       False,
                                       day_count
                                      )
                     
fra_start = [3,6,9,12]
fra_end = [6,9,12,15]
fra_mid = [0.079, 0.08005, 0.0802, 0.07985]

fra_curve = pd.DataFrame(list(zip(fra_start, fra_end, fra_mid)), columns = ["Start", "End", "Rates"])
                     
FRAs = [ql.FraRateHelper(ql.QuoteHandle(ql.SimpleQuote(mid/100)),
                         start,
                         end,
                        settlementDays,
                        calendar, 
                        ql.ModifiedFollowing,
                        False,
                        day_count) for mid, start, end in zip(fra_curve["Rates"], fra_curve["Start"], fra_curve["End"])]
                     

    
Swap_helpers = [ql.SwapRateHelper(ql.QuoteHandle(ql.SimpleQuote(mid/100)),
                           ql.Period(start, ql.Years),
                           calendar,
                           ql.Quarterly,
                           ql.ModifiedFollowing,
                           day_count,
                           index) for mid, start in zip(curve["Rates"][1:], curve["Term"][1:])]



helpers = []
helpers.append(deposit_helpers)
helpers.append(FRAs)
helpers.append(Swap_helpers)


curve_yield = ql.PiecewiseFlatForward(spot,helpers, day_count)

When I run this code, it gives me

TypeError: Wrong number or type of arguments for overloaded function 'new_PiecewiseFlatForward'

I tried everything I could think of but had no success. I suspect the problem lies in the helpers, but I can't quite figure out what is the problem. Can anyone help me?


Solution

  • As written, your helpers is a list of lists of helpers, not a list of helpers. This will work:

    helpers = []
    helpers.append(deposit_helpers) # add a single helper
    helpers += FRAs                 # add a list
    helpers += Swap_helpers         # add a list
    

    At that point, the curve will tell you that you have more than one helper with the same maturity (I'm guessing the 9x12 FRA and the 1-year swap) and you'll have to choose which one to keep and which one to discard.