Пример #1
0
        public static double ValueZARSwap1Curve([ExcelArgument(Description = "The name of the swap.")] IRSwap swap,
                                                [ExcelArgument(Description = "The date on which valuation is required.  Cannot be before the anchor date of the curve.")] Date valueDate,
                                                [ExcelArgument(Description = "The discounting curve.  Will also be used for forecasting Jibar and providing the most recent required Jibar fix.")] IDiscountingSource curve)
        {
            // Get the required objects off the map
            FloatingIndex index = swap.GetFloatingIndex();

            // Calculate the first fixing off the curve to use at all past dates.
            double df1       = curve.GetDF(valueDate);
            Date   laterDate = valueDate.AddTenor(index.tenor);
            double df2       = curve.GetDF(laterDate);
            double dt        = (laterDate - valueDate) / 365.0;
            double rate      = (df1 / df2 - 1) / dt;

            //Set up the valuation engine.
            IFloatingRateSource forecastCurve = new ForecastCurveFromDiscount(curve, index,
                                                                              new FloatingRateFixingCurve1Rate(rate, index));
            DeterminsiticCurves curveSim = new DeterminsiticCurves(curve);

            curveSim.AddRateForecast(forecastCurve);
            Coordinator coordinator = new Coordinator(curveSim, new List <Simulator>(), 1);

            // Run the valuation
            double value = coordinator.Value(new Product[] { swap }, valueDate);

            return(value);
        }
Пример #2
0
        public void TestManySwaps()
        {
            Debug.StartTimer();
            Product[] allSwaps = GetListOfSwaps();

            Debug.WriteLine("Create swaps took: " + Debug.ElapsedTime().ToString());

            // Set up the model
            Date valueDate = new Date(2016, 11, 21);

            Date[]              dates         = { new Date(2016, 11, 21), new Date(2047, 11, 21) };
            Date[]              datesLong     = { new Date(2016, 11, 21), new Date(2018, 11, 21), new Date(2020, 11, 21), new Date(2022, 11, 21), new Date(2024, 11, 21), new Date(2047, 11, 21) };
            double[]            rates         = { 0.07, 0.07 };
            double[]            ratesLong     = { 0.07, 0.071, 0.072, 0.073, 0.074, 0.08 };
            IDiscountingSource  discountCurve = new DatesAndRates(Currency.ZAR, valueDate, datesLong, ratesLong);
            IFloatingRateSource forecastCurve = new ForecastCurveFromDiscount(discountCurve, FloatingIndex.JIBAR3M,
                                                                              new FloatingRateFixingCurve1Rate(0.07, FloatingIndex.JIBAR3M));
            DeterminsiticCurves curveSim = new DeterminsiticCurves(discountCurve);

            curveSim.AddRateForecast(forecastCurve);
            Coordinator coordinator = new Coordinator(curveSim, new List <Simulator>(), 1);

            // Run the valuation
            Debug.StartTimer();
            double value = coordinator.Value(allSwaps, valueDate);

            Debug.WriteLine("Value took: " + Debug.ElapsedTime().ToString());
        }
