Exemplo n.º 1
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);
            }));
        }
Exemplo n.º 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;
            }));
        }
Exemplo n.º 3
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);
            }));
        }
Exemplo n.º 4
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();
            }));
        }
Exemplo n.º 5
0
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ObjectConverter dc = ObjectConverters.Find(targetType);

            if (dc != null && value is string s)
            {
                try
                {
                    return(dc.Construct(s));
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Unable to parse " + value.ToString() + ": " + e.Message);
                }
            }

            return(null);
        }
Exemplo n.º 6
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();
            }));
        }
Exemplo n.º 7
0
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     if (value != null && targetType == typeof(string))
     {
         ObjectConverter dc = ObjectConverters.Find(value.GetType());
         if (dc != null)
         {
             try
             {
                 return(dc.Format(value));
             }
             catch (Exception e)
             {
                 Debug.WriteLine("Unable to format " + value.ToString() + ": " + e.Message);
             }
         }
     }
     return(null);
 }
Exemplo n.º 8
0
 public void TestAdd()
 {
     ObjectConverters.Add(new ObjectConverter(typeof(System.Drawing.Size),
                                              new Type[] { typeof(int), typeof(int) },
                                              (d) =>
     {
         var s = (System.Drawing.Size)d;
         return(new object[] { s.Width, s.Height });
     },
                                              (ol) => new System.Drawing.Size((int)ol[0], (int)ol[1]),
                                              "({0},{1})"
                                              ));
     ObjectConverters.Add(new ObjectConverter(typeof(Complex),
                                              new Type[] { typeof(double), typeof(double) },
                                              (d) =>
     {
         var c = (Complex)d;
         return(new object[] { c.Real, c.Imaginary });
     },
                                              (ol) => new Complex((double)ol[0], (double)ol[1]),
                                              "({0},{1})"));
     Assert.Equal(2, ObjectConverters.Instance.Count);
     Assert.Equal(typeof(Complex), ObjectConverters.Find(typeof(Complex)).Foundation);
 }