public async Task <IActionResult> Edit(int id, [Bind("ID,State")] UnitedStates unitedStates) { if (id != unitedStates.ID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(unitedStates); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UnitedStatesExists(unitedStates.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(unitedStates)); }
/** * The value of the annuity (or RPV01 - the premium leg per unit of coupon) at a specified valuation time. * The actual value of the leg is this multiplied by the notional and the fractional coupon (i.e. coupon * in basis points divided by 10,000). * <p> * If this is a spot starting CDS (effective protection start = 0) then cash flows from premium payments * and accrual-on-default are risky discounted to t=0 ('today'), then rolled forward (risk-free) to the * valuation time; if the annuity is requested clean, the accrued premium (paid at the cash-settle time) is * rolled (again risk-free) to the valuation time; the absolute value of this amount is subtracted from the * other cash flows to give the clean annuity. * <p> * If this is a forward starting CDS (effective protection start > 0), then the premium payments are again * risky discounted to t=0; if the annuity is requested clean, the accrued premium is risk-free discounted * to the effective protection start, then risky discounted to t=0 - this gives the t=0 value of the annuity * including the chance that a default occurs before protection starts. * <p> * If valuationTime > 0, the value of the annuity is rolled forward (risk-free) to that time. * To compute the Expected value of the annuity conditional on no default before the valuationTime, * one must divide this number by the survival probability to the valuationTime (for unit coupon). * * @param cds the analytic description of a CDS traded at a certain time * @param yieldCurve the yield (or discount) curve * @param creditCurve the credit (or survival) curve * @param cleanOrDirty the clean or dirty price * @param valuationTime the valuation time * @return 10,000 times the RPV01 (on a notional of 1) */ public double Annuity(CDS cds, PiecewiseconstantHazardRate hazard, YieldTermStructure yt, CdsPriceType cleanOrDirt) { List <CashFlow> cf = cds.FixLeg; DateTime tradedate = cds.tradedate; DateTime settlementDate = tradedate.AddDays(cds.Cashsettlement); double recoveryrate = cds.Recovery; DateTime Stepindate = tradedate.AddDays(1); OMLib.Conventions.DayCount.Actual360 dc = new OMLib.Conventions.DayCount.Actual360(); double notional = cds.Notional; double coupon = cds.PremiumRate; DateTime lastpayment = cds.formerpaymentdate; if (cf.Count() == 0) { return(0.0); } double ita = (double)365 / 360; double totalNPV = 0.0; for (int i = 0; i < cf.Count; ++i) { totalNPV += cf[i].Amount * cf[i].DiscountFactor * cf[i].Survivalprobability; } double accrualpaidondefault = calculateSinglePeriodAccrualOnDefault(cds, yt, hazard); totalNPV += ita * coupon * accrualpaidondefault * notional / yt.discount(tradedate.AddDays(3)); Calendar calendar = new UnitedStates(); return(totalNPV / yt.discount(settlementDate)); }
public async Task <IActionResult> Create([Bind("ID,State")] UnitedStates unitedStates) { if (ModelState.IsValid) { _context.Add(unitedStates); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(unitedStates)); }
public CDS(double Coupon, double notional, DateTime maturity, DateTime firstpaymentday, DateTime tradedate, DateTime formerpaymentday, string frequency, double recovery, int settlement, int Cashsettlement) { // Product Setup OMLib.Conventions.DayCount.Actual360 AccuralDCC = new OMLib.Conventions.DayCount.Actual360(); OMLib.Conventions.DayCount.Actual365 curveDCC = new OMLib.Conventions.DayCount.Actual365(); Calendar calendar = new UnitedStates(); formerpaymentday = calendar.adjust(formerpaymentday, BusinessDayConvention.Following); int accrued = AccuralDCC.DayCount(formerpaymentday, calendar.adjust(tradedate, BusinessDayConvention.Following)) + 1; this.accruedday = accrued; this.marketvalue = new double(); this.accruedamt = notional * Coupon * accrued / 360; this.Notional = notional; this._payAccOnDefault = true; OMLib.Conventions.BusinessDayConvention convention = new OMLib.Conventions.BusinessDayConvention(Enums.BusinessDayConvention.ModifiedFollowing, tradedate); this.tradedate = calendar.adjust(tradedate, BusinessDayConvention.ModifiedFollowing); /*convention.AdjustedDate;*/ this.Recovery = recovery; convention = new OMLib.Conventions.BusinessDayConvention(Enums.BusinessDayConvention.ModifiedFollowing, firstpaymentday); this.firstpaymentdate = CdsAnalyticFactory.getNextIMMDate(tradedate); /*convention.AdjustedDate;*/ convention = new OMLib.Conventions.BusinessDayConvention(Enums.BusinessDayConvention.ModifiedFollowing, formerpaymentday); this.formerpaymentdate = CdsAnalyticFactory.getPrevIMMDate(tradedate);//calendar.adjust(formerpaymentday,BusinessDayConvention.ModifiedFollowing); /*convention.AdjustedDate;*/ this.Maturity = maturity; this.PremiumRate = Coupon; this.Frequency = frequency; this.Cashsettlement = Cashsettlement; DateTime valueDate = calendar.adjust(tradedate.AddDays(Cashsettlement)); convention = new OMLib.Conventions.BusinessDayConvention(Enums.BusinessDayConvention.ModifiedFollowing, tradedate.AddDays(3)); this.evalDate = calendar.adjust(tradedate.AddDays(settlement), BusinessDayConvention.ModifiedFollowing); /*convention.AdjustedDate;*/ this.Payment_Schedule = PremiumDates(this.Maturity, CdsAnalyticFactory.getNextIMMDate(tradedate), this.Frequency); QLNet.Calendar.OrthodoxImpl cal = new Calendar.OrthodoxImpl(); IsdaPremiumLegSchedule paymentSchedule = new IsdaPremiumLegSchedule(formerpaymentdate, maturity, payment_interval, StubConvention.SHORT_INITIAL, QLNet.BusinessDayConvention.ModifiedFollowing, cal, true); _coupons = CdsCoupon.makeCoupons(tradedate, paymentSchedule, true, ACT_360, ACT_365); OMLib.Conventions.DayCount.Actual365 CurveDCC = new OMLib.Conventions.DayCount.Actual365(); DateTime effectiveStartDate = tradedate; _accStart = DateTime.Compare(formerpaymentdate, tradedate) < 0 ?-CurveDCC.YearFraction(formerpaymentdate, tradedate) : CurveDCC.YearFraction(tradedate, formerpaymentdate); _cashSettlementTime = CurveDCC.YearFraction(tradedate, valueDate); _effectiveProtectionStart = DateTime.Compare(effectiveStartDate, tradedate) < 0 ? -CurveDCC.YearFraction(effectiveStartDate, tradedate) : CurveDCC.YearFraction(tradedate, effectiveStartDate); _protectionEnd = CurveDCC.YearFraction(tradedate, maturity); DateTime accStart = paymentSchedule.getAccStartDate(0); }
private IEnumerable <TradingDay> PopulateTradingDays(DateTime start, DateTime end) { var symbols = _securityManager.Keys; var holidays = new HashSet <DateTime>(); foreach (var symbol in symbols) { var entry = _marketHoursDatabase.GetEntry(symbol.ID.Market, symbol, symbol.ID.SecurityType); foreach (var holiday in entry.ExchangeHours.Holidays) { holidays.Add(holiday.Date); } } var qlCalendar = new UnitedStates(); var options = symbols.Where(x => x.ID.SecurityType.IsOption()).ToList(); var futures = symbols.Where(x => x.ID.SecurityType == SecurityType.Future).ToList(); var totalDays = (int)(end.Date.AddDays(1.0) - start.Date).TotalDays; if (totalDays < 0) { throw new ArgumentException( $"TradingCalendar.PopulateTradingDays(): Total days is negative ({totalDays}), indicating reverse start and end times." + $" Check your usage of TradingCalendar to ensure proper arrangement of variables"); } foreach (var dayIdx in Enumerable.Range(0, totalDays)) { var currentDate = start.Date.AddDays(dayIdx); var publicHoliday = holidays.Contains(currentDate) || !qlCalendar.isBusinessDay(currentDate); var weekend = currentDate.DayOfWeek == DayOfWeek.Sunday || currentDate.DayOfWeek == DayOfWeek.Saturday; var businessDay = !publicHoliday && !weekend; yield return (new TradingDay { Date = currentDate, PublicHoliday = publicHoliday, Weekend = weekend, BusinessDay = businessDay, OptionExpirations = options.Where(x => x.ID.Date.Date == currentDate), FutureExpirations = futures.Where(x => x.ID.Date.Date == currentDate) }); } }
public double ComputeTax(IEnumerable <ShoppingCartQuantityProduct> productQuantities, double subtotal, double shippingCost, string country, string zipCode) { var tax = (subtotal + shippingCost) * Rate; var state = UnitedStates.State(zipCode); var sameState = String.IsNullOrWhiteSpace(State) || State == "*" || State != null && State.Equals(state ?? "", StringComparison.CurrentCultureIgnoreCase); var sameCountry = !String.IsNullOrWhiteSpace(Country) && (Country == "*" || Country.Equals(country, StringComparison.CurrentCultureIgnoreCase)); if (sameState && sameCountry) { return(tax); } return(0); }
public List <DateTime> PremiumDates(DateTime maturity, DateTime firstpaymentdate, string freqency) { List <DateTime> date = new List <DateTime>(); DateTime t = firstpaymentdate; Calendar calendar = new UnitedStates(); date.Add(t); int i = 1; do { switch (freqency) { case "Monthly": t = firstpaymentdate.AddMonths(i); payment_interval = 1; break; case "Quarterly": t = firstpaymentdate.AddMonths(i * 3); payment_interval = 3; break; case "Semiyear": t = firstpaymentdate.AddMonths(i * 6); payment_interval = 6; break; case "Yearly": t = firstpaymentdate.AddMonths(i * 12); payment_interval = 12; break; default: break; } OMLib.Conventions.BusinessDayConvention convention = new OMLib.Conventions.BusinessDayConvention(Enums.BusinessDayConvention.ModifiedFollowing, t); t = calendar.adjust(t, BusinessDayConvention.ModifiedFollowing); date.Add(t /*convention.AdjustedDate;*/); i++; } while (DateTime.Compare(date[i - 1], maturity) < 0); return(date); }
private IEnumerable <TradingDay> PopulateTradingDays(DateTime start, DateTime end) { var symbols = _securityManager.Keys; var holidays = new HashSet <DateTime>(); foreach (var symbol in symbols) { var entry = _marketHoursDatabase.GetEntry(symbol.ID.Market, symbol, symbol.ID.SecurityType); foreach (var holiday in entry.ExchangeHours.Holidays) { holidays.Add(holiday.Date); } } var qlCalendar = new UnitedStates(); var options = symbols.Where(x => x.ID.SecurityType == SecurityType.Option || x.ID.SecurityType == SecurityType.FutureOption).ToList(); var futures = symbols.Where(x => x.ID.SecurityType == SecurityType.Future).ToList(); foreach (var dayIdx in Enumerable.Range(0, (int)(end.Date.AddDays(1.0) - start.Date).TotalDays)) { var currentDate = start.Date.AddDays(dayIdx); var publicHoliday = holidays.Contains(currentDate) || !qlCalendar.isBusinessDay(currentDate); var weekend = currentDate.DayOfWeek == DayOfWeek.Sunday || currentDate.DayOfWeek == DayOfWeek.Saturday; var businessDay = !publicHoliday && !weekend; yield return (new TradingDay { Date = currentDate, PublicHoliday = publicHoliday, Weekend = weekend, BusinessDay = businessDay, OptionExpirations = options.Where(x => x.ID.Date.Date == currentDate), FutureExpirations = futures.Where(x => x.ID.Date.Date == currentDate) }); } }
public double PremiumLegNPV_Exact(CDS cds, PiecewiseconstantHazardRate hazard, YieldTermStructure yt, DateTime tradedate, DateTime settlementDate, double notional, double coupon, List <double> Jumps, DateTime lastpayment) { double ita = (double)365 / 360; double totalNPV = 0.0; CdsCoupon[] cf = cds.getCoupons(); for (int i = 0; i < cf.Length; ++i) { totalNPV += cf[i].getYearFrac() * notional * Math.Exp(-hazard.getRT_(cf[i].getEffEnd())) * Math.Exp(-yt.getRT_(cf[i].getEffEnd())); } double accrualpaidondefault = calculateSinglePeriodAccrualOnDefault(cf, coupon, tradedate, yt, hazard, lastpayment); totalNPV += ita * coupon * accrualpaidondefault * notional / yt.discount(tradedate.AddDays(3)); OMLib.Conventions.DayCount.Actual360 dc = new OMLib.Conventions.DayCount.Actual360(); Calendar calendar = new UnitedStates(); return(totalNPV / Math.Exp(-yt.getRT_(cds.getCashSettleTime()))); }
public void testActualActualWithAnnualSchedule() { // Testing actual/actual with schedule "for undefined annual reference periods // Now do an annual schedule Calendar calendar = new UnitedStates(); Schedule schedule = new MakeSchedule() .from(new Date(10, Month.January, 2017)) .withFirstDate(new Date(31, Month.August, 2017)) .to(new Date(28, Month.February, 2026)) .withFrequency(Frequency.Annual) .withCalendar(calendar) .withConvention(BusinessDayConvention.Unadjusted) .backwards().endOfMonth(false).value(); Date referencePeriodStart = schedule.date(1); Date referencePeriodEnd = schedule.date(2); Date testDate = schedule.date(1); DayCounter dayCounter = new ActualActual(ActualActual.Convention.ISMA, schedule); while (testDate < referencePeriodEnd) { double difference = ISMAYearFractionWithReferenceDates(dayCounter, testDate, referencePeriodEnd, referencePeriodStart, referencePeriodEnd) - dayCounter.yearFraction(testDate, referencePeriodEnd); if (Math.Abs(difference) > 1.0e-10) { QAssert.Fail("Failed to correctly use the schedule " + "to find the reference period for Act/Act:\n" + testDate + " to " + referencePeriodEnd + "\n Ref: " + referencePeriodStart + " to " + referencePeriodEnd); } testDate = calendar.advance(testDate, 1, TimeUnit.Days); } }
public List <DateTime> PremiumDates(DateTime maturity, DateTime firstpaymentdate, string freqency) { List <DateTime> date = new List <DateTime>(); DateTime t = firstpaymentdate; Calendar calendar = new UnitedStates(); date.Add(t); int i = 1; do { switch (freqency) { case "Monthly": t = firstpaymentdate.AddMonths(i); break; case "Quarterly": t = firstpaymentdate.AddMonths(i * 3); break; case "Semiyear": t = firstpaymentdate.AddMonths(i * 6); break; case "Yearly": t = firstpaymentdate.AddMonths(i * 12); break; default: break; } date.Add(t /*convention.AdjustedDate;*/); i++; } while (DateTime.Compare(date[i - 1], maturity) < 0); return(date); }
public double PremiumLegNPV_Exact(List <CashFlow> cf, PiecewiseconstantHazardRate hazard, YieldTermStructure yt, DateTime tradedate, DateTime settlementDate, double notional, double coupon, List <DateTime> Jumps, DateTime lastpayment) { if (cf.Count() == 0) { return(0.0); } double ita = (double)365 / 360; double totalNPV = 0.0; for (int i = 0; i < cf.Count; ++i) { totalNPV += cf[i].Amount * cf[i].DiscountFactor * cf[i].Survivalprobability; } double accrualpaidondefault = calculateSinglePeriodAccrualOnDefault(cf, coupon, tradedate, yt, hazard, Jumps, lastpayment); totalNPV += ita * coupon * accrualpaidondefault * notional / yt.discount(tradedate.AddDays(3)); OMLib.Conventions.DayCount.Actual360 dc = new OMLib.Conventions.DayCount.Actual360(); Calendar calendar = new UnitedStates(); return(totalNPV / yt.discount(settlementDate)); }
public UnitedStates(UnitedStates.Market m) : this(NQuantLibcPINVOKE.new_UnitedStates__SWIG_0((int)m), true) { if (NQuantLibcPINVOKE.SWIGPendingException.Pending) throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve(); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(UnitedStates obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; }
public void testCachedMarketValue() { // Testing credit-default swap against cached market values... using (SavedSettings backup = new SavedSettings()) { Settings.setEvaluationDate(new Date(9, Month.June, 2006)); Date evalDate = Settings.evaluationDate(); Calendar calendar = new UnitedStates(); List <Date> discountDates = new List <Date>(); discountDates.Add(evalDate); discountDates.Add(calendar.advance(evalDate, 1, TimeUnit.Weeks, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 1, TimeUnit.Months, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 2, TimeUnit.Months, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 3, TimeUnit.Months, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 6, TimeUnit.Months, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 12, TimeUnit.Months, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 2, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 3, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 4, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 5, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 6, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 7, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 8, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 9, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 10, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); discountDates.Add(calendar.advance(evalDate, 15, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); List <double> dfs = new List <double>(); dfs.Add(1.0); dfs.Add(0.9990151375768731); dfs.Add(0.99570502636871183); dfs.Add(0.99118260474528685); dfs.Add(0.98661167950906203); dfs.Add(0.9732592953359388); dfs.Add(0.94724424481038083); dfs.Add(0.89844996737120875); dfs.Add(0.85216647839921411); dfs.Add(0.80775477692556874); dfs.Add(0.76517289234200347); dfs.Add(0.72401019553182933); dfs.Add(0.68503909569219212); dfs.Add(0.64797499814013748); dfs.Add(0.61263171936255534); dfs.Add(0.5791942350748791); dfs.Add(0.43518868769953606); DayCounter curveDayCounter = new Actual360(); RelinkableHandle <YieldTermStructure> discountCurve = new RelinkableHandle <YieldTermStructure>(); discountCurve.linkTo(new InterpolatedDiscountCurve <LogLinear>(discountDates, dfs, curveDayCounter, null, null, null, new LogLinear())); DayCounter dayCounter = new Thirty360(); List <Date> dates = new List <Date>(); dates.Add(evalDate); dates.Add(calendar.advance(evalDate, 6, TimeUnit.Months, BusinessDayConvention.ModifiedFollowing)); dates.Add(calendar.advance(evalDate, 1, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); dates.Add(calendar.advance(evalDate, 2, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); dates.Add(calendar.advance(evalDate, 3, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); dates.Add(calendar.advance(evalDate, 4, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); dates.Add(calendar.advance(evalDate, 5, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); dates.Add(calendar.advance(evalDate, 7, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); dates.Add(calendar.advance(evalDate, 10, TimeUnit.Years, BusinessDayConvention.ModifiedFollowing)); List <double> defaultProbabilities = new List <double>(); defaultProbabilities.Add(0.0000); defaultProbabilities.Add(0.0047); defaultProbabilities.Add(0.0093); defaultProbabilities.Add(0.0286); defaultProbabilities.Add(0.0619); defaultProbabilities.Add(0.0953); defaultProbabilities.Add(0.1508); defaultProbabilities.Add(0.2288); defaultProbabilities.Add(0.3666); List <double> hazardRates = new List <double>(); hazardRates.Add(0.0); for (int i = 1; i < dates.Count; ++i) { double t1 = dayCounter.yearFraction(dates[0], dates[i - 1]); double t2 = dayCounter.yearFraction(dates[0], dates[i]); double S1 = 1.0 - defaultProbabilities[i - 1]; double S2 = 1.0 - defaultProbabilities[i]; hazardRates.Add(Math.Log(S1 / S2) / (t2 - t1)); } RelinkableHandle <DefaultProbabilityTermStructure> piecewiseFlatHazardRate = new RelinkableHandle <DefaultProbabilityTermStructure>(); piecewiseFlatHazardRate.linkTo(new InterpolatedHazardRateCurve <BackwardFlat>(dates, hazardRates, new Thirty360())); // Testing credit default swap // Build the schedule Date issueDate = new Date(20, Month.March, 2006); Date maturity = new Date(20, Month.June, 2013); Frequency cdsFrequency = Frequency.Semiannual; BusinessDayConvention cdsConvention = BusinessDayConvention.ModifiedFollowing; Schedule schedule = new Schedule(issueDate, maturity, new Period(cdsFrequency), calendar, cdsConvention, cdsConvention, DateGeneration.Rule.Forward, false); // Build the CDS double recoveryRate = 0.25; double fixedRate = 0.0224; DayCounter dayCount = new Actual360(); double cdsNotional = 100.0; CreditDefaultSwap cds = new CreditDefaultSwap(Protection.Side.Seller, cdsNotional, fixedRate, schedule, cdsConvention, dayCount, true, true); cds.setPricingEngine(new MidPointCdsEngine(piecewiseFlatHazardRate, recoveryRate, discountCurve)); double calculatedNpv = cds.NPV(); double calculatedFairRate = cds.fairSpread(); double npv = -1.364048777; // from Bloomberg we have 98.15598868 - 100.00; double fairRate = 0.0248429452; // from Bloomberg we have 0.0258378; double tolerance = 1e-9; if (Math.Abs(npv - calculatedNpv) > tolerance) { Assert.Fail( "Failed to reproduce the npv for the given credit-default swap\n" + " computed NPV: " + calculatedNpv + "\n" + " Given NPV: " + npv); } if (Math.Abs(fairRate - calculatedFairRate) > tolerance) { Assert.Fail("Failed to reproduce the fair rate for the given credit-default swap\n" + " computed fair rate: " + calculatedFairRate + "\n" + " Given fair rate: " + fairRate); } } }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(UnitedStates obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
public void testRegression() { // Testing fixed-coupon convertible bond in known regression case Date today = new Date(23, Month.December, 2008); Date tomorrow = today + 1; Settings.Instance.setEvaluationDate(tomorrow); Handle <Quote> u = new Handle <Quote>(new SimpleQuote(2.9084382818797443)); List <Date> dates = new InitializedList <Date>(25); List <double> forwards = new InitializedList <double>(25); dates[0] = new Date(29, Month.December, 2008); forwards[0] = 0.0025999342800; dates[1] = new Date(5, Month.January, 2009); forwards[1] = 0.0025999342800; dates[2] = new Date(29, Month.January, 2009); forwards[2] = 0.0053123275500; dates[3] = new Date(27, Month.February, 2009); forwards[3] = 0.0197049598721; dates[4] = new Date(30, Month.March, 2009); forwards[4] = 0.0220524845296; dates[5] = new Date(29, Month.June, 2009); forwards[5] = 0.0217076395643; dates[6] = new Date(29, Month.December, 2009); forwards[6] = 0.0230349627478; dates[7] = new Date(29, Month.December, 2010); forwards[7] = 0.0087631647476; dates[8] = new Date(29, Month.December, 2011); forwards[8] = 0.0219084299499; dates[9] = new Date(31, Month.December, 2012); forwards[9] = 0.0244798766219; dates[10] = new Date(30, Month.December, 2013); forwards[10] = 0.0267885498456; dates[11] = new Date(29, Month.December, 2014); forwards[11] = 0.0266922867562; dates[12] = new Date(29, Month.December, 2015); forwards[12] = 0.0271052126386; dates[13] = new Date(29, Month.December, 2016); forwards[13] = 0.0268829891648; dates[14] = new Date(29, Month.December, 2017); forwards[14] = 0.0264594744498; dates[15] = new Date(31, Month.December, 2018); forwards[15] = 0.0273450367424; dates[16] = new Date(30, Month.December, 2019); forwards[16] = 0.0294852614749; dates[17] = new Date(29, Month.December, 2020); forwards[17] = 0.0285556119719; dates[18] = new Date(29, Month.December, 2021); forwards[18] = 0.0305557764659; dates[19] = new Date(29, Month.December, 2022); forwards[19] = 0.0292244738422; dates[20] = new Date(29, Month.December, 2023); forwards[20] = 0.0263917004194; dates[21] = new Date(29, Month.December, 2028); forwards[21] = 0.0239626970243; dates[22] = new Date(29, Month.December, 2033); forwards[22] = 0.0216417108090; dates[23] = new Date(29, Month.December, 2038); forwards[23] = 0.0228343838422; dates[24] = new Date(31, Month.December, 2199); forwards[24] = 0.0228343838422; Handle <YieldTermStructure> r = new Handle <YieldTermStructure>(new InterpolatedForwardCurve <BackwardFlat>(dates, forwards, new Actual360())); Handle <BlackVolTermStructure> sigma = new Handle <BlackVolTermStructure>(new BlackConstantVol(tomorrow, new NullCalendar(), 21.685235548092248, new Thirty360(Thirty360.Thirty360Convention.BondBasis))); BlackProcess process = new BlackProcess(u, r, sigma); Handle <Quote> spread = new Handle <Quote>(new SimpleQuote(0.11498700678012874)); Date issueDate = new Date(23, Month.July, 2008); Date maturityDate = new Date(1, Month.August, 2013); Calendar calendar = new UnitedStates(); Schedule schedule = new MakeSchedule().from(issueDate) .to(maturityDate) .withTenor(new Period(6, TimeUnit.Months)) .withCalendar(calendar) .withConvention(BusinessDayConvention.Unadjusted).value(); int settlementDays = 3; Exercise exercise = new EuropeanExercise(maturityDate); double conversionRatio = 100.0 / 20.3175; List <double> coupons = new InitializedList <double>(schedule.size() - 1, 0.05); DayCounter dayCounter = new Thirty360(Thirty360.Thirty360Convention.BondBasis); CallabilitySchedule no_callability = new CallabilitySchedule(); DividendSchedule no_dividends = new DividendSchedule(); double redemption = 100.0; ConvertibleFixedCouponBond bond = new ConvertibleFixedCouponBond(exercise, conversionRatio, no_dividends, no_callability, spread, issueDate, settlementDays, coupons, dayCounter, schedule, redemption); bond.setPricingEngine(new BinomialConvertibleEngine <CoxRossRubinstein> (process, 600)); try { double x = bond.NPV(); // should throw; if not, an INF was not detected. QAssert.Fail("INF result was not detected: " + x + " returned"); } catch (Exception) { // as expected. Do nothing. // Note: we're expecting an Error we threw, not just any // exception. If something else is thrown, then there's // another problem and the test must fail. } }
public List <CashFlow> Calculation(double fixrate, double initialNotional, List <DateTime> fixedSchedule, DateTime tradedate, YieldTermStructure yt, PiecewiseconstantHazardRate piecewiseFlatHazardRate, int settlement, DateTime lastpaymentdate) { List <CashFlow> result = new List <CashFlow>(); OMLib.Conventions.DayCount.Actual360 daycountconvention = new OMLib.Conventions.DayCount.Actual360(); for (int i = 0; i < fixedSchedule.Count; i++) { CashFlow cf = new CashFlow(); Calendar calendar = new UnitedStates(); //fixedSchedule[i] = calendar.adjust(fixedSchedule[i], BusinessDayConvention.Following); cf.CashFlowDate = fixedSchedule[i]; if (i == 0) { //The protection buyer pays the next coupon in full on the coupon date, //(even if this is the next day); in return the buyer receives (from the protection seller) the accrued //interest[Geoff Chaplin. Credit Derivatives.], which is paid on the cash settlement date. if (lastpaymentdate != null) { lastpaymentdate = calendar.adjust(lastpaymentdate, BusinessDayConvention.Following); cf.Amount = (double)initialNotional * fixrate * (daycountconvention.DayCount(lastpaymentdate, fixedSchedule[i])) / 360; } else { cf.Amount = (double)initialNotional * fixrate * (daycountconvention.DayCount(tradedate, fixedSchedule[i])) / 360; } } else { //Each coupon is equal to (annual coupon/360) (# of days in accrual period). //The accrual period always stretches from (previous coupon payment date) through (this coupon //payment date–1), inclusive; except a contract's last accrual period, which ends with (and //includes) the unadjusted maturity date. if (i == fixedSchedule.Count - 1) { cf.Amount = (double)initialNotional * fixrate * (daycountconvention.DayCount(fixedSchedule[i - 1], fixedSchedule[i].AddDays(1))) / 360; } else { cf.Amount = (double)initialNotional * fixrate * (daycountconvention.DayCount(fixedSchedule[i - 1], fixedSchedule[i])) / 360; } } //Assume all cash flows are discounted to the cash settlement date cf.DiscountFactor = yt.discount(fixedSchedule[i]); if (i == fixedSchedule.Count - 1) { cf.Survivalprobability = piecewiseFlatHazardRate.SurvivalProb(fixedSchedule[i].AddDays(1)); } else { cf.Survivalprobability = piecewiseFlatHazardRate.SurvivalProb(fixedSchedule[i]); } result.Add(cf); } return(result); }
/// <summary> /// Evaluates the specified option contract to compute a theoretical price, IV and greeks /// </summary> /// <param name="security">The option security object</param> /// <param name="slice">The current data slice. This can be used to access other information /// available to the algorithm</param> /// <param name="contract">The option contract to evaluate</param> /// <returns>An instance of <see cref="OptionPriceModelResult"/> containing the theoretical /// price of the specified option contract</returns> public OptionPriceModelResult Evaluate(Security security, Slice slice, OptionContract contract) { try { // setting up option pricing parameters var calendar = new UnitedStates(); var dayCounter = new Actual365Fixed(); var optionSecurity = (Option)security; var settlementDate = contract.Time.Date.AddDays(Option.DefaultSettlementDays); var maturityDate = contract.Expiry.Date.AddDays(Option.DefaultSettlementDays); var underlyingQuoteValue = new SimpleQuote((double)optionSecurity.Underlying.Price); var dividendYieldValue = new SimpleQuote(_dividendYieldEstimator.Estimate(security, slice, contract)); var dividendYield = new Handle <YieldTermStructure>(new FlatForward(0, calendar, dividendYieldValue, dayCounter)); var riskFreeRateValue = new SimpleQuote(_riskFreeRateEstimator.Estimate(security, slice, contract)); var riskFreeRate = new Handle <YieldTermStructure>(new FlatForward(0, calendar, riskFreeRateValue, dayCounter)); var underlyingVolValue = new SimpleQuote(_underlyingVolEstimator.Estimate(security, slice, contract)); var underlyingVol = new Handle <BlackVolTermStructure>(new BlackConstantVol(0, calendar, new Handle <Quote>(underlyingVolValue), dayCounter)); // preparing stochastic process and payoff functions var stochasticProcess = new BlackScholesMertonProcess(new Handle <Quote>(underlyingQuoteValue), dividendYield, riskFreeRate, underlyingVol); var payoff = new PlainVanillaPayoff(contract.Right == OptionRight.Call ? QLNet.Option.Type.Call : QLNet.Option.Type.Put, (double)contract.Strike); // creating option QL object var option = contract.Symbol.ID.OptionStyle == OptionStyle.American ? new VanillaOption(payoff, new AmericanExercise(settlementDate, maturityDate)) : new VanillaOption(payoff, new EuropeanExercise(maturityDate)); Settings.setEvaluationDate(settlementDate); // preparing pricing engine QL object option.setPricingEngine(_pricingEngineFunc(contract.Symbol, stochasticProcess)); // running calculations var npv = EvaluateOption(option); // function extracts QL greeks catching exception if greek is not generated by the pricing engine and reevaluates option to get numerical estimate of the seisitivity Func <Func <double>, Func <double>, decimal> tryGetGreekOrReevaluate = (greek, reevalFunc) => { try { return((decimal)greek()); } catch (Exception) { return(EnableGreekApproximation ? (decimal)reevalFunc() : 0.0m); } }; // function extracts QL greeks catching exception if greek is not generated by the pricing engine Func <Func <double>, decimal> tryGetGreek = greek => tryGetGreekOrReevaluate(greek, () => 0.0); // function extracts QL IV catching exception if IV is not generated by the pricing engine Func <decimal> tryGetImpliedVol = () => { try { return((decimal)option.impliedVolatility((double)optionSecurity.Price, stochasticProcess)); } catch (Exception err) { Log.Debug("tryGetImpliedVol() error: " + err.Message); return(0m); } }; Func <Tuple <decimal, decimal> > evalDeltaGamma = () => { try { return(Tuple.Create((decimal)option.delta(), (decimal)option.gamma())); } catch (Exception) { if (EnableGreekApproximation) { var step = 0.01; var initial = underlyingQuoteValue.value(); underlyingQuoteValue.setValue(initial - step); var npvMinus = EvaluateOption(option); underlyingQuoteValue.setValue(initial + step); var npvPlus = EvaluateOption(option); underlyingQuoteValue.setValue(initial); return(Tuple.Create((decimal)((npvPlus - npvMinus) / (2 * step)), (decimal)((npvPlus - 2 * npv + npvMinus) / (step * step)))); } else { return(Tuple.Create(0.0m, 0.0m)); } } }; Func <double> reevalVega = () => { var step = 0.001; var initial = underlyingVolValue.value(); underlyingVolValue.setValue(initial + step); var npvPlus = EvaluateOption(option); underlyingVolValue.setValue(initial); return((npvPlus - npv) / step); }; Func <double> reevalTheta = () => { var step = 1.0 / 365.0; Settings.setEvaluationDate(settlementDate.AddDays(-1)); var npvMinus = EvaluateOption(option); Settings.setEvaluationDate(settlementDate); return((npv - npvMinus) / step); }; Func <double> reevalRho = () => { var step = 0.001; var initial = riskFreeRateValue.value(); riskFreeRateValue.setValue(initial + step); var npvPlus = EvaluateOption(option); riskFreeRateValue.setValue(initial); return((npvPlus - npv) / step); }; // producing output with lazy calculations of IV and greeks return(new OptionPriceModelResult((decimal)npv, tryGetImpliedVol, () => new Greeks(evalDeltaGamma, () => tryGetGreekOrReevaluate(() => option.vega(), reevalVega), () => tryGetGreekOrReevaluate(() => option.theta(), reevalTheta), () => tryGetGreekOrReevaluate(() => option.rho(), reevalRho), () => tryGetGreek(() => option.elasticity())))); } catch (Exception err) { Log.Debug("QLOptionPriceModel.Evaluate() error: " + err.Message); return(new OptionPriceModelResult(0m, new Greeks())); } }
public void testActualActualWithSemiannualSchedule() { // Testing actual/actual with schedule for undefined semiannual reference periods Calendar calendar = new UnitedStates(); Date fromDate = new Date(10, Month.January, 2017); Date firstCoupon = new Date(31, Month.August, 2017); Date quasiCoupon = new Date(28, Month.February, 2017); Date quasiCoupon2 = new Date(31, Month.August, 2016); Schedule schedule = new MakeSchedule() .from(fromDate) .withFirstDate(firstCoupon) .to(new Date(28, Month.February, 2026)) .withFrequency(Frequency.Semiannual) .withCalendar(calendar) .withConvention(BusinessDayConvention.Unadjusted) .backwards().endOfMonth(true).value(); Date testDate = schedule.date(1); DayCounter dayCounter = new ActualActual(ActualActual.Convention.ISMA, schedule); DayCounter dayCounterNoSchedule = new ActualActual(ActualActual.Convention.ISMA); Date referencePeriodStart = schedule.date(1); Date referencePeriodEnd = schedule.date(2); // Test QAssert.IsTrue(dayCounter.yearFraction(referencePeriodStart, referencePeriodStart).IsEqual(0.0), "This should be zero."); QAssert.IsTrue(dayCounterNoSchedule.yearFraction(referencePeriodStart, referencePeriodStart).IsEqual(0.0), "This should be zero"); QAssert.IsTrue(dayCounterNoSchedule.yearFraction(referencePeriodStart, referencePeriodStart, referencePeriodStart, referencePeriodStart).IsEqual(0.0), "This should be zero"); QAssert.IsTrue(dayCounter.yearFraction(referencePeriodStart, referencePeriodEnd).IsEqual(0.5), "This should be exact using schedule; " + referencePeriodStart + " to " + referencePeriodEnd + "Should be 0.5"); QAssert.IsTrue(dayCounterNoSchedule.yearFraction(referencePeriodStart, referencePeriodEnd, referencePeriodStart, referencePeriodEnd).IsEqual(0.5), "This should be exact for explicit reference periods with no schedule"); while (testDate < referencePeriodEnd) { double difference = dayCounter.yearFraction(testDate, referencePeriodEnd, referencePeriodStart, referencePeriodEnd) - dayCounter.yearFraction(testDate, referencePeriodEnd); if (Math.Abs(difference) > 1.0e-10) { QAssert.Fail("Failed to correctly use the schedule to find the reference period for Act/Act"); } testDate = calendar.advance(testDate, 1, TimeUnit.Days); } //Test long first coupon double calculatedYearFraction = dayCounter.yearFraction(fromDate, firstCoupon); double expectedYearFraction = 0.5 + ((double)dayCounter.dayCount(fromDate, quasiCoupon)) / (2 * dayCounter.dayCount(quasiCoupon2, quasiCoupon)); QAssert.IsTrue(Math.Abs(calculatedYearFraction - expectedYearFraction) < 1.0e-10, "Failed to compute the expected year fraction " + "\n expected: " + expectedYearFraction + "\n calculated: " + calculatedYearFraction); // test multiple periods schedule = new MakeSchedule() .from(new Date(10, Month.January, 2017)) .withFirstDate(new Date(31, Month.August, 2017)) .to(new Date(28, Month.February, 2026)) .withFrequency(Frequency.Semiannual) .withCalendar(calendar) .withConvention(BusinessDayConvention.Unadjusted) .backwards().endOfMonth(false).value(); Date periodStartDate = schedule.date(1); Date periodEndDate = schedule.date(2); dayCounter = new ActualActual(ActualActual.Convention.ISMA, schedule); while (periodEndDate < schedule.date(schedule.size() - 2)) { double expected = actualActualDaycountComputation(schedule, periodStartDate, periodEndDate); double calculated = dayCounter.yearFraction(periodStartDate, periodEndDate); if (Math.Abs(expected - calculated) > 1e-8) { QAssert.Fail("Failed to compute the correct year fraction " + "given a schedule: " + periodStartDate + " to " + periodEndDate + "\n expected: " + expected + " calculated: " + calculated); } periodEndDate = calendar.advance(periodEndDate, 1, TimeUnit.Days); } }
static void Main(string[] args) { // boost::timer timer; Date today = new Date(16, Month.October, 2007); Settings.setEvaluationDate(today); Console.WriteLine(); Console.WriteLine("Pricing a callable fixed rate bond using"); Console.WriteLine("Hull White model w/ reversion parameter = 0.03"); Console.WriteLine("BAC4.65 09/15/12 ISIN: US06060WBJ36"); Console.WriteLine("roughly five year tenor, quarterly coupon and call dates"); Console.WriteLine("reference date is : " + today.ToLongDateString()); Console.WriteLine(""); /* Bloomberg OAS1: "N" model (Hull White) * varying volatility parameter * * The curve entered into Bloomberg OAS1 is a flat curve, * at constant yield = 5.5%, semiannual compounding. * Assume here OAS1 curve uses an ACT/ACT day counter, * as documented in PFC1 as a "default" in the latter case. */ // set up a flat curve corresponding to Bloomberg flat curve double bbCurveRate = 0.055; DayCounter bbDayCounter = new ActualActual(ActualActual.Convention.Bond); InterestRate bbIR = new InterestRate(bbCurveRate, bbDayCounter, Compounding.Compounded, Frequency.Semiannual); Handle <YieldTermStructure> termStructure = new Handle <YieldTermStructure>(flatRate(today, bbIR.rate(), bbIR.dayCounter(), bbIR.compounding(), bbIR.frequency())); // set up the call schedule CallabilitySchedule callSchedule = new CallabilitySchedule(); double callPrice = 100.0; int numberOfCallDates = 24; Date callDate = new Date(15, Month.September, 2006); for (int i = 0; i < numberOfCallDates; i++) { Calendar nullCalendar = new NullCalendar(); Callability.Price myPrice = new Callability.Price(callPrice, Callability.Price.Type.Clean); callSchedule.Add(new Callability(myPrice, Callability.Type.Call, callDate)); callDate = nullCalendar.advance(callDate, 3, TimeUnit.Months); } // set up the callable bond Date dated = new Date(16, Month.September, 2004); Date issue = dated; Date maturity = new Date(15, Month.September, 2012); int settlementDays = 3; // Bloomberg OAS1 settle is Oct 19, 2007 Calendar bondCalendar = new UnitedStates(UnitedStates.Market.GovernmentBond); double coupon = .0465; Frequency frequency = Frequency.Quarterly; double redemption = 100.0; double faceAmount = 100.0; /* The 30/360 day counter Bloomberg uses for this bond cannot * reproduce the US Bond/ISMA (constant) cashflows used in PFC1. * Therefore use ActAct(Bond) */ DayCounter bondDayCounter = new ActualActual(ActualActual.Convention.Bond); // PFC1 shows no indication dates are being adjusted // for weekends/holidays for vanilla bonds BusinessDayConvention accrualConvention = BusinessDayConvention.Unadjusted; BusinessDayConvention paymentConvention = BusinessDayConvention.Unadjusted; Schedule sch = new Schedule(dated, maturity, new Period(frequency), bondCalendar, accrualConvention, accrualConvention, DateGeneration.Rule.Backward, false); int maxIterations = 1000; double accuracy = 1e-8; int gridIntervals = 40; double reversionParameter = .03; // output price/yield results for varying volatility parameter double sigma = Const.QL_EPSILON; // core dumps if zero on Cygwin ShortRateModel hw0 = new HullWhite(termStructure, reversionParameter, sigma); IPricingEngine engine0 = new TreeCallableFixedRateBondEngine(hw0, gridIntervals, termStructure); CallableFixedRateBond callableBond = new CallableFixedRateBond(settlementDays, faceAmount, sch, new InitializedList <double>(1, coupon), bondDayCounter, paymentConvention, redemption, issue, callSchedule); callableBond.setPricingEngine(engine0); Console.WriteLine("sigma/vol (%) = {0:0.00}", (100.0 * sigma)); Console.WriteLine("QLNet price/yld (%) {0:0.00} / {1:0.00} ", callableBond.cleanPrice(), 100.0 * callableBond.yield(bondDayCounter, Compounding.Compounded, frequency, accuracy, maxIterations)); Console.WriteLine("Bloomberg price/yld (%) 96,50 / 5,47"); Console.WriteLine(""); // sigma = .01; Console.WriteLine("sigma/vol (%) = {0:0.00}", (100.0 * sigma)); ShortRateModel hw1 = new HullWhite(termStructure, reversionParameter, sigma); IPricingEngine engine1 = new TreeCallableFixedRateBondEngine(hw1, gridIntervals, termStructure); callableBond.setPricingEngine(engine1); Console.WriteLine("QLNet price/yld (%) {0:0.00} / {1:0.00} ", callableBond.cleanPrice(), 100.0 * callableBond.yield(bondDayCounter, Compounding.Compounded, frequency, accuracy, maxIterations)); Console.WriteLine("Bloomberg price/yld (%) 95,68 / 5,66"); Console.WriteLine(""); // sigma = .03; Console.WriteLine("sigma/vol (%) = {0:0.00}", (100.0 * sigma)); ShortRateModel hw2 = new HullWhite(termStructure, reversionParameter, sigma); IPricingEngine engine2 = new TreeCallableFixedRateBondEngine(hw2, gridIntervals, termStructure); callableBond.setPricingEngine(engine2); Console.WriteLine("QLNet price/yld (%) {0:0.00} / {1:0.00} ", callableBond.cleanPrice(), 100.0 * callableBond.yield(bondDayCounter, Compounding.Compounded, frequency, accuracy, maxIterations)); Console.WriteLine("Bloomberg price/yld (%) 92,34 / 6,49"); Console.WriteLine(""); // sigma = .06; Console.WriteLine("sigma/vol (%) = {0:0.00}", (100.0 * sigma)); ShortRateModel hw3 = new HullWhite(termStructure, reversionParameter, sigma); IPricingEngine engine3 = new TreeCallableFixedRateBondEngine(hw3, gridIntervals, termStructure); callableBond.setPricingEngine(engine3); Console.WriteLine("QLNet price/yld (%) {0:0.00} / {1:0.00} ", callableBond.cleanPrice(), 100.0 * callableBond.yield(bondDayCounter, Compounding.Compounded, frequency, accuracy, maxIterations)); Console.WriteLine("Bloomberg price/yld (%) 87,16 / 7,83"); Console.WriteLine(""); // sigma = .12; Console.WriteLine("sigma/vol (%) = {0:0.00}", (100.0 * sigma)); ShortRateModel hw4 = new HullWhite(termStructure, reversionParameter, sigma); IPricingEngine engine4 = new TreeCallableFixedRateBondEngine(hw4, gridIntervals, termStructure); callableBond.setPricingEngine(engine4); Console.WriteLine("QLNet price/yld (%) {0:0.00} / {1:0.00} ", callableBond.cleanPrice(), 100.0 * callableBond.yield(bondDayCounter, Compounding.Compounded, frequency, accuracy, maxIterations)); Console.WriteLine("Bloomberg price/yld (%) 77,31 / 10,65"); }