Пример #3
0
        public static double ValueSwap(IRSwap swap, Date valueDate, IDiscountingSource curve)
        {
            // Get the required objects off the map
            var index = swap.GetFloatingIndex();

            // Calculate the first fixing off the curve to use at all past dates.
            var df1       = curve.GetDF(valueDate);
            var laterDate = valueDate.AddTenor(index.Tenor);
            var df2       = curve.GetDF(laterDate);
            var dt        = (laterDate - valueDate) / 365.0;
            var rate      = (df1 / df2 - 1) / dt;

            //Set up the valuation engine.
            IFloatingRateSource forecastCurve = new ForecastCurveFromDiscount(curve, index,
                                                                              new FloatingRateFixingCurve1Rate(valueDate, rate, index));
            var curveSim = new DeterministicCurves(curve);

            curveSim.AddRateForecast(forecastCurve);
            var coordinator = new Coordinator(curveSim, new List <Simulator>(), 1);

            // Run the valuation
            var value = coordinator.Value(new Product[] { swap }, valueDate);

            return(value);
        }
        private DatesAndRates InitializeCurve(Date calibrationDate, InitialValueCollector initialValueCollector,
                                              ObjectiveFunction objective, DiscountingSourceDescription curveToStrip,
                                              IEnumerable <FloatRateIndex> indicesToBaseOffCurve)
        {
            if (curveToStrip == null)
            {
                return(null);
            }
            var curveNames = new List <string> {
                curveToStrip.Name
            };
            var indices = indicesToBaseOffCurve.ToList();

            curveNames.AddRange(
                indices.Select(ind => new FloatingRateSourceDescription(ind).Name));
            var initial = initialValueCollector.GetValues(curveNames);
            var curve   = new DatesAndRates(curveToStrip.Currency, calibrationDate,
                                            initial.dates, initial.values);

            foreach (var index in indices)
            {
                var name = new FloatingRateSourceDescription(index).Name;
                _floatingRateSources[name] = new ForecastCurveFromDiscount(curve, index, null);
            }

            objective.AddCurve(curve, initial.values);
            return(curve);
        }
Пример #5
0
        public void TestManySwaps()
        {
            Debug.StartTimer();
            var allSwaps = GetListOfSwaps();

            Debug.WriteLine("Create swaps took: " + Debug.ElapsedTime());

            // Set up the model
            var valueDate = new Date(2016, 11, 21);

            Date[] datesLong =
            {
                new Date(2016, 11, 21), new Date(2018, 11, 21), new Date(2020, 11, 21), new Date(2022, 11, 21),
                new Date(2024, 11, 21), new Date(2047, 11, 21)
            };
            double[]            ratesLong     = { 0.07, 0.071, 0.072, 0.073, 0.074, 0.08 };
            IDiscountingSource  discountCurve = new DatesAndRates(TestHelpers.ZAR, valueDate, datesLong, ratesLong);
            IFloatingRateSource forecastCurve = new ForecastCurveFromDiscount(discountCurve, TestHelpers.Jibar3M,
                                                                              new FloatingRateFixingCurve1Rate(valueDate, 0.07, TestHelpers.Jibar3M));
            var curveSim = new DeterministicCurves(discountCurve);

            curveSim.AddRateForecast(forecastCurve);
            var coordinator = new Coordinator(curveSim, new List <Simulator>(), 1);

            // Run the valuation
            Debug.StartTimer();
            var value = coordinator.Value(allSwaps, valueDate);

            Debug.WriteLine("Value took: " + Debug.ElapsedTime());
        }
        public bool TryCalibrate(Date calibrationDate, IMarketDataContainer marketDataContainer)
        {
            if (!marketDataContainer.Contains(_baseCurveDescription))
            {
                return(false);
            }
            var underlyingDiscountCurve = marketDataContainer.Get(_baseCurveDescription);

            _curve = new ForecastCurveFromDiscount(underlyingDiscountCurve, _index, null);
            return(true);
        }
