Example #1
0
        public CDXTest()
        {
            int a = 1;
            CdsAnalyticFactory FACTORY = new CdsAnalyticFactory(INDEX_RECOVERY);

            CDX = FACTORY.makeCdx(TRADE_DATE, CDSIndexProvider.INDEX_TENORS);
        }
Example #2
0
        //Constructor
        public Index_Pricing()
        {
            //User Input of Trade Date
            TRADE_DATE = new DateTime(2014, 2, 13);
            NOTIONAL   = 1.0e6;

            //Contract Detail
            Read_Contract();

            //Index Market Data
            PRICES     = CDSIndexProvider.CDX_NA_HY_20140213_PRICES;
            PILLAR_PUF = new PointsUpFront[PRICES.Length];

            //Build the Interest Rate Curve
            Build_yield_curve(TRADE_DATE);

            //Read constituent features, build credit curves
            Build_credit_curves(TRADE_DATE);

            //Build Index data bundle
            INTRINSIC_DATA = new IntrinsicIndexDataBundle(CREDIT_CURVES, RECOVERY_RATES);

            //Create CDX class
            CdsAnalyticFactory FACTORY = new CdsAnalyticFactory(INDEX_RECOVERY);

            CDX = FACTORY.makeCdx(TRADE_DATE, CDSIndexProvider.INDEX_TENORS);
        }
        public Index_Pricing_725()
        {
            TRADE_DATE = new DateTime(2016, 7, 25);
            NOTIONAL   = 1.0e6;

            //Contract Detail
            Read_Contract();

            //Index Market Data
            PRICES     = CDSIndexProvider.CDX_NA_HY_20160725_PRICES;
            PILLAR_PUF = new PointsUpFront[PRICES.Length];

            //Build the Interest Rate Curve
            Build_yield_curve(TRADE_DATE);

            //Read constituent features, build credit curves
            Build_credit_curves(TRADE_DATE);

            //Build Index data bundle
            INTRINSIC_DATA = new IntrinsicIndexDataBundle(CREDIT_CURVES, RECOVERY_RATES);

            weights_ = new double[component_num];
            weights_ = Enumerable.Repeat((double)1.0 / component_num, component_num).ToArray();
            INTRINSIC_DATA._weights = weights_;
            //Create CDX class
            CdsAnalyticFactory FACTORY = new CdsAnalyticFactory(INDEX_RECOVERY);

            int[] CDX_Tenor = new int[] { 12, 24, 36, 60, 84, 120 };
            CDX = FACTORY.makeCdx(TRADE_DATE, CDX_Tenor);
        }
Example #4
0
        public static PiecewiseconstantHazardRate[] buildCreditCurves(DateTime tradeDate, double[,] parSpreads, double[] recoveryRates, int[] tenors,
                                                                      YieldTermStructure yieldCurve)
        {
            CdsAnalyticFactory factory = new CdsAnalyticFactory(0.0);

            CDS[] pillarCDS = factory.makeImmCds(tradeDate, CDX_HY_TENORS);
            int   indexSize = parSpreads.GetLength(0);

            PiecewiseconstantHazardRate[] creditCurves = new PiecewiseconstantHazardRate[indexSize];

            //this section of code is hugely wasteful. If we do this for real (i.e. not just in a test), must improve

            for (int i = 0; i < indexSize; i++)
            {
                int      m       = parSpreads.GetLength(1);
                double[] spreads = new double[m];
                for (int j = 0; j < m; j++)
                {
                    spreads[j] = parSpreads[i, j];
                }
                int      nPillars    = spreads.Length;
                CDS[]    tempCDS     = new CDS[nPillars];
                double[] tempSpreads = new double[nPillars];
                int      count       = 0;
                for (int j = 0; j < nPillars; j++)
                {
                    if (!double.IsNaN(parSpreads[i, j]))
                    {
                        tempCDS[count]     = pillarCDS[j].withRecoveryRate(recoveryRates[i]);
                        tempSpreads[count] = spreads[j];
                        count++;
                    }
                }

                CDS[]    calCDS     = null;
                double[] calSpreads = null;
                if (count == nPillars)
                {
                    calCDS     = tempCDS;
                    calSpreads = tempSpreads;
                }
                else
                {
                    calCDS     = new CDS[count];
                    calSpreads = new double[count];
                    Array.Copy(tempCDS, 0, calCDS, 0, count);
                    Array.Copy(tempSpreads, 0, calSpreads, 0, count);
                }

                CreditCurveCalibrator calibrator = new CreditCurveCalibrator(calCDS, yieldCurve);
                creditCurves[i] = calibrator.calibrate(calSpreads);
            }
            return(creditCurves);
        }
Example #5
0
        public CDS(double Coupon, double notional, DateTime maturity, DateTime firstpaymentday, DateTime tradedate,
                   DateTime formerpaymentday, string frequency, double recovery, int settlement, int Cashsettlement)
        {
            //  Product Setup
            OMLib.Conventions.DayCount.Actual360 AccuralDCC = new OMLib.Conventions.DayCount.Actual360();
            OMLib.Conventions.DayCount.Actual365 curveDCC   = new OMLib.Conventions.DayCount.Actual365();
            Calendar calendar = new UnitedStates();

            formerpaymentday = calendar.adjust(formerpaymentday, BusinessDayConvention.Following);
            int accrued = AccuralDCC.DayCount(formerpaymentday, calendar.adjust(tradedate, BusinessDayConvention.Following)) + 1;

            this.accruedday       = accrued;
            this.marketvalue      = new double();
            this.accruedamt       = notional * Coupon * accrued / 360;
            this.Notional         = notional;
            this._payAccOnDefault = true;
            OMLib.Conventions.BusinessDayConvention convention = new OMLib.Conventions.BusinessDayConvention(Enums.BusinessDayConvention.ModifiedFollowing, tradedate);
            this.tradedate = calendar.adjust(tradedate, BusinessDayConvention.ModifiedFollowing); /*convention.AdjustedDate;*/

            this.Recovery         = recovery;
            convention            = new OMLib.Conventions.BusinessDayConvention(Enums.BusinessDayConvention.ModifiedFollowing, firstpaymentday);
            this.firstpaymentdate = CdsAnalyticFactory.getNextIMMDate(tradedate);   /*convention.AdjustedDate;*/

            convention             = new OMLib.Conventions.BusinessDayConvention(Enums.BusinessDayConvention.ModifiedFollowing, formerpaymentday);
            this.formerpaymentdate = CdsAnalyticFactory.getPrevIMMDate(tradedate);//calendar.adjust(formerpaymentday,BusinessDayConvention.ModifiedFollowing); /*convention.AdjustedDate;*/
            this.Maturity          = maturity;
            this.PremiumRate       = Coupon;
            this.Frequency         = frequency;
            this.Cashsettlement    = Cashsettlement;
            DateTime valueDate = calendar.adjust(tradedate.AddDays(Cashsettlement));

            convention            = new OMLib.Conventions.BusinessDayConvention(Enums.BusinessDayConvention.ModifiedFollowing, tradedate.AddDays(3));
            this.evalDate         = calendar.adjust(tradedate.AddDays(settlement), BusinessDayConvention.ModifiedFollowing); /*convention.AdjustedDate;*/
            this.Payment_Schedule = PremiumDates(this.Maturity, CdsAnalyticFactory.getNextIMMDate(tradedate), this.Frequency);
            QLNet.Calendar.OrthodoxImpl cal             = new Calendar.OrthodoxImpl();
            IsdaPremiumLegSchedule      paymentSchedule = new IsdaPremiumLegSchedule(formerpaymentdate, maturity, payment_interval, StubConvention.SHORT_INITIAL, QLNet.BusinessDayConvention.ModifiedFollowing, cal, true);

            _coupons = CdsCoupon.makeCoupons(tradedate, paymentSchedule, true, ACT_360, ACT_365);
            OMLib.Conventions.DayCount.Actual365 CurveDCC = new OMLib.Conventions.DayCount.Actual365();

            DateTime effectiveStartDate = tradedate;

            _accStart = DateTime.Compare(formerpaymentdate, tradedate) < 0 ?-CurveDCC.YearFraction(formerpaymentdate, tradedate) :
                        CurveDCC.YearFraction(tradedate, formerpaymentdate);
            _cashSettlementTime       = CurveDCC.YearFraction(tradedate, valueDate);
            _effectiveProtectionStart = DateTime.Compare(effectiveStartDate, tradedate) < 0 ?
                                        -CurveDCC.YearFraction(effectiveStartDate, tradedate) :
                                        CurveDCC.YearFraction(tradedate, effectiveStartDate);
            _protectionEnd = CurveDCC.YearFraction(tradedate, maturity);


            DateTime accStart = paymentSchedule.getAccStartDate(0);
        }
