-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move bond instruments to follow upstream hierarchy
- Loading branch information
1 parent
11e6411
commit a165d1a
Showing
24 changed files
with
596 additions
and
481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.