Example #1
0
        public static object CubeToCSV(
            [ExcelArgument(Description = "Input cube name")] string InputObjectName,
            [ExcelArgument(Description = "Output filename")] string FileName)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var cubeCache = ContainerStores.GetObjectCache <ICube>();
                var inCube = cubeCache.GetObjectOrThrow(InputObjectName, $"Could not find cube {InputObjectName}");

                inCube.Value.ToCSVFile(FileName);

                return $"Saved to {FileName}";
            }));
        }
Example #2
0
        public static object CreateInvertedFxSurface(
            [ExcelArgument(Description = "Object name")] string ObjectName,
            [ExcelArgument(Description = "Input surface name")] string InputSurface)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                if (ContainerStores.GetObjectCache <IVolSurface>().TryGetObject(InputSurface, out var volSurface) && volSurface.Value is IATMVolSurface atmSurface)
                {
                    return ExcelHelper.PushToCache <IVolSurface>(new InverseFxSurface(ObjectName, atmSurface, ContainerStores.CurrencyProvider), ObjectName);
                }

                return $"Vol surface {InputSurface} not found in cache or could not be case to ATM Surface";
            }));
        }
Example #3
0
        public static object ExtractCurveFromModel(
            [ExcelArgument(Description = "Funding model name")] string FundingModelName,
            [ExcelArgument(Description = "Curve name")]  string CurveName,
            [ExcelArgument(Description = "Output curve object name")] string OutputName)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var model = ContainerStores.GetObjectCache <IFundingModel>().GetObject(FundingModelName).Value;

                return model.Curves.TryGetValue(CurveName, out var curve) ?
                ExcelHelper.PushToCache <IIrCurve>(curve, OutputName) :
                $"Curve {CurveName} not found in model";
            }));
        }
Example #4
0
        public static object GetAveragePrice(
            [ExcelArgument(Description = "Object name")] string ObjectName,
            [ExcelArgument(Description = "Price dates")] double[] Dates)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                if (ContainerStores.GetObjectCache <IPriceCurve>().TryGetObject(ObjectName, out var curve))
                {
                    return curve.Value.GetAveragePriceForDates(Dates.ToDateTimeArray());
                }

                return $"Price curve {ObjectName} not found in cache";
            }));
        }
Example #5
0
        public static object CreatePriceCurve(
            [ExcelArgument(Description = "Object name")] string ObjectName,
            [ExcelArgument(Description = "Asset Id")] string AssetId,
            [ExcelArgument(Description = "Build date")] DateTime BuildDate,
            [ExcelArgument(Description = "Array of pillar dates")] double[] Pillars,
            [ExcelArgument(Description = "Array of prices values")] double[] Prices,
            [ExcelArgument(Description = "Type of curve, e.g. LME, ICE, NYMEX etc")] object CurveType,
            [ExcelArgument(Description = "Array of pillar labels (optional)")] object PillarLabels,
            [ExcelArgument(Description = "Currency - default USD")] object Currency,
            [ExcelArgument(Description = "Collateral spec, required for delta calculation - default LIBOR.3M")] object CollateralSpec,
            [ExcelArgument(Description = "Spot lag, required for theta, default 0b")] object SpotLag,
            [ExcelArgument(Description = "Spot calendar, required for theta, default USD")] object SpotCalendar)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var curveTypeStr = CurveType.OptionalExcel("Linear");
                var ccy = Currency.OptionalExcel("USD");
                var colSpec = CollateralSpec.OptionalExcel("LIBOR.3M");
                var spotLagStr = SpotLag.OptionalExcel("0b");
                var spotCalStr = SpotCalendar.OptionalExcel("USD");

                if (!Enum.TryParse(curveTypeStr, out PriceCurveType cType))
                {
                    return $"Could not parse price curve type - {curveTypeStr}";
                }

                ContainerStores.SessionContainer.GetService <ICalendarProvider>().Collection.TryGetCalendar(ccy, out var ccyCal);
                ContainerStores.SessionContainer.GetService <ICalendarProvider>().Collection.TryGetCalendar(spotCalStr, out var spotCal);
                var ccyObj = ContainerStores.GlobalContainer.GetRequiredService <ICurrencyProvider>()[ccy];

                var labels = (PillarLabels is ExcelMissing) ? null : ((object[, ])PillarLabels).ObjectRangeToVector <string>();

                var pDates = Pillars.ToDateTimeArray();
                var cObj = new BasicPriceCurve(BuildDate, pDates, Prices, cType, ContainerStores.GlobalContainer.GetRequiredService <ICurrencyProvider>(), labels)
                {
                    Name = AssetId ?? ObjectName,
                    AssetId = AssetId ?? ObjectName,
                    Currency = ccyObj,
                    CollateralSpec = colSpec,
                    SpotCalendar = spotCal,
                    SpotLag = new Frequency(spotLagStr)
                };

                var cache = ContainerStores.GetObjectCache <IPriceCurve>();
                cache.PutObject(ObjectName, new SessionItem <IPriceCurve> {
                    Name = ObjectName, Value = cObj
                });
                return ObjectName + '¬' + cache.GetObject(ObjectName).Version;
            }));
        }
