/// <summary>
 /// Get an array of <see cref="FloatingIndex"/> from an excel range of strings.
 /// </summary>
 /// <param name="values"></param>
 /// <param name="inputName">The name of the input in the Excel function so that sensible errors can be returned.</param>
 /// <returns></returns>
 public static FloatingIndex[] GetFloatingIndex1D(object[,] values, string inputName)
 {
     if (values.GetLength(0) == 1 && values.GetLength(1) >= 1) // row of inputs
     {
         FloatingIndex[] result = new FloatingIndex[values.GetLength(1)];
         for (int i = 0; i < values.GetLength(1); i++)
         {
             result[i] = GetFloatingIndex(values[0, i], inputName);
         }
         return(result);
     }
     else if (values.GetLength(0) >= 1 && values.GetLength(1) == 1) // column of inputs
     {
         FloatingIndex[] result = new FloatingIndex[values.GetLength(0)];
         for (int i = 0; i < values.GetLength(0); i++)
         {
             result[i] = GetFloatingIndex(values[i, 0], inputName);
         }
         return(result);
     }
     else
     {
         throw new ArgumentException(inputName + " must be a single row or column of strings representing floating indices.");
     }
 }
Example #2
0
        public static double ValueZARSwap1Curve([ExcelArgument(Description = "The name of the swap.")] IRSwap swap,
                                                [ExcelArgument(Description = "The date on which valuation is required.  Cannot be before the anchor date of the curve.")] Date valueDate,
                                                [ExcelArgument(Description = "The discounting curve.  Will also be used for forecasting Jibar and providing the most recent required Jibar fix.")] IDiscountingSource curve)
        {
            // Get the required objects off the map
            FloatingIndex index = swap.GetFloatingIndex();

            // Calculate the first fixing off the curve to use at all past dates.
            double df1       = curve.GetDF(valueDate);
            Date   laterDate = valueDate.AddTenor(index.tenor);
            double df2       = curve.GetDF(laterDate);
            double dt        = (laterDate - valueDate) / 365.0;
            double rate      = (df1 / df2 - 1) / dt;

            //Set up the valuation engine.
            IFloatingRateSource forecastCurve = new ForecastCurveFromDiscount(curve, index,
                                                                              new FloatingRateFixingCurve1Rate(rate, index));
            DeterminsiticCurves curveSim = new DeterminsiticCurves(curve);

            curveSim.AddRateForecast(forecastCurve);
            Coordinator coordinator = new Coordinator(curveSim, new List <Simulator>(), 1);

            // Run the valuation
            double value = coordinator.Value(new Product[] { swap }, valueDate);

            return(value);
        }
Example #3
0
 public static object _CreateFloatLeg(string objectName,
                                      object[,] currency,
                                      object[,] floatingIndex,
                                      object[,] resetDates,
                                      object[,] paymentDates,
                                      object[,] notionals,
                                      object[,] spreads,
                                      object[,] accrualFractions)
 {
     try
     {
         Currency      _currency         = XU.GetCurrency0D(currency, "currency");
         FloatingIndex _floatingIndex    = XU.GetFloatingIndex0D(floatingIndex, "floatingIndex");
         Date[]        _resetDates       = XU.GetDate1D(resetDates, "resetDates");
         Date[]        _paymentDates     = XU.GetDate1D(paymentDates, "paymentDates");
         Double[]      _notionals        = XU.GetDouble1D(notionals, "notionals");
         Double[]      _spreads          = XU.GetDouble1D(spreads, "spreads");
         Double[]      _accrualFractions = XU.GetDouble1D(accrualFractions, "accrualFractions");
         FloatLeg      _result           = XLRates.CreateFloatLeg(_currency, _floatingIndex, _resetDates, _paymentDates, _notionals, _spreads, _accrualFractions);
         return(XU.AddObject(objectName, _result));
     }
     catch (Exception e)
     {
         return(XU.Error0D(e));
     }
 }
Example #4
0
 public static LoanFloatingRate CreateLoanFloatingRate([ExcelArgument(Description = "The currency of the cashflows.")] Currency currency,
                                                       [ExcelArgument(Description = "The dates on which the loan balances are known.  All dates other than the first one will be assumed to also be cashflow dates.")] Date[] balanceDates,
                                                       [ExcelArgument(Description = "The notionals on which the payments are based.")] double[] balanceAmounts,
                                                       [ExcelArgument(Description = "The reference index on which the floating flows are based.")] FloatingIndex floatingIndex,
                                                       [ExcelArgument(Description = "The spread that will be added to the floating index.")] double floatingSpread)
 {
     return(LoanFloatingRate.CreateSimple(balanceDates, balanceAmounts, floatingIndex, floatingSpread, currency));
 }
