Ejemplo n.º 1
0
        public void TestValuationCoordinator()
        {
            var     exerciseDate = new Date(2017, 08, 28);
            var     shareCode    = "AAA";
            var     strike       = 100.0;
            Product p            = new EuropeanOption(new Share(shareCode, Currency.ZAR), strike, exerciseDate);

            var shares = new[]
            {
                new Share(shareCode, Currency.ZAR)
            }; // One needs to know the index that will be required by the product to simulate it.
            var valueDate    = new Date(2016, 08, 28);
            var divYield     = new[] { 0.02 };
            var vol          = new[] { 0.22 };
            var spotPrice    = new[] { 100.0 };
            var correlations = new[, ] {
                { 1.0 }
            };
            IDiscountingSource discountCurve = new DatesAndRates(Currency.ZAR, valueDate,
                                                                 new[] { valueDate, valueDate.AddMonths(120) },
                                                                 new[] { 0.07, 0.07 });
            var rateForecastCurves = new IFloatingRateSource[0];

            var sim = new EquitySimulator(shares, spotPrice, vol, divYield,
                                          correlations, discountCurve, rateForecastCurves);

            var coordinator = new Coordinator(sim, new List <Simulator>(), 10000);
            var value       = coordinator.Value(new[] { p }, valueDate);
            var refValue    = BlackEtc.BlackScholes(PutOrCall.Call, strike, (exerciseDate - valueDate) / 365.0,
                                                    spotPrice[0],
                                                    vol[0], 0.07, divYield[0]);

            Assert.AreEqual(refValue, value, refValue * 0.05);
        }
 /// <summary>
 /// Will use the discount factors to obtain the forward rates after the curve's anchor date and the fixing curve before that date.
 /// </summary>
 /// <param name="discountCurve"></param>
 /// <param name="index"></param>
 /// <param name="fixingCurve"></param>
 public ForecastCurveFromDiscount(IDiscountingSource discountCurve, FloatingIndex index,
                                  IFloatingRateSource fixingCurve)
 {
     this.discountCurve = discountCurve;
     this.index         = index;
     this.fixingCurve   = fixingCurve;
 }
Ejemplo n.º 3
0
        public static IFloatingRateSource CreateRateForecastCurveFromDiscount(
            [ExcelArgument(Description = "The floating rate that this curve will be used to forecast.")]
            FloatRateIndex floatingRateIndex,
            [ExcelArgument(Description =
                               "The name of the discount curve that will be used to obtain the forward rates.")]
            IDiscountingSource discountCurve,
            [QuantSAExcelArgument(
                 Description =
                     "Optional: The name of the fixing curve for providing floating rates at dates before the anchor date of the discount curve.  If it is left out then the first floating rate implied by the discount curve will be used for all historical fixes.",
                 Default = null)]
            IFloatingRateSource fixingCurve)
        {
            if (fixingCurve == null)
            {
                // Calculate the first fixing off the curve to use at all past dates.
                var df1       = 1.0;
                var laterDate = discountCurve.GetAnchorDate().AddTenor(floatingRateIndex.Tenor);
                var df2       = discountCurve.GetDF(laterDate);
                var dt        = (laterDate - discountCurve.GetAnchorDate()) / 365.0;
                var rate      = (df1 / df2 - 1) / dt;
                fixingCurve = new FloatingRateFixingCurve1Rate(discountCurve.GetAnchorDate(), rate, floatingRateIndex);
            }

            return(new ForecastCurveFromDiscount(discountCurve, floatingRateIndex, fixingCurve));
        }