Example #6
0
        public static object FieldValues(
            [ExcelArgument(Description = "Input cube name")] string InputObjectName,
            [ExcelArgument(Description = "Field name")] string FieldName)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var cubeCache = ContainerStores.GetObjectCache <ICube>();
                var inCube = cubeCache.GetObjectOrThrow(InputObjectName, $"Could not find cube {InputObjectName}");

                var output = inCube.Value.KeysForField <string>(FieldName);

                return ((object[])output).ReturnExcelRangeVector();
            }));
        }
        public static object ImplySolveStages(
            [ExcelArgument(Description = "Funding Instrument Collection Name")] string FICName,
            [ExcelArgument(Description = "Fx Matrix Name")] string FxMatrixName)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var fic = ContainerStores.GetObjectCache <FundingInstrumentCollection>().GetObjectOrThrow(FICName, $"Could not find FIC {FICName}");
                var fx = ContainerStores.GetObjectCache <FxMatrix>().GetObjectOrThrow(FxMatrixName, $"Could not find FxMatrix {FxMatrixName}");

                var stages = fic.Value.ImplySolveStages(fx.Value);

                return stages.DictionaryToRange();
            }));
        }
Example #8
0
        public static object CreateCreditSettings(
            [ExcelArgument(Description = "Credit settings object name")] string ObjectName,
            [ExcelArgument(Description = "Forward exposure dates for PFE etc")] object PFEDates,
            [ExcelArgument(Description = "Portfolio regression method for PFE etc")] object PortfolioRegressor,
            [ExcelArgument(Description = "Metric to calculate, default PV")] object Metric,
            [ExcelArgument(Description = "Credit curve for CVA calc")] object CreditCurve,
            [ExcelArgument(Description = "Funding curve for xVA")] object FundingCurve,
            [ExcelArgument(Description = "Base discount curve for xVA")] object BaseDiscountCurve,
            [ExcelArgument(Description = "Loss-given-default, e.g. 0.4")] double LGD,
            [ExcelArgument(Description = "Confidence interval, e.g. 0.95")] double ConfidenceInterval,
            [ExcelArgument(Description = "Counterparty risk weighting, e.g. 1.20")] double PartyRiskWeighting)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                if (!Enum.TryParse(PortfolioRegressor.OptionalExcel("MultiLinear"), true, out PFERegressorType regType))
                {
                    return $"Could not parse portfolio regressor type - {PortfolioRegressor}";
                }

                if (!Enum.TryParse(Metric.OptionalExcel("PV"), true, out BaseMetric metric))
                {
                    return $"Could not parse metric - {Metric}";
                }

                var fCurve = FundingCurve is ExcelMissing || !(FundingCurve is string fStr) ?
                             null :
                             ContainerStores.GetObjectCache <IIrCurve>().GetObjectOrThrow(fStr, $"Unable to find IrCurve {FundingCurve}");

                var bCurve = BaseDiscountCurve is ExcelMissing || !(BaseDiscountCurve is string bStr) ?
                             null :
                             ContainerStores.GetObjectCache <IIrCurve>().GetObjectOrThrow(bStr, $"Unable to find IrCurve {BaseDiscountCurve}");

                var cCurve = CreditCurve is ExcelMissing || !(CreditCurve is string cStr) ?
                             null :
                             ContainerStores.GetObjectCache <HazzardCurve>().GetObjectOrThrow(cStr, $"Unable to find hazzard curve {CreditCurve}");

                var cSettings = new CreditSettings
                {
                    Metric = metric,
                    FundingCurve = fCurve?.Value,
                    CreditCurve = cCurve?.Value,
                    BaseDiscountCurve = bCurve?.Value,
                    ExposureDates = PFEDates is object[,] pd ? pd.ObjectRangeToVector <double>().ToDateTimeArray(DateTime.MinValue.AddDays(1)) :
                                    (PFEDates is double pdd ? new[] { DateTime.FromOADate(pdd) } : null),
                    PfeRegressorType = regType,
                    ConfidenceInterval = ConfidenceInterval,
                    LGD = LGD,
                    CounterpartyRiskWeighting = PartyRiskWeighting,
                };
