internal static global::System.Runtime.InteropServices.HandleRef getCPtr(IsdaCdsEngine obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
Example #2
0
        public void testIsdaEngine()
        {
            // Testing ISDA engine calculations for credit-default swaps

            SavedSettings backup = new SavedSettings();

            Date tradeDate = new Date(21, Month.May, 2009);

            Settings.setEvaluationDate(tradeDate);


            //build an ISDA compliant yield curve
            //data comes from Markit published rates
            List <RateHelper> isdaRateHelpers = new List <RateHelper>();

            int[]    dep_tenors = { 1, 2, 3, 6, 9, 12 };
            double[] dep_quotes = { 0.003081,
                                    0.005525,
                                    0.007163,
                                    0.012413,
                                    0.014,
                                    0.015488 };

            for (int i = 0; i < dep_tenors.Length; i++)
            {
                isdaRateHelpers.Add(new DepositRateHelper(dep_quotes[i], new Period(dep_tenors[i], TimeUnit.Months), 2,
                                                          new WeekendsOnly(), BusinessDayConvention.ModifiedFollowing, false, new Actual360())
                                    );
            }
            int[]    swap_tenors = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20, 25, 30 };
            double[] swap_quotes = { 0.011907,
                                     0.01699,
                                     0.021198,
                                     0.02444,
                                     0.026937,
                                     0.028967,
                                     0.030504,
                                     0.031719,
                                     0.03279,
                                     0.034535,
                                     0.036217,
                                     0.036981,
                                     0.037246,
                                     0.037605 };

            IborIndex isda_ibor = new IborIndex("IsdaIbor", new Period(3, TimeUnit.Months), 2, new USDCurrency(),
                                                new WeekendsOnly(), BusinessDayConvention.ModifiedFollowing, false, new Actual360());

            for (int i = 0; i < swap_tenors.Length; i++)
            {
                isdaRateHelpers.Add(new SwapRateHelper(swap_quotes[i], new Period(swap_tenors[i], TimeUnit.Years),
                                                       new WeekendsOnly(), Frequency.Semiannual, BusinessDayConvention.ModifiedFollowing, new Thirty360(),
                                                       isda_ibor));
            }

            RelinkableHandle <YieldTermStructure> discountCurve = new RelinkableHandle <YieldTermStructure>();

            discountCurve.linkTo(new PiecewiseYieldCurve <Discount, LogLinear>(0, new WeekendsOnly(), isdaRateHelpers,
                                                                               new Actual365Fixed()));


            RelinkableHandle <DefaultProbabilityTermStructure> probabilityCurve = new RelinkableHandle <DefaultProbabilityTermStructure>();

            Date[]   termDates = { new Date(20, Month.June, 2010),
                                   new Date(20,   Month.June, 2011),
                                   new Date(20,   Month.June, 2012),
                                   new Date(20,   Month.June, 2016),
                                   new Date(20,   Month.June, 2019) };
            double[] spreads    = { 0.001, 0.1 };
            double[] recoveries = { 0.2, 0.4 };

            double[] markitValues = { 97798.29358,  //0.001
                                      97776.11889,  //0.001
                                      -914971.5977, //0.1
                                      -894985.6298, //0.1
                                      186921.3594,  //0.001
                                      186839.8148,  //0.001
                                      -1646623.672, //0.1
                                      -1579803.626, //0.1
                                      274298.9203,
                                      274122.4725,
                                      -2279730.93,
                                      -2147972.527,
                                      592420.2297,
                                      591571.2294,
                                      -3993550.206,
                                      -3545843.418,
                                      797501.1422,
                                      795915.9787,
                                      -4702034.688,
                                      -4042340.999 };
#if !QL_USE_INDEXED_COUPON
            double tolerance = 1.0e-2; //TODO Check calculation , tolerance must be 1.0e-6;
#else
            /* The risk-free curve is a bit off. We might skip the tests
             * altogether and rely on running them with indexed coupons
             * disabled, but leaving them can be useful anyway. */
            double tolerance = 1.0e-3;
#endif

            int l = 0;

            for (int i = 0; i < termDates.Length; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    for (int k = 0; k < 2; k++)
                    {
                        CreditDefaultSwap quotedTrade = new MakeCreditDefaultSwap(termDates[i], spreads[j])
                                                        .withNominal(10000000.0).value();

                        double h = quotedTrade.impliedHazardRate(0.0,
                                                                 discountCurve,
                                                                 new Actual365Fixed(),
                                                                 recoveries[k],
                                                                 1e-10,
                                                                 PricingModel.ISDA);

                        probabilityCurve.linkTo(new FlatHazardRate(0, new WeekendsOnly(), h, new Actual365Fixed()));

                        IsdaCdsEngine engine = new IsdaCdsEngine(probabilityCurve, recoveries[k], discountCurve);

                        CreditDefaultSwap conventionalTrade = new MakeCreditDefaultSwap(termDates[i], 0.01)
                                                              .withNominal(10000000.0)
                                                              .withPricingEngine(engine).value();

                        double x = conventionalTrade.notional().Value;
                        double y = conventionalTrade.fairUpfront();

                        double calculated = Math.Abs((x * y) - markitValues[l]);

                        QAssert.IsTrue(calculated <= tolerance);

                        l++;
                    }
                }
            }
        }