Beispiel #1
0
        public void ConstVol()
        {
            var constVolA       = 0.32;
            var constVolB       = 0.16;
            var originDate      = new DateTime(2017, 02, 21);
            var impliedSurfaceA = new ConstantVolSurface(originDate, constVolA)
            {
                AssetId = "A"
            };
            var impliedSurfaceB = new ConstantVolSurface(originDate, constVolB)
            {
                AssetId = "B"
            };

            var model = new AssetFxModel(originDate, null);

            model.AddVolSurface("A", impliedSurfaceA);
            model.AddVolSurface("B", impliedSurfaceB);

            var timesteps    = Enumerable.Range(1, 4).Select(x => x / 4.0).ToArray();
            var termCorrels  = new[] { 0.9, 0.85, 0.8, 0.75 };
            var correlVector = new CorrelationTimeVector("A", "B", termCorrels, timesteps);

            model.CorrelationMatrix = correlVector;

            var lc    = model.LocalCorrelationRaw(timesteps);
            var lcVec = lc.Select(x => x[0][0]).ToArray();

            for (var i = 0; i < termCorrels.Length; i++)
            {
                var expected = lcVec.Take(i + 1).Average();
                Assert.Equal(expected, termCorrels[i], 8);
            }
        }
Beispiel #2
0
        private AssetFxMCModel GetSut()
        {
            var buildDate = DateTime.Parse("2018-10-04");
            var usd       = TestProviderHelper.CurrencyProvider["USD"];

            TestProviderHelper.CalendarProvider.Collection.TryGetCalendar("NYC", out var usdCal);
            var dfCurve = new IrCurve(new[] { buildDate, buildDate.AddDays(1000) }, new[] { 0.0, 0.0 }, buildDate, "disco", Interpolator1DType.Linear, usd, "DISCO");

            var comCurve = new BasicPriceCurve(buildDate, new[] { buildDate, buildDate.AddDays(15), buildDate.AddDays(100) }, new[] { 100.0, 100.0, 100.0 }, PriceCurveType.NYMEX, TestProviderHelper.CurrencyProvider)
            {
                Name    = "CL",
                AssetId = "CL"
            };
            var comSurface = new ConstantVolSurface(buildDate, 0.32)
            {
                AssetId = "CL"
            };
            var fModel = new FundingModel(buildDate, new Dictionary <string, IrCurve> {
                { "DISCO", dfCurve }
            }, TestProviderHelper.CurrencyProvider, TestProviderHelper.CalendarProvider);
            var fxM = new FxMatrix(TestProviderHelper.CurrencyProvider);

            fxM.Init(usd, buildDate, new Dictionary <Currency, double>(), new List <FxPair>(), new Dictionary <Currency, string> {
                { usd, "DISCO" }
            });
            fModel.SetupFx(fxM);

            var aModel = new AssetFxModel(buildDate, fModel);

            aModel.AddVolSurface("CL", comSurface);
            aModel.AddPriceCurve("CL", comCurve);

            var product = AssetProductFactory.CreateAsianOption(buildDate.AddDays(10), buildDate.AddDays(20), 101, "CL", OptionType.Call, usdCal, buildDate.AddDays(21), usd);

            product.TradeId       = "waaah";
            product.DiscountCurve = "DISCO";


            var pfolio = new Portfolio {
                Instruments = new List <IInstrument> {
                    product
                }
            };
            var settings = new McSettings
            {
                Generator         = RandomGeneratorType.MersenneTwister,
                NumberOfPaths     = (int)System.Math.Pow(2, 13),
                NumberOfTimesteps = 1,
                ReportingCurrency = usd,
                Parallelize       = false,
                CreditSettings    = new CreditSettings
                {
                    ExposureDates = new DateTime[] { buildDate.AddDays(5), buildDate.AddDays(20), buildDate.AddDays(22) }
                },
            };
            var sut = new AssetFxMCModel(buildDate, pfolio, aModel, settings, TestProviderHelper.CurrencyProvider, TestProviderHelper.FutureSettingsProvider, TestProviderHelper.CalendarProvider);

            return(sut);
        }
