public static string eqIRCurveFuturesRateHelper( [ExcelArgument(Description = "(String) id of rate helper object ")] String ObjectId, [ExcelArgument(Description = "(double) quote of ED futures e.g. 99.5 ")] double price, [ExcelArgument(Description = "(double) convexity adjustment default 0 ")] double convadj, [ExcelArgument(Description = "order of ED futures start from 1 ")] int order, [ExcelArgument(Description = "trigger ")] object trigger) { if (ExcelUtil.CallFromWizard()) { return(""); } string callerAddress = ExcelUtil.getActiveCellAddress(); try { // use default value EliteQuant.IborIndex idx = new EliteQuant.USDLibor(new EliteQuant.Period(3, EliteQuant.TimeUnit.Months)); EliteQuant.Date today = EliteQuant.Settings.instance().getEvaluationDate(); EliteQuant.Date settlementdate = idx.fixingCalendar().advance(today, (int)idx.fixingDays(), EliteQuant.TimeUnit.Days); EliteQuant.QuoteHandle quote_ = new EliteQuant.QuoteHandle(new EliteQuant.SimpleQuote(price)); EliteQuant.QuoteHandle conv_ = new EliteQuant.QuoteHandle(new EliteQuant.SimpleQuote(convadj)); EliteQuant.Date imm_startdate = EliteQuant.IMM.nextDate(settlementdate, false); for (int i = 0; i < order - 1; i++) { imm_startdate = EliteQuant.IMM.nextDate(cal_usd_gbp.advance(imm_startdate, 1, EliteQuant.TimeUnit.Days), false); } EliteQuant.Date enddate = imm_startdate + 90; EliteQuant.RateHelper rh = new EliteQuant.FuturesRateHelper(quote_, imm_startdate, enddate, dc_act_360, conv_); string id = "RHED@" + ObjectId; OHRepository.Instance.storeObject(id, rh, callerAddress); return(id + "#" + DateTime.Now.ToString("HH:mm:ss")); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return("#EQ_ERR!"); } }
static public DateTime DateToDateTime(EliteQuant.Date dt) { int serial = dt.serialNumber(); return(DateTime.FromOADate((double)serial)); }
public static string eqIRCurveLinearZero( [ExcelArgument(Description = "(String) id of curve (USDOIS, USDLIB3M) ")] string ObjectId, [ExcelArgument(Description = "array of rate helpers ")] object[] ratehelpers, [ExcelArgument(Description = "Interpolation Method (default LogLinear) ")] string interp, [ExcelArgument(Description = "trigger ")] object trigger) { if (ExcelUtil.CallFromWizard()) { return(""); } string callerAddress = ExcelUtil.getActiveCellAddress(); Xl.Range rng = ExcelUtil.getActiveCellRange(); try { string interpmethod; if (ExcelUtil.isNull(interp)) { interpmethod = "LOGLINEAR"; } else { interpmethod = interp.ToUpper(); } EliteQuant.RateHelperVector rhv = new EliteQuant.RateHelperVector(); EliteQuant.Date today = EliteQuant.Settings.instance().getEvaluationDate(); List <EliteQuant.Date> dates = new List <EliteQuant.Date>(); dates.Add(today); // today has discount 1 foreach (var rid in ratehelpers) { if (ExcelUtil.isNull(rid)) { continue; } try { EliteQuant.RateHelper rh = OHRepository.Instance.getObject <EliteQuant.RateHelper>((string)rid); rhv.Add(rh); dates.Add(rh.latestDate()); } catch (Exception) { // skip null instruments } } // set reference date to today. or discount to 1 EliteQuant.YieldTermStructure yth = new EliteQuant.PiecewiseLogLinearDiscount(today, rhv, dc_act_360); EliteQuant.DateVector dtv = new EliteQuant.DateVector(); EliteQuant.DoubleVector discv = new EliteQuant.DoubleVector(); foreach (var dt in dates) { double disc = yth.discount(dt); dtv.Add(dt); discv.Add(disc); } // reconstruct the discount curve // note that discount curve is LogLinear EliteQuant.YieldTermStructure yth2 = null; yth2 = new EliteQuant.DiscountCurve(dtv, discv, dc_act_360, ObjectId.Contains("OIS") ? cal_usd : cal_usd_gbp); if (!ObjectId.Contains('@')) { ObjectId = "CRV@" + ObjectId; } //string id = "IRCRV@" + ObjectId; string id = ObjectId; OHRepository.Instance.storeObject(id, yth2, callerAddress); return(id + "#" + DateTime.Now.ToString("HH:mm:ss")); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return("#EQ_ERR!"); } }
public static T ConvertObject <T>(Object obj, Object defaultValue) { try { // pass through if (obj.GetType() == typeof(T)) { return((T)(Object)obj); } if (typeof(T) == typeof(DateTime)) // To DateTime { if (obj is EliteQuant.Date) { return((T)(Object)DateTime.FromOADate(((EliteQuant.Date)obj).serialNumber())); } else if (obj is String) { return((T)(Object)DateTime.ParseExact((String)obj, "M/d/yyyy", null)); } else if (obj is double) { return((T)(Object)DateTime.FromOADate((double)obj)); } else if (obj is object[, ]) { return((T)(Object)EliteQuant.EQConverter.ConvertObject2Array(obj, DateTime.MinValue)[0]); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to System.DateTime"); } } else if (typeof(T) == typeof(EliteQuant.Date)) // to QuanLib.Date { if (obj is DateTime) { EliteQuant.Date d = new EliteQuant.Date(Convert.ToInt32(((DateTime)obj).ToOADate())); return((T)(Object)d); } else if (obj is double) { EliteQuant.Date d = new EliteQuant.Date(Convert.ToInt32((double)obj)); return((T)(Object)d); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to EliteQuant.Date"); } } else if (typeof(T) == typeof(EliteQuant.Calendar)) // to EliteQuant.Calendar { if (obj is String) { string c = (string)obj; return((T)(Object)(EliteQuant.EQConverter.ConvertStringToCalendar(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to Calendar"); } } else if (typeof(T) == typeof(EliteQuant.DayCounter)) // to DayCounter { if (obj is string) { string c = (string)obj; return((T)(object)(EliteQuant.EQConverter.ConvertStringToDayCounter(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to DayCounter"); } } else if (typeof(T) == typeof(EliteQuant.BusinessDayConvention)) // to bdc { if (obj is string) { string c = (string)obj; return((T)(object)(EliteQuant.EQConverter.ConvertStringToBDC(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to BusinessDayConvention"); } } else if (typeof(T) == typeof(EliteQuant.DateGeneration.Rule)) // to rule { if (obj is string) { string c = (string)obj; return((T)(object)(EliteQuant.EQConverter.ConvertStringToDGRule(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to DateGenerationRule"); } } else if (typeof(T) == typeof(EliteQuant.Frequency)) // to rule { if (obj is string) { string c = (string)obj; return((T)(object)(EliteQuant.EQConverter.ConvertStringToFrequency(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to Frequency"); } } else if (typeof(T) == typeof(EliteQuant.Compounding)) // to rule { if (obj is string) { string c = (string)obj; return((T)(object)(EliteQuant.EQConverter.ConvertStringToCompounding(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to Compounding"); } } else if (typeof(T) == typeof(EliteQuant.Period)) { if (obj is String) // String (2D, 3M, 10Y) { return((T)(Object)(EliteQuant.EQConverter.ConvertStringToPeriod((string)obj))); } else if (obj is EliteQuant.Frequency) // Monthly, Annual { string[] freqNames = Enum.GetNames(typeof(EliteQuant.Frequency)); StringCollection sc = new StringCollection(); sc.AddRange(freqNames); if (sc.Contains(obj as string)) { EliteQuant.Frequency frq = (EliteQuant.Frequency)Enum.Parse(typeof(EliteQuant.Frequency), obj as string); return((T)(Object) new EliteQuant.Period(frq)); } else { return((T)(Object) new EliteQuant.Period((EliteQuant.Frequency)obj)); } } else if (obj is int) { return((T)(Object) new EliteQuant.Period((int)obj, EliteQuant.TimeUnit.Years)); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to EliteQuant.Period"); } } else if (typeof(T) == typeof(DateTime[])) { if (obj is object[, ] || obj is DateTime || obj is double) { return((T)(Object)EQConverter.ConvertObject2Array(obj, DateTime.MinValue)); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to DateTime[]"); } } else if (typeof(T) == typeof(EliteQuant.Period[])) { if (obj is object[, ] || obj is String) { return((T)(Object)EQConverter.ConvertObject2Array(obj, new EliteQuant.Period())); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to EliteQuant.Period[]"); } } else if (typeof(T) == typeof(double)) { if (obj is String) { double amount = 0.0; double.TryParse((String)obj, out amount); return((T)(Object)amount); } } else if (typeof(T) == typeof(double[])) { if (obj is object[, ] || obj is double) { double def = (defaultValue is double) ? (double)defaultValue : 0.0; return((T)(Object)EQConverter.ConvertObject2Array <double>(obj, def)); } } else if (typeof(T).IsEnum) { if (obj is String) { if (EQEnum.EnumDictionary.ContainsKey(obj as string)) { obj = EQEnum.EnumDictionary[obj as string]; } return((T)Enum.Parse(typeof(T), (string)obj, true)); } } else if (typeof(T) == typeof(String[])) { if (obj is object[, ] || obj is string) { StringCollection tmp = EQConverter.ConvertObject2StringCollection(obj); String[] ret = new String[tmp.Count]; tmp.CopyTo(ret, 0); return((T)(Object)ret); } } else if (typeof(T) == typeof(StringCollection)) { if (obj is object[, ] || obj is string || obj is double) { return((T)(Object)EQConverter.ConvertObject2StringCollection(obj)); } } else if (typeof(T) == typeof(bool[])) { if (obj is object[, ] || obj is bool) { bool def = (defaultValue is bool) ? (bool)defaultValue : false; return((T)(Object)EQConverter.ConvertObject2Array <bool>(obj, def)); } } else if (typeof(T) == typeof(int[])) { if (obj is object[, ] || obj is double) { double def = (defaultValue is int) ? Convert.ToDouble(defaultValue) : 0.0; double[] tmp = EQConverter.ConvertObject2Array <double>(obj, def); int[] ret = new int[tmp.Length]; for (int i = 0; i < ret.Length; i++) { ret[i] = (int)tmp[i]; } return((T)(Object)ret); } } } catch (System.Exception e) { if (defaultValue != null) { return(DefaultValue <T>(defaultValue)); } else { throw new Exception(" EQConverter failed: " + e.Message); } } return(DefaultValue <T>(defaultValue)); }