QuantLib.jl icon indicating copy to clipboard operation
QuantLib.jl copied to clipboard

Price bond with yield

Open Skumin opened this issue 4 years ago • 0 comments

I'm trying to price a fixed-rate coupon bond using the bond's yield. The code I'm using is the following:

using QuantLib, Dates

# Params
settlement_date = Date(2015, 1, 5)
maturity_date = Date(2025, 1, 1)
issue_date = Date(2010, 1, 1)
coupon_rate = 5 / 100
yild = 5 / 100
face = 100.0

# Run
set_eval_date!(settings, settlement_date)

fixed_schedule = QuantLib.Time.Schedule(issue_date, maturity_date, QuantLib.Time.TenorPeriod(QuantLib.Time.Semiannual()), QuantLib.Time.Unadjusted(), QuantLib.Time.Unadjusted(), QuantLib.Time.DateGenerationBackwards(), false, QuantLib.Time.USGovernmentBondCalendar());
ts = FlatForwardTermStructure(0, QuantLib.Time.USGovernmentBondCalendar(), Quote(yild), QuantLib.Time.ISMAActualActual(), CompoundedCompounding() , QuantLib.Time.Semiannual());

bond = FixedRateBond(0, face, fixed_schedule, coupon_rate, QuantLib.Time.ISMAActualActual(), QuantLib.Time.ModifiedFollowing(), face, issue_date, fixed_schedule.cal, DiscountingBondEngine(ts));
npv(bond)

This returns the following error: ERROR: negative time not allowed! Time fraction is -4.5. If I re-run the last two lines, the code works, however, and I get a price of 100. Am I doing something wrong in the first run of the code?

Skumin avatar Nov 12 '20 12:11 Skumin