Beispiel #3
0
        private AssetFxModel GetModel()
        {
            var irCurveZar = new ConstantRateIrCurve(0.07, ValDate, "ZAR-IR", zar);
            var irCurveUsd = new ConstantRateIrCurve(0.02, ValDate, "USD-IR", zar);
            var fxMatrix   = new FxMatrix(TestProviderHelper.CurrencyProvider);
            var fxPair     = new FxPair()
            {
                Domestic          = usd,
                Foreign           = zar,
                PrimaryCalendar   = TestProviderHelper.CalendarProvider.GetCalendar("ZAR"),
                SecondaryCalendar = TestProviderHelper.CalendarProvider.GetCalendar("USD"),
                SpotLag           = new Frequency("2b")
            };

            fxMatrix.Init(usd, ValDate, new Dictionary <Currency, double> {
                { zar, 20.0 }
            }, new List <FxPair> {
                fxPair
            }, new Dictionary <Currency, string> {
                { zar, "ZAR-IR" }, { usd, "USD-IR" }
            });
            var fModel = new FundingModel(ValDate, new[] { irCurveUsd, irCurveZar }, TestProviderHelper.CurrencyProvider, TestProviderHelper.CalendarProvider);

            fModel.SetupFx(fxMatrix);
            var fxSurface = new ConstantVolSurface(ValDate, 0.16);

            fModel.VolSurfaces.Add("USD/ZAR", fxSurface);
            var crudeCurve = new ConstantPriceCurve(100, ValDate, TestProviderHelper.CurrencyProvider)
            {
                Name     = "OIL",
                AssetId  = "OIL",
                Currency = usd
            };
            var crudeSurface = new ConstantVolSurface(ValDate, 0.32)
            {
                Name     = "OIL",
                AssetId  = "OIL",
                Currency = usd
            };
            var aModel = new AssetFxModel(ValDate, fModel);

            aModel.AddPriceCurve("OIL", crudeCurve);
            aModel.AddVolSurface("OIL", crudeSurface);
            aModel.CorrelationMatrix = new CorrelationMatrix(new[] { "OIL" }, new[] { "USD/ZAR" }, new double[][] { new [] { 0.5 } });
            return(aModel);
        }
Beispiel #4
0
        public static object CreateAssetFxModel(
            [ExcelArgument(Description = "Object name")] string ObjectName,
            [ExcelArgument(Description = "Build date")] DateTime BuildDate,
            [ExcelArgument(Description = "Price curves")] object[] PriceCurves,
            [ExcelArgument(Description = "Vol surfaces")] object[] VolSurfaces,
            [ExcelArgument(Description = "Funding model")] object[] FundingModel,
            [ExcelArgument(Description = "Fixing dictionaries")] object[] Fixings,
            [ExcelArgument(Description = "Correlation matrix")] object[] CorrelationMatrix)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var curves = PriceCurves.GetAnyFromCache <IPriceCurve>();
                var fundingModel = FundingModel.GetAnyFromCache <IFundingModel>().First();
                var fixings = Fixings.GetAnyFromCache <IFixingDictionary>();
                var volSurfaces = VolSurfaces.GetAnyFromCache <IVolSurface>();
                var correlatinMatrix = CorrelationMatrix.GetAnyFromCache <ICorrelationMatrix>();

                var model = new AssetFxModel(BuildDate, fundingModel);

                foreach (var curve in curves)
                {
                    model.AddPriceCurve(curve.Name, curve);
                }
                foreach (var vs in volSurfaces)
                {
                    model.AddVolSurface(vs.Name, vs);
                }
                foreach (var f in fixings)
                {
                    model.AddFixingDictionary(f.Name, f);
                }

                if (correlatinMatrix.Any())
                {
                    model.CorrelationMatrix = correlatinMatrix.First();
                }

                return ExcelHelper.PushToCache <IAssetFxModel>(model, ObjectName);
            }));
        }