Example #9
0
        public static object GetDF(
            [ExcelArgument(Description = "Curve object name")] string ObjectName,
            [ExcelArgument(Description = "Discount factor start date")] DateTime StartDate,
            [ExcelArgument(Description = "Discount factor end date")] DateTime EndDate)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                if (ContainerStores.GetObjectCache <IIrCurve>().TryGetObject(ObjectName, out var curve))
                {
                    return curve.Value.GetDf(StartDate, EndDate);
                }

                return $"IR curve {ObjectName} not found in cache";
            }));
        }
Example #10
0
 public static object ComputeEAD(
     [ExcelArgument(Description = "Portfolio object")] string PortfolioName,
     [ExcelArgument(Description = "AssetFx model")] string VanillaModel,
     [ExcelArgument(Description = "Reporting currency")] string ReportingCurrency,
     [ExcelArgument(Description = "AssetId to Category map")] object[,] AssetIdToCategoryMap)
 {
     return(ExcelHelper.Execute(_logger, () =>
     {
         var pf = Instruments.InstrumentFunctions.GetPortfolioOrTradeFromCache(PortfolioName);
         var model = ContainerStores.GetObjectCache <IAssetFxModel>().GetObjectOrThrow(VanillaModel, $"Model {VanillaModel} not found");
         var ccy = ContainerStores.CurrencyProvider.GetCurrency(ReportingCurrency);
         var mappingDict = AssetIdToCategoryMap.RangeToDictionary <string, string>();
         return pf.SaCcrEAD(model.Value, ccy, mappingDict);
     }));
 }
Example #11
0
 public static object CreateConstantVolSurface(
     [ExcelArgument(Description = "Object name")] string ObjectName,
     [ExcelArgument(Description = "Origin date")] DateTime OriginDate,
     [ExcelArgument(Description = "Volatility")] double Volatility)
 {
     return(ExcelHelper.Execute(_logger, () =>
     {
         var surface = new ConstantVolSurface(OriginDate, Volatility);
         var cache = ContainerStores.GetObjectCache <ConstantVolSurface>();
         cache.PutObject(ObjectName, new SessionItem <ConstantVolSurface> {
             Name = ObjectName, Value = surface
         });
         return ObjectName + '¬' + cache.GetObject(ObjectName).Version;
     }));
 }
Example #12
0
        public static IEnumerable <T> GetAnyFromCache <T>(this object[] Names)
        {
            var tCache = ContainerStores.GetObjectCache <T>();
            var ts     = new List <T>();

            for (var i = 0; i < Names.GetLength(0); i++)
            {
                var s = Names[i];
                if (!(s is ExcelMissing) && !(s is ExcelEmpty) && !string.IsNullOrWhiteSpace(s as string) &&
                    tCache.TryGetObject(s as string, out var o))
                {
                    ts.Add(o.Value);
                }
            }
            return(ts);
        }
Example #13
0
 public static object PortfolioIrBenchmarkDelta(
     [ExcelArgument(Description = "Result object name")] string ResultObjectName,
     [ExcelArgument(Description = "Portolio object name")] string PortfolioName,
     [ExcelArgument(Description = "Asset-FX model name")] string ModelName,
     [ExcelArgument(Description = "Funding instrument collection name")] string FICName,
     [ExcelArgument(Description = "Reporting currency")] string ReportingCcy)
 {
     return(ExcelHelper.Execute(_logger, () =>
     {
         var model = InstrumentFunctions.GetModelFromCache(ModelName, PortfolioName);
         var fic = ContainerStores.GetObjectCache <FundingInstrumentCollection>().GetObjectOrThrow(FICName, $"FIC {FICName} not found in cache");
         var ccy = ContainerStores.CurrencyProvider.GetCurrency(ReportingCcy);
         var result = model.BenchmarkRisk(fic.Value, ContainerStores.CurrencyProvider, ccy);
         return PushCubeToCache(result, ResultObjectName);
     }));
 }
Example #14
0
 public static object GetRiskyDiscountFactor(
     [ExcelArgument(Description = "Credit curve object name")] string ObjectName,
     [ExcelArgument(Description = "Discount curve object name")] string DiscoObjectName,
     [ExcelArgument(Description = "Start date")] DateTime StartDate,
     [ExcelArgument(Description = "End date")] DateTime EndDate,
     [ExcelArgument(Description = "LGD, e.g. 0.45")] double LGD)
 {
     return(ExcelHelper.Execute(_logger, () =>
     {
         var disco = ContainerStores.GetObjectCache <IIrCurve>().GetObjectOrThrow(DiscoObjectName, $"Could not find discount curve {DiscoObjectName}");
         return ContainerStores
         .GetObjectCache <HazzardCurve>()
         .GetObjectOrThrow(ObjectName, $"Could not find curve {ObjectName}")
         .Value.RiskyDiscountFactor(StartDate, EndDate, disco.Value, LGD);
     }));
 }
