Esempio n. 1
0
 public ConundrumIntegrand(VanillaOptionPricer o, YieldTermStructure curve, GFunction gFunction, Date fixingDate, Date paymentDate, double annuity, double forwardValue, double strike, Option.Type optionType)
 {
     vanillaOptionPricer_ = o;
     forwardValue_        = forwardValue;
     annuity_             = annuity;
     fixingDate_          = fixingDate;
     paymentDate_         = paymentDate;
     strike_     = strike;
     optionType_ = optionType;
     gFunction_  = gFunction;
 }
Esempio n. 2
0
 public FdmHestonVariancePart(
     FdmMesher mesher,
     YieldTermStructure rTS,
     double sigma, double kappa, double theta)
 {
     dyMap_ = new SecondDerivativeOp(1, mesher)
              .mult(0.5 * sigma * sigma * mesher.locations(1))
              .add(new FirstDerivativeOp(1, mesher)
                   .mult(kappa * (theta - mesher.locations(1))));
     mapT_ = new TripleBandLinearOp(1, mesher);
     rTS_  = rTS;
 }
Esempio n. 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() + ")",
                             QLNetExceptionEnum.NotTradableException);

            return(CashFlows.bps(bond.cashflows(), discountCurve, false, settlementDate) * 100.0 / bond.notional(settlementDate));
        }
        public LongstaffSchwartzPathPricer(TimeGrid times, IEarlyExercisePathPricer <PathType, double> pathPricer,
                                           YieldTermStructure termStructure)
        {
            calibrationPhase_ = true;
            pathPricer_       = pathPricer;
            coeff_            = new InitializedList <Vector>(times.size() - 1);
            dF_ = new InitializedList <double>(times.size() - 1);
            v_  = pathPricer_.basisSystem();

            for (int i = 0; i < times.size() - 1; ++i)
            {
                dF_[i] = termStructure.discount(times[i + 1])
                         / termStructure.discount(times[i]);
            }
        }
Esempio n. 5
0
        // 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(Leg leg, YieldTermStructure discountCurve, bool includeSettlementDateFlows,
                                     Date settlementDate = null, Date npvDate = null, double?targetNpv = null)
        {
            if (settlementDate == null)
            {
                settlementDate = Settings.Instance.evaluationDate();
            }

            if (npvDate == null)
            {
                npvDate = settlementDate;
            }

            double        npv  = 0.0;
            BPSCalculator calc = new BPSCalculator(discountCurve);

            for (int i = 0; i < leg.Count; ++i)
            {
                CashFlow cf = leg[i];
                if (!cf.hasOccurred(settlementDate, includeSettlementDateFlows) &&
                    !cf.tradingExCoupon(settlementDate))
                {
                    npv += cf.amount() * discountCurve.discount(cf.date());
                    cf.accept(calc);
                }
            }

            if (targetNpv == null)
            {
                targetNpv = npv - calc.nonSensNPV();
            }
            else
            {
                targetNpv *= discountCurve.discount(npvDate);
                targetNpv -= calc.nonSensNPV();
            }

            if (targetNpv.IsEqual(0.0))
            {
                return(0.0);
            }

            double bps = calc.bps();

            Utils.QL_REQUIRE(bps.IsNotEqual(0.0), () => "null bps: impossible atm rate");

            return(targetNpv.Value / bps);
        }
Esempio n. 6
0
        public static double cleanPrice(Bond bond, YieldTermStructure discount, double zSpread, DayCounter dayCounter, Compounding compounding,
                                        Frequency frequency, Date settlementDate = null)
        {
            if (settlementDate == null)
            {
                settlementDate = bond.settlementDate();
            }

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

            double dirtyPrice = CashFlows.npv(bond.cashflows(), discount, zSpread, dayCounter, compounding, frequency, false, settlementDate) *
                                100.0 / bond.notional(settlementDate);

            return(dirtyPrice - bond.accruedAmount(settlementDate));
        }
Esempio n. 7
0
        public static double atmRate(Bond bond, YieldTermStructure discountCurve, Date settlementDate = null, double?cleanPrice = null)
        {
            if (settlementDate == null)
            {
                settlementDate = bond.settlementDate();
            }

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

            double?dirtyPrice      = cleanPrice == null ? null : cleanPrice + bond.accruedAmount(settlementDate);
            double currentNotional = bond.notional(settlementDate);
            double?npv             = dirtyPrice / 100.0 * currentNotional;

            return(CashFlows.atmRate(bond.cashflows(), discountCurve, false, settlementDate, settlementDate, npv));
        }