Beispiel #5
0
        private AssetFxModel GetModel()
        {
            var vol = new ConstantVolSurface(valDate, assetVol)
            {
                AssetId = assetId,
                Name    = assetId
            };

            var fwd = new ConstantPriceCurve(assetPrice, valDate, TestProviderHelper.CurrencyProvider)
            {
                AssetId = assetId,
                Name    = assetId
            };
            var ir = new FlatIrCurve(0.0, usd, "USD");
            var fm = new FundingModel(valDate, new[] { ir }, TestProviderHelper.CurrencyProvider, TestProviderHelper.CalendarProvider);
            var am = new AssetFxModel(valDate, fm);

            am.AddPriceCurve(assetId, fwd);
            am.AddVolSurface(assetId, vol);

            return(am);
        }
Beispiel #6
0
        private AssetFxMCModel GetSut()
        {
            var buildDate = DateTime.Parse("2018-10-04");
            var usd       = TestProviderHelper.CurrencyProvider["USD"];
            var zar       = TestProviderHelper.CurrencyProvider["ZAR"];

            TestProviderHelper.CalendarProvider.Collection.TryGetCalendar("NYC", out var usdCal);
            var pair = new FxPair()
            {
                Domestic = zar, Foreign = usd, PrimaryCalendar = usdCal, SpotLag = 2.Bd()
            };

            var dfCurve = new IrCurve(new[] { buildDate, buildDate.AddDays(1000) }, new[] { 0.0, 0.0 }, buildDate, "disco", Interpolator1DType.Linear, usd, "DISCO");

            var dates    = new[] { buildDate, buildDate.AddDays(32), buildDate.AddDays(60), buildDate.AddDays(90) };
            var times    = dates.Select(d => buildDate.CalculateYearFraction(d, DayCountBasis.Act365F)).ToArray();
            var vols     = new[] { 0.32, 0.30, 0.29, 0.28 };
            var comCurve = new PriceCurve(buildDate, dates, new[] { 100.0, 100.0, 100.0, 100.0 }, PriceCurveType.NYMEX, TestProviderHelper.CurrencyProvider)
            {
                Name    = "CL",
                AssetId = "CL"
            };
            var comSurface = new GridVolSurface(buildDate, new[] { 0.5 }, dates, vols.Select(x => new double[] { x }).ToArray(), StrikeType.ForwardDelta, Interpolator1DType.Linear, Interpolator1DType.LinearInVariance, DayCountBasis.Act365F)
            {
                AssetId = "CL"
            };
            var fxSurface = new ConstantVolSurface(buildDate, 0.16)
            {
                AssetId = "USD/ZAR"
            };
            var correlVector = new CorrelationTimeVector("CL", "USD/ZAR", _correls, times);
            var fModel       = new FundingModel(buildDate, new Dictionary <string, IrCurve> {
                { "DISCO", dfCurve }
            }, TestProviderHelper.CurrencyProvider, TestProviderHelper.CalendarProvider);
            var fxM = new FxMatrix(TestProviderHelper.CurrencyProvider);

            fxM.Init(usd, buildDate, new Dictionary <Currency, double>()
            {
                { zar, 14.0 }
            }, new List <FxPair>()
            {
                pair
            }, new Dictionary <Currency, string> {
                { usd, "DISCO" }, { zar, "DISCO" }
            });
            fModel.SetupFx(fxM);
            fModel.VolSurfaces.Add("ZAR/USD", fxSurface);
            fModel.VolSurfaces.Add("USD/ZAR", fxSurface);

            var aModel = new AssetFxModel(buildDate, fModel);

            aModel.AddVolSurface("CL", comSurface);
            aModel.AddPriceCurve("CL", comCurve);
            aModel.CorrelationMatrix = correlVector;

            var product1 = AssetProductFactory.CreateAsianOption(dates[1], dates[1], 1400, "CL", OptionType.Call, usdCal, dates[1], zar);

            product1.TradeId          = "P1";
            product1.DiscountCurve    = "DISCO";
            product1.FxConversionType = FxConversionType.AverageThenConvert;
            var product2 = AssetProductFactory.CreateAsianOption(dates[2], dates[2], 1400, "CL", OptionType.Call, usdCal, dates[2], zar);

            product2.TradeId          = "P2";
            product2.DiscountCurve    = "DISCO";
            product2.FxConversionType = FxConversionType.AverageThenConvert;
            var product3 = AssetProductFactory.CreateAsianOption(dates[3], dates[3], 1400, "CL", OptionType.Call, usdCal, dates[3], zar);

            product3.TradeId          = "P3";
            product3.DiscountCurve    = "DISCO";
            product3.FxConversionType = FxConversionType.AverageThenConvert;

            var pfolio = new Portfolio {
                Instruments = new List <IInstrument> {
                    product1, product2, product3
                }
            };
            var settings = new McSettings
            {
                Generator         = RandomGeneratorType.MersenneTwister,
                NumberOfPaths     = (int)2.0.IntPow(15),
                NumberOfTimesteps = 1,
                ReportingCurrency = zar,
                Parallelize       = false,
                LocalCorrelation  = true,
            };
            var sut = new AssetFxMCModel(buildDate, pfolio, aModel, settings, TestProviderHelper.CurrencyProvider, TestProviderHelper.FutureSettingsProvider, TestProviderHelper.CalendarProvider);

            return(sut);
        }