Example #15
0
 public static object CreateHazzardCurveFromCDSs(
     [ExcelArgument(Description = "Object name")] string ObjectName,
     [ExcelArgument(Description = "Origin date")] DateTime OriginDate,
     [ExcelArgument(Description = "CDSs")] object[,] CDSs,
     [ExcelArgument(Description = "Recovery Rate")] double RecoveryRate,
     [ExcelArgument(Description = "Discount Curve")] string DiscountCurve)
 {
     return(ExcelHelper.Execute(_logger, () =>
     {
         var cdsObjects = ExcelHelper.GetAnyFromCache <CDS>(CDSs);
         var disco = ContainerStores.GetObjectCache <IIrCurve>().GetObjectOrThrow(DiscountCurve, $"Could not find discount curve {DiscountCurve}");
         var solver = new NewtonRaphsonCreditCurveSolver();
         var hzCurve = solver.Solve(cdsObjects.ToList(), RecoveryRate, disco.Value, OriginDate);
         return ExcelHelper.PushToCache(hzCurve, ObjectName);
     }));
 }
Example #16
0
        public static object CubeToMatrix(
            [ExcelArgument(Description = "Input cube name")] string InputObjectName,
            [ExcelArgument(Description = "Field name vertical")] string FieldNameV,
            [ExcelArgument(Description = "Field name horizontal")] string FieldNameH,
            [ExcelArgument(Description = "Sort fields - true or false")] bool SortFields)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var cubeCache = ContainerStores.GetObjectCache <ICube>();
                var inCube = cubeCache.GetObjectOrThrow(InputObjectName, $"Could not find cube {InputObjectName}");

                var output = inCube.Value.ToMatrix(FieldNameV, FieldNameH, SortFields);

                return output.ReturnPrettyExcelRangeVector();
            }));
        }
Example #17
0
        public static object GetForwardFxRate(
            [ExcelArgument(Description = "Funding model object name")] string ObjectName,
            [ExcelArgument(Description = "Settlement date")] DateTime SettleDate,
            [ExcelArgument(Description = "Currency pair")] string CcyPair)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                if (!ContainerStores.GetObjectCache <IFundingModel>().TryGetObject(ObjectName, out var model))
                {
                    return $"Funding model with name {ObjectName} not found";
                }

                var fwd = model.Value.GetFxRate(SettleDate, CcyPair);
                return fwd;
            }));
        }
Example #18
0
        public static object CreatePriceCurveFromBasisSwaps(
            [ExcelArgument(Description = "Object name")] string ObjectName,
            [ExcelArgument(Description = "Asset Id")] string AssetId,
            [ExcelArgument(Description = "Base curve object")] string BaseCurve,
            [ExcelArgument(Description = "Build date")] DateTime BuildDate,
            [ExcelArgument(Description = "Array of pillar dates")] double[] Pillars,
            [ExcelArgument(Description = "Array of swap objects")] object[] Swaps,
            [ExcelArgument(Description = "Discount curve name")] string DiscountCurveName,
            [ExcelArgument(Description = "Type of curve, e.g. LME, ICE, NYMEX etc")] object CurveType)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var curveTypeStr = CurveType.OptionalExcel <string>("Coal");
                if (!Enum.TryParse(curveTypeStr, out PriceCurveType cType))
                {
                    return $"Could not parse price curve type - {curveTypeStr}";
                }

                var irCache = ContainerStores.GetObjectCache <IIrCurve>();
                if (!irCache.TryGetObject(DiscountCurveName, out var irCurveObj))
                {
                    return $"Could not find ir curve with name {DiscountCurveName}";
                }
                var irCurve = irCurveObj.Value;

                var curveCache = ContainerStores.GetObjectCache <IPriceCurve>();
                if (!curveCache.TryGetObject(BaseCurve, out var bCurveObj))
                {
                    return $"Could not find ir curve with name {DiscountCurveName}";
                }
                var baseCurve = bCurveObj.Value;

                var swapCache = ContainerStores.GetObjectCache <AsianBasisSwap>();
                var swaps = Swaps.Select(s => swapCache.GetObject(s as string)).Select(x => (IAssetInstrument)x.Value);

                var pDates = Pillars.ToDateTimeArray();
                var fitter = new Models.Calibrators.NewtonRaphsonAssetBasisCurveSolver(ContainerStores.CurrencyProvider);
                var cObj = (BasicPriceCurve)fitter.SolveCurve(swaps.ToList(), pDates.ToList(), irCurve, baseCurve, BuildDate, cType);
                cObj.Name = AssetId ?? ObjectName;
                cObj.AssetId = AssetId ?? ObjectName;

                curveCache.PutObject(ObjectName, new SessionItem <IPriceCurve> {
                    Name = ObjectName, Value = cObj
                });
                return ObjectName + '¬' + curveCache.GetObject(ObjectName).Version;
            }));
        }
