0
$\begingroup$
# Set evaluation date

spotRates = [0.02514, 0.026285, 0.027326299999999998,
         0.0279, 0.029616299999999998, 0.026526,
         0.026028, 0.0258695, 0.025958000000000002,
         0.0261375, 0.026355, 0.0266255,
         0.026898, 0.0271745, 0.02741,
         0.027666, 0.028107000000000004, 0.028412000000000003,
         0.028447, 0.0284165]

spotPeriod = [Period(1, Weeks), Period(1, Months),
          Period(3, Months), Period(6, Months),
          Period(9, Months), Period(1, Years),
          Period(3, Years),Period(5, Years)
          Period(10, Years),Period(15, Years), Period(20, Years),
          Period(30, Years), Period(50, Years)]
$\endgroup$
1
  • $\begingroup$ Thats not a problem I converted the dates into the formats I required in my program. I valued more than 1000 records but few have given me such error. Am confused why it is so ? $\endgroup$
    – Rahul
    Mar 12 '20 at 17:28
1
$\begingroup$

Had to change a few things because you had 20 spot rates but only 13 spot period, and added a bogus fixing, but here is a working example.

import QuantLib as ql
import pandas as pd

todaysDate = ql.Date(13, 9, 2019)
calendar = ql.SouthAfrica()
day_count = ql.Actual365Fixed()
currency = ql.ZARCurrency()

todaysDate = calendar.adjust(todaysDate)
ql.Settings.instance().evaluationDate = todaysDate

spotRates = [0.02514, 0.026285, 0.027326299999999998,
         0.0279, 0.029616299999999998, 0.026526,
         0.026028, 0.0258695, 0.025958000000000002,
         0.0261375, 0.026355, 0.0266255,
         0.026898, 0.0271745, 0.02741,
         0.027666, 0.028107000000000004, 0.028412000000000003,
         0.028447, 0.0284165]

spotPeriod = [ql.Period(1, ql.Weeks), ql.Period(1, ql.Months),
          ql.Period(3, ql.Months), ql.Period(6, ql.Months),
          ql.Period(9, ql.Months), ql.Period(1, ql.Years),
          ql.Period(3, ql.Years),  ql.Period(5, ql.Years),
          ql.Period(10, ql.Years), ql.Period(15, ql.Years), ql.Period(20, ql.Years),
          ql.Period(30, ql.Years), ql.Period(50, ql.Years)]

dates = [calendar.advance(todaysDate, period) for period in spotPeriod]
curve = ql.ZeroCurve(dates, spotRates[:13], day_count, calendar)
yts = ql.YieldTermStructureHandle(curve)
engine = ql.DiscountingSwapEngine(yts)

issue_date = ql.Date(18, 4, 2018)
maturity_date = ql.Date(18, 4, 2028)
fixedRate = .09
index = ql.Jibar(ql.Period('3M'), yts)
fix_payment_frequency = ql.Annual
float_payment_frequency = ql.Quarterly

fixedSchedule = ql.MakeSchedule(issue_date, maturity_date, ql.Period('1Y'), calendar=calendar)
floatSchedule = ql.MakeSchedule(issue_date, maturity_date, ql.Period('3M'), calendar=calendar)
swap = ql.VanillaSwap(
    ql.VanillaSwap.Payer, 100,
    fixedSchedule, fixedRate, day_count,
    floatSchedule, index, 0, index.dayCounter()
    )

index.addFixing(ql.Date(18,7,2019), 0.02)

swap.setPricingEngine(engine)
data = []
for cf in map(ql.as_coupon, swap.leg(1)):
    if cf.date() > todaysDate:
        data.append({
            'accuralStart': cf.accrualStartDate(),
            'accrualEnd': cf.accrualEndDate(),
            'amount': cf.amount(),
            'rate': cf.rate(),
            'discount': yts.discount(cf.date())

        })
pd.DataFrame(data).head()

which would output:

enter image description here

I suspect you were trying to get a discount factor from a date before the curve date.

$\endgroup$
1
  • $\begingroup$ Thanks for your reply. Very neat answer. Secondly, if my valuation date is 13th sep 2019(as above), are we accounting all cash flows for valuation or any accruals needed to be adjusted separately to get the correct valuation ? $\endgroup$
    – Rahul
    Mar 13 '20 at 13:32

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .