Exemple #1
0
        public CDS[] makeCds(DateTime tradeDate, DateTime accStartDate, DateTime[] maturities)
        {
            QLNet.UnitedStates calendar   = new QLNet.UnitedStates();
            DateTime           stepinDate = calendar.adjust(tradeDate.AddDays(_stepIn), QLNet.BusinessDayConvention.Following);
            DateTime           valueDate  = calendar.adjust(tradeDate.AddDays(_cashSettle), QLNet.BusinessDayConvention.Following);

            return(makeCds(tradeDate, stepinDate, valueDate, accStartDate, maturities));
        }
Exemple #2
0
        /**
         * Set up an on-the-run index represented as a single name CDS (i.e. by CdsAnalytic).
         * The index roll dates (when new indices are issued) are 20 Mar & Sep,
         * and the index is defined to have a maturity that is its nominal tenor plus 3M on issuance,
         * so a 5Y index on the 6-Feb-2014 will have a maturity of 20-Dec-2018 (5Y3M on the issue date of 20-Sep-2013).
         * The accrual start date will be the previous IMM date (before the trade date), business-day adjusted.
         * <b>Note</b> it payment interval is changed from the
         * default of 3M, this will produce a (possibly incorrect) non-standard first coupon.
         *
         * @param tradeDate  the trade date
         * @param tenor  the nominal length of the index
         * @return a CDS analytic description
         */
        public CDS makeCdx(DateTime tradeDate, int tenor)
        {
            QLNet.UnitedStates calendar      = new QLNet.UnitedStates();
            DateTime           effectiveDate = calendar.adjust(getPrevIMMDate(tradeDate), QLNet.BusinessDayConvention.ModifiedFollowing);
            DateTime           roll          = getNextIndexRollDate(tradeDate);
            DateTime           maturity      = roll.AddMonths(tenor - 3);

            return(makeCds(tradeDate, effectiveDate, maturity));
        }
Exemple #3
0
        /**
         * Set up a strip of on-the-run indexes represented as a single name CDSs (i.e. by CdsAnalytic).
         * The index roll dates (when new indices are issued) are 20 Mar & Sep,
         * and the index is defined to have a maturity that is its nominal tenor plus 3M on issuance,
         * so a 5Y index on the 6-Feb-2014 will have a maturity of
         * 20-Dec-2018 (5Y3M on the issue date of 20-Sep-2013).
         * The accrual start date will be the previous IMM date (before the trade date), business-day adjusted.
         * <b>Note</b> it payment interval is changed from the
         * default of 3M, this will produce a (possibly incorrect) non-standard first coupon.
         *
         * @param tradeDate  the trade date
         * @param tenors  the nominal lengths of the indexes
         * @return an array of CDS analytic descriptions
         */
        public CDS[] makeCdx(DateTime tradeDate, int[] tenors)
        {
            QLNet.UnitedStates calendar      = new QLNet.UnitedStates();
            DateTime           effectiveDate = calendar.adjust(getPrevIMMDate(tradeDate), QLNet.BusinessDayConvention.ModifiedFollowing);
            DateTime           mid           = getNextIndexRollDate(tradeDate).AddMonths(-3);

            DateTime[] maturities = getIMMDateSet(mid, tenors);
            return(makeCds(tradeDate, effectiveDate, maturities));
        }
Exemple #4
0
        /**
         * Make a set of CDSs with a common trade date and maturities dates the given periods after
         * the next IMM date (after the trade-date).
         * The accrual start date will  be the previous IMM date (before the trade date).
         * <b>Note</b> it payment interval is changed from the default of 3M, this will produce a
         * (possibly incorrect) non-standard first coupon.
         *
         * @param tradeDate  the trade date
         * @param tenors  the tenors (lengths) of the CDSs
         * @param makeEffBusDay  is the accrual start day business-day adjusted.
         * @return an array of CDS analytic descriptions
         */
        public CDS[] makeImmCds(DateTime tradeDate, int[] tenors, Boolean makeEffBusDay)
        {
            QLNet.UnitedStates calendar  = new QLNet.UnitedStates();
            DateTime           effective = calendar.adjust(getPrevIMMDate(tradeDate), QLNet.BusinessDayConvention.ModifiedFollowing);

            DateTime effectiveDate = makeEffBusDay ? effective : getPrevIMMDate(tradeDate);

            getPrevIMMDate(tradeDate);
            return(makeImmCds(tradeDate, effectiveDate, tenors));
        }
Exemple #5
0
        /**
         * Make a CDS with a maturity date the given period on from the next IMM date after the trade-date.
         * The accrual start date will be the previous IMM date (before the trade date).
         * <b>Note</b> it payment interval is changed from the
         * default of 3M, this will produce a (possibly incorrect) non-standard first coupon.
         *
         * @param tradeDate  the trade date
         * @param tenor  the tenor (length) of the CDS
         * @param makeEffBusDay  is the accrual start day business-day adjusted.
         * @return a CDS analytic description
         */
        public CDS makeImmCds(DateTime tradeDate, int tenor, Boolean makeEffBusDay)
        {
            QLNet.UnitedStates calendar  = new QLNet.UnitedStates();
            DateTime           effective = calendar.adjust(getPrevIMMDate(tradeDate), QLNet.BusinessDayConvention.ModifiedFollowing);

            DateTime effectiveDate = makeEffBusDay? effective : getPrevIMMDate(tradeDate);

            DateTime nextIMM  = getNextIMMDate(tradeDate);
            DateTime maturity = nextIMM.AddMonths(tenor);

            return(makeCds(tradeDate, effectiveDate, maturity));
        }
Exemple #6
0
        public BasicFixedLeg(
            DateTime spotDate,
            DateTime mat,
            int swapInterval
            )
        {
            OMLib.Conventions.DayCount.Thirty360 swapDCC        = new OMLib.Conventions.DayCount.Thirty360();
            OMLib.Conventions.DayCount.Actual360 moneyMarketDCC = new OMLib.Conventions.DayCount.Actual360();
            OMLib.Conventions.DayCount.Actual365 curveDCC       = new OMLib.Conventions.DayCount.Actual365();
            QLNet.UnitedStates cal   = new QLNet.UnitedStates();
            List <DateTime>    list  = new List <DateTime>();
            DateTime           tDate = mat;
            int step = 1;

            while (DateTime.Compare(tDate, spotDate) > 0)
            {
                list.Add(tDate);
                tDate = mat.AddMonths(-swapInterval * (step++));
            }

            // remove spotDate from list, if it ends up there
            list.Remove(spotDate);

            _nPayments        = list.Count();
            _swapPaymentTimes = new double[_nPayments];
            _yearFraction     = new double[_nPayments];

            DateTime prev = spotDate;
            int      j    = _nPayments - 1;

            for (int i = 0; i < _nPayments; i++, j--)
            {
                DateTime current = list[j];
                DateTime adjCurr = cal.adjust(current, QLNet.BusinessDayConvention.Following);

                _yearFraction[i]     = swapDCC.YearFraction(prev, adjCurr);
                _swapPaymentTimes[i] = curveDCC.YearFraction(spotDate, adjCurr); // Payment times always good business days
                prev = adjCurr;
            }
        }
 private DateTime businessDayAdjustDate(DateTime date, QLNet.Calendar calendar, QLNet.BusinessDayConvention convention)
 {
     QLNet.UnitedStates cal = new QLNet.UnitedStates();
     return(cal.adjust(date, convention));
 }
