Пример #1
0
        public static object BachelierGreek(double fwd, double strike, double maturity, double vol, string request)
        {
            return(FunctionRunnerUtils.Run("BachelierGreek", () =>
            {
                double gamma, theta, vega, vanna, vomma;
                Bachelier.Greeks(fwd, strike, maturity, vol,
                                 out gamma, out theta, out vega, out vanna, out vomma);
                switch (request.Trim().ToLower())
                {
                case "gamma":
                    return gamma;

                case "theta":
                    return theta;

                case "vega":
                    return vega;

                case "vanna":
                    return vanna;

                case "vomma":
                    return vomma;

                default:
                    throw new Exception(string.Format("Unknow greek : {0}", request));
                }
            }));
        }
Пример #2
0
        public static object LocalVolDenominator(object mktObj, object[] dates, double[] strikes, string assetName)
        {
            return(FunctionRunnerUtils.Run("LocalVolSurface", () =>
            {
                Market market = MarketManager.Instance.GetMarket(mktObj);
                AssetMarket assetMarket = market.AssetMarketFromName(assetName);
                VolatilitySurface volSurface = assetMarket.VolSurface();

                var localVariance = volSurface.LocalVariance;
                var moneyness = volSurface.Moneyness;
                var time = volSurface.Time;

                var result = new double[dates.Length, strikes.Length];
                for (int i = 0; i < dates.Length; i++)
                {
                    var date = ObjectConverters.ConvertDate(dates[i], assetMarket.RefDate);
                    double t = time[date];
                    var denomFunc = localVariance.Denominator(t);
                    var denoms = strikes.Map(k =>
                    {
                        var y = moneyness.Moneyness(t, k);
                        return denomFunc.Eval(y);
                    });
                    ArrayUtils.SetRow(ref result, i, denoms);
                }
                return result;
            }));
        }
Пример #3
0
        public static object EquityImpliedVol(object mktObj, string assetName, object maturity, double strike, double price, string optionType)
        {
            return(FunctionRunnerUtils.Run("EquityImpliedVol", () =>
            {
                Market market = MarketManager.Instance.GetMarket(mktObj);
                AssetMarket assetMkt = market.AssetMarketFromName(assetName);
                var pricer = BlackScholesWithDividendOption.Build(assetMkt.Spot,
                                                                  assetMkt.Dividends,
                                                                  assetMkt.RiskFreeDiscount,
                                                                  assetMkt.Time);
                double q;
                switch (optionType.Trim().ToLower())
                {
                case "call":
                    q = 1.0;
                    break;

                case "put":
                    q = -1.0;
                    break;

                default:
                    throw new Exception(string.Format("Unknow option type : {0}", optionType));
                }

                var matAsDate = ObjectConverters.ConvertDate(maturity, assetMkt.RefDate);
                return pricer.ImpliedVol(assetMkt.Time[matAsDate], strike, price, q);
            }));
        }
Пример #4
0
 public static object MarketRefDate(object mktObj)
 {
     return(FunctionRunnerUtils.Run("MarketRefDate", () =>
     {
         var market = MarketManager.Instance.GetMarket(mktObj);
         return market.RefDate;
     }));
 }
Пример #5
0
        public static object LocalVolSurface(object mktObj, object[] dates, double[] strikes, string assetName)
        {
            return(FunctionRunnerUtils.Run("LocalVolSurface", () =>
            {
                Market market = MarketManager.Instance.GetMarket(mktObj);
                AssetMarket assetMarket = market.AssetMarketFromName(assetName);
                VolatilitySurface volSurface = assetMarket.VolSurface();
                var maturities = ObjectConverters.ConvertDateArray(dates, assetMarket.RefDate);

                return ArrayUtils.CartesianProd(maturities, strikes, volSurface.LocalVol);
            }));
        }
Пример #6
0
        public static object CubicSpline(double[] abscissae, double[] values, double[] points)
        {
            return(FunctionRunnerUtils.Run("CubicSpline", () =>
            {
                var interpoler = SplineInterpoler.BuildCubicSpline(abscissae, values);

                var result = new double[points.Length, 1];
                for (int i = 0; i < points.Length; i++)
                {
                    result[i, 0] = interpoler.Eval(points[i]);
                }
                return result;
            }));
        }
Пример #7
0
        public static object Bergomi2FAtmfSkewApprox(object mktObj, object[,] modelBag, object[] maturities)
        {
            return(FunctionRunnerUtils.Run("Bergomi2FAtmfSkewApprox", () =>
            {
                Market market = MarketManager.Instance.GetMarket(mktObj);
                ICalibrationDescription b2FCalibDesc = Bergomi2FModelFactoryFromBag.Instance.Build(modelBag);
                IModelDescription b2FDesc = ModelCalibration.Instance.Calibrate(b2FCalibDesc, market);
                var b2Fmodel = (Bergomi2FModel)ModelFactory.Instance.Build(b2FDesc, market);

                var mats = b2Fmodel.Time[ObjectConverters.ConvertDateArray(maturities, market.RefDate)];

                var skews = Bergomi2FUtils.AtmfSkewApprox(b2Fmodel, mats);
                return skews.AsColumn();
            }));
        }
