Example #1
0
        public void TestSwap()
        {
            // Make the swap
            var    rate      = 0.08;
            var    payFixed  = true;
            double notional  = 1000000;
            var    startDate = new Date(2016, 9, 17);
            var    tenor     = Tenor.Years(5);
            var    swap      = IRSwap.CreateZARSwap(rate, payFixed, notional, startDate, tenor);

            // Set up the model
            var valueDate = new Date(2016, 9, 17);

            Date[]              dates         = { new Date(2016, 9, 17), new Date(2026, 9, 17) };
            double[]            rates         = { 0.07, 0.07 };
            IDiscountingSource  discountCurve = new DatesAndRates(Currency.ZAR, valueDate, dates, rates);
            IFloatingRateSource forecastCurve = new ForecastCurve(valueDate, FloatingIndex.JIBAR3M, dates, rates);
            var curveSim = new DeterminsiticCurves(discountCurve);

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

            // Run the valuation
            var value    = coordinator.Value(new Product[] { swap }, valueDate);
            var refValue = -41838.32; // See RateProductTest.xlsx

            Assert.AreEqual(refValue, value, 0.01);
        }
Example #2
0
        public void TestFixedLegsZARUSD()
        {
            Date[] cfDates = { new Date(2016, 12, 23), new Date(2017, 03, 23) };

            var legZAR = new FixedLeg(TestHelpers.ZAR, cfDates, new double[] { -16000000, -16000000 }, new[] { 0.07, 0.07 },
                                      new[] { 0.25, 0.25 });
            var legUSD = new FixedLeg(TestHelpers.USD, cfDates, new double[] { 1000000, 1000000 }, new[] { 0.01, 0.01 },
                                      new[] { 0.25, 0.25 });

            // Set up the model
            var valueDate = new Date(2016, 9, 23);

            Date[]              dates         = { new Date(2016, 9, 23), new Date(2026, 9, 23) };
            double[]            rates         = { 0.0725, 0.0725 };
            double[]            basisRates    = { 0.0735, 0.0735 };
            double[]            usdRates      = { 0.01, 0.012 };
            IDiscountingSource  discountCurve = new DatesAndRates(TestHelpers.ZAR, valueDate, dates, rates);
            IDiscountingSource  zarBasis      = new DatesAndRates(TestHelpers.ZAR, valueDate, dates, basisRates);
            IDiscountingSource  usdCurve      = new DatesAndRates(TestHelpers.USD, valueDate, dates, usdRates);
            IFloatingRateSource forecastCurve = new ForecastCurve(valueDate, TestHelpers.Jibar3M, dates, rates);
            IFXSource           fxSource      = new FXForecastCurve(TestHelpers.USDZAR, 13.66, usdCurve, zarBasis);
            var curveSim = new DeterminsiticCurves(discountCurve);

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

            // Run the valuation
            var value    = coordinator.Value(new Product[] { legZAR, legUSD }, valueDate);
            var refValue = -477027.31; // See GeneralSwapTest.xlsx

            Assert.AreEqual(refValue, value, 0.01);
        }
