Bootstrapping Treasury Curve

''' data is in the format [('2025-01-01', 0.01),] ''' def treasury(data, today=None): if today == None: today = date.today().isoformat() today_date = datetime.fromisoformat(today) tsplit = today.split('-') # Set evaluation date calc_date = ql.Date(int(tsplit[2]), int(tsplit[1]), int(tsplit[0])) ql.Settings.instance().evaluationDate = calc_date # Define common settings calendar = ql.UnitedStates(ql.UnitedStates.GovernmentBond) day_count = ql.ActualActual(ql.ActualActual.ISDA) business_convention = ql.Following face_amount = 100 settlement_days = 0 ''' In QuantLib, you use DepositRateHelper to model Treasury Bills (T-Bills) at the short end of a yield curve because both instruments are fundamentally discount securities. T-Bills do not pay periodic coupons. Instead, they are sold at a discount to face value and mature at par, meaning their yield calculation maps perfectly to the simple compounding and actual/360 day-counting behavior built into DepositRateHelper. ''' # Helpers for a T-bills bill_helpers = [] for key in [x for x in data if ('date' not in x and 'Y' not in x and '.' not in x)]: period = key[:-1] bill_quote = ql.QuoteHandle(ql.SimpleQuote(float(data[key]))) bill_helper = ql.DepositRateHelper( bill_quote, ql.Period(int(period), ql.Months), settlement_days, calendar, business_convention, True, day_count ) bill_helpers.append(bill_helper) pass # Helpers for coupon bonds (Example: 2-year and 5-year Treasury notes) # Assumes you have their par yields bond_helpers = [] for key in [x for x in data if ('date' not in x and 'M' not in x)]: issue_date = calc_date maturity = today_date + relativedelta(years=int(key[:-1])) maturity_date = ql.Date.from_date(maturity) schedule = ql.Schedule( issue_date, maturity_date, ql.Period(ql.Semiannual), calendar, business_convention, business_convention, ql.DateGeneration.Backward, False ) quote = ql.SimpleQuote(100.0) bond_helper = ql.FixedRateBondHelper( ql.QuoteHandle(quote), settlement_days, face_amount, schedule, [float(data[key])], day_count, business_convention ) bond_helpers.append(bond_helper) helpers = bill_helpers + bond_helpers # Combine into a bootstrapped yield curve yield_curve = ql.PiecewiseLogLinearDiscount( calc_date, helpers, day_count ) # Enable daily recalculation yield_curve.enableExtrapolation() return yield_curve