Example #19
0
        public static object DisplayFixingDictionary(
            [ExcelArgument(Description = "Object name")] string ObjectName,
            [ExcelArgument(Description = "Optional - fixing date")] object FixingDate,
            [ExcelArgument(Description = "Reverse sort order, False (default) or True")] bool ReverseSort)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var dict = ContainerStores.GetObjectCache <IFixingDictionary>().GetObjectOrThrow(ObjectName, $"Fixing dictionary not found with name {ObjectName}");

                if (FixingDate is ExcelMissing)
                {
                    var o = new object[dict.Value.Count, 2];
                    var c = 0;

                    if (ReverseSort)
                    {
                        foreach (var kv in dict.Value.OrderByDescending(x => x.Key))
                        {
                            o[c, 0] = kv.Key;
                            o[c, 1] = kv.Value;
                            c++;
                        }
                    }
                    else
                    {
                        foreach (var kv in dict.Value)
                        {
                            o[c, 0] = kv.Key;
                            o[c, 1] = kv.Value;
                            c++;
                        }
                    }

                    return o;
                }
                else
                {
                    var d = FixingDate as double?;
                    if (!d.HasValue || !dict.Value.TryGetValue(DateTime.FromOADate(d.Value), out var fixing))
                    {
                        throw new Exception($"Fixing not found for date {FixingDate}");
                    }

                    return fixing;
                }
            }));
        }
Example #20
0
        public static object GetVolForAbsoluteStrike(
            [ExcelArgument(Description = "Object name")] string ObjectName,
            [ExcelArgument(Description = "Absolute Strike")] double Strike,
            [ExcelArgument(Description = "Expiry")] DateTime Expiry,
            [ExcelArgument(Description = "Forward")] double Forward
            )
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                if (ContainerStores.GetObjectCache <IVolSurface>().TryGetObject(ObjectName, out var volSurface))
                {
                    return volSurface.Value.GetVolForAbsoluteStrike(Strike, Expiry, Forward);
                }

                return $"Vol surface {ObjectName} not found in cache";
            }));
        }
