// setup public CommonVars() { // force garbage collection // garbage collection in .NET is rather weird and we do need when we run several tests in a row GC.Collect(); // data calendar = new TARGET(); settlementDays = 2; today = new Date(9, Month.June, 2009); compounding = Compounding.Continuous; dayCount = new Actual360(); settlementDate = calendar.advance(today, settlementDays, TimeUnit.Days); Settings.setEvaluationDate(today); int[] ts = new int[] { 13, 41, 75, 165, 256, 345, 524, 703 }; double[] r = new double[] { 0.035, 0.033, 0.034, 0.034, 0.036, 0.037, 0.039, 0.040 }; List<double> rates = new List<double>() { 0.035 }; List<Date> dates = new List<Date>() { settlementDate }; for (int i = 0; i < 8; ++i) { dates.Add(calendar.advance(today, ts[i], TimeUnit.Days)); rates.Add(r[i]); } termStructure = new InterpolatedZeroCurve<Linear>(dates, rates, dayCount); }
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); }
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; }
// 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); }
//public FxTermStructure fxForwardTS_ { get; set; } public FxUnderlying(CurrencyType baseCurrency, YieldTermStructure baseCurrencyTS, CurrencyType counterCurrency, YieldTermStructure counterCurrencyTS, double spotRate) { this.baseCurrency_ = baseCurrency; this.counterCurrency_ = counterCurrency; this.baseCurrencyTS_ = baseCurrencyTS; this.counterCurrencyTS_ = counterCurrencyTS; this.spotRate_ = spotRate; }
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)); }
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]); } }
// 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.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); }
public static double cleanPrice(Bond bond, YieldTermStructure discountCurve, Date settlementDate = null) { if (settlementDate == null) { settlementDate = bond.settlementDate(); } Utils.QL_REQUIRE(BondFunctions.isTradable(bond, settlementDate), () => "non tradable at " + settlementDate + " settlementDate date (maturity being " + bond.maturityDate() + ")"); double dirtyPrice = CashFlows.npv(bond.cashflows(), discountCurve, false, settlementDate) * 100.0 / bond.notional(settlementDate); return(dirtyPrice - bond.accruedAmount(settlementDate)); }
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() + ")"); 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)); }
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() + ")"); double dirtyPrice = CashFlows.npv(bond.cashflows(), discount, zSpread, dayCounter, compounding, frequency, false, settlementDate) * 100.0 / bond.notional(settlementDate); return(dirtyPrice - bond.accruedAmount(settlementDate)); }
public void addEvalCurve(string index_cd) { QLNet.YieldTermStructure ql_curve = new QLNet.YieldTermStructure(); YieldCurve cm = new YieldCurve(); clsSET_EVALUATIONCURVE_TB clstb = new clsSET_EVALUATIONCURVE_TB(); clstb.INDEX_CD = index_cd; clstb.SelectOwn(); string curve_cd = clstb.CURVE_ID; // ql _ yield curve build //this.EvalCurveMap_.Add(index_cd, ql_curve); this.EvalCurveManagerMap_.Add(index_cd, cm); }
public override string value(DateTime dateTime, Excel_instrumentViewModel e_instVM, Excel_underlyingInfoViewModel excel_uivm) { // 국고 커브를 로드함 DateTime maturity = e_instVM.Excel_interfaceViewModel_.Excel_issueInfoViewModel_.MaturityDate_; Excel_yieldCurveViewModel e_ycvm = e_instVM.Excel_parameterViewModel_.ParameterSettingManager_.DiscountCurveParaSetting_.discountCurve(dateTime, "KRW"); QLNet.YieldTermStructure q_y = e_ycvm.yieldCurve(); TimeSpan timeSpan = maturity - dateTime; double t = timeSpan.TotalDays / 365; InterestRate rate = q_y.zeroRate(t, QLNet.Compounding.Compounded); double drift = rate.value(); return(drift.ToString()); }
//! 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.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)); }
public YieldTermStructure buildQLNet_YieldTS(Excel_irCurveDataViewModel e_irCurveData) { QLNet.YieldTermStructure ql_yts = new YieldTermStructure(); List<QLNet.Date> dates = new List<QLNet.Date>(); List<double> yields = new List<double>(); foreach (var item in e_irCurveData.Excel_rateDataViewModelList_) { string tenor = item.Tenor_; if (tenor.Substring(0, 1) == "D") { int addDays = Convert.ToInt32(tenor.Substring(1, 3)); dates.Add(ProgramVariable.ReferenceDate_.AddDays(addDays)); } else if (tenor.Substring(0, 1) == "M") { int addMonths = Convert.ToInt32(tenor.Substring(1, 3)); dates.Add(ProgramVariable.ReferenceDate_.AddMonths(addMonths)); } else { throw new Exception("unknown type tenor : " + tenor); } yields.Add(Convert.ToDouble(item.Value_)); } QLNet.DayCounter dc = new QLNet.Actual365Fixed(); QLNet.InterpolatedZeroCurve<QLNet.Linear> curve = new QLNet.InterpolatedZeroCurve<QLNet.Linear>( dates, yields, dc, new QLNet.Linear()); return ql_yts; }
// Basis-point sensitivity of the cash flows. // The result is the change in NPV due to a uniform 1-basis-point change in the rate paid by the cash flows. The change for each coupon is discounted according to the given term structure. public static double bps(List <CashFlow> cashflows, YieldTermStructure discountCurve, Date settlementDate = null, Date npvDate = null, int exDividendDays = 0) { if (cashflows.Count == 0) { return(0.0); } if (settlementDate == null) { settlementDate = discountCurve.referenceDate(); } BPSCalculator calc = new BPSCalculator(discountCurve, npvDate); for (int i = 0; i < cashflows.Count; i++) { if (!cashflows[i].hasOccurred(settlementDate + exDividendDays)) { cashflows[i].accept(calc); } } return(basisPoint_ * calc.result()); }
public static double zSpread(Bond bond, double cleanPrice, YieldTermStructure discount, DayCounter dayCounter, Compounding compounding, Frequency frequency, Date settlementDate = null, double accuracy = 1.0e-10, int maxIterations = 100, double guess = 0.0) { if (settlementDate == null) { settlementDate = bond.settlementDate(); } Utils.QL_REQUIRE(BondFunctions.isTradable(bond, settlementDate), () => "non tradable at " + settlementDate + " (maturity being " + bond.maturityDate() + ")"); double dirtyPrice = cleanPrice + bond.accruedAmount(settlementDate); dirtyPrice /= 100.0 / bond.notional(settlementDate); return(CashFlows.zSpread(bond.cashflows(), discount, dirtyPrice, dayCounter, compounding, frequency, false, settlementDate, settlementDate, accuracy, maxIterations, guess)); }
// NPV of the cash flows. // The NPV is the sum of the cash flows, each discounted // according to the z-spreaded term structure. The result // is affected by the choice of the z-spread compounding // and the relative frequency and day counter. public static double npv(Leg leg,YieldTermStructure discountCurve,double zSpread,DayCounter dc,Compounding comp, Frequency freq,bool includeSettlementDateFlows, Date settlementDate = null,Date npvDate = null) { if (leg.empty()) return 0.0; if (settlementDate == null) settlementDate = Settings.evaluationDate(); if (npvDate == null) npvDate = settlementDate; Handle<YieldTermStructure> discountCurveHandle = new Handle<YieldTermStructure>(discountCurve); Handle<Quote> zSpreadQuoteHandle = new Handle<Quote>(new SimpleQuote(zSpread)); ZeroSpreadedTermStructure spreadedCurve = new ZeroSpreadedTermStructure(discountCurveHandle,zSpreadQuoteHandle, comp, freq, dc); spreadedCurve.enableExtrapolation(discountCurveHandle.link.allowsExtrapolation()); return npv(leg, spreadedCurve, includeSettlementDateFlows, settlementDate, npvDate); }
public BPSCalculator(YieldTermStructure termStructure, Date npvDate) { termStructure_ = termStructure; npvDate_ = npvDate; result_ = 0; }
public double guess(YieldTermStructure c, Date d) { return c.discount(d, true); }
public Date initialDate(YieldTermStructure c) { return traits_.initialDate(c); }
public virtual double atmRate(YieldTermStructure discountCurve) { return(CashFlows.atmRate(yoyLeg_, discountCurve, false, discountCurve.referenceDate())); }
public BPSCalculator(YieldTermStructure discountCurve) { discountCurve_ = discountCurve; nonSensNPV_ = 0.0; bps_ = 0.0; }
public double guess(YieldTermStructure c, Date d) { return(traits_.guess(c, d)); }
public double guess(YieldTermStructure c, Date d) { return c.zeroRate(d, c.dayCounter(), Compounding.Continuous, Frequency.Annual, true).rate(); }
// cleanup // SavedSettings backup; // setup public CommonVars() { calendar = new TARGET(); settlementDays = 2; Date today = calendar.adjust(Date.Today); Settings.setEvaluationDate(today); Date settlement = calendar.advance(today, settlementDays, TimeUnit.Days); int deposits = depositData.Length, swaps = swapData.Length; var instruments = new List<RateHelper>(deposits + swaps); for (int i = 0; i < deposits; i++) { instruments.Add(new DepositRateHelper(depositData[i].rate / 100, new Period(depositData[i].n, depositData[i].units), settlementDays, calendar, BusinessDayConvention.ModifiedFollowing, true, new Actual360())); } IborIndex index = new IborIndex("dummy", new Period(6, TimeUnit.Months), settlementDays, new Currency(), calendar, BusinessDayConvention.ModifiedFollowing, false, new Actual360()); for (int i = 0; i < swaps; ++i) { instruments.Add(new SwapRateHelper(swapData[i].rate / 100, new Period(swapData[i].n, swapData[i].units), calendar, Frequency.Annual, BusinessDayConvention.Unadjusted, new Thirty360(), index)); } termStructure = new PiecewiseYieldCurve<Discount, LogLinear>(settlement, instruments, new Actual360()); dummyTermStructure = new PiecewiseYieldCurve<Discount, LogLinear>(settlement, instruments, new Actual360()); }
// 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; }
// Basis-point sensitivity of the cash flows. // The result is the change in NPV due to a uniform 1-basis-point change in the rate paid by the cash flows. The change for each coupon is discounted according to the given term structure. public static double bps(List<CashFlow> cashflows, YieldTermStructure discountCurve, Date settlementDate = null, Date npvDate = null, int exDividendDays = 0 ) { if (cashflows.Count == 0) return 0.0; if (settlementDate == null) settlementDate = discountCurve.referenceDate(); BPSCalculator calc = new BPSCalculator(discountCurve, npvDate); for (int i = 0; i < cashflows.Count; i++) if (!cashflows[i].hasOccurred(settlementDate + exDividendDays)) cashflows[i].accept(calc); return basisPoint_ * calc.result(); }
//@} //! NPV of the cash flows. The NPV is the sum of the cash flows, each discounted according to the given term structure. public static double npv(List<CashFlow> cashflows, YieldTermStructure discountCurve, Date settlementDate = null,Date npvDate = null, int exDividendDays = 0) { if (cashflows.Count == 0) return 0.0; if (settlementDate == null) settlementDate = discountCurve.referenceDate(); double totalNPV = cashflows.Where(x => !x.hasOccurred(settlementDate + exDividendDays)). Sum(c => c.amount() * discountCurve.discount(c.date())); if (npvDate == null) return totalNPV; else return totalNPV / discountCurve.discount(npvDate); }
// 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.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==0.0) return 0.0; double bps = calc.bps(); Utils.QL_REQUIRE( bps != 0.0, () => "null bps: impossible atm rate" ); return targetNpv.Value/bps; }
} // initial guess public double guess(YieldTermStructure c, Date d) { return(c.discount(d, true)); } // further guesses
// 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); }
} // start of curve data public double initialValue(YieldTermStructure c) { return(1); } // value at reference date
public double initialValue(YieldTermStructure c) { return avgRate; }
public override void setTermStructure(YieldTermStructure t) { // no need to register---the index is not lazy termStructureHandle_.linkTo(t, false); base.setTermStructure(t); }
public double initialValue(YieldTermStructure c) { return traits_.initialValue(c); }
public Date initialDate(YieldTermStructure c) { return(c.referenceDate()); } // start of curve data
public override void initialize(FloatingRateCoupon coupon) { coupon_ = coupon as CmsCoupon; Utils.QL_REQUIRE( coupon_ != null, () => "CMS coupon needed" ); gearing_ = coupon_.gearing(); spread_ = coupon_.spread(); fixingDate_ = coupon_.fixingDate(); paymentDate_ = coupon_.date(); SwapIndex swapIndex = coupon_.swapIndex(); rateCurve_ = swapIndex.forwardingTermStructure().link; Date today = Settings.evaluationDate(); if (paymentDate_ > today) discount_ = rateCurve_.discount(paymentDate_); else discount_ = 1.0; spreadLegValue_ = spread_ * coupon_.accrualPeriod() * discount_; if (fixingDate_ > today) { swapTenor_ = swapIndex.tenor(); VanillaSwap swap = swapIndex.underlyingSwap(fixingDate_); swapRateValue_ = swap.fairRate(); double bp = 1.0e-4; annuity_ = (swap.floatingLegBPS() / bp); int q = (int)swapIndex.fixedLegTenor().frequency(); Schedule schedule = swap.fixedSchedule(); DayCounter dc = swapIndex.dayCounter(); //DayCounter dc = coupon.dayCounter(); double startTime = dc.yearFraction(rateCurve_.referenceDate(), swap.startDate()); double swapFirstPaymentTime = dc.yearFraction(rateCurve_.referenceDate(), schedule.date(1)); double paymentTime = dc.yearFraction(rateCurve_.referenceDate(), paymentDate_); double delta = (paymentTime - startTime) / (swapFirstPaymentTime - startTime); switch (modelOfYieldCurve_) { case GFunctionFactory.YieldCurveModel.Standard: gFunction_ = GFunctionFactory.newGFunctionStandard(q, delta, swapTenor_.length()); break; case GFunctionFactory.YieldCurveModel.ExactYield: gFunction_ = GFunctionFactory.newGFunctionExactYield(coupon_); break; case GFunctionFactory.YieldCurveModel.ParallelShifts: { Handle<Quote> nullMeanReversionQuote = new Handle<Quote>(new SimpleQuote(0.0)); gFunction_ = GFunctionFactory.newGFunctionWithShifts(coupon_, nullMeanReversionQuote); } break; case GFunctionFactory.YieldCurveModel.NonParallelShifts: gFunction_ = GFunctionFactory.newGFunctionWithShifts(coupon_, meanReversion_); break; default: throw new ApplicationException("unknown/illegal gFunction type"); } vanillaOptionPricer_ = new BlackVanillaOptionPricer(swapRateValue_, fixingDate_, swapTenor_, swaptionVolatility().link); } }
//! deprecated implied Z-spread. public static double zSpread(Leg leg, YieldTermStructure d, double npv, 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) { return zSpread(leg, npv, d, dayCounter, compounding, frequency, includeSettlementDateFlows, settlementDate, npvDate, accuracy, maxIterations, guess); }
////////////////////////////////////////////////////////////////////////////////////// // methods public double price(YieldTermStructure yts) { return(amount() * yts.discount(date())); }
// Basis-point sensitivity of the cash flows. // The result is the change in NPV due to a uniform 1-basis-point change in the rate paid by the cash flows. The change for each coupon is discounted according to the given term structure. public static double bps(Leg leg, YieldTermStructure discountCurve, bool includeSettlementDateFlows, Date settlementDate = null, Date npvDate = null) { if (leg.empty()) return 0.0; if (settlementDate == null) settlementDate = Settings.evaluationDate(); if (npvDate == null) npvDate = settlementDate; BPSCalculator calc = new BPSCalculator(discountCurve); for (int i = 0; i < leg.Count; ++i) { if (!leg[i].hasOccurred(settlementDate, includeSettlementDateFlows) && !leg[i].tradingExCoupon(settlementDate)) leg[i].accept(calc); } return basisPoint_ * calc.bps() / discountCurve.discount(npvDate); }
//! 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.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); }
// 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.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]; } }
public ZSpreadFinder(Leg leg,YieldTermStructure discountCurve,double npv,DayCounter dc,Compounding comp,Frequency freq, bool includeSettlementDateFlows, Date settlementDate, Date npvDate) { leg_ = leg; npv_ = npv; zSpread_ = new SimpleQuote(0.0); curve_ = new ZeroSpreadedTermStructure(new Handle<YieldTermStructure>(discountCurve), new Handle<Quote>(zSpread_), comp, freq, dc); includeSettlementDateFlows_ = includeSettlementDateFlows; settlementDate_ = settlementDate; npvDate_ = npvDate; if (settlementDate == null) settlementDate = Settings.evaluationDate(); if (npvDate == null) npvDate = settlementDate; // if the discount curve allows extrapolation, let's // the spreaded curve do too. curve_.enableExtrapolation(discountCurve.allowsExtrapolation()); }
public double atmRate(YieldTermStructure discountCurve) { return(CashFlows.atmRate(floatingLeg_, discountCurve)); }
//// need to refactor the bond classes and remove the following //public static Date previousCouponDate(List<CashFlow> leg, bool includeSettlementDateFlows,Date refDate) //{ // var cf = previousCashFlow(leg,includeSettlementDateFlows, refDate); // if (cf == leg.Last()) return null; // return cf.date(); //} //public static Date nextCouponDate(List<CashFlow> leg, bool includeSettlementDateFlows,Date refDate) //{ // var cf = nextCashFlow(leg,includeSettlementDateFlows, refDate); // if (cf == leg.Last()) return null; // return cf.date(); //} ////@} #region YieldTermStructure functions //! NPV of the cash flows. The NPV is the sum of the cash flows, each discounted according to the given term structure. public static double npv(Leg leg,YieldTermStructure discountCurve, bool includeSettlementDateFlows, Date settlementDate = null, Date npvDate = null) { if (leg.empty()) return 0.0; if (settlementDate == null) settlementDate = Settings.evaluationDate(); if (npvDate == null) npvDate = settlementDate; double totalNPV = 0.0; for (int i=0; i<leg.Count; ++i) { if (!leg[i].hasOccurred(settlementDate, includeSettlementDateFlows) && !leg[i].tradingExCoupon(settlementDate)) totalNPV += leg[i].amount() * discountCurve.discount(leg[i].date()); } return totalNPV/discountCurve.discount(npvDate); }
} // initial guess // further guesses public double guess(YieldTermStructure c, Date d) { return(c.forwardRate(d, d, c.dayCounter(), Compounding.Continuous, Frequency.Annual, true).rate()); }
//! NPV and BPS of the cash flows. // The NPV and BPS of the cash flows calculated together for performance reason public static void npvbps(Leg leg,YieldTermStructure discountCurve, bool includeSettlementDateFlows, Date settlementDate, Date npvDate, out double npv,out double bps) { npv = 0.0; if (leg.empty()) { bps = 0.0; return; } 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); } } double d = discountCurve.discount(npvDate); npv /= d; bps = basisPoint_ * calc.bps() / d; }
//! NPV of a single cash flows public static double npv(CashFlow cashflow, YieldTermStructure discountCurve, Date settlementDate = null, Date npvDate = null, int exDividendDays = 0) { double NPV = 0.0; if (cashflow == null) return 0.0; if (settlementDate == null) settlementDate = Settings.evaluationDate(); if (npvDate == null) npvDate = settlementDate; if (!cashflow.hasOccurred(settlementDate + exDividendDays)) NPV = cashflow.amount() * discountCurve.discount(cashflow.date()); return NPV / discountCurve.discount(npvDate); }
} // start of curve data public double initialValue(YieldTermStructure c) { return(avgRate); } // dummy value at reference date
public double initialValue(YieldTermStructure c) { return(traits_.initialValue(c)); }
public double guess(YieldTermStructure c, Date d) { return traits_.guess(c, d); }
public Date initialDate(YieldTermStructure c) { return(traits_.initialDate(c)); }
public Date initialDate(YieldTermStructure c) { return c.referenceDate(); }