Пример #7
0
        public static ResultStore AssetSwapMeasures(this AssetSwap assetSwap, Date settleDate, double ytm, IDiscountingSource discountCurve)
        {
            //Create Asset Swap
            var swap = CreateAssetSwap(assetSwap.payFixed, assetSwap.underlyingBond, settleDate, assetSwap.index, assetSwap.spread, assetSwap.zaCalendar,
                                       assetSwap.ccy, discountCurve);

            //Create trade date of the swap
            var effectiveDateDays = 3;
            var unAdjTradeDate    = settleDate.AddDays(-effectiveDateDays);
            var tradeDate         = BusinessDayStore.ModifiedFollowing.Adjust(unAdjTradeDate, assetSwap.zaCalendar);

            //Set value date
            assetSwap.SetValueDate(tradeDate);

            // Calculate the first fixing off the curve to use at all past dates.
            var df1       = discountCurve.GetDF(tradeDate);
            var laterDate = tradeDate.AddTenor(assetSwap.index.Tenor);
            var df2       = discountCurve.GetDF(laterDate);
            var noOfDays  = 365;
            var dt        = (laterDate - tradeDate) / noOfDays;
            var rate      = (df1 / df2 - 1) / dt;

            // Create the forecast curve from the discount curve
            IFloatingRateSource forecastCurve = new ForecastCurveFromDiscount(discountCurve, assetSwap.index,
                                                                              new FloatingRateFixingCurve1Rate(tradeDate, rate, assetSwap.index));

            //Setting index values
            var indexValues = new double[swap.indexDates.Count];

            for (var i = 0; i < swap.indexDates.Count; i++)
            {
                indexValues[i] = forecastCurve.GetForwardRate(swap.indexDates[i]);
            }
            assetSwap.SetIndexValues(assetSwap.index, indexValues);

            //Calculate present value of fixed and floating cashflows
            var numeratorCFs = swap.GetCFs().PV(discountCurve);

            //Calculate present value of denominator cashflows for spread equation
            var denomCFs      = new List <Cashflow>();
            var nominalAmount = 100;

            for (var i = 0; i < swap.paymentDatesFloating.Count; i++)
            {
                if (i <= swap.paymentDatesFloating.Count)
                {
                    denomCFs.Add(new Cashflow(swap.paymentDatesFloating[i], -nominalAmount * swap.accrualFractions[i], swap.ccy));
                }
            }

            var bondresults    = assetSwap.underlyingBond.GetSpotMeasures(settleDate, ytm);
            var roundedAip     = (double)bondresults.GetScalar(BesaJseBondEx.Keys.RoundedAip);
            var denominatorCFs = denomCFs.PV(discountCurve);

            var firstCF = new List <Cashflow>();

            for (var i = 0; i < 1; i++)
            {
                if (i <= 1)
                {
                    firstCF.Add(new Cashflow(settleDate, (roundedAip - 100), swap.ccy));
                }
            }
            var pvFirstCF = firstCF.PV(discountCurve);

            //This is the assetSwapSpread calculation
            var assetSwapSpread = (pvFirstCF + numeratorCFs) / denominatorCFs;

            var results = new ResultStore();

            results.Add(Keys.RoundedAip, roundedAip);
            results.Add(Keys.PVFirstCF, pvFirstCF);
            results.Add(Keys.NumeratorCashFlowsPrice, numeratorCFs);
            results.Add(Keys.DenominatorCashFlowsPrice, denominatorCFs);
            results.Add(Keys.AssetSwapSpread, assetSwapSpread);

            return(results);
        }