Ejemplo n.º 4
0
        public void TestIndexOnlyProvidedOnce()
        {
            var     exerciseDate = new Date(2017, 08, 28);
            var     shareCode    = "AAA";
            var     strike       = 100.0;
            Product p            = new EuropeanOption(new Share(shareCode, Currency.ZAR), strike, exerciseDate);

            var shares = new[]
            {
                new Share("AAA", Currency.ZAR)
            }; // One needs to know the index that will be required by the product to simulate it.
            var valueDate    = new Date(2016, 08, 28);
            var divYield     = new[] { 0.02 };
            var vol          = new[] { 0.22 };
            var spotPrice    = new[] { 100.0 };
            var correlations = new[, ] {
                { 1.0 }
            };
            IDiscountingSource discountCurve = new DatesAndRates(Currency.ZAR, valueDate,
                                                                 new[] { valueDate, valueDate.AddMonths(120) },
                                                                 new[] { 0.07, 0.07 });
            var rateForecastCurves = new IFloatingRateSource[0];

            var sim = new EquitySimulator(shares, spotPrice, vol, divYield,
                                          correlations, discountCurve, rateForecastCurves);

            var coordinator = new Coordinator(sim, new List <Simulator> {
                sim
            }, 1000);

            var value = coordinator.Value(new[] { p }, valueDate);
        }
 /// <summary>
 /// Will use the discount factors to obtain the forward rates after the curve's anchor date and the fixing curve before
 /// that date.
 /// </summary>
 /// <param name="discountCurve"></param>
 /// <param name="index"></param>
 /// <param name="fixingCurve"></param>
 public ForecastCurveFromDiscount(IDiscountingSource discountCurve, FloatRateIndex index,
                                  IFloatingRateSource fixingCurve)
 {
     _discountCurve = discountCurve;
     _index         = index;
     _fixingCurve   = fixingCurve;
     _name          = new FloatingRateSourceDescription(index).Name;
 }