Example #5
0
 public static FloatLeg CreateFloatLeg([ExcelArgument(Description = "The currency of the cashflows. (Currency)")] Currency currency,
                                       [ExcelArgument(Description = "A string describing the floating index.")] FloatingIndex floatingIndex,
                                       [ExcelArgument(Description = "The dates on which the floating indices reset.")] Date[] resetDates,
                                       [ExcelArgument(Description = "The dates on which the payments are made.")] Date[] paymentDates,
                                       [ExcelArgument(Description = "The notionals on which the payments are based.")] double[] notionals,
                                       [ExcelArgument(Description = "The spreads that apply to the simple floating rates on each of the payment dates.")] double[] spreads,
                                       [ExcelArgument(Description = "The accrual fraction to be used in calulating the fixed flow.  Will depend on the daycount convention agreed in the contract.")] double[] accrualFractions)
 {
     FloatingIndex[] floatingIndices = Enumerable.Range(1, resetDates.Length).Select(i => floatingIndex).ToArray();
     return(new FloatLeg(currency, paymentDates, notionals, resetDates, floatingIndices, spreads, accrualFractions));
 }
Example #6
0
        public static HullWhite1F CreateHWModelDemo([ExcelArgument(Description = "The constant rate of mean reversion.")] double meanReversion,
                                                    [ExcelArgument(Description = "The constant short rate volatility.  Note that this is a Gaussian vol and will in general be lower than the vol that would be used in Black.")] double flatVol,
                                                    [ExcelArgument(Description = "The curve to which zero coupon bond prices will be calibrated.")] IDiscountingSource baseCurve,
                                                    [ExcelArgument(Description = "The indices that should be forecast with this same cuve.  No spreads are added.")] FloatingIndex forecastIndices)
        {
            Date        anchorDate    = baseCurve.GetAnchorDate();
            double      flatCurveRate = -Math.Log(baseCurve.GetDF(anchorDate.AddTenor(Tenor.Years(1))));
            HullWhite1F model         = new HullWhite1F(baseCurve.GetCurrency(), meanReversion, flatVol, flatCurveRate, flatCurveRate, anchorDate);

            model.AddForecast(forecastIndices);
            return(model);
        }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FRA"/> class.
 /// </summary>
 /// <param name="notional">The notional.</param>
 /// <param name="accrualFraction">The accrual fraction.  Used for calculating the cashflow and
 /// discounting it FRAs that pay on the reset date.</param>
 /// <param name="nearDate">The near date.</param>
 /// <param name="farDate">The far date.</param>
 /// <param name="floatIndex">The floating rate index that will referenced by the FRA.  Will also
 /// determine the currency of the cashflow.</param>
 public FRA(double notional, double accrualFraction, double rate, bool payFixed, Date nearDate,
            Date farDate, FloatingIndex floatIndex)
 {
     this.accrualFraction = accrualFraction;
     this.notional        = notional;
     this.rate            = rate;
     this.payFixed        = payFixed;
     this.nearDate        = nearDate;
     this.farDate         = farDate;
     this.floatIndex      = floatIndex;
     ccy = floatIndex.currency;
     Init();
 }
Example #8
0
 public static IFloatingRateSource CreateRateForecastCurveFromDiscount([ExcelArgument(Description = "The floating rate that this curve will be used to forecast.")] FloatingIndex floatingRateIndex,
                                                                       [ExcelArgument(Description = "The name of the discount curve that will be used to obtain the forward rates.")] IDiscountingSource discountCurve,
                                                                       [QuantSAExcelArgument(Description = "Optional: The name of the fixing curve for providing floating rates at dates before the anchor date of the discount curve.  If it is left out then the first floating rate implied by the discount curve will be used for all historical fixes.", Default = "null")] IFloatingRateSource fixingCurve)
 {
     if (fixingCurve == null)
     {
         // Calculate the first fixing off the curve to use at all past dates.
         double df1       = 1.0;
         Date   laterDate = discountCurve.GetAnchorDate().AddTenor(floatingRateIndex.tenor);
         double df2       = discountCurve.GetDF(laterDate);
         double dt        = (laterDate - discountCurve.GetAnchorDate()) / 365.0;
         double rate      = (df1 / df2 - 1) / dt;
         fixingCurve = new FloatingRateFixingCurve1Rate(rate, floatingRateIndex);
     }
     return(new ForecastCurveFromDiscount(discountCurve, floatingRateIndex, fixingCurve));
 }
Example #9
0
        public override bool ProvidesIndex(MarketObservable index)
        {
            FloatingIndex floatIndex = index as FloatingIndex;

            if (floatIndex != null)
            {
                return(forecastCurves.ContainsKey(floatIndex));
            }
            CurrencyPair currencyPair = index as CurrencyPair;

            if (currencyPair != null)
            {
                return(fxCurves.ContainsKey(currencyPair));
            }
            return(false);
        }