Пример #8
0
        public static AssetSwap CreateAssetSwap(double payFixed, BesaJseBond besaJseBond, Date settleDate, FloatRateIndex index, double spread, Calendar calendar, Currency ccy, IDiscountingSource discountCurve)
        {
            //Design floating leg inputs
            var dayCount = Actual365Fixed.Instance;
            var unAdjResetDatesFloating   = new List <Date>();
            var unAdjPaymentDatesFloating = new List <Date>();
            var resetDatesFloating        = new List <Date>();
            var paymentDatesFloating      = new List <Date>();
            var accrualFractions          = new List <double>();
            var endDate             = besaJseBond.maturityDate;
            var paymentDateFloating = new Date(endDate);
            var resetDateFloating   = paymentDateFloating.SubtractTenor(index.Tenor);

            while (resetDateFloating >= settleDate)
            {
                unAdjPaymentDatesFloating.Add(paymentDateFloating);
                unAdjResetDatesFloating.Add(resetDateFloating);
                resetDatesFloating.Add(BusinessDayStore.ModifiedFollowing.Adjust(resetDateFloating, calendar));
                paymentDatesFloating.Add(BusinessDayStore.ModifiedFollowing.Adjust(paymentDateFloating, calendar));
                accrualFractions.Add(dayCount.YearFraction(BusinessDayStore.ModifiedFollowing.Adjust(resetDateFloating, calendar),
                                                           BusinessDayStore.ModifiedFollowing.Adjust(paymentDateFloating, calendar)));
                paymentDateFloating = new Date(resetDateFloating);
                resetDateFloating   = paymentDateFloating.SubtractTenor(index.Tenor);
            }

            resetDatesFloating.Reverse();
            paymentDatesFloating.Reverse();
            accrualFractions.Reverse();

            resetDatesFloating[0] = new Date(settleDate);
            var firstResetDate   = resetDatesFloating.First();
            var firstPaymentDate = paymentDatesFloating.First();

            accrualFractions[0] = dayCount.YearFraction(firstResetDate, firstPaymentDate);

            //Design Fixed leg inputs
            var unAdjPaymentDatesFixed = new List <Date>();
            var paymentDatesFixed      = new List <Date>();

            var thisYearCpn1 = new Date(settleDate.Year, besaJseBond.couponMonth1, besaJseBond.couponDay1);
            var thisYearCpn2 = new Date(settleDate.Year, besaJseBond.couponMonth2, besaJseBond.couponDay2);
            var lastYearCpn2 = new Date(settleDate.Year - 1, besaJseBond.couponMonth2, besaJseBond.couponDay2);

            Date lcd; //lcd stands for last coupon date

            if (settleDate > thisYearCpn2)
            {
                lcd = new Date(thisYearCpn2.Year, thisYearCpn2.Month, thisYearCpn2.Day);
            }
            if (settleDate > thisYearCpn1)
            {
                lcd = new Date(thisYearCpn1.Year, thisYearCpn1.Month, thisYearCpn1.Day);
            }
            lcd = new Date(lastYearCpn2.Year, lastYearCpn2.Month, lastYearCpn2.Day);

            Date ncd; //ncd stands for next coupon date

            if (lcd.Month == besaJseBond.couponMonth2)
            {
                ncd = new Date(lcd.Year + 1, besaJseBond.couponMonth1, besaJseBond.couponDay1);
            }
            else
            {
                ncd = new Date(lcd.Year, besaJseBond.couponMonth2, besaJseBond.couponDay2);
            }

            var paymentDateFixed = new Date(ncd.AddTenor(Tenor.FromMonths(6)));

            while (paymentDateFixed <= endDate)
            {
                unAdjPaymentDatesFixed.Add(paymentDateFixed);
                paymentDatesFixed.Add(BusinessDayStore.ModifiedFollowing.Adjust(paymentDateFixed, calendar));
                paymentDateFixed = paymentDateFixed.AddTenor(Tenor.FromMonths(6));
            }

            //create new instance of asset swap
            var assetSwap = new AssetSwap(payFixed, index, besaJseBond, resetDatesFloating, paymentDatesFloating, paymentDatesFixed, spread,
                                          accrualFractions, calendar, ccy);

            //Create trade date of the swap
            var effectiveDateDays = 3;
            var unAdjTradeDate    = settleDate.AddDays(-effectiveDateDays);
            var tradeDate         = BusinessDayStore.ModifiedFollowing.Adjust(unAdjTradeDate, assetSwap.zaCalendar);

            //Set value date
            assetSwap.SetValueDate(tradeDate);

            // Calculate the first fixing off the curve to use at all past dates.
            var df1       = discountCurve.GetDF(tradeDate);
            var laterDate = tradeDate.AddTenor(assetSwap.index.Tenor);
            var df2       = discountCurve.GetDF(laterDate);
            var dt        = (laterDate - tradeDate) / 365.0;
            var rate      = (df1 / df2 - 1) / dt;

            // Create the forecast curve from the discount curve
            IFloatingRateSource forecastCurve = new ForecastCurveFromDiscount(discountCurve, assetSwap.index,
                                                                              new FloatingRateFixingCurve1Rate(tradeDate, rate, assetSwap.index));

            //Setting index values
            var indexValues = new double[assetSwap.indexDates.Count];

            for (var i = 0; i < assetSwap.indexDates.Count; i++)
            {
                indexValues[i] = forecastCurve.GetForwardRate(assetSwap.indexDates[i]);
            }
            assetSwap.SetIndexValues(assetSwap.index, indexValues);

            return(assetSwap);
        }