Skip to content

Commit

Permalink
allow OvernightIndexedSwap in Swaption
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasibule committed Jul 31, 2024
1 parent ab650eb commit ca58e7c
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 212 deletions.
56 changes: 56 additions & 0 deletions quantlib/instruments/_fixedvsfloatingswap.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from libcpp.vector cimport vector

from quantlib.types cimport Natural, Rate, Real, Spread
from quantlib.handle cimport shared_ptr, optional
from quantlib.time._calendar cimport BusinessDayConvention, Calendar
from quantlib.time._date cimport Date
from quantlib.time._daycounter cimport DayCounter
from quantlib.time._schedule cimport Schedule
from quantlib._cashflow cimport Leg
from ._swap cimport Swap
from .swap cimport Type
from quantlib.indexes._ibor_index cimport IborIndex

cdef extern from 'ql/instruments/fixedvsfloatingswap.hpp' namespace 'QuantLib':
cdef cppclass FixedVsFloatingSwap(Swap):
FixedVsFloating(Type type,
vector[Real] fixed_nominals,
Schedule& fixedSchedule,
Rate fixedRate,
DayCounter& fixedDayCount,
vector[Real] floating_nominals,
Schedule& floatSchedule,
shared_ptr[IborIndex] iborIndex,
Spread spread,
DayCounter& floatingDayCount,
optional[BusinessDayConvention] paymentConvention,
Natural payment_lag, # = 0
Calendar payment_calendar) # = Calendar()

Type type()
Real nominal()
vector[Real] nominals()

vector[Real] fixedNominals()
Schedule& fixedSchedule()
Rate fixedRate()
DayCounter& fixedDayCount()

vector[Real] floatingNominals()
Schedule& floatingSchedule()
shared_ptr[IborIndex]& iborIndex()
Spread spread()
DayCounter& floatingDayCount()

BusinessDayConvention paymentConvention()

Leg& fixedLeg()
Leg& floatingLeg()

Real fixedLegBPS() except +
Real fixedLegNPV() except +
Rate fairRate() except +

Real floatingLegBPS() except +
Real floatingLegNPV() except +
Spread fairSpread() except +
20 changes: 3 additions & 17 deletions quantlib/instruments/_overnightindexedswap.pxd
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

from quantlib.types cimport Natural, Rate, Real, Spread
from quantlib.cashflows.rateaveraging cimport RateAveraging
from libcpp cimport bool
Expand All @@ -10,11 +11,11 @@ from quantlib.time._daycounter cimport DayCounter
from quantlib.indexes._ibor_index cimport OvernightIndex
from quantlib.time._period cimport Frequency

from ._swap cimport Swap
from ._fixedvsfloatingswap cimport FixedVsFloatingSwap
from .swap cimport Type

cdef extern from 'ql/instruments/overnightindexedswap.hpp' namespace 'QuantLib':
cdef cppclass OvernightIndexedSwap(Swap):
cdef cppclass OvernightIndexedSwap(FixedVsFloatingSwap):
OvernightIndexedSwap(Type type,
Real nominal,
const Schedule& schedule,
Expand All @@ -40,27 +41,12 @@ cdef extern from 'ql/instruments/overnightindexedswap.hpp' namespace 'QuantLib':
const Calendar& paymentCalendar, # = Calendar(),
bool telescopicValueDates, # = false,
RateAveraging averagingMethod) # = RateAveraging::Compound);

Type type() const
Real nominal() const
vector[Real] nominals() const

Frequency paymentFrequency()

Rate fixedRate() const
const DayCounter& fixedDayCount()

const shared_ptr[OvernightIndex]& overnightIndex()
Spread spread() const

const Leg& fixedLeg() const
const Leg& overnightLeg() const

RateAveraging averagingMethod() const
Real fixedLegBPS() const
Real fixedLegNPV() const
Real fairRate() const

Real overnightLegBPS() const
Real overnightLegNPV() const
Spread fairSpread() const
7 changes: 4 additions & 3 deletions quantlib/instruments/_swaption.pxd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include '../types.pxi'
from quantlib.handle cimport shared_ptr, optional
from ._vanillaswap cimport VanillaSwap
from ._fixedvsfloatingswap cimport FixedVsFloatingSwap
from ._overnightindexedswap cimport OvernightIndexedSwap
from ._option cimport Option
from ._exercise cimport Exercise
from .swap cimport Type as SwapType
Expand All @@ -12,7 +13,7 @@ from .swaption cimport Type, Method
cdef extern from 'ql/instruments/swaption.hpp' namespace 'QuantLib':