Example #10
0
        /// <summary>
        /// Adds the specified product to the curve stripping set.
        /// <para/>
        /// The product will be changed when it is added by its valueDate being set.  The curve itself will
        /// also be changed when the <see cref="Strip"/> method is called.
        /// </summary>
        /// <typeparam name="CurveType">The type of the curve type that this product will add a node date to.</typeparam>
        /// <param name="product">The product.</param>
        /// <param name="targetMetric">The function that will be evaluated during the optimization.</param>
        /// <param name="targetValue"></param>
        /// <param name="targetWeight"></param>
        /// <param name="curve">The curve to which the node date for this product will be added.</param>
        /// <param name="index">The index that the product depends on and that the curve provides that will be used to find the
        /// node date.  The node date will be the last date that this index is required.</param>
        public void AddForecast <CurveType>(Product product, Func <double> targetMetric, double targetValue, double targetWeight,
                                            CurveType curve, FloatingIndex index)
            where CurveType : IFloatingRateSource, ICurveForStripping
        {
            targetMetrics.Add(targetMetric);
            targetValues.Add(targetValue);
            targetWeights.Add(targetWeight);
            product.SetValueDate(valueDate);
            Date lastDate = product.GetRequiredIndexDates(index).Last();

            if (!curveDates.ContainsKey(curve))
            {
                curveSet.Add(curve);
                curveDates.Add(curve, new List <Date>());
            }
            curveDates[curve].Add(lastDate);
        }
Example #11
0
 public static object _CreateRateForecastCurveFromDiscount(string objectName,
                                                           object[,] floatingRateIndex,
                                                           object[,] discountCurve,
                                                           object[,] fixingCurve)
 {
     try
     {
         FloatingIndex       _floatingRateIndex = XU.GetFloatingIndex0D(floatingRateIndex, "floatingRateIndex");
         IDiscountingSource  _discountCurve     = XU.GetObject0D <IDiscountingSource>(discountCurve, "discountCurve");
         IFloatingRateSource _fixingCurve       = XU.GetObject0D <IFloatingRateSource>(fixingCurve, "fixingCurve", null);
         IFloatingRateSource _result            = XLRates.CreateRateForecastCurveFromDiscount(_floatingRateIndex, _discountCurve, _fixingCurve);
         return(XU.AddObject(objectName, _result));
     }
     catch (Exception e)
     {
         return(XU.Error0D(e));
     }
 }
Example #12
0
 public static object _CreateHWModelDemo(string objectName,
                                         object[,] meanReversion,
                                         object[,] flatVol,
                                         object[,] baseCurve,
                                         object[,] forecastIndices)
 {
     try
     {
         Double             _meanReversion   = XU.GetDouble0D(meanReversion, "meanReversion");
         Double             _flatVol         = XU.GetDouble0D(flatVol, "flatVol");
         IDiscountingSource _baseCurve       = XU.GetObject0D <IDiscountingSource>(baseCurve, "baseCurve");
         FloatingIndex      _forecastIndices = XU.GetFloatingIndex0D(forecastIndices, "forecastIndices");
         HullWhite1F        _result          = XLRates.CreateHWModelDemo(_meanReversion, _flatVol, _baseCurve, _forecastIndices);
         return(XU.AddObject(objectName, _result));
     }
     catch (Exception e)
     {
         return(XU.Error0D(e));
     }
 }
Example #13
0
 public static object _CreateLoanFloatingRate(string objectName,
                                              object[,] currency,
                                              object[,] balanceDates,
                                              object[,] balanceAmounts,
                                              object[,] floatingIndex,
                                              object[,] floatingSpread)
 {
     try
     {
         Currency         _currency       = XU.GetCurrency0D(currency, "currency");
         Date[]           _balanceDates   = XU.GetDate1D(balanceDates, "balanceDates");
         Double[]         _balanceAmounts = XU.GetDouble1D(balanceAmounts, "balanceAmounts");
         FloatingIndex    _floatingIndex  = XU.GetFloatingIndex0D(floatingIndex, "floatingIndex");
         Double           _floatingSpread = XU.GetDouble0D(floatingSpread, "floatingSpread");
         LoanFloatingRate _result         = XLRates.CreateLoanFloatingRate(_currency, _balanceDates, _balanceAmounts, _floatingIndex, _floatingSpread);
         return(XU.AddObject(objectName, _result));
     }
     catch (Exception e)
     {
         return(XU.Error0D(e));
     }
 }
Example #14
0
 public void AddForecast(FloatingIndex index)
 {
     forecastTenors.Add(index, index.tenor);
 }
Example #15
0
        private FloatLeg CreateFloatingLeg(Currency ccy, Date startDate, double notional, FloatingIndex index,
                                           int tenorYears)
        {
            var quarters         = tenorYears * 4;
            var paymentDates     = Enumerable.Range(1, quarters).Select(i => startDate.AddMonths(3 * i)).ToArray();
            var resetDates       = Enumerable.Range(0, quarters).Select(i => startDate.AddMonths(3 * i)).ToArray();
            var notionals        = Vector.Ones(quarters).Multiply(notional);
            var spreads          = Vector.Zeros(quarters);
            var accrualFractions = Vector.Ones(quarters).Multiply(0.25);
            var floatingIndices  = Enumerable.Range(1, quarters).Select(i => index).ToArray();
            var leg = new FloatLeg(ccy, paymentDates, notionals, resetDates, floatingIndices, spreads,
                                   accrualFractions);

            return(leg);
        }