Example #21
0
        public void ComputeEADFacts()
        {
            var curve = new PriceCurve(DateTime.MinValue, new[] { DateTime.MinValue }, new[] { 0.0 }, PriceCurveType.Flat, ContainerStores.CurrencyProvider)
            {
                Currency = ContainerStores.CurrencyProvider.GetCurrency("ZAR")
            };
            var moqModel = new Mock <IAssetFxModel>();

            moqModel.Setup(m => m.GetPriceCurve("fakeAsset", null)).Returns(curve);
            moqModel.Setup(m => m.VanillaModel).Returns(moqModel.Object);
            moqModel.Setup(m => m.Rebuild(It.IsAny <IAssetFxModel>(), It.IsAny <Portfolio>())).Returns(moqModel.Object);
            var cube = new ResultCube();

            cube.Initialize(new Dictionary <string, Type>()
            {
                { "xxx", typeof(string) }
            });
            moqModel.Setup(m => m.PV(It.IsAny <Currency>())).Returns(cube);
            var swap = new AsianSwap()
            {
                AssetId         = "fakeAsset",
                PaymentCurrency = ContainerStores.CurrencyProvider.GetCurrency("ZAR"),
                FixingDates     = new[] { DateTime.MinValue }
            };
            var pf = new Portfolio {
                Instruments = new List <IInstrument>()
                {
                    swap
                }
            };

            ContainerStores.GetObjectCache <Portfolio>().PutObject("moqPf", new SessionItem <Portfolio>()
            {
                Name = "moqPf", Value = pf
            });
            ContainerStores.GetObjectCache <IAssetFxModel>().PutObject("moqModel", new SessionItem <IAssetFxModel>()
            {
                Name = "moqModel", Value = moqModel.Object
            });

            Assert.Equal("Could not find portfolio or trade with name blash", CapitalFunctions.ComputeEAD("blash", "frah", "ZAR", null));
            Assert.Equal("Model frah not found", CapitalFunctions.ComputeEAD("moqPf", "frah", "ZAR", null));
            Assert.Equal(0.0, CapitalFunctions.ComputeEAD("moqPf", "moqModel", "ZAR", new object[, ] {
                { "fakeAsset", "woooh" }
            }));
        }
        public static object CreateSTIRFromCode(
            [ExcelArgument(Description = "Object name")] string ObjectName,
            [ExcelArgument(Description = "Value date")] DateTime ValDate,
            [ExcelArgument(Description = "Futures Code, e.g. EDZ9")] string FuturesCode,
            [ExcelArgument(Description = "Rate Index")] string RateIndex,
            [ExcelArgument(Description = "Price")] double Price,
            [ExcelArgument(Description = "Quantity in lots")] double Quantity,
            [ExcelArgument(Description = "Convexity adjustment")] double ConvexityAdjustment,
            [ExcelArgument(Description = "Forecast Curve")] string ForecastCurve,
            [ExcelArgument(Description = "Solve Curve name ")] object SolveCurve,
            [ExcelArgument(Description = "Solve Pillar Date")] object SolvePillarDate)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                if (!ContainerStores.GetObjectCache <FloatRateIndex>().TryGetObject(RateIndex, out var rIndex))
                {
                    _logger?.LogInformation("Rate index {index} not found in cache", RateIndex);
                    return $"Rate index {RateIndex} not found in cache";
                }

                var c = new FutureCode(FuturesCode, DateTime.Today.Year - 2, ContainerStores.SessionContainer.GetService <IFutureSettingsProvider>());

                var expiry = c.GetExpiry();
                var accrualStart = expiry.AddPeriod(RollType.F, rIndex.Value.HolidayCalendars, rIndex.Value.FixingOffset);
                var accrualEnd = accrualStart.AddPeriod(rIndex.Value.RollConvention, rIndex.Value.HolidayCalendars, rIndex.Value.ResetTenor);
                //var dcf = accrualStart.CalculateYearFraction(accrualEnd, rIndex.Value.DayCountBasis);
                var product = new STIRFuture
                {
                    Currency = rIndex.Value.Currency,
                    ContractSize = c.Settings.LotSize,
                    //DCF = dcf,
                    ConvexityAdjustment = ConvexityAdjustment,
                    Expiry = expiry,
                    ForecastCurve = ForecastCurve,
                    Index = rIndex.Value,
                    Position = Quantity,
                    Price = Price,
                    SolveCurve = SolveCurve.OptionalExcel(ForecastCurve),
                    PillarDate = SolvePillarDate.OptionalExcel(accrualEnd),
                    TradeId = ObjectName
                };

                return ExcelHelper.PushToCache(product, ObjectName);
            }));
        }
Example #23
0
        public static object GenerateCDFFromInterpolator(
            [ExcelArgument(Description = "Output interpolator name")] string ObjectName,
            [ExcelArgument(Description = "Input interpolator name")] string VolInterpolator,
            [ExcelArgument(Description = "Expiry as year fraction")] double ExpiryYearFraction,
            [ExcelArgument(Description = "Forward")] double Forward,
            [ExcelArgument(Description = "Number of samples")] int NumberOfSamples)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                if (ContainerStores.GetObjectCache <IInterpolator1D>().TryGetObject(VolInterpolator, out var smile))
                {
                    var interpolator = smile.Value.GenerateCDF(NumberOfSamples, ExpiryYearFraction, Forward);
                    return ExcelHelper.PushToCache(interpolator, ObjectName);
                }

                return $"Interpolator {VolInterpolator} not found in cache";
            }));
        }
Example #24
0
        public static object GeneratePDF(
            [ExcelArgument(Description = "Output interpolator name")] string ObjectName,
            [ExcelArgument(Description = "Volsurface name")] string VolSurface,
            [ExcelArgument(Description = "Expiry date")] DateTime ExpiryDate,
            [ExcelArgument(Description = "Forward")] double Forward,
            [ExcelArgument(Description = "Number of samples")] int NumberOfSamples)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                if (ContainerStores.GetObjectCache <IVolSurface>().TryGetObject(VolSurface, out var volSurface))
                {
                    var interpolator = volSurface.Value.GeneratePDF(NumberOfSamples, ExpiryDate, Forward);
                    return ExcelHelper.PushToCache(interpolator, ObjectName);
                }

                return $"Vol surface {VolSurface} not found in cache";
            }));
        }