Ejemplo n.º 6
0
        public void TestDynamicCallFromFile()
        {
            Stopwatch watch;
            // Make a product at runtime
            var runtimeProduct = RuntimeProduct.CreateFromScript(@"ScriptEuropeanOption.txt");

            // Setup an appropriate simulation
            var shares = new[]
            {
                new Share("AAA", TestHelpers.ZAR)
            }; // One needs to know the index that will be required by the product to simulate it.
            var valueDate = new Date(2016, 08, 28);

            var divYield     = new[] { 0.02 };
            var vol          = new[] { 0.22 };
            var spotPrice    = new[] { 100.0 };
            var correlations = new[, ] {
                { 1.0 }
            };
            IDiscountingSource discountCurve = new DatesAndRates(TestHelpers.ZAR, valueDate,
                                                                 new[] { valueDate, valueDate.AddMonths(120) },
                                                                 new[] { 0.07, 0.07 });
            var rateForecastCurves = new IFloatingRateSource[0];

            var sim = new EquitySimulator(shares, spotPrice, vol, divYield,
                                          correlations, discountCurve, rateForecastCurves);

            // Value the runtime product
            Coordinator coordinator;

            coordinator = new Coordinator(sim, new List <Simulator>(), 100000);
            watch       = Stopwatch.StartNew();
            var valueRuntime = coordinator.Value(new[] { runtimeProduct }, valueDate);

            watch.Stop();
            var timeRuntime = watch.ElapsedMilliseconds;

            // Setup the same product statically
            var     exerciseDate  = new Date(2017, 08, 28);
            var     strike        = 100.0;
            Product staticProduct = new EuropeanOption(new Share("AAA", TestHelpers.ZAR), strike, exerciseDate);

            // Value the static product
            coordinator = new Coordinator(sim, new List <Simulator>(), 100000);
            watch       = Stopwatch.StartNew();
            var valueStatic = coordinator.Value(new[] { staticProduct }, valueDate);

            watch.Stop();
            var timeStatic = watch.ElapsedMilliseconds;

            var refValue = BlackEtc.BlackScholes(PutOrCall.Call, strike, (exerciseDate - valueDate) / 365, spotPrice[0],
                                                 vol[0], 0.07, divYield[0]);

            Assert.AreEqual(refValue, valueRuntime, refValue * 0.03);
            Assert.AreEqual(refValue, valueStatic, refValue * 0.03);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Adds a source for interest rate forecasts.
 /// </summary>
 /// <param name="forecastCurve">The forecast curve.</param>
 /// <exception cref="System.ArgumentException"></exception>
 /// <seealso cref="AddFXForecast(IFXSource[])"/>
 public void AddRateForecast(IFloatingRateSource forecastCurve)
 {
     if (!forecastCurves.ContainsKey(forecastCurve.GetFloatingIndex()))
     {
         forecastCurves.Add(forecastCurve.GetFloatingIndex(), forecastCurve);
     }
     else
     {
         throw new ArgumentException(forecastCurve.GetFloatingIndex() + " has already been added to the model.");
     }
 }
Ejemplo n.º 8
0
        public void TestDynamicCallFromString()
        {
            var source =
                @"Date exerciseDate = new Date(2017, 08, 28);
Share share = new Share(""AAA"", new Currency(""ZAR""));
double strike = 100.0;

public override List<Cashflow> GetCFs()
{
    double amount = Math.Max(0, Get(share, exerciseDate) - strike);
    return new List<Cashflow> { new Cashflow(exerciseDate, amount, share.Currency) };
}";
            // Make a product at runtime
            var runtimeProduct = RuntimeProduct.CreateFromString("MyEuropeanOption", source);

            // Setup an approriate simulation
            var shares = new[]
            {
                new Share("AAA", TestHelpers.ZAR)
            }; // One needs to know the index that will be required by the product to simulate it.
            var valueDate = new Date(2016, 08, 28);

            var divYield     = new[] { 0.02 };
            var vol          = new[] { 0.22 };
            var spotPrice    = new[] { 100.0 };
            var correlations = new[, ] {
                { 1.0 }
            };
            IDiscountingSource discountCurve = new DatesAndRates(TestHelpers.ZAR, valueDate,
                                                                 new[] { valueDate, valueDate.AddMonths(120) },
                                                                 new[] { 0.07, 0.07 });
            var rateForecastCurves = new IFloatingRateSource[0];

            var sim = new EquitySimulator(shares, spotPrice, vol, divYield,
                                          correlations, discountCurve, rateForecastCurves);

            // Value the runtime product
            Coordinator coordinator;

            coordinator = new Coordinator(sim, new List <Simulator>(), 100000);
            var valueRuntime = coordinator.Value(new[] { runtimeProduct }, valueDate);

            var exerciseDate = new Date(2017, 08, 28);
            var strike       = 100.0;
            var refValue     = BlackEtc.BlackScholes(PutOrCall.Call, strike, (exerciseDate - valueDate) / 365, spotPrice[0],
                                                     vol[0], 0.07, divYield[0]);

            Assert.AreEqual(refValue, valueRuntime, refValue * 0.03);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Curve based valuation of <see cref="FloatLeg"/>.  Assumes that correct forecast and discount curves have been provided.
        /// </summary>
        /// <param name="leg"></param>
        /// <param name="forecastCurve"></param>
        /// <param name="discountCurve"></param>
        /// <returns></returns>
        public static double CurvePV(this FloatLeg leg, IFloatingRateSource forecastCurve,
                                     IDiscountingSource discountCurve)
        {
            var legIndex     = forecastCurve.GetFloatingIndex();
            var resetDates1  = leg.GetRequiredIndexDates(legIndex);
            var indexValues1 = new double[resetDates1.Count];

            for (var i = 0; i < resetDates1.Count; i++)
            {
                indexValues1[i] = forecastCurve.GetForwardRate(resetDates1[i]);
            }
            leg.SetIndexValues(legIndex, indexValues1);
            var cfs1   = leg.GetCFs();
            var value1 = cfs1.PV(discountCurve);

            return(value1);
        }
Ejemplo n.º 10
0
 public static object _CreateRateForecastCurveFromDiscount(string objectName,
                                                           object[,] floatingRateIndex,
                                                           object[,] discountCurve,
                                                           object[,] fixingCurve)
 {
     try
     {
         FloatingIndex       _floatingRateIndex = XU.GetFloatingIndex0D(floatingRateIndex, "floatingRateIndex");
         IDiscountingSource  _discountCurve     = XU.GetObject0D <IDiscountingSource>(discountCurve, "discountCurve");
         IFloatingRateSource _fixingCurve       = XU.GetObject0D <IFloatingRateSource>(fixingCurve, "fixingCurve", null);
         IFloatingRateSource _result            = XLRates.CreateRateForecastCurveFromDiscount(_floatingRateIndex, _discountCurve, _fixingCurve);
         return(XU.AddObject(objectName, _result));
     }
     catch (Exception e)
     {
         return(XU.Error0D(e));
     }
 }
Ejemplo n.º 11
0
        public static ResultStore InflationLinkedSwapMeasures(this InflationLinkedSwap inflationLinkedSwap, Date[] cpiDates, double[] cpiRates, IFloatingRateSource forecastCurve)
        {
            //Create Inflation Swap
            var swap = CreateInflationLinkedSwap(inflationLinkedSwap.payFixed, inflationLinkedSwap.startDate, inflationLinkedSwap.nominal, inflationLinkedSwap.tenor,
                                                 inflationLinkedSwap.fixedRate, inflationLinkedSwap.index, inflationLinkedSwap.spread, inflationLinkedSwap.zaCalendar, inflationLinkedSwap.ccy);

            //Set value date
            swap.SetValueDate(inflationLinkedSwap.startDate);

            //Set index values
            var indexValues = new double[swap.indexDates.Length];

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

            //Determine swap end date
            var unAdjEndDate = inflationLinkedSwap.startDate.AddMonths(12 * inflationLinkedSwap.tenor.Years);
            var endDate      = BusinessDayStore.ModifiedFollowing.Adjust(unAdjEndDate, inflationLinkedSwap.zaCalendar);

            //Calculate value of fixed and floating cashflows
            var floatingCashFlows    = swap.GetCFs();
            var floatingLegCashFlows = floatingCashFlows.Last().Amount;

            var cpiStartDate = LaggedCPI.GetCPI(inflationLinkedSwap.startDate, cpiDates, cpiRates);
            var cpiEndDate   = LaggedCPI.GetCPI(endDate, cpiDates, cpiRates);

            var fixedCashFlows = inflationLinkedSwap.payFixed * inflationLinkedSwap.nominal * Math.Pow((1 + inflationLinkedSwap.fixedRate / 2),
                                                                                                       2 * (endDate - inflationLinkedSwap.startDate) / 365) * cpiEndDate / cpiStartDate;

            var netCashFlows = floatingLegCashFlows + fixedCashFlows;

            // Store results
            var results = new ResultStore();

            results.Add(Keys.FloatingLegCashFlows, floatingLegCashFlows);
            results.Add(Keys.FixedLegCashFlows, fixedCashFlows);
            results.Add(Keys.NetCashFlows, netCashFlows);

            return(results);
        }
 public double PV(Date valueDate, IDiscountingSource discountingCurve, IFloatingRateSource forecastingCurve)
 {
     return(0.0);
 }
 public void SetMarketData(IMarketDataContainer marketData)
 {
     _curve = marketData.Get(_floatingRateSourceDescription);
 }
 public ForwardRatesCurveForStripping(Date anchorDate, FloatingIndex index)
 {
     this.anchorDate = anchorDate;
     this.index      = index;
     underlyingCurve = new ZeroFloatingRates(index);
 }
 public ForwardRatesCurveForStripping(Date anchorDate, FloatingIndex index, IDiscountingSource underlyingCurve)
 {
     this.anchorDate      = anchorDate;
     this.index           = index;
     this.underlyingCurve = new DiscountBasedFloatingRates(index, underlyingCurve);
 }
 public ForwardRatesCurveForStripping(Date anchorDate, FloatingIndex index, IFloatingRateSource underlyingCurve)
 {
     this.anchorDate      = anchorDate;
     this.index           = index;
     this.underlyingCurve = underlyingCurve;
 }
Ejemplo n.º 17
0
 public void SetMarketData(IMarketDataContainer marketData)
 {
     _leg1Curve     = marketData.Get(new FloatingRateSourceDescription(_leg1Index));
     _leg2Curve     = marketData.Get(new FloatingRateSourceDescription(_leg2Index));
     _discountCurve = marketData.Get(_discountCurveDescription);
 }
Ejemplo n.º 18
0
 public void SetMarketData(IMarketDataContainer marketData)
 {
     _forecastCurve = marketData.Get(new FloatingRateSourceDescription(_index));
     _discountCurve = marketData.Get(_discountCurveDescription);
 }