示例#1
0
        // Instrument interface
        public override void calculate()
        {
            if (discountCurve_.empty())
            {
                throw new ArgumentException("no discounting term structure set");
            }

            results_.value         = results_.cash = 0;
            results_.errorEstimate = null;
            results_.legNPV        = new InitializedList <double?>(arguments_.legs.Count);
            results_.legBPS        = new InitializedList <double?>(arguments_.legs.Count);
            List <double?> startDiscounts = new InitializedList <double?>(arguments_.legs.Count);

            for (int i = 0; i < arguments_.legs.Count; ++i)
            {
                results_.legNPV[i] = arguments_.payer[i] * CashFlows.npv(arguments_.legs[i], discountCurve_);
                results_.legBPS[i] = arguments_.payer[i] * CashFlows.bps(arguments_.legs[i], discountCurve_);
                results_.value    += results_.legNPV[i];
                results_.cash     += arguments_.payer[i] * CashFlows.cash(arguments_.legs[i]);
                try {
                    Date d = CashFlows.startDate(arguments_.legs[i]);
                    startDiscounts[i] = discountCurve_.link.discount(d);
                } catch {
                    startDiscounts[i] = null;
                }
            }
            results_.additionalResults.Add("startDiscounts", startDiscounts);
        }
        // At-the-money rate of the cash flows.
        // The result is the fixed rate for which a fixed rate cash flow  vector, equivalent to the input vector, has the required NPV according to the given term structure. If the required NPV is
        //  not given, the input cash flow vector's NPV is used instead.
        public static double atmRate(List <CashFlow> cashflows, YieldTermStructure discountCurve,
                                     Date settlementDate = null, Date npvDate = null, int exDividendDays = 0, double?npv = null)
        {
            double bps = CashFlows.bps(cashflows, discountCurve, settlementDate, npvDate, exDividendDays);

            if (npv == null)
            {
                npv = CashFlows.npv(cashflows, discountCurve, settlementDate, npvDate, exDividendDays);
            }
            return(basisPoint_ * npv.Value / bps);
        }
示例#3
0
        public static double bps(Bond bond, YieldTermStructure discountCurve, Date settlementDate = null)
        {
            if (settlementDate == null)
            {
                settlementDate = bond.settlementDate();
            }

            Utils.QL_REQUIRE(BondFunctions.isTradable(bond, settlementDate), () =>
                             "non tradable at " + settlementDate +
                             " (maturity being " + bond.maturityDate() + ")");

            return(CashFlows.bps(bond.cashflows(), discountCurve, false, settlementDate) * 100.0 / bond.notional(settlementDate));
        }
示例#4
0
        public static double bps(Bond bond, InterestRate yield, Date settlementDate = null)
        {
            if (settlementDate == null)
            {
                settlementDate = bond.settlementDate();
            }

            Utils.QL_REQUIRE(BondFunctions.isTradable(bond, settlementDate),
                             "non tradable at " + settlementDate +
                             " (maturity being " + bond.maturityDate() + ")");

            return(CashFlows.bps(bond.cashflows(), yield, false, settlementDate) *
                   100.0 / bond.notional(settlementDate));
        }
示例#5
0
        public override void calculate()
        {
            double basisPoint = 1.0e-4;

            Date exerciseDate = arguments_.exercise.date(0);

            // the part of the swap preceding exerciseDate should be truncated
            // to avoid taking into account unwanted cashflows
            // for the moment we add a check avoiding this situation
            VanillaSwap swap = arguments_.swap;

            double          strike      = swap.fixedRate;
            List <CashFlow> fixedLeg    = swap.fixedLeg();
            FixedRateCoupon firstCoupon = fixedLeg[0] as FixedRateCoupon;

            Utils.QL_REQUIRE(firstCoupon != null, () => "wrong coupon type");

            Utils.QL_REQUIRE(firstCoupon.accrualStartDate() >= exerciseDate,
                             () => "swap start (" + firstCoupon.accrualStartDate() + ") before exercise date ("
                             + exerciseDate + ") not supported in Black swaption engine");

            // using the forecasting curve
            swap.setPricingEngine(new DiscountingSwapEngine(swap.iborIndex().forwardingTermStructure()));
            double atmForward = swap.fairRate();

            // Volatilities are quoted for zero-spreaded swaps.
            // Therefore, any spread on the floating leg must be removed
            // with a corresponding correction on the fixed leg.
            if (swap.spread.IsNotEqual(0.0))
            {
                double correction = swap.spread * Math.Abs(swap.floatingLegBPS() / swap.fixedLegBPS());
                strike     -= correction;
                atmForward -= correction;
                results_.additionalResults["spreadCorrection"] = correction;
            }
            else
            {
                results_.additionalResults["spreadCorrection"] = 0.0;
            }
            results_.additionalResults["strike"]     = strike;
            results_.additionalResults["atmForward"] = atmForward;

            // using the discounting curve
            swap.setPricingEngine(new DiscountingSwapEngine(discountCurve_, false));
            double annuity = 0;

            switch (arguments_.settlementType)
            {
            case Settlement.Type.Physical:
            {
                annuity = Math.Abs(swap.fixedLegBPS()) / basisPoint;
                break;
            }

            case Settlement.Type.Cash:
            {
                DayCounter dayCount = firstCoupon.dayCounter();

                // we assume that the cash settlement date is equal
                // to the swap start date
                Date discountDate = model_ == CashAnnuityModel.DiscountCurve
                                   ? firstCoupon.accrualStartDate()
                                   : discountCurve_.link.referenceDate();

                double fixedLegCashBPS =
                    CashFlows.bps(fixedLeg,
                                  new InterestRate(atmForward, dayCount, Compounding.Compounded, Frequency.Annual), false,
                                  discountDate);

                annuity = Math.Abs(fixedLegCashBPS / basisPoint) * discountCurve_.link.discount(discountDate);
                break;
            }

            default:
                Utils.QL_FAIL("unknown settlement type");
                break;
            }
            results_.additionalResults["annuity"] = annuity;

            double swapLength = vol_.link.swapLength(swap.floatingSchedule().dates().First(),
                                                     swap.floatingSchedule().dates().Last());

            results_.additionalResults["swapLength"] = swapLength;

            double variance = vol_.link.blackVariance(exerciseDate,
                                                      swapLength,
                                                      strike);
            double displacement = displacement_ == null
                               ? vol_.link.shift(exerciseDate, swapLength)
                               : Convert.ToDouble(displacement_);

            double stdDev = Math.Sqrt(variance);

            results_.additionalResults["stdDev"] = stdDev;
            Option.Type w = (arguments_.type == VanillaSwap.Type.Payer) ? Option.Type.Call : Option.Type.Put;
            results_.value = new Spec().value(w, strike, atmForward, stdDev, annuity, displacement);

            double exerciseTime = vol_.link.timeFromReference(exerciseDate);

            results_.additionalResults["vega"] =
                new Spec().vega(strike, atmForward, stdDev, exerciseTime, annuity, displacement);
        }