Example #3
0
        public void TestInflationLinkedSwap()
        {
            //Testing the zero-coupon inflation swap
            //Parameterise test inputs below
            var payFixed  = -1;
            var startDate = new Date(2005, 8, 31);
            var nominal   = (double)5000000;
            var tenor     = Tenor.FromYears(2);
            var fixedRate = 0.036;
            var ccy       = TestHelpers.ZAR;
            var index     = new FloatRateIndex("ZAR.JIBAR.3M", ccy, "JIBAR", Tenor.FromMonths(3));
            var spread    = 0.0;

            Date[] cpiDates =
            { new Date(2005, 3, 1), new Date(2005,  4, 1), new Date(2005,  5, 1), new Date(2005,  6, 1), new Date(2005, 7, 1), new Date(2005, 8, 1),
              new Date(2005, 9, 1), new Date(2005, 10, 1), new Date(2005, 11, 1), new Date(2005, 12, 1), new Date(2006, 1, 1), new Date(2006, 2, 1),
              new Date(2006, 3, 1), new Date(2006,  4, 1), new Date(2006,  5, 1), new Date(2006,  6, 1), new Date(2006, 7, 1), new Date(2006, 8, 1),
              new Date(2006, 9, 1), new Date(2006, 10, 1), new Date(2006, 11, 1), new Date(2006, 12, 1), new Date(2007, 1, 1), new Date(2007, 2, 1),
              new Date(2007, 3, 1), new Date(2007,  4, 1), new Date(2007,  5, 1), new Date(2007,  6, 1), new Date(2007, 7, 1), new Date(2007, 8, 1),
              new Date(2007, 9, 1), new Date(2007, 10,              1)
              ,              };

            double[] cpiRates =
            {
                126.90, 127.60, 127.60, 127.40, 128.50,  129.0, 129.50, 129.60, 129.50, 129.50, 130.40, 130.50, 131.20, 131.80, 132.60, 133.60, 134.90,
                136.0,  136.30, 136.60, 136.50,  137.0, 138.20, 139.20,  138.0,  141.0, 141.80,  143.0, 144.40, 145.10, 146.10, 147.40
            };

            var zaCalendar = new Calendar("Test");

            Date[] curveDates =
            {
                new Date(2005, 8, 31), new Date(2005, 11, 30), new Date(2006, 2, 28), new Date(2006, 5, 31), new Date(2006, 8, 31), new Date(2006, 11, 30),
                new Date(2007, 2, 28), new Date(2007,  5, 31)
            };

            double[] curveRates = { 0.07004, 0.07164, 0.07092, 0.07079, 0.08224, 0.08918, 0.09075, 0.09354 };

            //Create curve used to determine swap cash flows
            IFloatingRateSource forecastCurve = new ForecastCurve(startDate, index, curveDates, curveRates);

            //Create instance of an inflation swap
            var inflationSwap = InflationLinkedSwapEx.CreateInflationLinkedSwap(payFixed, startDate, nominal, tenor, fixedRate, index, spread, zaCalendar, ccy);

            //Get results
            var results = inflationSwap.InflationLinkedSwapMeasures(cpiDates, cpiRates, forecastCurve);

            Assert.AreEqual(5856959.45, Math.Round((double)results.GetScalar(InflationLinkedSwapEx.Keys.FloatingLegCashFlows), 2), 1e-8);
            Assert.AreEqual(-5966334.90, Math.Round((double)results.GetScalar(InflationLinkedSwapEx.Keys.FixedLegCashFlows), 2), 1e-8);
            Assert.AreEqual(-109375.45, Math.Round((double)results.GetScalar(InflationLinkedSwapEx.Keys.NetCashFlows), 2), 1e-8);
        }