cdef cppclass Swaption(Option):
Swaption(const shared_ptr[VanillaSwap]& swap,
Swaption(const shared_ptr[FixedVsFloatingSwap]& swap,
const shared_ptr[Exercise]& exercise,
Type delivery, # = Settlement::Physical
Method settlementMethod) # Settlement::PhysicalOTC
Expand All @@ -28,4 +29,4 @@ cdef extern from 'ql/instruments/swaption.hpp' namespace 'QuantLib':
VolatilityType type,# = ShiftedLognormal,
Real displacement)# = 0.0)
SwapType type() const
const shared_ptr[VanillaSwap]& underlyingSwap()
const shared_ptr[FixedVsFloatingSwap]& underlyingSwap()
36 changes: 3 additions & 33 deletions quantlib/instruments/_vanillaswap.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@
FOR A PARTICULAR PURPOSE. See the license for more details.
"""

include '../types.pxi'

from libcpp.vector cimport vector
from libcpp cimport bool
from quantlib.types cimport Rate, Real, Spread

from quantlib.handle cimport shared_ptr, optional
from .._instrument cimport Instrument
from quantlib.time._calendar cimport BusinessDayConvention, Calendar
from quantlib.time._date cimport Date
from quantlib.time._daycounter cimport DayCounter
from quantlib.time._schedule cimport Schedule
from quantlib._cashflow cimport Leg
from ._swap cimport Swap
from ._fixedvsfloatingswap cimport FixedVsFloatingSwap
from .swap cimport Type
from quantlib.indexes._ibor_index cimport IborIndex

cdef extern from 'ql/instruments/vanillaswap.hpp' namespace 'QuantLib':
cdef cppclass VanillaSwap(Swap):
cdef cppclass VanillaSwap(FixedVsFloatingSwap):
VanillaSwap(Type type,
Real nominal,
Schedule& fixedSchedule,
Expand All @@ -35,28 +30,3 @@ cdef extern from 'ql/instruments/vanillaswap.hpp' namespace 'QuantLib':
Spread spread,
DayCounter& floatingDayCount,
optional[BusinessDayConvention] paymentConvention)

Type type()
Real nominal()

Schedule& fixedSchedule()
Rate fixedRate()
DayCounter& fixedDayCount()

Schedule& floatingSchedule()
shared_ptr[IborIndex]& iborIndex()
Spread spread()
DayCounter& floatingDayCount()

BusinessDayConvention paymentConvention()

Leg& fixedLeg()
Leg& floatingLeg()

Real fixedLegBPS() except +
Real fixedLegNPV() except +
Rate fairRate() except +

Real floatingLegBPS() except +
Real floatingLegNPV() except +
Spread fairSpread() except +
5 changes: 5 additions & 0 deletions quantlib/instruments/fixedvsfloatingswap.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .swap cimport Swap
from . cimport _fixedvsfloatingswap

cdef class FixedVsFloatingSwap(Swap):
cdef inline _fixedvsfloatingswap.FixedVsFloatingSwap* get_ptr(self)
92 changes: 92 additions & 0 deletions quantlib/instruments/fixedvsfloatingswap.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from quantlib.cashflows.ibor_coupon cimport IborLeg
from quantlib.time.schedule cimport Schedule
from quantlib.time.daycounter cimport DayCounter
from quantlib.time._daycounter cimport DayCounter as QlDayCounter
from quantlib.cashflows.fixed_rate_coupon cimport FixedRateLeg

cdef class FixedVsFloatingSwap(Swap):


cdef inline _fixedvsfloatingswap.FixedVsFloatingSwap* get_ptr(self):
return <_fixedvsfloatingswap.FixedVsFloatingSwap*>self._thisptr.get()

@property
def fair_rate(self):
return self.get_ptr().fairRate()

@property
def fair_spread(self):
return self.get_ptr().fairSpread()

@property
def nominal(self):
return self.get_ptr().nominal()

@property
def nominals(self):
return self.get_ptr().nominals()

@property
def type(self):
return self.get_ptr().type()

@property
def fixed_rate(self):
return self.get_ptr().fixedRate()

@property
def fixed_schedule(self):
cdef Schedule sched = Schedule.__new__(Schedule)
sched._thisptr = self.get_ptr().fixedSchedule()
return sched

@property
def fixed_day_count(self):
cdef DayCounter dc = DayCounter.__new__(DayCounter)
dc._thisptr = new QlDayCounter(self.get_ptr().fixedDayCount())
return dc

@property
def floating_schedule(self):
cdef Schedule sched = Schedule.__new__(Schedule)
sched._thisptr = self.get_ptr().floatingSchedule()
return sched

@property
def spread(self):
return self.get_ptr().spread()

@property
def floating_day_count(self):
cdef DayCounter dc = DayCounter.__new__(DayCounter)
dc._thisptr = new QlDayCounter(self.get_ptr().floatingDayCount())
return dc

@property
def fixed_leg(self):
cdef FixedRateLeg leg = FixedRateLeg.__new__(FixedRateLeg)
leg._thisptr = self.get_ptr().fixedLeg()
return leg

@property
def fixed_leg_NPV(self):
return self.get_ptr().fixedLegNPV()

@property
def fixed_leg_BPS(self):
return self.get_ptr().fixedLegBPS()

@property
def floating_leg(self):
cdef IborLeg leg = IborLeg.__new__(IborLeg)
leg._thisptr = self.get_ptr().floatingLeg()
return leg

@property
def floating_leg_NPV(self):
return self.get_ptr().floatingLegNPV()


@property
def floating_leg_BPS(self):
return self.get_ptr().floatingLegBPS()
4 changes: 2 additions & 2 deletions quantlib/instruments/overnightindexedswap.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .swap cimport Swap
from .fixedvsfloatingswap cimport FixedVsFloatingSwap

cdef class OvernightIndexedSwap(Swap):
cdef class OvernightIndexedSwap(FixedVsFloatingSwap):
pass
52 changes: 3 additions & 49 deletions quantlib/instruments/overnightindexedswap.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ from quantlib.types cimport Real
from cython.operator cimport dereference as deref
from libcpp cimport bool
from libcpp.vector cimport vector
from quantlib.cashflows.fixed_rate_coupon cimport FixedRateLeg
from quantlib.cashflows.overnight_indexed_coupon cimport OvernightLeg
from quantlib.cashflows.rateaveraging cimport RateAveraging
from quantlib.cashflows.rateaveraging import RateAveraging as PyRateAveraging
from quantlib.types cimport Natural, Rate, Real, Spread
from quantlib.indexes.ibor_index cimport OvernightIndex
from quantlib.time.businessdayconvention cimport BusinessDayConvention, Following
from quantlib.time.calendar cimport Calendar
from quantlib.time.schedule cimport Schedule
from quantlib.time.daycounter cimport DayCounter
from quantlib.time.schedule cimport Schedule
from quantlib.handle cimport make_shared, static_pointer_cast
from .swap cimport Type
from . cimport _overnightindexedswap as _ois
Expand All @@ -24,7 +23,7 @@ cimport quantlib.time._daycounter as _dc
cdef inline _ois.OvernightIndexedSwap* get_OIS(OvernightIndexedSwap self):
return <_ois.OvernightIndexedSwap*>self._thisptr.get()

cdef class OvernightIndexedSwap(Swap):
cdef class OvernightIndexedSwap(FixedVsFloatingSwap):
"""Overnight indexed swap: fix vs compounded overnight rate"""
def __init__(self, Type swap_type, nominal, Schedule schedule,
Rate fixed_rate, DayCounter fixed_dc, OvernightIndex overnight_index,
Expand All @@ -35,6 +34,7 @@ cdef class OvernightIndexedSwap(Swap):
cdef vector[double] nominals
cdef double n


if isinstance(nominal, float):
self._thisptr = static_pointer_cast[Instrument](
make_shared[_ois.OvernightIndexedSwap](
Expand All @@ -56,32 +56,10 @@ cdef class OvernightIndexedSwap(Swap):
)
)

@property
def type(self):
return get_OIS(self).type()

@property
def nominal(self):
return get_OIS(self).nominal()

@property
def nominals(self):
return get_OIS(self).nominals()

@property
def payment_frequency(self):
return get_OIS(self).paymentFrequency()

@property
def fixed_rate(self):
return get_OIS(self).fixedRate()

@property
def fixed_day_count(self):
cdef DayCounter d = DayCounter.__new__(DayCounter)
d._thisptr = new _dc.DayCounter(get_OIS(self).fixedDayCount())
return d

@property
def overnight_index(self):
cdef OvernightIndex index = OvernightIndex.__new__(OvernightIndex)
Expand All @@ -92,25 +70,6 @@ cdef class OvernightIndexedSwap(Swap):
def averaging_method(self):
return PyRateAveraging(get_OIS(self).averagingMethod())

@property
def fixed_leg_BPS(self):
return get_OIS(self).fixedLegBPS()

@property
def fixed_leg_NPV(self):
return get_OIS(self).fixedLegNPV()

@property
def fair_rate(self):
return get_OIS(self).fairRate()

@property
def spread(self):
return get_OIS(self).spread()

@property
def fair_spread(self):
return get_OIS(self).fairSpread()

@property
def overnight_leg_BPS(self):
Expand All @@ -120,11 +79,6 @@ cdef class OvernightIndexedSwap(Swap):
def overnight_leg_NPV(self):
return get_OIS(self).overnightLegNPV()

@property
def fixed_leg(self):
cdef FixedRateLeg leg = FixedRateLeg.__new__(FixedRateLeg)
leg._thisptr = get_OIS(self).fixedLeg()
return leg

@property
def overnight_leg(self):
Expand Down
Loading

0 comments on commit ca58e7c

Please sign in to comment.