public static object eqTimeAdvanceDate( [ExcelArgument(Description = "Date ")] DateTime date, [ExcelArgument(Description = "Calendar (default NYC) ")] string calendar, [ExcelArgument(Description = "Tenor (e.g. '3D' or '2Y') ")] string tenor, [ExcelArgument(Description = "BusinessDayConvention (default ModifiedFollowing) ")] string bdc, [ExcelArgument(Description = "is endofmonth ")] bool eom) { if (ExcelUtil.CallFromWizard()) { return(""); } string callerAddress = ""; callerAddress = ExcelUtil.getActiveCellAddress(); OHRepository.Instance.removeErrorMessage(callerAddress); try { if (date == DateTime.MinValue) { throw new Exception("Date must not be empty. "); } EliteQuant.Date d = EliteQuant.EQConverter.ConvertObject <EliteQuant.Date>(date); if (string.IsNullOrEmpty(calendar)) { calendar = "NYC"; } EliteQuant.Calendar can = EliteQuant.EQConverter.ConvertObject <EliteQuant.Calendar>(calendar); if (string.IsNullOrEmpty(tenor)) { tenor = "1D"; } EliteQuant.Period period = EliteQuant.EQConverter.ConvertObject <EliteQuant.Period>(tenor); if (string.IsNullOrEmpty(bdc)) { bdc = "MF"; } BusinessDayConvention bdc2 = EliteQuant.EQConverter.ConvertObject <BusinessDayConvention>(bdc); Date newday = can.advance(d, period, bdc2, eom); return(newday.serialNumber()); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return(e.Message); } }
public void calculate() { model_ = new HullWhite(discountCurve_, a_, sigma_); // initialize data structure // this includes time 0 pfeMatrix_ = new Dictionary <int, List <List <double> > >(portfolio_.Count + 1); // last one is portfolio int nSteps = (int)timeGrid_.size(); // with mandatory steps, this may not equal to simsteps+1 double npvPortfolio = 0; for (int v = 0; v < portfolio_.Count; v++) // for each asset { DiscountingSwapEngine pricingEngine_ = new DiscountingSwapEngine(discountCurve_); portfolio_[v].setPricingEngine(pricingEngine_); double npv = portfolio_[v].NPV(); npvPortfolio += npv; List <List <double> > oneDeal = new List <List <double> >(sampleSize_); for (int j = 0; j < sampleSize_; j++) { List <double> oneDealOnePath = new List <double>(nSteps); oneDealOnePath.Add(npv); oneDeal.Add(oneDealOnePath); } pfeMatrix_.Add(v, oneDeal); } // portfolio List <List <double> > onePort = new List <List <double> >(sampleSize_); for (int j = 0; j < sampleSize_; j++) { List <double> onePortOnePath = new List <double>(nSteps); onePortOnePath.Add(npvPortfolio); onePort.Add(onePortOnePath); } pfeMatrix_.Add(portfolio_.Count, onePort); // add fixing usdIndex_.addFixing(asOfDate_, discountCurve_.zeroRate(0.25, Compounding.Simple).rate()); // For each sample path for (int i = 0; i < sampleSize_; i++) { SamplePath path = pathGenerator_.next(); int steps = (int)path.value().timeGrid().size(); Debug.WriteLine("Path time grid size is: " + steps + " from time " + path.value().time(0) + " to time " + path.value().time((uint)steps - 1)); // for each simulation step for (uint j = 1; j < nSteps; j++) { Date d0 = schedule_.date(j); // new start date Date dT = calendar_.advance(d0, 30, TimeUnit.Years); double r0 = path.value().value(j); // new short rate Schedule sch = new Schedule(d0, dT, new Period(Frequency.Monthly), calendar_, BusinessDayConvention.ModifiedFollowing, BusinessDayConvention.ModifiedFollowing, DateGeneration.Rule.Forward, true); DateVector x = new DateVector(); // time DoubleVector y = new DoubleVector(); // discount for (uint ts = 0; ts < sch.size(); ts++) { x.Add(sch.date(ts)); y.Add(model_.discountBond(daycounter_.yearFraction(d0, d0), daycounter_.yearFraction(d0, sch.date(ts)), r0)); } // 1. construct discount yield curve YieldTermStructure newyieldcurve = new DiscountCurve(x, y, daycounter_); // LogLinear by default YieldTermStructureHandle newyieldcurvehandle = new YieldTermStructureHandle(newyieldcurve); //forwardingTS_.linkTo(newyieldcurve); DiscountingSwapEngine pricingEngine_ = new DiscountingSwapEngine(newyieldcurvehandle); // 2. reprice each deal Settings.instance().setEvaluationDate(d0); double discount = discountCurve_.discount(d0); // discount. Used for CVA/DVA double sum = 0; for (int v = 0; v < portfolio_.Count; v++) { portfolio_[v].setPricingEngine(pricingEngine_); // add to path matrix pfeMatrix_[v][i].Add(portfolio_[v].NPV()); sum += portfolio_[v].NPV(); } pfeMatrix_[portfolio_.Count][i].Add(sum); // clear fixings usdIndex_.addFixing(asOfDate_, newyieldcurve.zeroRate(0.25, Compounding.Simple).rate()); } // for each simulation step j } // for each sample path i // restore settings Settings.instance().setEvaluationDate(settingsToday_); } // function end