Example #4
0
        public void TestFloatLeg()
        {
            // Make the reference swap
            var    rate      = 0.0;
            var    payFixed  = true;
            double notional  = 1000000;
            var    startDate = new Date(2016, 9, 17);
            var    tenor     = Tenor.Years(1);
            var    swap      = IRSwap.CreateZARSwap(rate, payFixed, notional, startDate, tenor);

            // Make a FloatLeg
            var resetDates       = new Date[4];
            var paymentDates     = new Date[4];
            var accrualFractions = new double[4];
            var runningDate      = new Date(2016, 9, 17);

            for (var i = 0; i < 4; i++)
            {
                resetDates[i]       = new Date(runningDate);
                paymentDates[i]     = resetDates[i].AddMonths(3);
                accrualFractions[i] = (paymentDates[i] - resetDates[i]) / 365.0;
                runningDate         = paymentDates[i];
            }

            var floatLeg = new FloatLeg(Currency.ZAR, paymentDates, new[] { 1e6, 1e6, 1e6, 1e6 },
                                        resetDates,
                                        new[] { FloatingIndex.JIBAR3M, FloatingIndex.JIBAR3M, FloatingIndex.JIBAR3M, FloatingIndex.JIBAR3M },
                                        new double[] { 0, 0, 0, 0 }, accrualFractions);

            // Set up the model
            var valueDate = new Date(2016, 9, 17);

            Date[]              dates         = { new Date(2016, 9, 17), new Date(2026, 9, 17) };
            double[]            rates         = { 0.07, 0.07 };
            IDiscountingSource  discountCurve = new DatesAndRates(Currency.ZAR, valueDate, dates, rates);
            IFloatingRateSource forecastCurve = new ForecastCurve(valueDate, FloatingIndex.JIBAR3M, dates, rates);
            var curveSim = new DeterminsiticCurves(discountCurve);

            curveSim.AddRateForecast(forecastCurve);

            // Run the valuation
            var coordinator   = new Coordinator(curveSim, new List <Simulator>(), 1);
            var swapValue     = coordinator.Value(new Product[] { swap }, valueDate);
            var floatLegValue = coordinator.Value(new Product[] { floatLeg }, valueDate);

            Assert.AreEqual(swapValue, floatLegValue, 0.01);
        }
        public void TestDynamicCallFromStringFRA()
        {
            var source =
                @"Date date = new Date(2017, 08, 28);
FloatRateIndex jibar = new FloatRateIndex(""ZAR.JIBAR.3M"", new Currency(""ZAR""), ""JIBAR"", Tenor.FromMonths(3));
double dt = 91.0/365.0;
double fixedRate = 0.071;
double notional = 1000000.0;
Currency currency = new Currency(""ZAR"");

public override List<Cashflow> GetCFs()
{
    double reset = Get(jibar, date);
    double cfAmount = notional * (reset - fixedRate)*dt/(1+dt*reset);
    return new List<Cashflow>() { new Cashflow(date, cfAmount, currency) };
}";
            // Make a product at runtime
            var runtimeProduct = RuntimeProduct.CreateFromString("MyEuropeanOption", source);

            // Set up the model
            var valueDate = new Date(2016, 9, 17);

            Date[]              dates         = { new Date(2016, 9, 17), new Date(2026, 9, 17) };
            double[]            rates         = { 0.07, 0.07 };
            IDiscountingSource  discountCurve = new DatesAndRates(TestHelpers.ZAR, valueDate, dates, rates);
            IFloatingRateSource forecastCurve = new ForecastCurve(valueDate, TestHelpers.Jibar3M, dates, rates);
            var curveSim = new DeterministicCurves(discountCurve);

            curveSim.AddRateForecast(forecastCurve);

            // Run the valuation
            var coordinator = new Coordinator(curveSim, new List <Simulator>(), 1);
            var fraValue    = coordinator.Value(new[] { runtimeProduct }, valueDate);

            var date      = new Date(2017, 08, 28);
            var t         = (date - valueDate) / 365.0;
            var dt        = 91.0 / 365.0;
            var fixedRate = 0.071;
            var notional  = 1000000.0;
            var fwdRate   = 0.07;
            var refValue  = notional * (fwdRate - fixedRate) * dt / (1 + fwdRate * dt) * Math.Exp(-t * 0.07);

            Assert.AreEqual(refValue, fraValue, 0.01);
        }
        public void TestDynamicCallFromStringFRA()
        {
            string source =
                @"Date date = new Date(2017, 08, 28);
FloatingIndex jibar = FloatingIndex.JIBAR3M;
double dt = 91.0/365.0;
double fixedRate = 0.071;
double notional = 1000000.0;
Currency currency = Currency.ZAR;

public override List<Cashflow> GetCFs()
{
    double reset = Get(jibar, date);
    double cfAmount = notional * (reset - fixedRate)*dt/(1+dt*reset);
    return new List<Cashflow>() { new Cashflow(date, cfAmount, currency) };
}";
            // Make a product at runtime
            Product runtimeProduct = RuntimeProduct.CreateFromString("MyEuropeanOption", source);

            // Set up the model
            Date valueDate = new Date(2016, 9, 17);

            Date[]              dates         = { new Date(2016, 9, 17), new Date(2026, 9, 17) };
            double[]            rates         = { 0.07, 0.07 };
            IDiscountingSource  discountCurve = new DatesAndRates(Currency.ZAR, valueDate, dates, rates);
            IFloatingRateSource forecastCurve = new ForecastCurve(valueDate, FloatingIndex.JIBAR3M, dates, rates);
            DeterminsiticCurves curveSim      = new DeterminsiticCurves(discountCurve);

            curveSim.AddRateForecast(forecastCurve);

            // Run the valuation
            Coordinator coordinator = new Coordinator(curveSim, new List <Simulator>(), 1);
            double      fraValue    = coordinator.Value(new Product[] { runtimeProduct }, valueDate);

            Date   date      = new Date(2017, 08, 28);
            double t         = (date - valueDate) / 365.0;
            double dt        = 91.0 / 365.0;
            double fixedRate = 0.071;
            double notional  = 1000000.0;
            double fwdRate   = 0.07;
            double refValue  = notional * (fwdRate - fixedRate) * dt / (1 + fwdRate * dt) * Math.Exp(-t * 0.07);

            Assert.AreEqual(refValue, fraValue, 0.01);
        }
        /// <summary>
        /// Calculates the specified model data.
        /// </summary>
        /// <param name="modelData">The model data.</param>
        /// <returns></returns>
        public override BasicAssetValuation Calculate(IAssetControllerData modelData)
        {
            ModelData = modelData;
            switch (ModelIdentifier)
            {
            case "CapAsset":
                AnalyticsModel = new RateOptionAssetAnalytic();
                break;

            case "DiscountCapAsset":
                AnalyticsModel = new RateOptionAssetAnalytic();
                break;
            }
            var metrics = MetricsHelper.GetMetricsToEvaluate(Metrics, AnalyticsModel.Metrics);
            // Determine if DFAM has been requested - if so thats all we evaluate - every other metric is ignored
            var bEvalLastForewardVolatiltiy = false;

            if (metrics.Contains(RateOptionMetrics.VolatilityAtExpiry))
            {
                bEvalLastForewardVolatiltiy = true;
                metrics.RemoveAll(
                    metricItem => metricItem != RateOptionMetrics.VolatilityAtExpiry);
            }
            var metricsToEvaluate = metrics.ToArray();
            IRateOptionAssetParameters analyticModelParameters = new RateOptionAssetParameters();

            AnalyticResults = new RateOptionAssetResults();
            var marketEnvironment = modelData.MarketEnvironment;

            //1. instantiate curve
            if (marketEnvironment.GetType() == typeof(SimpleMarketEnvironment))
            {
                DiscountCurve     = (IRateCurve)((ISimpleMarketEnvironment)marketEnvironment).GetPricingStructure();
                DiscountCurveName = DiscountCurve.GetPricingStructureId().UniqueIdentifier;
                ForecastCurve     = DiscountCurve;
                ForecastCurveName = DiscountCurveName;
                var volatilities = CreateList((double)Volatility, TimesToExpiry.Count);
                analyticModelParameters.Volatilities = volatilities;
            }
            if (marketEnvironment.GetType() == typeof(SwapLegEnvironment))
            {
                DiscountCurve       = ((ISwapLegEnvironment)marketEnvironment).GetDiscountRateCurve();
                DiscountCurveName   = DiscountCurve.GetPricingStructureId().UniqueIdentifier;
                ForecastCurve       = ((ISwapLegEnvironment)marketEnvironment).GetForecastRateCurve();
                ForecastCurveName   = ForecastCurve.GetPricingStructureId().UniqueIdentifier;
                VolatilityCurve     = ((ISwapLegEnvironment)marketEnvironment).GetVolatilitySurface();
                VolatilityCurveName = VolatilityCurve.GetPricingStructureId().UniqueIdentifier;
                analyticModelParameters.Volatilities = GetVolatilties(VolatilityCurve, TimesToExpiry, Strikes);
            }
            if (marketEnvironment.GetType() == typeof(MarketEnvironment))
            {
                if (DiscountCurveName != null)
                {
                    DiscountCurve = (IRateCurve)modelData.MarketEnvironment.GetPricingStructure(DiscountCurveName);
                }
                if (ForecastCurveName != null)
                {
                    ForecastCurve = (IRateCurve)modelData.MarketEnvironment.GetPricingStructure(ForecastCurveName);
                }
                if (VolatilityCurveName != null)
                {
                    VolatilityCurve = (IVolatilitySurface)modelData.MarketEnvironment.GetPricingStructure(VolatilityCurveName);
                }
                analyticModelParameters.Volatilities = GetVolatilties(VolatilityCurve, TimesToExpiry, Strikes);
            }
            analyticModelParameters.FlatVolatility = Volatility;
            //2. Set the premium
            if (Premium == null)
            {
                Premium = CalculatePremium();
            }
            if (Premium != null)
            {
                analyticModelParameters.Premium = (double)Premium;
            }
            if (bEvalLastForewardVolatiltiy)
            {
                //3. Set the start diccount factor and vol.
                analyticModelParameters.PremiumPaymentDiscountFactor =
                    GetDiscountFactor(DiscountCurve, AdjustedStartDate, modelData.ValuationDate);
                //4. Get the respective year fractions
                analyticModelParameters.YearFractions = GetYearFractions();
                //5. Set the anaytic input parameters and Calculate the respective metrics
                AnalyticResults =
                    AnalyticsModel.Calculate <IRateOptionAssetResults, RateOptionAssetResults>(analyticModelParameters,
                                                                                               metricsToEvaluate);
            }
            else
            {
                analyticModelParameters.IsDiscounted = false;
                analyticModelParameters.IsPut        = !IsCap;
                analyticModelParameters.Notionals    = Notionals;
                analyticModelParameters.ForwardRates = ResetRates;
                //2. Get the discount factors
                analyticModelParameters.PaymentDiscountFactors =
                    GetDiscountFactors(DiscountCurve, AdjustedPeriodDates.ToArray(), modelData.ValuationDate);
                analyticModelParameters.ForecastDiscountFactors =
                    GetDiscountFactors(ForecastCurve, AdjustedPeriodDates.ToArray(), modelData.ValuationDate);
                //3. Get the respective year fractions
                analyticModelParameters.YearFractions = GetYearFractions();
                analyticModelParameters.Strikes       = Strikes;
                analyticModelParameters.Rate          = (double)CalculateImpliedParRate(modelData.ValuationDate);
                analyticModelParameters.TimesToExpiry = TimesToExpiry;
                //5. Set the anaytic input parameters and Calculate the respective metrics
                AnalyticResults =
                    AnalyticsModel.Calculate <IRateOptionAssetResults, RateOptionAssetResults>(analyticModelParameters,
                                                                                               metricsToEvaluate);
            }
            return(GetValue(AnalyticResults));
        }