Пример #8
0
        public static object Discount(object mktObj, object[,] dates, string curveId)
        {
            return(FunctionRunnerUtils.Run("Discount", () =>
            {
                var market = MarketManager.Instance.GetMarket(mktObj);
                var finCurveId = FinancingId.Parse(curveId);
                DiscountCurve curve = market.DiscountCurve(finCurveId);

                var result = dates.Map(o =>
                {
                    var dateOrDuration = DateAndDurationConverter.ConvertDateOrDuration(o);
                    var date = dateOrDuration.ToDate(curve.RefDate);
                    return curve.Zc(date);
                });

                return result;
            }));
        }
Пример #9
0
        public static object ExoticPrice(object requestObj, object[,] productBag, object mktObj, object[,] modelBag, object[,] algorithmBag)
        {
            return(FunctionRunnerUtils.Run("ExoticPrice", () =>
            {
                Trace.WriteLine("");

                TaskFactory taskFactory = Task.Factory;
                Task <Market> market = BuildMarket(taskFactory, mktObj);
                Task <ICalibrationDescription> modelCalibDesc = BuildCalibration(taskFactory, modelBag);
                Task <IProduct> product = BuildProduct(taskFactory, productBag);

                Task <IModel> model = CalibrateModel(taskFactory, market, modelCalibDesc);
                Task <IPricer> pricer = BuildPricer(taskFactory, requestObj, algorithmBag);
                Task <IPricingResult> priceResult = ComputePrice(taskFactory, pricer, product, model, market);

                Trace.WriteLine("");
                return PriceResultPublisher.Instance.Publish(priceResult.Result);
            }));
        }
Пример #10
0
        public static object Bergomi2FVolOfVol(object mktObj, object[,] modelBag, object[] starts, object[] ends)
        {
            return(FunctionRunnerUtils.Run("Bergomi2FVolOfVol", () =>
            {
                Market market = MarketManager.Instance.GetMarket(mktObj);
                ICalibrationDescription b2FCalibDesc = Bergomi2FModelFactoryFromBag.Instance.Build(modelBag);
                IModelDescription b2FDesc = ModelCalibration.Instance.Calibrate(b2FCalibDesc, market);
                var b2Fmodel = (Bergomi2FModel)ModelFactory.Instance.Build(b2FDesc, market);

                var startDates = b2Fmodel.Time[ObjectConverters.ConvertDateArray(starts, market.RefDate)];
                var endDates = b2Fmodel.Time[ObjectConverters.ConvertDateArray(ends, market.RefDate)];
                if (startDates.Length != endDates.Length)
                {
                    throw new ArgumentException("Incompatible size between starts and ends");
                }

                return Bergomi2FUtils.FwdVolInstantVol(b2Fmodel, startDates, endDates).AsColumn();
            }));
        }
Пример #11
0
        public static object AssetForward(object mktObj, object[,] dates, string assetName)
        {
            return(FunctionRunnerUtils.Run("AssetForward", () =>
            {
                var market = MarketManager.Instance.GetMarket(mktObj);
                var assetMarket = market.AssetMarketFromName(assetName);

                AssetForwardCurve assetForward = assetMarket.Forward();

                var result = dates.Map(o =>
                {
                    var dateOrDuration = DateAndDurationConverter.ConvertDateOrDuration(o);
                    var date = dateOrDuration.ToDate(assetForward.RefDate);
                    return assetForward.Fwd(date);
                });

                return result;
            }));
        }
Пример #12
0
        public static object BachelierImpliedVol(double forward, double strike, double maturity, double price, string optionType)
        {
            return(FunctionRunnerUtils.Run("BachelierImpliedVol", () =>
            {
                double q;
                switch (optionType.Trim().ToLower())
                {
                case "call":
                    q = 1.0;
                    break;

                case "put":
                    q = -1.0;
                    break;

                default:
                    throw new Exception(string.Format("Unknow option type : {0}", optionType));
                }
                return Bachelier.ImpliedVol(price, forward, strike, maturity, q);
            }));
        }
Пример #13
0
        public static object BlackOption(double forward, double strike, double maturity, double vol, string optionType)
        {
            return(FunctionRunnerUtils.Run("BlackOption", () =>
            {
                double q;
                switch (optionType.Trim().ToLower())
                {
                case "call":
                    q = 1.0;
                    break;

                case "put":
                    q = -1.0;
                    break;

                default:
                    throw new Exception(string.Format("Unknow option type : {0}", optionType));
                }

                return BlackScholesOption.Price(forward, strike, vol, maturity, q);
            }));
        }
Пример #14
0
        public static object Sobol(string directionType, int dimension, int nbPaths,
                                   int nbSkippedPaths, object startDimIndexDisplay)
        {
            return(FunctionRunnerUtils.Run("CubicSpline", () =>
            {
                SobolDirection direction;
                if (!Enum.TryParse(directionType, true, out direction))
                {
                    throw new Exception(string.Format("Unknow sobol direction type {0}", directionType));
                }

                var sobolGen = new SobolRsg(dimension, direction);
                sobolGen.SkipTo((ulong)nbSkippedPaths);

                int startIndex;
                if (!NumberConverter.TryConvertInteger(startDimIndexDisplay, out startIndex))
                {
                    startIndex = 0;
                }
                if (startIndex >= dimension)
                {
                    throw new Exception(string.Format("invalid startDimIndexDisplay : {0}", startIndex));
                }

                var result = new double[nbPaths, dimension - startIndex];
                for (int i = 0; i < nbPaths; i++)
                {
                    var sample = sobolGen.NextSequence();
                    for (int j = 0; j < dimension - startIndex; j++)
                    {
                        result[i, j] = sample[startIndex + j];
                    }
                }
                return result;
            }));
        }