示例#6
0
        public override void calculate()
        {
            double basisPoint = 1.0e-4;

            Date exerciseDate = arguments_.exercise.date(0);

            // the part of the swap preceding exerciseDate should be truncated
            // to avoid taking into account unwanted cashflows
            VanillaSwap swap = arguments_.swap;

            double strike = swap.fixedRate;

            // using the forecasting curve
            swap.setPricingEngine(new DiscountingSwapEngine(swap.iborIndex().forwardingTermStructure()));
            double atmForward = swap.fairRate();

            // Volatilities are quoted for zero-spreaded swaps.
            // Therefore, any spread on the floating leg must be removed
            // with a corresponding correction on the fixed leg.
            if (swap.spread != 0.0)
            {
                double correction = swap.spread * Math.Abs(swap.floatingLegBPS() / swap.fixedLegBPS());
                strike     -= correction;
                atmForward -= correction;
                results_.additionalResults["spreadCorrection"] = correction;
            }
            else
            {
                results_.additionalResults["spreadCorrection"] = 0.0;
            }
            results_.additionalResults["strike"]     = strike;
            results_.additionalResults["atmForward"] = atmForward;

            // using the discounting curve
            swap.setPricingEngine(new DiscountingSwapEngine(termStructure_));
            double annuity;

            switch (arguments_.settlementType)
            {
            case Settlement.Type.Physical: {
                annuity = Math.Abs(swap.fixedLegBPS()) / basisPoint;
                break;
            }

            case Settlement.Type.Cash: {
                List <CashFlow> fixedLeg        = swap.fixedLeg();
                FixedRateCoupon firstCoupon     = (FixedRateCoupon)fixedLeg[0];
                DayCounter      dayCount        = firstCoupon.dayCounter();
                double          fixedLegCashBPS =
                    CashFlows.bps(fixedLeg,
                                  new InterestRate(atmForward, dayCount, QLNet.Compounding.Compounded, Frequency.Annual), false,
                                  termStructure_.link.referenceDate());
                annuity = Math.Abs(fixedLegCashBPS / basisPoint);
                break;
            }

            default:
                throw new ApplicationException("unknown settlement type");
            }
            results_.additionalResults["annuity"] = annuity;

            // the swap length calculation might be improved using the value date
            // of the exercise date
            double swapLength = volatility_.link.swapLength(exerciseDate,
                                                            arguments_.floatingPayDates.Last());

            results_.additionalResults["swapLength"] = swapLength;

            double variance = volatility_.link.blackVariance(exerciseDate,
                                                             swapLength,
                                                             strike);
            double stdDev = Math.Sqrt(variance);

            results_.additionalResults["stdDev"] = stdDev;
            Option.Type w = (arguments_.type == VanillaSwap.Type.Payer) ?
                            Option.Type.Call : Option.Type.Put;
            results_.value = Utils.blackFormula(w, strike, atmForward, stdDev, annuity);

            double exerciseTime = volatility_.link.timeFromReference(exerciseDate);

            results_.additionalResults["vega"] = Math.Sqrt(exerciseTime) *
                                                 Utils.blackFormulaStdDevDerivative(strike, atmForward, stdDev, annuity);
        }