Example #25
0
        public static object PortfolioPvCcrCapital(
            [ExcelArgument(Description = "Portfolio")] string Portfolio,
            [ExcelArgument(Description = "Expected EAD cube name")] string EADCubeName,
            [ExcelArgument(Description = "Credit settings object name")] string CreditSettingsName,
            [ExcelArgument(Description = "Origin date")] DateTime OriginDate)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var eadCube = ContainerStores.GetObjectCache <ICube>()
                              .GetObjectOrThrow(EADCubeName, $"Could not find cube with name {EADCubeName}");
                var creditSettings = ContainerStores.GetObjectCache <CreditSettings>()
                                     .GetObjectOrThrow(CreditSettingsName, $"Could not find credit settings with name {CreditSettingsName}");
                var portfolio = Instruments.InstrumentFunctions.GetPortfolioOrTradeFromCache(Portfolio);

                var result = CapitalCalculator.PVCapital_BII_IMM(OriginDate, eadCube.Value, creditSettings.Value.CreditCurve, creditSettings.Value.BaseDiscountCurve, creditSettings.Value.LGD, portfolio);
                return result;
            }));
        }
        public static object CreateOISFutureFromCode(
            [ExcelArgument(Description = "Object name")] string ObjectName,
            [ExcelArgument(Description = "Value date")] DateTime ValDate,
            [ExcelArgument(Description = "Futures Code, e.g. EDZ9")] string FuturesCode,
            [ExcelArgument(Description = "Rate Index")] string RateIndex,
            [ExcelArgument(Description = "Price")] double Price,
            [ExcelArgument(Description = "Quantity in lots")] double Quantity,
            [ExcelArgument(Description = "Forecast Curve")] string ForecastCurve,
            [ExcelArgument(Description = "Solve Curve name ")] object SolveCurve,
            [ExcelArgument(Description = "Solve Pillar Date")] object SolvePillarDate)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                if (!ContainerStores.GetObjectCache <FloatRateIndex>().TryGetObject(RateIndex, out var rIndex))
                {
                    _logger?.LogInformation("Rate index {index} not found in cache", RateIndex);
                    return $"Rate index {RateIndex} not found in cache";
                }

                var c = new FutureCode(FuturesCode, DateTime.Today.Year - 2, ContainerStores.SessionContainer.GetService <IFutureSettingsProvider>());

                var expiry = c.GetExpiry();
                var accrualStart = expiry.FirstDayOfMonth();
                var accrualEnd = expiry.LastDayOfMonth();
                var dcf = accrualStart.CalculateYearFraction(accrualEnd, rIndex.Value.DayCountBasis);
                var product = new OISFuture
                {
                    Currency = rIndex.Value.Currency,
                    ContractSize = c.Settings.LotSize,
                    DCF = dcf,
                    AverageStartDate = accrualStart,
                    AverageEndDate = accrualEnd,
                    ForecastCurve = ForecastCurve,
                    Index = rIndex.Value,
                    Position = Quantity,
                    Price = Price,
                    SolveCurve = SolveCurve.OptionalExcel(rIndex.Name),
                    PillarDate = SolvePillarDate.OptionalExcel(accrualEnd),
                    TradeId = ObjectName
                };

                return ExcelHelper.PushToCache(product, ObjectName);
            }));
        }
        public static object CreateIRBasisSwap(
            [ExcelArgument(Description = "Object name")] string ObjectName,
            [ExcelArgument(Description = "Value date")] DateTime ValDate,
            [ExcelArgument(Description = "Tenor")] string SwapTenor,
            [ExcelArgument(Description = "Rate Index Pay")] string RateIndexPay,
            [ExcelArgument(Description = "Rate Index Receive")] string RateIndexRec,
            [ExcelArgument(Description = "Par Spread Pay")] double ParSpread,
            [ExcelArgument(Description = "Spread on Pay leg?")] object ParSpreadOnPay,
            [ExcelArgument(Description = "Notional")] double Notional,
            [ExcelArgument(Description = "Forecast Curve Pay")] string ForecastCurvePay,
            [ExcelArgument(Description = "Forecast Curve Receive")] string ForecastCurveRec,
            [ExcelArgument(Description = "Discount Curve")] string DiscountCurve,
            [ExcelArgument(Description = "Solve Curve name ")] object SolveCurve,
            [ExcelArgument(Description = "Solve Pillar Date")] object SolvePillarDate)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                if (!ContainerStores.GetObjectCache <FloatRateIndex>().TryGetObject(RateIndexPay, out var rIndexPay))
                {
                    _logger?.LogInformation("Rate index {index} not found in cache", RateIndexPay);
                    return $"Rate index {RateIndexPay} not found in cache";
                }

                if (!ContainerStores.GetObjectCache <FloatRateIndex>().TryGetObject(RateIndexRec, out var rIndexRec))
                {
                    _logger?.LogInformation("Rate index {index} not found in cache", RateIndexRec);
                    return $"Rate index {RateIndexRec} not found in cache";
                }

                var spreadOnPay = ParSpreadOnPay.OptionalExcel(true);
                var tenor = new Frequency(SwapTenor);

                var product = new IrBasisSwap(ValDate, tenor, ParSpread, spreadOnPay, rIndexPay.Value, rIndexRec.Value, ForecastCurvePay, ForecastCurveRec, DiscountCurve)
                {
                    SolveCurve = SolveCurve.OptionalExcel(rIndexPay.Name),
                    TradeId = ObjectName
                };
                product.PillarDate = SolvePillarDate.OptionalExcel(product.EndDate);

                return ExcelHelper.PushToCache(product, ObjectName);
            }));
        }
