Treasury

Overview

Object Definitions

@dataclass class TreasuryCurve(BootstrapCurve): data:list[Any] = Field(default_factory=list) date: str | None = None spreads:list[Any]|None = None pass

data is a list of two items:

  • Period - given as a number plus a period indicator, where 'M' indicates months, and 'Y' indicates years.
  • Rate = a float indicating the rate

Sample Code

import bootstrap as bt item,stack = ty.current() treasury_curve1 = bt.TreasuryCurve(data = [['6M',0.03],['1Y',0.04],['10Y',0.05]])

Sample Data Desktop Code

The following code shows how to build a treasury curve from the data downloaded from the US Treasury website. (see data desktop)
import bootstrap as bt import treasury as ty item,stack = ty.current() treasury_curve1 = cv.TreasuryCurve(data = [[x['period'], x['value']/100] for x in stack])

Native Quantlib Objects

You can bypass the pydantic data classes and get a native Quantlib object, using the treasury function in the bootstrap.py file.
def treasury(data, today=None):

Script

The following script converts a Treasu
''' data is in the format [('2025-01-01', 0.01),] ''' def treasury(data, today=None): if len(data)>0 and isinstance(data[0], list): data = [(x[0],x[1]) for x in data] if today == None: today = date.today().isoformat() today_date = datetime.fromisoformat(today) tsplit = today.split('-') calc_date = ql.Date(int(tsplit[2]), int(tsplit[1]), int(tsplit[0])) # 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)]: data = [[x[0], x[1]] for x in data] for item in [x for x in data if ('M' in x[0] and '.' not in x[0])]: #period = key[:-1] period = item[0] bill_quote = ql.QuoteHandle(ql.SimpleQuote(item[1])) bill_helper = ql.DepositRateHelper( bill_quote, ql.Period(int(period[:-1]), 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)]: for item in [x for x in data if ('Y' in x[0])]: issue_date = calc_date period = item[0] maturity = today_date + relativedelta(years=int(period[:-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(item[1])], 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