Beispiel #7
0
        private (IAssetFxModel startModel, IAssetFxModel endModel, Portfolio portfolio) GenerateTestData()
        {
            Utils.Parallel.ParallelUtils.Instance.MultiThreaded = false;

            var usd = TestProviderHelper.CurrencyProvider.GetCurrency("USD");
            var zar = TestProviderHelper.CurrencyProvider.GetCurrency("ZAR");
            var nyc = TestProviderHelper.CalendarProvider.Collection["NYC"];

            var originDate = DateTime.Parse("2019-04-25");
            var ins        = new FxForward
            {
                TradeId              = "TestA",
                DeliveryDate         = originDate.AddDays(30),
                DomesticCCY          = zar,
                ForeignCCY           = usd,
                DomesticQuantity     = 1e6,
                Strike               = 14,
                ForeignDiscountCurve = "DISCO-USD"
            };
            var pf = new Portfolio {
                Instruments = new List <IInstrument> {
                    ins
                }
            };

            var discoUsd = new FlatIrCurve(0.02, usd, "DISCO-USD");
            var discoZar = new FlatIrCurve(0.05, zar, "DISCO-ZAR");
            var fxpairs  = new List <FxPair>
            {
                new FxPair {
                    Domestic = usd, Foreign = zar, PrimaryCalendar = nyc, SpotLag = 2.Bd()
                },
                new FxPair {
                    Domestic = zar, Foreign = usd, PrimaryCalendar = nyc, SpotLag = 2.Bd()
                },
            };
            var fxMatrix = new FxMatrix(TestProviderHelper.CurrencyProvider);

            fxMatrix.Init(zar, originDate, new Dictionary <Currency, double> {
                { usd, 14.0 }
            }, fxpairs, new Dictionary <Currency, string> {
                { usd, "DISCO-USD" }, { zar, "DISCO-ZAR" }
            });

            var fModel = new FundingModel(originDate, new[] { discoUsd, discoZar }, TestProviderHelper.CurrencyProvider, TestProviderHelper.CalendarProvider);

            fModel.SetupFx(fxMatrix);
            var startModel = new AssetFxModel(originDate, fModel);

            startModel.AddFixingDictionary("FakeAsset", new FixingDictionary());
            startModel.AddPriceCurve("FakeAsset", new ConstantPriceCurve(100, originDate, TestProviderHelper.CurrencyProvider));
            startModel.AddVolSurface("FakeAsset", new ConstantVolSurface(originDate, 1.00)
            {
                AssetId = "FakeAsset", Currency = usd
            });

            var endFModel = fModel.DeepClone();

            endFModel.FxMatrix.SpotRates[usd] = 15;
            var endModel = startModel.Clone(endFModel);

            endModel.AddFixingDictionary("FakeAsset", new FixingDictionary());
            endModel.AddPriceCurve("FakeAsset", new ConstantPriceCurve(100, originDate, TestProviderHelper.CurrencyProvider));
            endModel.AddVolSurface("FakeAsset", new ConstantVolSurface(originDate, 1.00)
            {
                AssetId = "FakeAsset", Currency = usd
            });
            return(startModel, endModel, pf);
        }
