Beispiel #1
0
        public void TestValuationCoordinator()
        {
            Date    exerciseDate = new Date(2017, 08, 28);
            string  shareCode    = "AAA";
            double  strike       = 100.0;
            Product p            = new EuropeanOption(new Share(shareCode, Currency.ZAR), strike, exerciseDate);

            Share[] shares    = new Share[] { new Share(shareCode, Currency.ZAR) };// One needs to know the index that will be required by the product to simulate it.
            Date    valueDate = new Date(2016, 08, 28);

            double[] divYield  = new double[] { 0.02 };
            double[] vol       = new double[] { 0.22 };
            double[] spotPrice = new double[] { 100.0 };
            double[,] correlations = new double[, ] {
                { 1.0 }
            };
            IDiscountingSource discountCurve = new DatesAndRates(Currency.ZAR, valueDate,
                                                                 new Date[] { valueDate, valueDate.AddMonths(120) },
                                                                 new double[] { 0.07, 0.07 });

            IFloatingRateSource[] rateForecastCurves = new IFloatingRateSource[0];

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

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

            Assert.AreEqual(refValue, value, refValue * 0.05);
        }
        public void TestDynamicCallFromFile()
        {
            Stopwatch watch;
            // Make a product at runtime
            Product runtimeProduct = RuntimeProduct.CreateFromSourceFile(@"ScriptEuropeanOption.txt");

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

            double[] divYield  = new double[] { 0.02 };
            double[] vol       = new double[] { 0.22 };
            double[] spotPrice = new double[] { 100.0 };
            double[,] correlations = new double[, ] {
                { 1.0 }
            };
            IDiscountingSource discountCurve = new DatesAndRates(Currency.ZAR, valueDate,
                                                                 new Date[] { valueDate, valueDate.AddMonths(120) },
                                                                 new double[] { 0.07, 0.07 });

            IFloatingRateSource[] rateForecastCurves = new IFloatingRateSource[0];

            EquitySimulator 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();
            double valueRuntime = coordinator.Value(new Product[] { runtimeProduct }, valueDate);

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

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

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

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

            double refValue = Formulae.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);
        }
        public void TestEquitySimulatorMultiAssetCall()
        {
            // The model
            EquitySimulator sim         = new EquitySimulator(shares, prices, vols, divYields, correlations, discountCurve, new IFloatingRateSource[0]);
            Coordinator     coordinator = new Coordinator(sim, new List <Simulator> {
            }, 10000);

            // Products
            Date   exerciseDate = new Date(2017, 08, 28);
            int    p;
            double strike;

            // ALSI
            p      = 0;
            strike = prices[p] * 1.05;
            Product call0     = new EuropeanOption(shares[p], strike, exerciseDate);
            double  value0    = coordinator.Value(new Product[] { call0 }, anchorDate);
            double  refValue0 = Formulae.BlackScholes(PutOrCall.Call, strike, (exerciseDate - anchorDate) / 365.0, prices[p],
                                                      vols[p], 0.07, divYields[p]);

            Assert.AreEqual(refValue0, value0, refValue0 * 0.05);

            // AAA
            p      = 1;
            strike = prices[p] * 1.05;
            Product call1     = new EuropeanOption(shares[p], strike, exerciseDate);
            double  value1    = coordinator.Value(new Product[] { call1 }, anchorDate);
            double  refValue1 = Formulae.BlackScholes(PutOrCall.Call, strike, (exerciseDate - anchorDate) / 365.0, prices[p],
                                                      vols[p], 0.07, divYields[p]);

            Assert.AreEqual(refValue1, value1, refValue1 * 0.05);

            // BBB
            p      = 2;
            strike = prices[p] * 1.05;
            Product call2     = new EuropeanOption(shares[p], strike, exerciseDate);
            double  value2    = coordinator.Value(new Product[] { call2 }, anchorDate);
            double  refValue2 = Formulae.BlackScholes(PutOrCall.Call, strike, (exerciseDate - anchorDate) / 365.0, prices[p],
                                                      vols[p], 0.07, divYields[p]);

            Assert.AreEqual(refValue1, value1, refValue1 * 0.05);

            // All at once
            double valueAll = coordinator.Value(new Product[] { call0, call1, call2 }, anchorDate);
            double refTotal = refValue0 + refValue1 + refValue2;

            Assert.AreEqual(refTotal, valueAll, refTotal * 0.05);
        }
        public void TestDynamicCallFromString()
        {
            string source =
                @"Date exerciseDate = new Date(2017, 08, 28);
Share share = new Share(""AAA"", 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
            Product runtimeProduct = RuntimeProduct.CreateFromString("MyEuropeanOption", source);

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

            double[] divYield  = new double[] { 0.02 };
            double[] vol       = new double[] { 0.22 };
            double[] spotPrice = new double[] { 100.0 };
            double[,] correlations = new double[, ] {
                { 1.0 }
            };
            IDiscountingSource discountCurve = new DatesAndRates(Currency.ZAR, valueDate,
                                                                 new Date[] { valueDate, valueDate.AddMonths(120) },
                                                                 new double[] { 0.07, 0.07 });

            IFloatingRateSource[] rateForecastCurves = new IFloatingRateSource[0];

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

            // Value the runtime product
            Coordinator coordinator;

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

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

            Assert.AreEqual(refValue, valueRuntime, refValue * 0.03);
        }
Beispiel #5
0
 public static object FormulaBlackScholes([ExcelArgument(Description = "Strike")] object[,] strike,
                                          [ExcelArgument(Description = "The value date as and Excel date.")] object[,] valueDate,
                                          [ExcelArgument(Description = "The exercise date of the option.  Must be greater than the value date.")] object[,] exerciseDate,
                                          [ExcelArgument(Description = "The spot proce of the underlying at the value date.")] object[,] spotPrice,
                                          [ExcelArgument(Description = "Annualized volatility.")] object[,] vol,
                                          [ExcelArgument(Description = "Continuously compounded risk free rate.")] object[,] riskfreeRate,
                                          [ExcelArgument(Description = "Continuously compounded dividend yield.")] object[,] divYield)
 {
     try {
         return(Formulae.BlackScholes(PutOrCall.Call, XU.GetDouble0D(strike, "strike"),
                                      (XU.GetDate0D(exerciseDate, "exerciseDate") - XU.GetDate0D(valueDate, "valueDate")) / 365.0,
                                      XU.GetDouble0D(spotPrice, "spotPrice"), XU.GetDouble0D(vol, "vol"),
                                      XU.GetDouble0D(riskfreeRate, "riskfreeRate"), XU.GetDouble0D(divYield, "divYield")));
     } catch (Exception e)
     {
         return(XU.Error0D(e));
     }
 }