Exemple #8
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;
            }
        }
        public YieldTermStructure calculation(DateTime tradedate, List <double> QuotedSpot)
        {
            DateTime SpotDate = tradedate.AddDays(2);

            /*********************
             *
             *  **  CURVE BUILDING **
             *
             *  *********************/

            DateTime d1m  = SpotDate.AddMonths(1);
            DateTime d2m  = SpotDate.AddMonths(2);
            DateTime d3m  = SpotDate.AddMonths(3);
            DateTime d6m  = SpotDate.AddMonths(6);
            DateTime d9m  = SpotDate.AddMonths(9);
            DateTime d1y  = SpotDate.AddYears(1);
            DateTime d2y  = SpotDate.AddYears(2);
            DateTime d3y  = SpotDate.AddYears(3);
            DateTime d4y  = SpotDate.AddYears(4);
            DateTime d5y  = SpotDate.AddYears(5);
            DateTime d6y  = SpotDate.AddYears(6);
            DateTime d7y  = SpotDate.AddYears(7);
            DateTime d8y  = SpotDate.AddYears(8);
            DateTime d9y  = SpotDate.AddYears(9);
            DateTime d10y = SpotDate.AddYears(10);
            DateTime d11y = SpotDate.AddYears(11);
            DateTime d12y = SpotDate.AddYears(12);
            DateTime d15y = SpotDate.AddYears(15);
            DateTime d20y = SpotDate.AddYears(20);
            DateTime d25y = SpotDate.AddYears(25);
            DateTime d30y = SpotDate.AddYears(30);

            List <DateTime> dates = new List <DateTime>();

            dates.Add(d1m);
            dates.Add(d2m);
            dates.Add(d3m);
            dates.Add(d6m);
            dates.Add(d9m);
            dates.Add(d1y);
            dates.Add(d2y);
            dates.Add(d3y);
            dates.Add(d4y);
            dates.Add(d5y);
            dates.Add(d6y);
            dates.Add(d7y);
            dates.Add(d8y);
            dates.Add(d9y);
            dates.Add(d10y);
            dates.Add(d11y);
            dates.Add(d12y);
            dates.Add(d15y);
            dates.Add(d20y);
            dates.Add(d25y);
            dates.Add(d30y);
            // Any DayCounter would be fine.
            // ActualActual::ISDA ensures that 30 years is 30.0
            OMLib.Conventions.DayCount.Actual365 dc = new OMLib.Conventions.DayCount.Actual365();
            QLNet.UnitedStates calendar             = new QLNet.UnitedStates();
            String[]           YIELD_CURVE_POINTS   = new String[] { "1M", "2M", "3M", "6M", "9M", "1Y", "2Y", "3Y", "4Y", "5Y",
                                                                     "6Y", "7Y", "8Y", "9Y", "10Y", "11Y", "12Y", "15Y", "20Y", "25Y", "30Y" };
            String[] YIELD_CURVE_INSTRUMENTS = new String[] { "M", "M", "M", "M", "M", "M", "S", "S", "S", "S", "S",
                                                              "S", "S", "S", "S", "S", "S", "S", "S", "S", "S" };

            DateTime        today       = tradedate;
            List <DateTime> matDates    = dates;
            List <DateTime> adjMatDates = new List <DateTime>();

            OMLib.Conventions.DayCount.Thirty360 swapDCC        = new OMLib.Conventions.DayCount.Thirty360();
            OMLib.Conventions.DayCount.Actual360 moneyMarketDCC = new OMLib.Conventions.DayCount.Actual360();
            OMLib.Conventions.DayCount.Actual365 curveDCC       = new OMLib.Conventions.DayCount.Actual365();
            for (int i = 0; i < matDates.Count; i++)
            {
                adjMatDates.Add(calendar.adjust(matDates[i], QLNet.BusinessDayConvention.Following));
            }
            adjMatDates[2] = adjMatDates[2].AddDays(-1);
            int nMM = 0;
            int n   = YIELD_CURVE_INSTRUMENTS.Count();

            double[] _t = new double[n];
            for (int i = 0; i < n; i++)
            {
                _t[i] = curveDCC.YearFraction(SpotDate, adjMatDates[i]);
                if (YIELD_CURVE_INSTRUMENTS[i] == "M")
                {
                    nMM++;
                }
            }
            int nSwap = n - nMM;

            double[]        _mmYF     = new double[nMM];
            BasicFixedLeg[] _swaps    = new BasicFixedLeg[nSwap];
            int             mmCount   = 0;
            int             swapCount = 0;

            int swapInterval = 12;

            for (int i = 0; i < n; i++)
            {
                if (YIELD_CURVE_INSTRUMENTS[i] == "M")
                {
                    // TODO in ISDA code money market instruments of less than 21 days have special treatment
                    _mmYF[mmCount++] = moneyMarketDCC.YearFraction(SpotDate, adjMatDates[i]);
                }
                else
                {
                    _swaps[swapCount++] = new BasicFixedLeg(SpotDate, matDates[i], swapInterval);
                }
            }
            double _offset = DateTime.Compare(tradedate, SpotDate) > 0 ? curveDCC.YearFraction(SpotDate, tradedate) : -curveDCC.YearFraction(
                tradedate, SpotDate);
            YieldTermStructure curve = new YieldTermStructure(tradedate, QuotedSpot, dates, _t.ToList(), null);
            int mmCount_             = 0;
            int swapCount_           = 0;

            double[] rt_ = new double[n];
            for (int i = 0; i < n; i++)
            {
                if (YIELD_CURVE_INSTRUMENTS[i] == "M")
                {
                    // TODO in ISDA code money market instruments of less than 21 days have special treatment
                    double             z         = 1.0 / (1 + QuotedSpot[i] * _mmYF[mmCount_++]);
                    YieldTermStructure tempcurve = curve.withDiscountFactor(z, i);
                    curve = tempcurve;
                }
                else
                {
                    curve = curve.fitSwap(i, _swaps[swapCount_++], curve, QuotedSpot[i]);
                }
            }
            YieldTermStructure baseCurve = curve;
            List <double>      ZeroRates = curve.getKnotZeroRates();

            if (_offset == 0.0)
            {
                return(baseCurve);
            }

            this.Nodes = dates;
            return(baseCurve.withOffset(_offset));
        }
        public YieldTermStructure calculation2(DateTime tradedate, List <double> QuotedSpot)
        {
            /*********************
            ***  MARKET DATA  ***
            *********************/
            QLNet.UnitedStates cal = new QLNet.UnitedStates();


            DateTime SpotDate = tradedate.AddDays(4);
            //DateTime SpotDate = cal.advance(tradedate,2,QLNet.TimeUnit.Days,QLNet.BusinessDayConvention.ModifiedFollowing);
            // must be a business day

            /*********************
             *
             *  **  CURVE BUILDING **
             *
             *  *********************/

            DateTime d1m  = SpotDate.AddMonths(1);
            DateTime d2m  = SpotDate.AddMonths(2);
            DateTime d3m  = SpotDate.AddMonths(3);
            DateTime d6m  = SpotDate.AddMonths(6);
            DateTime d1y  = SpotDate.AddYears(1);
            DateTime d2y  = SpotDate.AddYears(2);
            DateTime d3y  = SpotDate.AddYears(3);
            DateTime d4y  = SpotDate.AddYears(4);
            DateTime d5y  = SpotDate.AddYears(5);
            DateTime d6y  = SpotDate.AddYears(6);
            DateTime d7y  = SpotDate.AddYears(7);
            DateTime d8y  = SpotDate.AddYears(8);
            DateTime d9y  = SpotDate.AddYears(9);
            DateTime d10y = SpotDate.AddYears(10);
            DateTime d12y = SpotDate.AddYears(12);
            DateTime d15y = SpotDate.AddYears(15);
            DateTime d20y = SpotDate.AddYears(20);
            DateTime d25y = SpotDate.AddYears(25);
            DateTime d30y = SpotDate.AddYears(30);

            List <DateTime> dates = new List <DateTime>();

            dates.Add(d1m);
            dates.Add(d2m);
            dates.Add(d3m);
            dates.Add(d6m);
            dates.Add(d1y);
            dates.Add(d2y);
            dates.Add(d3y);
            dates.Add(d4y);
            dates.Add(d5y);
            dates.Add(d6y);
            dates.Add(d7y);
            dates.Add(d8y);
            dates.Add(d9y);
            dates.Add(d10y);
            dates.Add(d12y);
            dates.Add(d15y);
            dates.Add(d20y);
            dates.Add(d25y);
            dates.Add(d30y);
            // Any DayCounter would be fine.
            // ActualActual::ISDA ensures that 30 years is 30.0
            OMLib.Conventions.DayCount.Actual365 dc = new OMLib.Conventions.DayCount.Actual365();
            QLNet.UnitedStates calendar             = new QLNet.UnitedStates();
            String[]           YIELD_CURVE_POINTS   = new String[] { "1M", "2M", "3M", "6M", "1Y", "2Y", "3Y", "4Y", "5Y",
                                                                     "6Y", "7Y", "8Y", "9Y", "10Y", "12Y", "15Y", "20Y", "25Y", "30Y" };
            String[] YIELD_CURVE_INSTRUMENTS = new String[] { "M", "M", "M", "M", "M", "S", "S", "S", "S", "S",
                                                              "S", "S", "S", "S", "S", "S", "S", "S", "S" };
            List <double> YIELD_CURVE_RATES = QuotedSpot;

            DateTime        spotDate    = SpotDate;
            List <DateTime> matDates    = dates;
            List <DateTime> adjMatDates = new List <DateTime>();

            OMLib.Conventions.DayCount.Thirty360 swapDCC        = new OMLib.Conventions.DayCount.Thirty360();
            OMLib.Conventions.DayCount.Actual360 moneyMarketDCC = new OMLib.Conventions.DayCount.Actual360();
            OMLib.Conventions.DayCount.Actual365 curveDCC       = new OMLib.Conventions.DayCount.Actual365();
            for (int i = 0; i < matDates.Count; i++)
            {
                adjMatDates.Add(calendar.adjust(matDates[i], QLNet.BusinessDayConvention.ModifiedFollowing));
            }
            int nMM = 0;
            int n   = YIELD_CURVE_INSTRUMENTS.Count();

            double[] _t = new double[n];
            for (int i = 0; i < n; i++)
            {
                _t[i] = curveDCC.YearFraction(spotDate, adjMatDates[i]);
                if (YIELD_CURVE_INSTRUMENTS[i] == "M")
                {
                    nMM++;
                }
            }
            int nSwap = n - nMM;

            double[]        _mmYF     = new double[nMM];
            BasicFixedLeg[] _swaps    = new BasicFixedLeg[nSwap];
            int             mmCount   = 0;
            int             swapCount = 0;

            int swapInterval = 6;

            for (int i = 0; i < n; i++)
            {
                if (YIELD_CURVE_INSTRUMENTS[i] == "M")
                {
                    // TODO in ISDA code money market instruments of less than 21 days have special treatment
                    _mmYF[mmCount++] = moneyMarketDCC.YearFraction(spotDate, adjMatDates[i]);
                }
                else
                {
                    _swaps[swapCount++] = new BasicFixedLeg(spotDate, matDates[i], swapInterval);
                }
            }
            double _offset = DateTime.Compare(tradedate, spotDate) > 0 ? curveDCC.YearFraction(spotDate, tradedate) : -curveDCC.YearFraction(
                tradedate, spotDate);
            YieldTermStructure curve = new YieldTermStructure(tradedate, YIELD_CURVE_RATES, dates, _t.ToList(), null);
            int mmCount_             = 0;
            int swapCount_           = 0;

            double[] rt_ = new double[n];
            for (int i = 0; i < n; i++)
            {
                if (YIELD_CURVE_INSTRUMENTS[i] == "M")
                {
                    // TODO in ISDA code money market instruments of less than 21 days have special treatment
                    double             z         = 1.0 / (1 + YIELD_CURVE_RATES[i] * _mmYF[mmCount_++]);
                    YieldTermStructure tempcurve = curve.withDiscountFactor(z, i);
                    curve = tempcurve;
                }
                else
                {
                    curve = curve.fitSwap(i, _swaps[swapCount_++], curve, YIELD_CURVE_RATES[i]);
                }
            }
            YieldTermStructure baseCurve = curve;
            List <double>      ZeroRates = curve.getKnotZeroRates();

            if (_offset == 0.0)
            {
                return(baseCurve);
            }

            this.Nodes = dates;
            curve      = baseCurve.withOffset(_offset);
            //List<double> rt = new List<double>() { 1.399914257002842E-4, 3.452229985902273E-4, 6.151397497988689E-4, 0.0017010975470283791, 0.005696357532861686, 0.008854793051714499, 0.0235368691596982, 0.04799336562986048, 0.07980430061988725, 0.11682686636178839, 0.1569272410971123, 0.1988340941576404, 0.24178776149530337, 0.2862865792161734, 0.37671732698783206, 0.512340347238558, 0.7299269275257245, 0.9365962573841474, 1.1363739062462221};
            //curve.rt = rt;
            return(curve);
        }