From dcfe0fab9e99a77e88ddc4cf671c79d52b604189 Mon Sep 17 00:00:00 2001 From: Andrea Maggiulli Date: Fri, 21 Aug 2015 15:40:18 +0200 Subject: [PATCH] Fix Vanilla Swap Pricer - thanks OfirMarom --- QLNet/Cashflows/Cashflowvectors.cs | 8 ++--- Test/T_Swaps.cs | 50 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/QLNet/Cashflows/Cashflowvectors.cs b/QLNet/Cashflows/Cashflowvectors.cs index 79bdf527a..956222009 100644 --- a/QLNet/Cashflows/Cashflowvectors.cs +++ b/QLNet/Cashflows/Cashflowvectors.cs @@ -91,8 +91,8 @@ public static List FloatingLeg FloatingLeg dates = new List(); + dates.Add( valueDate ); + dates.Add( valueDate + new Period( 1, TimeUnit.Years ) ); + dates.Add( valueDate + new Period( 2, TimeUnit.Years ) ); + dates.Add( valueDate + new Period( 5, TimeUnit.Years ) ); + dates.Add( valueDate + new Period( 10, TimeUnit.Years ) ); + dates.Add( valueDate + new Period( 20, TimeUnit.Years ) ); + + List rates = new List(); + rates.Add( 0.01 ); + rates.Add( 0.01 ); + rates.Add( 0.01 ); + rates.Add( 0.01 ); + rates.Add( 0.01 ); + rates.Add( 0.01 ); + + var discountCurveHandle = new RelinkableHandle(); + var forecastCurveHandle = new RelinkableHandle(); + GBPLibor index = new GBPLibor( new Period( 6, TimeUnit.Months ), forecastCurveHandle ); + InterpolatedZeroCurve zeroCurve = new InterpolatedZeroCurve( dates, rates, new Actual360(), new Linear() ); + var fixedSchedule = new Schedule( settlementDate, maturityDate, new Period( 1, TimeUnit.Years ), calendar, BusinessDayConvention.Following, BusinessDayConvention.Following, DateGeneration.Rule.Forward, false ); + var floatSchedule = new Schedule( settlementDate, maturityDate, index.tenor(), calendar, BusinessDayConvention.Following, BusinessDayConvention.Following, DateGeneration.Rule.Forward, false ); + VanillaSwap swap = new VanillaSwap( VanillaSwap.Type.Payer, 1000000, fixedSchedule, 0.01, new Actual360(), floatSchedule, index, 0, new Actual360() ); + discountCurveHandle.linkTo( zeroCurve ); + forecastCurveHandle.linkTo( zeroCurve ); + var swapEngine = new DiscountingSwapEngine( discountCurveHandle, false, null ); + swap.setPricingEngine( swapEngine ); + + try + { + double npv = swap.NPV(); + } + catch ( Exception ex ) + { + Assert.Fail( ex.Message ); + Console.WriteLine( ex ); + } + } + public void suite() { testFairRate();