Esempio n. 8
0
        //! implied Z-spread.
        public static double zSpread(Leg leg, double npv, YieldTermStructure discount, DayCounter dayCounter, Compounding compounding,
                                     Frequency frequency, bool includeSettlementDateFlows, Date settlementDate = null,
                                     Date npvDate = null, double accuracy = 1.0e-10, int maxIterations = 100, double guess = 0.0)
        {
            if (settlementDate == null)
            {
                settlementDate = Settings.Instance.evaluationDate();
            }

            if (npvDate == null)
            {
                npvDate = settlementDate;
            }

            Brent solver = new Brent();

            solver.setMaxEvaluations(maxIterations);
            ZSpreadFinder objFunction = new ZSpreadFinder(leg, discount, npv, dayCounter, compounding, frequency,
                                                          includeSettlementDateFlows, settlementDate, npvDate);
            double step = 0.01;

            return(solver.solve(objFunction, accuracy, guess, step));
        }
Esempio n. 9
0
 // RateHelper interface
 public override void setTermStructure(YieldTermStructure t)
 {
     // do not set the relinkable handle as an observer - force recalculation when needed
     termStructureHandle_.linkTo(t, false);
     base.setTermStructure(t);
 }
Esempio n. 10
0
 public BPSCalculator(YieldTermStructure discountCurve)
 {
     discountCurve_ = discountCurve;
     nonSensNPV_    = 0.0;
     bps_           = 0.0;
 }
Esempio n. 11
0
 public override void setTermStructure(YieldTermStructure t)
 {
     // no need to register---the index is not lazy
     termStructureHandle_.linkTo(t, false);
     base.setTermStructure(t);
 }
Esempio n. 12
0
 //////////////////////////////////////////////////////////////////////////////////////
 // methods
 public double price(YieldTermStructure yts)
 {
     return(amount() * yts.discount(date()));
 }
        // Instrument interface
        public override void calculate()
        {
            Utils.QL_REQUIRE(!discountCurve_.empty(), () => "discounting term structure handle is empty");

            results_.value         = results_.cash = 0;
            results_.errorEstimate = null;

            Date refDate = discountCurve_.link.referenceDate();

            Date settlementDate = settlementDate_;

            if (settlementDate_ == null)
            {
                settlementDate = refDate;
            }
            else
            {
                Utils.QL_REQUIRE(settlementDate >= refDate, () =>
                                 "settlement date (" + settlementDate + ") before " +
                                 "discount curve reference date (" + refDate + ")");
            }

            results_.valuationDate = npvDate_;
            if (npvDate_ == null)
            {
                results_.valuationDate = refDate;
            }
            else
            {
                Utils.QL_REQUIRE(npvDate_ >= refDate, () =>
                                 "npv date (" + npvDate_ + ") before " +
                                 "discount curve reference date (" + refDate + ")");
            }

            results_.npvDateDiscount = discountCurve_.link.discount(results_.valuationDate);

            int n = arguments_.legs.Count;

            results_.legNPV         = new InitializedList <double?>(n);
            results_.legBPS         = new InitializedList <double?>(n);
            results_.startDiscounts = new InitializedList <double?>(n);
            results_.endDiscounts   = new InitializedList <double?>(n);

            bool includeRefDateFlows =
                includeSettlementDateFlows_.HasValue ?
                includeSettlementDateFlows_.Value :
                Settings.Instance.includeReferenceDateEvents;

            for (int i = 0; i < n; ++i)
            {
                try
                {
                    YieldTermStructure discount_ref = discountCurve_.currentLink();
                    double             npv = 0, bps = 0;
                    CashFlows.npvbps(arguments_.legs[i],
                                     discount_ref,
                                     includeRefDateFlows,
                                     settlementDate,
                                     results_.valuationDate,
                                     out npv,
                                     out bps);
                    results_.legNPV[i] = npv * arguments_.payer[i];
                    results_.legBPS[i] = bps * arguments_.payer[i];

                    if (!arguments_.legs[i].empty())
                    {
                        Date d1 = CashFlows.startDate(arguments_.legs[i]);
                        if (d1 >= refDate)
                        {
                            results_.startDiscounts[i] = discountCurve_.link.discount(d1);
                        }
                        else
                        {
                            results_.startDiscounts[i] = null;
                        }

                        Date d2 = CashFlows.maturityDate(arguments_.legs[i]);
                        if (d2 >= refDate)
                        {
                            results_.endDiscounts[i] = discountCurve_.link.discount(d2);
                        }
                        else
                        {
                            results_.endDiscounts[i] = null;
                        }
                    }
                    else
                    {
                        results_.startDiscounts[i] = null;
                        results_.endDiscounts[i]   = null;
                    }
                }
                catch (Exception e)
                {
                    Utils.QL_FAIL((i + 1) + " leg: " + e.Message);
                }
                results_.value += results_.legNPV[i];
            }
        }
Esempio n. 14
0
 public virtual double atmRate(YieldTermStructure discountCurve)
 {
     return(CashFlows.atmRate(yoyLeg_, discountCurve,
                              false, discountCurve.referenceDate()));
 }