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
public static object eqIRCurveLoadCurveFromDisk( [ExcelArgument(Description = "asofdate ")] DateTime asofdate, [ExcelArgument(Description = "Interpolation Method (Linear, LogLinear) ")] string interp, [ExcelArgument(Description = "trigger ")] object trigger) { if (ExcelUtil.CallFromWizard()) { return(""); } string callerAddress = ""; callerAddress = ExcelUtil.getActiveCellAddress(); try { Xl.Range rng = ExcelUtil.getActiveCellRange(); if (ExcelUtil.isNull(asofdate)) { asofdate = DateTime.Today; } string[] curves = new string[] { "USDOIS", "USDLIB3M", "USDLIB1M", "USDLIB6M", "USDLIB12M" }; string path; EliteQuant.IborIndex idx = new EliteQuant.USDLibor(new EliteQuant.Period(3, EliteQuant.TimeUnit.Months)); foreach (var s in curves) { string targetcurve; if (s.Contains("@")) { targetcurve = s; } else { targetcurve = "CRV@" + s; } string interpmethod; if (ExcelUtil.isNull(interp)) { interpmethod = "LOGLINEAR"; } else { interpmethod = interp.ToUpper(); } path = EliteQuant.ConfigManager.Instance.IRRootDir + @"Curves\" + EliteQuant.EQConverter.DateTimeToString(asofdate) + "\\"; string[] crv = System.IO.File.ReadAllLines(path + s + ".csv"); EliteQuant.DateVector dtv = new EliteQuant.DateVector(); EliteQuant.DoubleVector discv = new EliteQuant.DoubleVector(); foreach (var c in crv) { string[] cc = c.Split(','); Date dt = new Date(Convert.ToInt32(cc[0])); dtv.Add(dt); discv.Add(Convert.ToDouble(cc[1])); } YieldTermStructure yth = null; if (interpmethod == "LINEAR") { yth = new EliteQuant.LinearDiscountCurve(dtv, discv, idx.dayCounter(), idx.fixingCalendar()); } else { yth = new EliteQuant.DiscountCurve(dtv, discv, idx.dayCounter(), idx.fixingCalendar()); } string targetcrvid = "CRV@" + s; OHRepository.Instance.storeObject(targetcrvid, yth, callerAddress); // callerAddress or null ? } return(asofdate); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return(e.Message); } }