public static object eqTimeNextIMMDate( [ExcelArgument(Description = "Date ")] DateTime date, [ExcelArgument(Description = "is main cycle ? ")] bool maincycle) { 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); EliteQuant.Date immdate = EliteQuant.IMM.nextDate(d, maincycle); return(immdate.serialNumber()); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return(e.Message); } }
public static object eqTimeAdjustDate( [ExcelArgument(Description = "Date ")] DateTime date, [ExcelArgument(Description = "Calendar (default NYC) ")] string calendar, [ExcelArgument(Description = "BusinessDayConvention (default ModifiedFollowing) ")] string bdc) { 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(bdc)) { bdc = "MF"; } BusinessDayConvention bdc2 = EliteQuant.EQConverter.ConvertObject <BusinessDayConvention>(bdc); Date newday = can.adjust(d, bdc2); return(newday.serialNumber()); } catch (TypeInitializationException e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); object[,] ret = new object[5, 1]; ret[0, 1] = e.ToString(); ret[1, 1] = e.Message; ret[2, 1] = e.StackTrace; ret[3, 3] = e.Source; ret[4, 1] = e.InnerException.Message; return(ret); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return(e.Message); } }
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 static object eqInstCommodityFuture( [ExcelArgument(Description = "id of instrument ")] string ObjectId, [ExcelArgument(Description = "name of instrument ")] string name, // given by user, could be the same as objectid [ExcelArgument(Description = "buy/sell (1/-1) ")] int buysell, [ExcelArgument(Description = "trade price ")] double tradePrice, [ExcelArgument(Description = "trade quantity ")] double quantity, [ExcelArgument(Description = "start date ")] DateTime startdate, [ExcelArgument(Description = "end date ")] DateTime enddate, [ExcelArgument(Description = "id of commodity index ")] string indexid, [ExcelArgument(Description = "id of discount curve ")] string discountId, [ExcelArgument(Description = "trigger ")] object trigger) { if (ExcelUtil.CallFromWizard()) { return(""); } string callerAddress = ""; callerAddress = ExcelUtil.getActiveCellAddress(); try { Xl.Range rng = ExcelUtil.getActiveCellRange(); CommodityIndexExt idx = OHRepository.Instance.getObject <CommodityIndexExt>(indexid); YieldTermStructureHandle discountcurve = OHRepository.Instance.getObject <YieldTermStructureHandle>(discountId); Date refDate = discountcurve.referenceDate(); EliteQuant.Date sd = EliteQuant.EQConverter.ConvertObject <EliteQuant.Date>(startdate); EliteQuant.Date ed = EliteQuant.EQConverter.ConvertObject <EliteQuant.Date>(enddate); PricingPeriodExt pp = new PricingPeriodExt(sd, ed, sd, quantity); // pay at start date EnergyFutureExt ef = new EnergyFutureExt(buysell, pp, tradePrice, idx, name, discountcurve); // Store the futures and return its id string id = "Fut@" + ObjectId; OHRepository.Instance.storeObject(id, ef, callerAddress); id += "#" + (String)DateTime.Now.ToString(@"HH:mm:ss"); return(id); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return("#EQ_ERR!"); } }
public static bool eqTimeIsBusinessDay( [ExcelArgument(Description = "Date ")] DateTime date, [ExcelArgument(Description = "Calendar (default NYC) ")] string calendar) { if (ExcelUtil.CallFromWizard()) { return(false); } string callerAddress = ""; try { callerAddress = ExcelUtil.getActiveCellAddress(); OHRepository.Instance.removeErrorMessage(callerAddress); } catch (Exception) { } 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); return(can.isBusinessDay(d)); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return(false); } }
public static string eqInstSpreadOptionMonteCarlo( [ExcelArgument(Description = "id of option to be constructed ")] string ObjectId, [ExcelArgument(Description = "Option type (Call/Put) ")] string optype, [ExcelArgument(Description = "Spot price leg 1")] double spot1, [ExcelArgument(Description = "Spot price leg 2")] double spot2, [ExcelArgument(Description = "Strike price ")] double stirkeprice, [ExcelArgument(Description = "Expiry Date ")] DateTime exdate, [ExcelArgument(Description = "Risk free rate ")] double riskfreerate, [ExcelArgument(Description = "Black-Scholes Vol for leg 1 ")] double vol1, [ExcelArgument(Description = "Black-Scholes Vol for leg 2 ")] double vol2, [ExcelArgument(Description = "correlation between leg 1 and leg 2 ")] double corr, [ExcelArgument(Description = "DayCounter ")] string daycounter, [ExcelArgument(Description = "Calendar ")] string calendar, [ExcelArgument(Description = "Pseudorandom (pr) or lowdiscrepancy (ld) ")] string traits, [ExcelArgument(Description = "trigger ")] object trigger) { if (ExcelUtil.CallFromWizard()) { return(""); } string callerAddress = ""; callerAddress = ExcelUtil.getActiveCellAddress(); try { if (exdate == DateTime.MinValue) { throw new Exception("Date must not be empty. "); } if (ExcelUtil.isNull(daycounter)) { daycounter = "ACTUAL365"; } if (ExcelUtil.isNull(calendar)) { calendar = "NYC"; } if (ExcelUtil.isNull(traits)) { traits = "pr"; } Option.Type optiontype; if (optype.ToUpper() == "CALL") { optiontype = Option.Type.Call; } else if (optype.ToUpper() == "PUT") { optiontype = Option.Type.Put; } else { throw new Exception("Unknow option type"); } EliteQuant.Calendar cal = EliteQuant.EQConverter.ConvertObject <EliteQuant.Calendar>(calendar); EliteQuant.DayCounter dc = EliteQuant.EQConverter.ConvertObject <EliteQuant.DayCounter>(daycounter); EliteQuant.Date maturitydate = EliteQuant.EQConverter.ConvertObject <EliteQuant.Date>(exdate); EliteQuant.Date today = EliteQuant.Settings.instance().getEvaluationDate(); EliteQuant.Date settlementdate = today; // T+2 if (maturitydate.serialNumber() <= today.serialNumber()) { throw new Exception("Option already expired."); } YieldTermStructureHandle rTSH = new YieldTermStructureHandle( new FlatForward(settlementdate, riskfreerate, dc)); BlackVolTermStructureHandle flatVolTSH1 = new BlackVolTermStructureHandle( new BlackConstantVol(settlementdate, cal, vol1, dc)); BlackVolTermStructureHandle flatVolTSH2 = new BlackVolTermStructureHandle( new BlackConstantVol(settlementdate, cal, vol2, dc)); Quote qh1 = new SimpleQuote(spot1); Quote qh2 = new SimpleQuote(spot2); QuoteHandle s1 = new QuoteHandle(qh1); QuoteHandle s2 = new QuoteHandle(qh2); BlackProcess p1 = new BlackProcess(s1, rTSH, flatVolTSH1); BlackProcess p2 = new BlackProcess(s2, rTSH, flatVolTSH2); StochasticProcessVector spv = new StochasticProcessVector(2); spv.Add(p1); spv.Add(p2); Matrix corrmtrx = new Matrix(2, 2); corrmtrx.set(0, 0, 1.0); corrmtrx.set(1, 1, 1.0); corrmtrx.set(0, 1, corr); corrmtrx.set(1, 0, corr); StochasticProcessArray spa = new StochasticProcessArray(spv, corrmtrx); PricingEngine engine = new MCEuropeanBasketEngine(spa, traits, 100, 1, false, true, 5000, 1e-6); Payoff payoff1 = new PlainVanillaPayoff(optiontype, stirkeprice); Payoff payoff2 = new SpreadBasketPayoff(payoff1); Exercise exercise = new EuropeanExercise(maturitydate); BasketOption bo = new BasketOption(payoff2, exercise); bo.setPricingEngine(engine); // Store the option and return its id string id = "OPTION@" + ObjectId; OHRepository.Instance.storeObject(id, bo, callerAddress); id += "#" + (String)DateTime.Now.ToString(@"HH:mm:ss"); return(id); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return("#EQ_ERR!"); } }
public static string eqInstAmericanOptionBaroneAdesiWhaley( [ExcelArgument(Description = "id of option to be constructed ")] string ObjectId, [ExcelArgument(Description = "Option type ")] string optype, [ExcelArgument(Description = "Spot price ")] double underlyingprice, [ExcelArgument(Description = "Strike price ")] double stirkeprice, [ExcelArgument(Description = "Expiry Date ")] DateTime date, [ExcelArgument(Description = "Risk free rate ")] double riskfreerate, [ExcelArgument(Description = "dividend/convenience rate ")] double dividendrate, [ExcelArgument(Description = "Black-Scholes Vol ")] double volatility, [ExcelArgument(Description = "DayCounter ")] string daycounter, [ExcelArgument(Description = "Calendar ")] string calendar, [ExcelArgument(Description = "trigger ")] object trigger) { if (ExcelUtil.CallFromWizard()) { return(""); } string callerAddress = ""; callerAddress = ExcelUtil.getActiveCellAddress(); try { if (date == DateTime.MinValue) { throw new Exception("Date must not be empty. "); } if (ExcelUtil.isNull(daycounter)) { daycounter = "ACTUAL365"; } if (ExcelUtil.isNull(calendar)) { calendar = "NYC"; } Option.Type optiontype; if (optype.ToUpper() == "CALL") { optiontype = Option.Type.Call; } else if (optype.ToUpper() == "PUT") { optiontype = Option.Type.Put; } else { throw new Exception("Unknow option type"); } EliteQuant.Calendar cal = EliteQuant.EQConverter.ConvertObject <EliteQuant.Calendar>(calendar); EliteQuant.DayCounter dc = EliteQuant.EQConverter.ConvertObject <EliteQuant.DayCounter>(daycounter); EliteQuant.Date maturitydate = EliteQuant.EQConverter.ConvertObject <EliteQuant.Date>(date); EliteQuant.Date today = EliteQuant.Settings.instance().getEvaluationDate(); EliteQuant.Date settlementdate = today; // T+2 if (maturitydate.serialNumber() <= today.serialNumber()) { throw new Exception("Option already expired."); } AmericanExercise americanExercise = new AmericanExercise(today, maturitydate); QuoteHandle underlyingQuoteH = new QuoteHandle(new EliteQuant.SimpleQuote(underlyingprice)); YieldTermStructureHandle flatRateTSH = new YieldTermStructureHandle( new FlatForward(settlementdate, riskfreerate, dc)); YieldTermStructureHandle flatDividendTSH = new YieldTermStructureHandle( new FlatForward(settlementdate, dividendrate, dc)); BlackVolTermStructureHandle flatVolTSH = new BlackVolTermStructureHandle( new BlackConstantVol(settlementdate, cal, volatility, dc)); BlackScholesMertonProcess stochasticProcess = new BlackScholesMertonProcess(underlyingQuoteH, flatDividendTSH, flatRateTSH, flatVolTSH); PlainVanillaPayoff payoff = new PlainVanillaPayoff(optiontype, stirkeprice); VanillaOption europeanOption = new VanillaOption(payoff, americanExercise); europeanOption.setPricingEngine(new BaroneAdesiWhaleyEngine(stochasticProcess)); // Store the option and return its id string id = "OPTION@" + ObjectId; OHRepository.Instance.storeObject(id, europeanOption, callerAddress); id += "#" + (String)DateTime.Now.ToString(@"HH:mm:ss"); return(id); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return("#EQ_ERR!"); } }
public static object eqInstIROISSwap( [ExcelArgument(Description = "trade id ")] string tradeid, [ExcelArgument(Description = "payer/receiver (1/0) ")] bool ispayer, [ExcelArgument(Description = "notional ")] double notional, [ExcelArgument(Description = "fixed rate ")] double fixedRate, [ExcelArgument(Description = "start date ")] DateTime startdate, [ExcelArgument(Description = " (String) forward start month, e.g. 7D, 3M, 7Y ")] string Tenor, [ExcelArgument(Description = "id of overnight index ")] string indexid, [ExcelArgument(Description = "floating leg spread ")] double spread, [ExcelArgument(Description = "id of discount curve ")] string discountId, [ExcelArgument(Description = "trigger ")] object trigger) { if (ExcelUtil.CallFromWizard()) { return(""); } string callerAddress = ""; callerAddress = ExcelUtil.getActiveCellAddress(); try { Xl.Range rng = ExcelUtil.getActiveCellRange(); // by default // endOfMonth_(1*Months<=swapTenor && swapTenor<=2*Years ? true : false), bool end_of_month = true; EliteQuant.DayCounter fixeddc = new EliteQuant.Actual360(); if (!indexid.Contains('@')) { indexid = "IDX@" + indexid; } OvernightIndex idx = OHRepository.Instance.getObject <OvernightIndex>(indexid); if (!discountId.Contains('@')) { discountId = "CRV@" + discountId; } YieldTermStructure discountcurve = OHRepository.Instance.getObject <YieldTermStructure>(discountId); YieldTermStructureHandle dch = new YieldTermStructureHandle(discountcurve); EliteQuant.Period tenor_ = EliteQuant.EQConverter.ConvertObject <EliteQuant.Period>(Tenor); EliteQuant.Date sdate = EliteQuant.EQConverter.ConvertObject <EliteQuant.Date>(startdate); EliteQuant.Date fdate = idx.fixingDate(sdate); EliteQuant.Date tdate = idx.fixingCalendar().advance(sdate, tenor_); // fixed leg 1 yr. Forward? Schedule fixedsch = new Schedule(sdate, tdate, new Period(1, TimeUnit.Years), idx.fixingCalendar(), idx.businessDayConvention(), idx.businessDayConvention(), DateGeneration.Rule.Forward, end_of_month); OvernightIndexedSwap swap = new OvernightIndexedSwap(ispayer ? _OvernightIndexedSwap.Type.Payer : _OvernightIndexedSwap.Type.Receiver, notional, fixedsch, fixedRate, fixeddc, idx, spread); DiscountingSwapEngine engine = new DiscountingSwapEngine(dch); swap.setPricingEngine(engine); Date refDate = discountcurve.referenceDate(); // Store the futures and return its id string id = "SWP@" + tradeid; OHRepository.Instance.storeObject(id, swap, callerAddress); id += "#" + (String)DateTime.Now.ToString(@"HH:mm:ss"); return(id); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return("#EQ_ERR!"); } }