Do we have any direct method to get the 'last' and 'next' cash flow date and amount from the date of valuation in Quantlib Python using fixed leg amt or floating leg amt, day counter,valuation date , notional,Index,payment frequency etc. Please suggest.
2 Answers
I don't think there is a direct method but you can easily implement that logic by extending the class you want.
Here is an example:
import QuantLib as ql
today = ql.Date().todaysDate()
calendar = ql.TARGET()
start = calendar.advance(today, 2, ql.Days)
maturity = calendar.advance(start, ql.Period('5y'))
fixedSchedule = ql.MakeSchedule(start, maturity, ql.Period('1Y'))
floatSchedule = ql.MakeSchedule(start, maturity, ql.Period('6M'))
class MyVanillaSwap(ql.VanillaSwap):
def fixedLegCashFlows(self):
return list(map(ql.as_coupon, self.fixedLeg()))
def nextFixed(self):
nextCashFlow = list(filter(lambda x: x.date() > today, self.fixedLegCashFlows()))[0]
return (nextCashFlow.date(), nextCashFlow.amount())
swap = MyVanillaSwap(
ql.VanillaSwap.Payer, 250e6,
fixedSchedule, 0.0195, ql.Thirty360(),
floatSchedule, ql.Euribor6M(), 0, ql.Actual360()
)
swap.nextFixed()
Output would be:
(Date(12,3,2021), 4875000.000000019)
Methods nextCashFlow
and previousCashFlow
were added to the CashFlows
class in Python in a recent pull request (see https://github.com/lballabio/QuantLib-SWIG/pull/255) and will be available in release 1.19. Once it's published (or earlier if you can compile the current master from GitHub), you'll be able to write
CashFlows.nextCashFlow(swap.fixedLeg())