C# Error when trying to price instrument 'System.ApplicationException : 2nd leg: empty Handle cannot be dereferenced'

Posted by Ahmad Mahomed on
URL: http://quantlib.414.s1.nabble.com/C-Error-when-trying-to-price-instrument-System-ApplicationException-2nd-leg-empty-Handle-cannot-be-d-tp8585.html

Hi,

I am receiving the following error when trying to price a 20x10 swap from a bootstrapped curve.

"SwapRatesServiceTests.ImpliedRate_ForTwenty_x_TenYearSwap_ReturnsRate: System.ApplicationException : 2nd leg: empty Handle cannot be dereferenced"

I don't have the faintest idea where to start to debug this issue. Any assistance will be highly appreciated.

IMPORTANT: I am using the C# Swig version of Quantlib, so my actual prod code is as follows based on the swapvaluation.cpp example:

The test method:

        [Test]
        public void ImpliedRate_ForTwenty_x_TenYearSwap_ReturnsRate() 
        {
            //Arrange
            var startingDate = new Date(10,Month.October,2030); // starting date of 20x10yr swap
            var length= 10;
            repo.Setup(r => r.Read(It.IsAny<string>())).Returns(LoadSwapPoints()); // LoadSwapPoints returns IEnumerable<RateHelpers>

            //Act
            service.ConstructSwapPoints(SettlementDate);
            var instrumentRate = service.ImpliedRate(startingDate, length);

            //Assert
            Assert.That(instrumentRate, Is.Not.Null); // this must change to a value test

        }

This is part of the larger ConstructSwapPoints method

            var depoFRASwapInstruments = PointVector; // RateHelperVector populated with RateHelpers
            DayCounter termStructureDayCounter = new ActualActual(ActualActual.Convention.Actual365);

            QuoteHandleVector quotes = new QuoteHandleVector();
            DateVector quoteDates = new DateVector();

            py = CreatePiecewiseLinearCurve(settlementDate, depoFRASwapInstruments, termStructureDayCounter, quotes, quoteDates);
            DiscountingTermStructure = new RelinkableYieldTermStructureHandle(py); //RelinkableYieldTermStructureHandle
            //DiscountingTermStructure.linkTo(py); // alternate way

            PricingEngine = new DiscountingSwapEngine(DiscountingTermStructure); // DiscountingSwapEngine           


With the ImpliedRate method as follows (i have snipped some parts out due to IP restrictions);
 
        public double ImpliedRate(Date startingDate, int length)
        {
            
            var swapMaturityDate = startingDate.Add(new Period(length, TimeUnit.Years));
            var curveMaturityDate = py.maxDate();
 
            Schedule fixedSchedule = new Schedule(startingDate, swapMaturityDate, new Period(Frequency.Quarterly), SouthAfricanCalender, Convention, Convention, DateGeneration.Rule.Forward, false);
            Schedule floatSchedule = new Schedule(startingDate, swapMaturityDate, new Period(Frequency.Quarterly), SouthAfricanCalender, Convention, Convention, DateGeneration.Rule.Forward, false);

            VanillaSwap impliedSwap = new VanillaSwap(
                _VanillaSwap.Type.Payer, 
                10000000.0, 
                fixedSchedule, 
                0.1, 
                Actual365FixedDayCounter, 
                floatSchedule, 
                new Jibar(new Period(Frequency.Quarterly)), 
                0, 
                Actual365FixedDayCounter);

            impliedSwap.setPricingEngine(PricingEngine);

            return impliedSwap.fairRate();
        }


I hope my terminology is correct as the finance jargon is still new to me.

--
Ahmad Mahomed

------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users