Skip to content

Commit

Permalink
move bond instruments to follow upstream hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasibule committed Nov 22, 2023
1 parent 11e6411 commit a165d1a
Show file tree
Hide file tree
Showing 24 changed files with 596 additions and 481 deletions.
2 changes: 0 additions & 2 deletions quantlib/cashflows/coupon_pricer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ include '../types.pxi'

from cython.operator cimport dereference as deref
from quantlib.cashflow cimport Leg
cimport quantlib.instruments._bonds as _bonds
from quantlib.termstructures.volatility.optionlet.optionlet_volatility_structure cimport OptionletVolatilityStructure
cimport quantlib.termstructures.volatility.optionlet._optionlet_volatility_structure as _ovs
from quantlib.termstructures.volatility.swaption.swaption_vol_structure \
Expand All @@ -16,7 +15,6 @@ from quantlib.time.daycounter cimport DayCounter
from quantlib.quote cimport Quote
from quantlib.quotes.simplequote cimport SimpleQuote
from .coupon_pricer cimport FloatingRateCouponPricer
from quantlib.instruments.bonds cimport Bond
cimport quantlib._cashflow as _cf

cdef class FloatingRateCouponPricer:
Expand Down
72 changes: 72 additions & 0 deletions quantlib/instruments/_bond.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from quantlib.types cimport Natural, Rate, Real, Size

from libcpp.vector cimport vector
from libcpp cimport bool

from .._instrument cimport Instrument
from quantlib.time._calendar cimport BusinessDayConvention, Calendar
from quantlib.time._date cimport Date
from quantlib.time._period cimport Frequency, Period
from quantlib.time._daycounter cimport DayCounter
from quantlib.time._schedule cimport Schedule
from quantlib._cashflow cimport Leg
from quantlib.indexes._ibor_index cimport IborIndex
from quantlib.indexes._inflation_index cimport ZeroInflationIndex
from quantlib.time._schedule cimport DateGeneration
from quantlib.compounding cimport Compounding

cdef extern from 'ql/instruments/bond.hpp' namespace 'QuantLib' nogil:
cdef cppclass Bond(Instrument):
cppclass Price:
enum Type:
Dirty
Clean
Price(Real amount, Type type)
Real amount()
Type type()
Natural settlementDays()
Calendar& calendar()
vector[Real]& notionals()
Real notional(Date d)
const Leg& cashflows()
const Leg& redemptions()
Date startDate() const
Date maturityDate()
Date issueDate()
Date settlementDate(Date d)
bool isTradable(Date d)
Real accruedAmount(Date d) except +


Real cleanPrice() except +
Real dirtyPrice() except +
Real settlementValue() except +

Rate bond_yield 'yield'(
DayCounter& dc,
Compounding comp,
Frequency freq,
Real accuracy, # = 1e-8
Size maxEvaluations, # = 100
Real guess, # = 0.5,
Bond.Price.Type price_type # = Bond.Price.Clean
) except +

Rate bond_yield 'yield'(
Real price,
DayCounter& dc,
Compounding comp,
Frequency freq,
Date settlementDate, # = Date()
Real accuracy, # = 1e-8
Size maxEvaluations, # = 100
Real guess, # = 0.5,
Bond.Price.Type price_type # = Bond.Price.Clean
) except +

Rate nextCouponRate(Date d) const

Rate previousCouponRate(Date d) const

Date nextCashFlowDate(Date d) except +
Date previousCashFlowDate(Date d) except +
140 changes: 0 additions & 140 deletions quantlib/instruments/_bonds.pxd

This file was deleted.

13 changes: 13 additions & 0 deletions quantlib/instruments/bond.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from quantlib.instrument cimport Instrument
from . cimport _bond

cdef extern from "ql/instruments/bond.hpp" namespace "QuantLib::Bond::Price" nogil:
cpdef enum Type:
Dirty
Clean

cdef class BondPrice:
pass

cdef class Bond(Instrument):
cdef inline _bond.Bond* as_ptr(self)
107 changes: 107 additions & 0 deletions quantlib/instruments/bond.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from quantlib.types cimport Real, Size
from . cimport _bond
cimport quantlib.time._date as _date

from cython.operator cimport dereference as deref
from libcpp.vector cimport vector
from libcpp cimport bool
from quantlib.cashflow cimport Leg
from quantlib.compounding cimport Compounding
from quantlib.time.businessdayconvention cimport BusinessDayConvention
from quantlib.time.calendar cimport Calendar
from quantlib.time.date cimport Date, date_from_qldate, Period
from quantlib.time.daycounter cimport DayCounter
from quantlib.time._period cimport Frequency

cdef class Price:
Clean = Type.Clean
Dirty = Type.Dirty

cdef class Bond(Instrument):
""" Base bond class
.. warning::
Most methods assume that the cash flows are stored
sorted by date, the redemption(s) being after any
cash flow at the same date. In particular, if there's
one single redemption, it must be the last cash flow,
"""
def __init__(self):
raise NotImplementedError('Cannot instantiate a Bond. Please use child classes.')

cdef inline _bond.Bond* as_ptr(self):
return <_bond.Bond*>self._thisptr.get()

@property
def settlement_days(self):
return self.as_ptr().settlementDays()

@property
def calendar(self):
cdef Calendar c = Calendar.__new__(Calendar)
c._thisptr = self.as_ptr().calendar()
return c

@property
def start_date(self):
""" Bond start date. """
return date_from_qldate(self.as_ptr().startDate())


@property
def maturity_date(self):
""" Bond maturity date. """
return date_from_qldate(self.as_ptr().maturityDate())

@property
def issue_date(self):
""" Bond issue date. """
return date_from_qldate(self.as_ptr().issueDate())

def settlement_date(self, Date from_date=Date()):
""" Returns the bond settlement date after the given date."""
return date_from_qldate(self.as_ptr().settlementDate(deref(from_date._thisptr)))

@property
def clean_price(self):
""" Bond clean price. """
return self.as_ptr().cleanPrice()

@property
def dirty_price(self):
""" Bond dirty price. """
return self.as_ptr().dirtyPrice()

def bond_yield(self, Real price, DayCounter dc not None,
Compounding comp, Frequency freq,
Date settlement_date=Date(), Real accuracy=1e-08,
Size max_evaluations=100, Real guess=0.5, Type price_type = Price.Clean):
""" Return the yield given a (clean) price and settlement date
The default bond settlement is used if no date is given.
This method is the original Bond.yield method in C++.
Python does not allow us to use the yield statement as a method name.
"""
return self.as_ptr().bond_yield(
price, deref(dc._thisptr), comp,
freq, deref(settlement_date._thisptr),
accuracy, max_evaluations, guess, price_type
)

def accrued_amount(self, Date date=Date()):
""" Returns the bond accrued amount at the given date. """
return self.as_ptr().accruedAmount(deref(date._thisptr))

@property
def cashflows(self):
""" cash flow stream as a Leg """
cdef Leg leg = Leg.__new__(Leg)
leg._thisptr = self.as_ptr().cashflows()
return leg

def notional(self, Date date=Date()):
return self.as_ptr().notional(deref(date._thisptr))
5 changes: 0 additions & 5 deletions quantlib/instruments/bonds.pxd

This file was deleted.

Loading

0 comments on commit a165d1a

Please sign in to comment.