Example #28
0
        public static object SortCube(
            [ExcelArgument(Description = "Output cube name")] string OutputObjectName,
            [ExcelArgument(Description = "Input cube name")] string InputObjectName,
            [ExcelArgument(Description = "Fields to sort on")] object[] SortDetails)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var cubeCache = ContainerStores.GetObjectCache <ICube>();
                var inCube = cubeCache.GetObjectOrThrow(InputObjectName, $"Could not find cube {InputObjectName}");

                var sortDeets = SortDetails.ObjectRangeToVector <string>().ToList();

                var outCube = inCube.Value.Sort(sortDeets);

                cubeCache.PutObject(OutputObjectName, new SessionItem <ICube> {
                    Name = OutputObjectName, Value = outCube
                });
                return OutputObjectName + '¬' + cubeCache.GetObject(OutputObjectName).Version;
            }));
        }
        public static object CreateIRS(
            [ExcelArgument(Description = "Object name")] string ObjectName,
            [ExcelArgument(Description = "Value date")] DateTime ValDate,
            [ExcelArgument(Description = "Tenor")] string SwapTenor,
            [ExcelArgument(Description = "Rate Index")] string RateIndex,
            [ExcelArgument(Description = "Par Rate")] double ParRate,
            [ExcelArgument(Description = "Notional")] double Notional,
            [ExcelArgument(Description = "Forecast Curve")] string ForecastCurve,
            [ExcelArgument(Description = "Discount Curve")] string DiscountCurve,
            [ExcelArgument(Description = "Pay / Receive")] object PayRec,
            [ExcelArgument(Description = "Solve Curve name ")] object SolveCurve,
            [ExcelArgument(Description = "Solve Pillar Date")] object SolvePillarDate)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var payRec = PayRec.OptionalExcel("Pay");

                if (!ContainerStores.GetObjectCache <FloatRateIndex>().TryGetObject(RateIndex, out var rIndex))
                {
                    _logger?.LogInformation("Rate index {index} not found in cache", RateIndex);
                    return $"Rate index {RateIndex} not found in cache";
                }

                if (!Enum.TryParse(payRec, out SwapPayReceiveType pType))
                {
                    return $"Could not parse pay/rec - {payRec}";
                }

                var tenor = new Frequency(SwapTenor);

                var product = new IrSwap(ValDate, tenor, rIndex.Value, ParRate, pType, ForecastCurve, DiscountCurve)
                {
                    TradeId = ObjectName,
                    SolveCurve = SolveCurve.OptionalExcel(rIndex.Name),
                    Notional = Notional
                };
                product.PillarDate = SolvePillarDate.OptionalExcel(product.EndDate);

                return ExcelHelper.PushToCache(product, ObjectName);
            }));
        }
Example #30
0
        public static object AggregateCube(
            [ExcelArgument(Description = "Output cube name")] string OutputObjectName,
            [ExcelArgument(Description = "Input cube name")] string InputObjectName,
            [ExcelArgument(Description = "Field to aggregate by")] object[] AggregationField,
            [ExcelArgument(Description = "Aggregation details")] string AggregateAction)
        {
            return(ExcelHelper.Execute(_logger, () =>
            {
                var cubeCache = ContainerStores.GetObjectCache <ICube>();
                var inCube = cubeCache.GetObjectOrThrow(InputObjectName, $"Could not find cube {InputObjectName}");

                var aggDeets = (AggregationAction)Enum.Parse(typeof(AggregationAction), AggregateAction);

                var outCube = inCube.Value.Pivot(AggregationField.ObjectRangeToVector <string>(), aggDeets);

                cubeCache.PutObject(OutputObjectName, new SessionItem <ICube> {
                    Name = OutputObjectName, Value = outCube
                });
                return OutputObjectName + '¬' + cubeCache.GetObject(OutputObjectName).Version;
            }));
        }