Beispiel #8
0
        public void FixingsVolsCurvesFacts()
        {
            var fModel    = new Mock <IFundingModel>();
            var matrix    = new Mock <IFxMatrix>();
            var pair      = new FxPair();
            var dict      = new Mock <IFixingDictionary>();
            var surface   = new Mock <IVolSurface>();
            var surface2  = new Mock <IVolSurface>();
            var surfaceFx = new Mock <IVolSurface>();
            var curve     = new Mock <IPriceCurve>();

            curve.Setup(c => c.GetPriceForDate(DateTime.Today)).Returns(456.0);
            curve.Setup(c => c.GetPriceForFixingDate(DateTime.Today)).Returns(457.0);
            surface.Setup(s => s.AssetId).Returns("blah");
            surface2.Setup(s => s.AssetId).Returns("blah2");
            matrix.Setup(f => f.GetFxPair(It.IsAny <string>())).Returns(pair);
            fModel.Setup(f => f.GetFxRate(It.IsAny <DateTime>(), It.IsAny <Currency>(), It.IsAny <Currency>())).Returns(77.0);
            fModel.Setup(f => f.FxMatrix).Returns(matrix.Object);
            fModel.Setup(f => f.VolSurfaces).Returns(new Dictionary <string, IVolSurface> {
                { "bla/haa", surfaceFx.Object }
            });
            var sut = new AssetFxModel(DateTime.Today, fModel.Object);

            sut.AddPriceCurve("blah", curve.Object);
            sut.AddPriceCurves(new Dictionary <string, IPriceCurve> {
                { "blah2", curve.Object }
            });
            Assert.Same(curve.Object, sut.GetPriceCurve("blah"));
            Assert.Same(curve.Object, sut.GetPriceCurve("blah2"));

            sut.AddFixingDictionary("blah", dict.Object);
            sut.AddFixingDictionaries(new Dictionary <string, IFixingDictionary> {
                { "blah2", dict.Object }
            });
            Assert.Same(dict.Object, sut.GetFixingDictionary("blah"));
            Assert.Same(dict.Object, sut.GetFixingDictionary("blah2"));
            Assert.False(sut.TryGetFixingDictionary("wooo", out var flob));

            sut.AddVolSurface("blah", surface.Object);
            sut.AddVolSurfaces(new Dictionary <string, IVolSurface> {
                { "blah2", surface2.Object }
            });
            Assert.Same(surface.Object, sut.GetVolSurface("blah"));
            Assert.Same(surface2.Object, sut.GetVolSurface("blah2"));

            sut.GetVolForStrikeAndDate("blah", DateTime.Today, 123);
            surface.Verify(s => s.GetVolForAbsoluteStrike(123, DateTime.Today, 456), Times.Once);
            sut.GetVolForDeltaStrikeAndDate("blah", DateTime.Today, 123);
            surface.Verify(s => s.GetVolForDeltaStrike(123, DateTime.Today, 457.0), Times.Once);

            sut.GetAverageVolForStrikeAndDates("blah", new[] { DateTime.Today }, 123);
            surface.Verify(s => s.GetVolForAbsoluteStrike(123, DateTime.Today, 456), Times.Exactly(2));
            sut.GetAverageVolForMoneynessAndDates("blah", new[] { DateTime.Today }, 1.0);
            surface.Verify(s => s.GetVolForAbsoluteStrike(456, DateTime.Today, 456), Times.Exactly(2));

            sut.GetFxVolForStrikeAndDate("bla/haa", DateTime.Today, 123);
            surfaceFx.Verify(s => s.GetVolForAbsoluteStrike(123, DateTime.Today, 77), Times.Once);
            sut.GetFxVolForDeltaStrikeAndDate("bla/haa", DateTime.Today, 123);
            surfaceFx.Verify(s => s.GetVolForDeltaStrike(123, DateTime.Today, 77), Times.Once);

            sut.OverrideBuildDate(DateTime.MinValue);
            Assert.Equal(DateTime.MinValue, sut.BuildDate);
            sut.OverrideBuildDate(DateTime.Today);
        }