Example #6
0
        public void Pricing()
        {
            for (int i = 0; i < PRICES.Length; i++)
            {
                PILLAR_PUF[i] = new PointsUpFront(INDEX_COUPON, 1 - PRICES[i]);
            }
            int pos        = 1; // target CDX is 5Y
            CDS targentCDX = CDX[pos];
            int n          = PILLAR_PUF.Length;

            double[] indexPUF = new double[n];
            for (int i = 0; i < n; i++)
            {
                indexPUF[i] = PILLAR_PUF[i].getPointsUpFront();
            }

            defaultedNames = new int[] { 2, 15, 37, 51 };

            IntrinsicIndexDataBundle dataDefaulted = INTRINSIC_DATA.withDefault(defaultedNames);
            int    accrualDays    = targentCDX.getAccuredDays();
            double accruedPremium = targentCDX.getAccruedPremium(INDEX_COUPON) * NOTIONAL * dataDefaulted.getIndexFactor();

            /*
             * Using credit curves for constituent single name CDSs.
             * The curves are adjusted by using only the target CDX.
             */
            IntrinsicIndexDataBundle adjCurves = PSA.adjustCurves(indexPUF[pos], CDX[pos], INDEX_COUPON, YIELD_CURVE,
                                                                  dataDefaulted);

            cleanPV      = INDEX_CAL.indexPV(targentCDX, INDEX_COUPON, YIELD_CURVE, adjCurves) * NOTIONAL;
            dirtyPV      = INDEX_CAL.indexPV(targentCDX, INDEX_COUPON, YIELD_CURVE, adjCurves, CdsPriceType.DIRTY) * NOTIONAL; // should be consistent with 1 - PRICES[pos]
            expectedLoss = INDEX_CAL.expectedDefaultSettlementValue(targentCDX.getProtectionEnd(), adjCurves) * NOTIONAL;
            cleanRPV01   = INDEX_CAL.indexAnnuity(targentCDX, YIELD_CURVE, adjCurves);
            dirtyRPV01   = INDEX_CAL.indexAnnuity(targentCDX, YIELD_CURVE, adjCurves, CdsPriceType.DIRTY);
            durationWeightedAverageSpread = INDEX_CAL.intrinsicIndexSpread(targentCDX, YIELD_CURVE, adjCurves) *
                                            TEN_THOUSAND;
            parallelIR01 = INDEX_CAL.parallelIR01(targentCDX, INDEX_COUPON, YIELD_CURVE, adjCurves) * NOTIONAL;
            double[] jumpToDefault = INDEX_CAL.jumpToDefault(targentCDX, INDEX_COUPON, YIELD_CURVE, adjCurves);
            for (int i = 0; i < jumpToDefault.Length; ++i)
            {
                jumpToDefault[i] *= NOTIONAL;
            }
            recovery01 = INDEX_CAL.recovery01(targentCDX, INDEX_COUPON, YIELD_CURVE, adjCurves);

            //Build Cash flow
            QLNet.UnitedStates cal     = new QLNet.UnitedStates();
            CdsCoupon[]        coupons = targentCDX.getCoupons();
            int npayments = coupons.Count();

            cashflow = new List <CouponPayment>();
            for (int i = 0; i < npayments; i++)
            {
                CouponPayment cf = new CouponPayment();
                cf.Amount = (-coupons[i].getEffStart() + coupons[i].getEffEnd()) * NOTIONAL * INDEX_COUPON;
                cf.Amount = Math.Round(cf.Amount, 2);
                double days = coupons[i].getEffEnd() * 365;
                cf.CashFlowDate = i == 0? CdsAnalyticFactory.getNextIMMDate(TRADE_DATE):
                                  CdsAnalyticFactory.getNextIMMDate(cashflow[i - 1].CashFlowDate);
                cf.CashFlowDate = cal.adjust(cf.CashFlowDate);
                cashflow.Add(cf);
            }

            for (int i = 0; i < recovery01.Length; ++i)
            {
                recovery01[i] *= NOTIONAL;
            }
        }