Example Pricing a Swap in QuantLib Java
Example Pricing a Swap in QuantLib Java
This example is modelled after the QuantLib Python swap pricing example:
package demo;
/*
Copyright (C) 2024 BN Algorithms Ltd
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
import java.util.ArrayList;
import org.junit.jupiter.api.Test;
import org.quantlib.*;
public class IRSwapExample {
static public final Date today = new Date(5, Month.June, 2020);
public void main() throws Exception {
var calendar = new TARGET();
var todaysDate = new Date(6, Month.November, 2001);
.instance().setEvaluationDate(today);
Settingsvar settlementDate = new Date(8, Month.November, 2001);
[] = {
Period maturitiesnew Period(2, TimeUnit.Years),
new Period(3, TimeUnit.Years),
new Period(5, TimeUnit.Years),
new Period(10,TimeUnit.Years),
new Period(15, TimeUnit.Years)
};
[] = {
SimpleQuote quotesnew SimpleQuote(0.037125),
new SimpleQuote(0.0398),
new SimpleQuote(0.0443),
new SimpleQuote(0.05165),
new SimpleQuote(0.055175),
};
var discountTermStructure =
new YieldTermStructureHandle(new FlatForward(settlementDate,
0.04,
new Actual360()));
ArrayList<RateHelper> swapHelpers = new ArrayList<RateHelper>();
for (int i=0; i<quotes.length; ++i) {
// These are for defining the helpers
var settlementDays = 2;
var fixedLegFrequency = Frequency.Annual;
var fixedLegTenor = new Period(1, TimeUnit.Years);
var fixedLegAdjustment = BusinessDayConvention.Unadjusted;
var fixedLegDayCounter = new Thirty360(Thirty360.Convention.BondBasis);
var floatingLegFrequency = Frequency.Quarterly;
var floatingLegTenor = new Period(3, TimeUnit.Months);
var floatingLegAdjustment = BusinessDayConvention.ModifiedFollowing;
.add(new SwapRateHelper(new QuoteHandle(quotes[i]),
swapHelpers[i],
maturities,
calendar,
fixedLegFrequency,
fixedLegAdjustment,
fixedLegDayCounternew Euribor3M(),
new QuoteHandle(),
new Period("0D"),
));
discountTermStructure}
var forecastTermStructure = new RelinkableYieldTermStructureHandle();
var swapCurve = new PiecewiseFlatForward(settlementDate,
new RateHelperVector(swapHelpers),
new Actual360());
var swapEngine = new DiscountingSwapEngine(discountTermStructure);
var nominal = 1000000;
var length = 5;
var maturity = calendar.advance(settlementDate, length,
TimeUnit.Years);
var payFixed = true;
var fixedLegFrequency = Frequency.Annual;
var fixedLegTenor = new Period(1, TimeUnit.Years);
var fixedLegAdjustment = BusinessDayConvention.Unadjusted;
var fixedLegDayCounter = new Thirty360(Thirty360.Convention.BondBasis);
var fixedRate = 0.04;
var floatingLegFrequency = Frequency.Quarterly;
var spread = 0.0;
var fixingDays = 2;
var index = new Euribor3M(forecastTermStructure);
var floatingLegAdjustment = BusinessDayConvention.ModifiedFollowing;;
var floatingLegDayCounter = index.dayCounter();
var floatingLegTenor = new Period(3, TimeUnit.Months);
var fixedSchedule = new Schedule(
,
settlementDate,
maturity,
fixedLegTenor,
calendar,
fixedLegAdjustment,
fixedLegAdjustment.Rule.Forward,
DateGenerationfalse
);
var floatingSchedule = new Schedule(
,
settlementDate,
maturity,
floatingLegTenor,
calendar,
floatingLegAdjustment,
floatingLegAdjustment.Rule.Forward,
DateGenerationfalse);
var spot = new VanillaSwap(
.Type.Payer,
Swap,
nominal,
fixedSchedule,
fixedRate,
fixedLegDayCounter,
floatingSchedule,
index,
spread);
floatingLegDayCounter.setPricingEngine(swapEngine);
spot
var forwardStart = calendar.advance(settlementDate, 1, TimeUnit.Years);
var forwardEnd = calendar.advance(forwardStart, length, TimeUnit.Years);
= new Schedule(
fixedSchedule ,
forwardStart,
forwardEnd,
fixedLegTenor,
calendar,
fixedLegAdjustment,
fixedLegAdjustment.Rule.Forward,
DateGenerationfalse);
= new Schedule(
floatingSchedule ,
forwardStart,
forwardEnd,
floatingLegTenor,
calendar,
floatingLegAdjustment,
floatingLegAdjustment.Rule.Forward,
DateGenerationfalse);
var forward = new VanillaSwap(
.Type.Payer,
Swap,
nominal,
fixedSchedule,
fixedRate,
fixedLegDayCounter,
floatingSchedule,
index,
spread);
floatingLegDayCounter
.setPricingEngine(swapEngine);
forward
System.out.println("spot swap NPV: " + spot.NPV());
System.out.println("fwd swap NPV: " + forward.NPV());
}
}
Contact
Copyright and published by: BN Algorihtms Ltd 2024. For general information only. Not to be relied for any purpose. Not advice about investment. No warranty of any kind. No liability for any use of this information accepted. Contact: webs@bnikolic.co.uk