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); }
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 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); }
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 DeterministicCurves(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); }
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.FromYears(5); var swap = TestHelpers.CreateZARSwap(rate, payFixed, notional, startDate, tenor, TestHelpers.Jibar3M); // 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); 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); }
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 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.FromYears(1); var swap = TestHelpers.CreateZARSwap(rate, payFixed, notional, startDate, tenor, TestHelpers.Jibar3M); // 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(TestHelpers.ZAR, paymentDates, new[] { 1e6, 1e6, 1e6, 1e6 }, resetDates, new[] { TestHelpers.Jibar3M, TestHelpers.Jibar3M, TestHelpers.Jibar3M, TestHelpers.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(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 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 static NumeraireSimulator CreateCurveModel([QuantSAExcelArgument(Description = "The discounting curve")] IDiscountingSource discountCurve, [QuantSAExcelArgument(Description = "The floating rate forecast curves for all the rates that the products in the portfolio will need.")] IFloatingRateSource[] rateForecastCurves, [QuantSAExcelArgument(Description = "The FX rate forecast curves for all the cashflow currencies other than the discounting currency.", Default = null)] IFXSource[] fxForecastCurves) { var model = new DeterministicCurves(discountCurve); model.AddRateForecast(rateForecastCurves); if (fxForecastCurves != null) { model.AddFXForecast(fxForecastCurves); } return(model); }