Beispiel #1
0
        public static object eqModelGetSVIInterpolatedValue(
            [ExcelArgument(Description = "id of SVI model ")] string ObjectId,
            [ExcelArgument(Description = "strik value ")] double x)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range         rng    = ExcelUtil.getActiveCellRange();
                SVIInterpolation option = OHRepository.Instance.getObject <SVIInterpolation>(ObjectId);
                return(option.call(x));
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return(e.Message);
            }
        }
Beispiel #2
0
        public static object eqModelGetSABRCalibratedParameters(
            [ExcelArgument(Description = "id of SABR model ")] string ObjectId,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range          rng    = ExcelUtil.getActiveCellRange();
                SABRInterpolation option = OHRepository.Instance.getObject <SABRInterpolation>(ObjectId);

                object[,] ret = new object[6, 2];
                ret[0, 0]     = "alpha:";  ret[0, 1] = option.alpha();
                ret[1, 0]     = "beta:"; ret[1, 1] = option.beta();
                ret[2, 0]     = "nu:"; ret[2, 1] = option.nu();
                ret[3, 0]     = "rho:"; ret[3, 1] = option.rho();
                ret[4, 0]     = "rmsError:"; ret[4, 1] = option.rmsError();
                ret[5, 0]     = "maxError:"; ret[5, 1] = option.maxError();

                return(ret);
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return(e.Message);
            }
        }
Beispiel #3
0
        public static object eqModelGetDoubleExponentialCalibratedParameters(
            [ExcelArgument(Description = "id of double exponential ATM model ")] string ObjectId,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range rng = ExcelUtil.getActiveCellRange();
                DoubleExponentialCalibration atmvol = OHRepository.Instance.getObject <DoubleExponentialCalibration>(ObjectId);

                object[,] ret = new object[6, 2];
                ret[0, 0]     = "sigma:"; ret[0, 1] = atmvol.sigma();
                ret[1, 0]     = "b1:"; ret[1, 1] = atmvol.b1();
                ret[2, 0]     = "b2:"; ret[2, 1] = atmvol.b2();
                ret[3, 0]     = "lambda:"; ret[3, 1] = atmvol.lambda();
                ret[4, 0]     = "error:"; ret[4, 1] = atmvol.error();
                ret[5, 0]     = "maxerror:"; ret[5, 1] = atmvol.maxError();

                return(ret);
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return(e.Message);
            }
        }
Beispiel #4
0
        public static object eqCurveIRGetDiscountFactor(
            [ExcelArgument(Description = "curve id ")] string ObjectId,
            [ExcelArgument(Description = "tenors ")] DateTime dt,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range rng = ExcelUtil.getActiveCellRange();

                if (!ObjectId.Contains('@'))
                {
                    ObjectId = "CRV@" + ObjectId;
                }

                YieldTermStructureHandle curve = OHRepository.Instance.getObject <YieldTermStructureHandle>(ObjectId);
                Date refDate = curve.referenceDate();

                Date   dt2      = EliteQuant.EQConverter.ConvertObject <Date>(dt);
                double discount = curve.discount(dt2);

                return(discount);
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return("#EQ_ERR!");
            }
        }
Beispiel #5
0
        public static object eqIRCurveDisplayDiscountCurve(
            [ExcelArgument(Description = "id of discount curve ")] string ObjectId,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range rng = ExcelUtil.getActiveCellRange();
                if (!ObjectId.Contains('@'))
                {
                    ObjectId = "CRV@" + ObjectId;
                }

                EliteQuant.YieldTermStructure curve = OHRepository.Instance.getObject <EliteQuant.YieldTermStructure>(ObjectId);
                if (curve.GetType() == typeof(EliteQuant.DiscountCurve))
                {
                    object[,] ret = new object[(curve as DiscountCurve).dates().Count, 2];

                    for (int i = 0; i < (curve as DiscountCurve).dates().Count; i++)
                    {
                        ret[i, 0] = (curve as DiscountCurve).dates()[i].serialNumber();
                        ret[i, 1] = (curve as DiscountCurve).discounts()[i].ToString();
                    }

                    return(ret);
                }
                else if (curve.GetType() == typeof(EliteQuant.LinearDiscountCurve))
                {
                    object[,] ret = new object[(curve as LinearDiscountCurve).dates().Count, 2];

                    for (int i = 0; i < (curve as LinearDiscountCurve).dates().Count; i++)
                    {
                        ret[i, 0] = (curve as LinearDiscountCurve).dates()[i].serialNumber();
                        ret[i, 1] = (curve as LinearDiscountCurve).discounts()[i].ToString();
                    }

                    return(ret);
                }
                else
                {
                    return(0);
                }
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return(e.Message);
            }
        }
Beispiel #6
0
        public static object eqIndexesLoadFixingFromFile(
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range rng      = ExcelUtil.getActiveCellRange();
                DateTime asofdate = DateTime.Today;

                string[] curves = new string[] { "USDOIS", "USDLIB3M", "USDLIB1M", "USDLIB6M", "USDLIB12M" };
                string   path   = EliteQuant.ConfigManager.Instance.IRRootDir + @"Curves\";
                string   name;
                foreach (var s in curves)
                {
                    string[]     data = System.IO.File.ReadAllLines(path + s + "_Fixing.csv");
                    DateVector   dt   = new DateVector(data.Length);
                    DoubleVector qs   = new DoubleVector(data.Length);

                    foreach (var fixing in data)
                    {
                        if (fixing == "")
                        {
                            continue;
                        }

                        string[] ff = fixing.Split(',');
                        dt.Add(EliteQuant.EQConverter.DateTimeToDate(EliteQuant.EQConverter.StringToDateTime(ff[0])));
                        qs.Add(Convert.ToDouble(ff[1]));
                    }

                    name = s;
                    if (!s.Contains("@"))
                    {
                        name = "IDX@" + s;
                    }

                    string         name2   = EliteQuant.Curves.IndexMapping.ExtIndexName2EQName(name);
                    RealTimeSeries fixings = new RealTimeSeries(dt, qs);
                    IndexManager.instance().setHistory(name2, fixings);
                }

                return(asofdate);
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return("#EQ_ERR!");
            }
        }
Beispiel #7
0
        public static object eqModelGetDoubleExponentialTermVol(
            [ExcelArgument(Description = "id of double exponential atm model ")] string ObjectId,
            [ExcelArgument(Description = "from t ")] double tfrom,
            [ExcelArgument(Description = "to t ")] double tto,        // 0 to t
            [ExcelArgument(Description = "T value ")] double maturityT,
            [ExcelArgument(Description = "T Vol ")] double TQuoteVol) // for T
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                if (ExcelUtil.isNull(tfrom))
                {
                    tfrom = 0.0;
                }

                bool bigT = true;
                if (ExcelUtil.isNull(TQuoteVol) || (TQuoteVol == 0.0))
                {
                    bigT = false;
                }

                Xl.Range rng = ExcelUtil.getActiveCellRange();
                DoubleExponentialCalibration atmvol = OHRepository.Instance.getObject <DoubleExponentialCalibration>(ObjectId);

                double scaler = 1.0;
                if (bigT)
                {
                    DoubleVector tv = new DoubleVector(); tv.Add(maturityT);
                    DoubleVector vv = new DoubleVector(); vv.Add(TQuoteVol);

                    scaler = atmvol.k(tv, vv)[0];
                }

                return(scaler * atmvol.value(tfrom, tto, maturityT));
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return(e.Message);
            }
        }
Beispiel #8
0
        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 object eqInstSaveIRSwapToDisk(
            [ExcelArgument(Description = "id of IR Swap ")] string tradeid,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range rng = ExcelUtil.getActiveCellRange();

                string genswaptplid_ = tradeid;
                if (!genswaptplid_.Contains('@'))
                {
                    genswaptplid_ = "SWP@" + genswaptplid_;
                }
                if (!genswaptplid_.Contains("_TPL"))
                {
                    genswaptplid_ = genswaptplid_ + "_TPL";
                }

                EliteQuant.Instruments.InterestRateGenericSwap genswaptpl = OHRepository.Instance.getObject <EliteQuant.Instruments.InterestRateGenericSwap>(genswaptplid_);

                string path = EliteQuant.ConfigManager.Instance.IRRootDir + @"Trades\";

                EliteQuant.Instruments.InterestRateGenericSwap.Serialize(genswaptpl,
                                                                         path + tradeid + ".xml");
                return(tradeid);
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return("#EQ_ERR!");
            }
        }
        public static object eqInstDisplayIRSwap(
            [ExcelArgument(Description = "id of IR Swap ")] string tradeid,
            [ExcelArgument(Description = "id of discount curve ")] string discountId,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            object[,] ret;
            try
            {
                Xl.Range rng = ExcelUtil.getActiveCellRange();


                if (!tradeid.Contains('@'))
                {
                    tradeid = "SWP@" + tradeid;
                }

                if (!discountId.Contains('@'))
                {
                    discountId = "CRV@" + discountId;
                }
                YieldTermStructure discountcurve = OHRepository.Instance.getObject <YieldTermStructure>(discountId);
                Date asofdate = Settings.instance().getEvaluationDate();

                GenericSwap inst = OHRepository.Instance.getObject <GenericSwap>(tradeid);

                int rows = Math.Max(inst.firstLegInfo().Count, inst.secondLegInfo().Count);
                ret = new object[rows, 20];     // 10 cols each leg
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < 20; j++)
                    {
                        ret[i, j] = "";         // initialization. null will be posted as 0; so explicitly set it to ""
                    }
                }

                // first leg
                string[] s;
                DateTime startdate, enddate, paymentdate, resetdate;
                double   balance = 0, rate = 0, spread = 0, payment = 0, discount = 0, pv = 0;
                for (int i = 0; i < inst.firstLegInfo().Count; i++)
                {
                    s           = inst.firstLegInfo()[i].Split(',');
                    startdate   = EliteQuant.EQConverter.DateToDateTime(new Date(Convert.ToInt32(s[0])));
                    enddate     = EliteQuant.EQConverter.DateToDateTime(new Date(Convert.ToInt32(s[1])));
                    paymentdate = EliteQuant.EQConverter.DateToDateTime(new Date(Convert.ToInt32(s[2])));
                    resetdate   = (s[3] == "") ? DateTime.MinValue : EliteQuant.EQConverter.DateToDateTime(new Date(Convert.ToInt32(s[3])));
                    balance     = Convert.ToDouble(s[4]);
                    rate        = Convert.ToDouble(s[5]);
                    spread      = Convert.ToDouble(s[6]);
                    payment     = Convert.ToDouble(s[7]);

                    // today's cashflow is not included
                    if (EliteQuant.EQConverter.DateTimeToDate(paymentdate).serialNumber() <= asofdate.serialNumber())
                    {
                        discount = 0.0;
                    }
                    else
                    {
                        discount = discountcurve.discount(EliteQuant.EQConverter.DateTimeToDate(paymentdate));
                    }

                    pv = payment * discount;

                    // and return the matrix to vba
                    ret[i, 0] = (object)startdate;
                    ret[i, 1] = (object)enddate;
                    ret[i, 2] = (object)paymentdate;
                    ret[i, 3] = (s[3] == "") ? "":(object)resetdate;
                    ret[i, 4] = (object)(balance == 0 ? "" : (object)balance);
                    ret[i, 5] = (object)(rate == 0 ? "" : (object)rate);
                    ret[i, 6] = (object)(spread == 0 ? "" : (object)spread);
                    ret[i, 7] = (object)(payment == 0 ? "" : (object)payment);
                    ret[i, 8] = (object)(discount == 0 ? "" : (object)discount);
                    ret[i, 9] = (object)(pv == 0 ? "" : (object)pv);
                }
                for (int i = 0; i < inst.secondLegInfo().Count; i++)
                {
                    s           = inst.secondLegInfo()[i].Split(',');
                    startdate   = EliteQuant.EQConverter.DateToDateTime(new Date(Convert.ToInt32(s[0])));
                    enddate     = EliteQuant.EQConverter.DateToDateTime(new Date(Convert.ToInt32(s[1])));
                    paymentdate = EliteQuant.EQConverter.DateToDateTime(new Date(Convert.ToInt32(s[2])));
                    resetdate   = (s[3] == "") ? DateTime.MinValue : EliteQuant.EQConverter.DateToDateTime(new Date(Convert.ToInt32(s[3])));
                    balance     = Convert.ToDouble(s[4]);
                    rate        = Convert.ToDouble(s[5]);
                    spread      = Convert.ToDouble(s[6]);
                    payment     = Convert.ToDouble(s[7]);

                    // today's cashflow is not included
                    if (EliteQuant.EQConverter.DateTimeToDate(paymentdate).serialNumber() <= asofdate.serialNumber())
                    {
                        discount = 0.0;
                    }
                    else
                    {
                        discount = discountcurve.discount(EliteQuant.EQConverter.DateTimeToDate(paymentdate));
                    }

                    pv = payment * discount;

                    // and return the matrix to vba
                    ret[i, 10] = (object)startdate;
                    ret[i, 11] = (object)enddate;
                    ret[i, 12] = (object)paymentdate;
                    ret[i, 13] = (s[3] == "") ? "" : (object)resetdate;
                    ret[i, 14] = (object)(balance == 0 ? "" : (object)balance);
                    ret[i, 15] = (object)(rate == 0 ? "" : (object)rate);
                    ret[i, 16] = (object)(spread == 0 ? "" : (object)spread);
                    ret[i, 17] = (object)(payment == 0 ? "" : (object)payment);
                    ret[i, 18] = (object)(discount == 0 ? "" : (object)discount);
                    ret[i, 19] = (object)(pv == 0 ? "" : (object)pv);
                }

                return(ret);
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return("#EQ_ERR!");
            }
        }
Beispiel #11
0
        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);
            }
        }
Beispiel #12
0
        public static object eqInstCommodityBasisSwap(
            [ExcelArgument(Description = "id of instrument ")] string ObjectId,
            [ExcelArgument(Description = "name of instrument ")] string name,
            [ExcelArgument(Description = "trade quantity ")] double[] quantities,
            [ExcelArgument(Description = "start date ")] object[] startdates,
            [ExcelArgument(Description = "end date ")] object[] enddates,
            [ExcelArgument(Description = "payCoeff ")] double[] payCoeff,
            [ExcelArgument(Description = "recCoeff ")] double[] recCoeff,
            [ExcelArgument(Description = "paySpread ")] double[] paySprd,
            [ExcelArgument(Description = "recSpread ")] double[] recSprd,
            [ExcelArgument(Description = "id of pay leg index ")] string payeridxid,
            [ExcelArgument(Description = "id of rec leg index ")] string recidxid,
            [ExcelArgument(Description = "id of pay leg discount curve ")] string paydiscountId,
            [ExcelArgument(Description = "id of rec leg discount curve ")] string recdiscountId,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                if (startdates.Length != enddates.Length)
                {
                    return("size mismatch");
                }

                Xl.Range                 rng              = ExcelUtil.getActiveCellRange();
                CommodityIndexExt        payeridx         = OHRepository.Instance.getObject <CommodityIndexExt>(payeridxid);
                CommodityIndexExt        recidx           = OHRepository.Instance.getObject <CommodityIndexExt>(recidxid);
                YieldTermStructureHandle paydiscountcurve = OHRepository.Instance.getObject <YieldTermStructureHandle>(paydiscountId);
                YieldTermStructureHandle recdiscountcurve = OHRepository.Instance.getObject <YieldTermStructureHandle>(recdiscountId);
                Date refDate = paydiscountcurve.referenceDate();

                PricingPeriodExts pps = new PricingPeriodExts(startdates.Length);

                for (int i = 0; i < startdates.Length; i++)
                {
                    //EliteQuant.Date sd = Conversion.ConvertObject<EliteQuant.Date>((DateTime)startdates[i], "date");
                    //EliteQuant.Date ed = Conversion.ConvertObject<EliteQuant.Date>((DateTime)enddates[i], "date");
                    Date sd = new Date(Convert.ToInt32(startdates[i]));
                    Date ed = new Date(Convert.ToInt32(enddates[i]));

                    PricingPeriodExt pp = new PricingPeriodExt(sd, ed, sd, quantities[i], payCoeff[i], recCoeff[i], paySprd[i], recSprd[i]);
                    pps.Add(pp);
                }

                EnergyBasisSwapExt ebs = new EnergyBasisSwapExt(payeridx, recidx, pps, name, paydiscountcurve, recdiscountcurve, Frequency.Monthly, new NullCalendar());

                // Store the futures and return its id
                string id = "Swp@" + ObjectId;
                OHRepository.Instance.storeObject(id, ebs, 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 eqInstGetOptionGreeks(
            [ExcelArgument(Description = "id of option ")] string ObjectId,
            [ExcelArgument(Description = "Greek type ")] string gtype,
            [ExcelArgument(Description = "Option type (VANILLA or MULTIASSET)")] string otype,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range rng = ExcelUtil.getActiveCellRange();

                if (ExcelUtil.isNull(gtype))
                {
                    gtype = "NPV";
                }
                if (ExcelUtil.isNull(otype))
                {
                    otype = "VANILLA";
                }

                if (otype == "VANILLA")
                {
                    VanillaOption option = OHRepository.Instance.getObject <VanillaOption>(ObjectId);
                    switch (gtype.ToUpper())
                    {
                    case "NPV":
                        return(option.NPV());

                    case "DELTA":
                        return(option.delta());

                    case "GAMMA":
                        return(option.gamma());

                    case "VEGA":
                        return(option.vega());

                    case "THETA":
                        return(option.theta());

                    case "RHO":
                        return(option.rho());

                    default:
                        return(0);
                    }
                }
                else if (otype == "MULTIASSET")
                {
                    BasketOption option = OHRepository.Instance.getObject <BasketOption>(ObjectId);
                    switch (gtype.ToUpper())
                    {
                    case "NPV":
                        return(option.NPV());

                    case "DELTA":
                        return(option.delta());

                    case "GAMMA":
                        return(option.gamma());

                    case "VEGA":
                        return(option.vega());

                    case "THETA":
                        return(option.theta());

                    case "RHO":
                        return(option.rho());

                    default:
                        return(0);
                    }
                }
                else
                {
                    return("Unknown option type");
                }
            }
            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!");
            }
        }
        public static object eqInstLoadIRSwapFromDisk(
            [ExcelArgument(Description = "id of IR Swap ")] string tradeid, // should not include @ and _TPL
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range rng           = ExcelUtil.getActiveCellRange();
                string   genswaptplid_ = tradeid;
                if (!genswaptplid_.Contains('@'))
                {
                    genswaptplid_ = "SWP@" + genswaptplid_;
                }
                if (!genswaptplid_.Contains("_TPL"))
                {
                    genswaptplid_ = genswaptplid_ + "_TPL";
                }

                string path = EliteQuant.ConfigManager.Instance.IRRootDir + @"Trades\";

                EliteQuant.Instruments.InterestRateGenericSwap genswaptpl =
                    EliteQuant.Instruments.InterestRateGenericSwap.Deserialize(path + tradeid + ".xml");

                // preserve same order as excel
                List <object> ret = new List <object>();
                ret.Add(genswaptpl.ContractId);
                ret.Add(genswaptpl.Entity);
                ret.Add(genswaptpl.EntityID);
                ret.Add(genswaptpl.Counterparty);
                ret.Add(genswaptpl.CounterpartyID);
                ret.Add(genswaptpl.SwapType);
                ret.Add(genswaptpl.FixingDays.ToString());
                ret.Add(string.IsNullOrEmpty(genswaptpl.TradeDate) ? "" : (object)EliteQuant.EQConverter.StringToDateTime(genswaptpl.TradeDate));
                ret.Add(string.IsNullOrEmpty(genswaptpl.SettlementDate) ? "" : (object)EliteQuant.EQConverter.StringToDateTime(genswaptpl.SettlementDate));
                ret.Add(string.IsNullOrEmpty(genswaptpl.MaturityDate) ? "" : (object)EliteQuant.EQConverter.StringToDateTime(genswaptpl.MaturityDate));
                ret.Add(genswaptpl.Tenor);
                if ((genswaptpl.FirstLegNotionals.Count > 1) || (genswaptpl.SecondLegNotionals.Count > 1))
                {
                    ret.Add("TRUE");
                }
                else
                {
                    ret.Add(genswaptpl.IsScheduleGiven.ToString());    // always false for now
                }
                ret.Add(genswaptpl.FirstLegIndex);
                ret.Add(genswaptpl.FirstLegFrequency);
                ret.Add(genswaptpl.FirstLegConvention);
                ret.Add(genswaptpl.FirstLegCalendar);
                ret.Add(genswaptpl.FirstLegDayCounter);
                ret.Add(genswaptpl.FirstLegDateGenerationRule);
                ret.Add(genswaptpl.FirstLegEOM.ToString());
                ret.Add(genswaptpl.FirstLegSpread.ToString());
                if (genswaptpl.FirstLegNotionals.Count > 1)
                {
                    ret.Add("Collection...");
                }
                else
                {
                    ret.Add(genswaptpl.FirstLegNotionals[0].ToString());
                }
                ret.Add("");
                ret.Add(genswaptpl.SecondLegIndex);
                ret.Add(genswaptpl.SecondLegFrequency);
                ret.Add(genswaptpl.SecondLegConvention);
                ret.Add(genswaptpl.SecondLegCalendar);
                ret.Add(genswaptpl.SecondLegDayCounter);
                ret.Add(genswaptpl.SecondLegDateGenerationRule);
                ret.Add(genswaptpl.SecondLegEOM.ToString());
                ret.Add(genswaptpl.SecondLegSpread.ToString());
                if (genswaptpl.SecondLegNotionals.Count > 1)
                {
                    ret.Add("Collection...");
                }
                else
                {
                    ret.Add(genswaptpl.SecondLegNotionals[0].ToString());
                }
                ret.Add("");

                return(ret.ToArray());
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return("#EQ_ERR!");
            }
        }
Beispiel #16
0
        public static object eqInstGetInstrumentNPV(
            [ExcelArgument(Description = "id of instrument ")] string ObjectId,
            [ExcelArgument(Description = "Greek type ")] string gtype,
            [ExcelArgument(Description = "is genericswap ")] bool isgenswap,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range rng = ExcelUtil.getActiveCellRange();

                bool isgenswap_ = false;
                if (ExcelUtil.isNull(isgenswap))
                {
                    isgenswap_ = false;
                }
                else
                {
                    isgenswap_ = (bool)isgenswap;
                }

                Instrument inst = null;
                if (isgenswap_)
                {
                    EliteQuant.Instruments.InterestRateGenericSwap genswap = OHRepository.Instance.getObject <EliteQuant.Instruments.InterestRateGenericSwap>(ObjectId);
                    inst = genswap.eqswap_;
                }
                else
                {
                    inst = OHRepository.Instance.getObject <Instrument>(ObjectId);
                }


                double ret = inst.NPV();

                Type type = inst.GetType();
                if (type == typeof(VanillaSwap))
                {
                    if (gtype.ToUpper() == "FAIRRATE")
                    {
                        ret = (inst as VanillaSwap).fairRate();
                    }
                }
                else if (type == typeof(OvernightIndexedSwap))
                {
                    if (gtype.ToUpper() == "FAIRRATE")
                    {
                        ret = (inst as OvernightIndexedSwap).fairRate();
                    }
                }
                else if (type == typeof(EliteQuant.Instruments.InterestRateGenericSwap))
                {
                    if (gtype.ToUpper() == "FAIRRATE")
                    {
                        ret = (inst as GenericSwap).fairRate();
                    }
                    else if (gtype.ToUpper() == "FAIRSPREAD")
                    {
                        ret = (inst as GenericSwap).fairSpread();
                    }
                    else if (gtype.ToUpper() == "FIRSTLEGBPS")
                    {
                        ret = (inst as GenericSwap).firstLegBPS();
                    }
                    else if (gtype.ToUpper() == "SECONDLEGBPS")
                    {
                        ret = (inst as GenericSwap).secondLegBPS();
                    }
                }

                return(ret);
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return("#EQ_ERR!");
            }
        }
Beispiel #17
0
        public static object eqInstDisplayEnergySwap(
            [ExcelArgument(Description = "id of energy ")] string ObjectId,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range rng = ExcelUtil.getActiveCellRange();

                EnergyCommodityExt inst = OHRepository.Instance.getObject <EnergyCommodityExt>(ObjectId);
                if (inst.GetType() == typeof(EnergyFutureExt))
                {
                    PricingPeriodExt pp = (inst as EnergyFutureExt).pricingPeriod();

                    object[,] ret = new object[2, 10];
                    ret[0, 0]     = "PayDate"; ret[0, 1] = "Quantity"; ret[0, 2] = "PayCoeff"; ret[0, 3] = "RecCoeff";
                    ret[0, 4]     = "PaySprd"; ret[0, 5] = "RecSprd";
                    ret[0, 6]     = "PayDelta"; ret[0, 7] = "DiscountedPayDelta";
                    ret[0, 8]     = "RecDelta"; ret[0, 9] = "DiscountedRecDelta";

                    ret[1, 0] = pp.paymentDate().serialNumber();
                    ret[1, 1] = pp.quantity();
                    ret[1, 2] = pp.payCoeff();
                    ret[1, 3] = pp.recCoeff();
                    ret[1, 4] = pp.paySpread();
                    ret[1, 5] = pp.recSpread();
                    ret[1, 6] = pp.getuPayDelta();
                    ret[1, 7] = pp.getdPayDelta();
                    ret[1, 8] = pp.getuRecDelta();
                    ret[1, 9] = pp.getdRecDelta();

                    return(ret);
                }
                else
                {
                    double            npv = inst.NPV();
                    PricingPeriodExts pps = (inst as EnergySwapExt).pricingPeriods();

                    object[,] ret = new object[pps.Count + 1, 10];

                    ret[0, 0] = "PayDate"; ret[0, 1] = "Quantity"; ret[0, 2] = "PayCoeff"; ret[0, 3] = "RecCoeff";
                    ret[0, 4] = "PaySprd"; ret[0, 5] = "RecSprd";
                    ret[0, 6] = "PayDelta"; ret[0, 7] = "DiscountedPayDelta";
                    ret[0, 8] = "RecDelta"; ret[0, 9] = "DiscountedRecDelta";
                    for (int i = 0; i < pps.Count; i++)
                    {
                        ret[i + 1, 0] = pps[i].paymentDate().serialNumber();
                        ret[i + 1, 1] = pps[i].quantity();
                        ret[i + 1, 2] = pps[i].payCoeff();
                        ret[i + 1, 3] = pps[i].recCoeff();
                        ret[i + 1, 4] = pps[i].paySpread();
                        ret[i + 1, 5] = pps[i].recSpread();
                        ret[i + 1, 6] = pps[i].getuPayDelta();
                        ret[i + 1, 7] = pps[i].getdPayDelta();
                        ret[i + 1, 8] = pps[i].getuRecDelta();
                        ret[i + 1, 9] = pps[i].getdRecDelta();
                    }

                    return(ret);
                }
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return("#EQ_ERR!");
            }
        }
Beispiel #18
0
        public static object eqIRCurveSaveCurveToDisk(
            [ExcelArgument(Description = "asofdate ")] DateTime asofdate,
            [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;
                foreach (var s in curves)
                {
                    string targetcurve;
                    if (s.Contains("@"))
                    {
                        targetcurve = s;
                    }
                    else
                    {
                        targetcurve = "CRV@" + s;
                    }

                    EliteQuant.YieldTermStructure curve = OHRepository.Instance.getObject <EliteQuant.YieldTermStructure>(targetcurve);
                    if (curve.GetType() == typeof(EliteQuant.DiscountCurve))
                    {
                        asofdate = EliteQuant.EQConverter.DateToDateTime((curve as DiscountCurve).dates()[0]);
                        string[] ret = new string[(curve as DiscountCurve).dates().Count];
                        for (int i = 0; i < (curve as DiscountCurve).dates().Count; i++)
                        {
                            ret[i] = (curve as DiscountCurve).dates()[i].serialNumber() + "," + (curve as DiscountCurve).discounts()[i].ToString();
                        }

                        path = EliteQuant.ConfigManager.Instance.IRRootDir + @"Curves\"
                               + EliteQuant.EQConverter.DateTimeToString(EliteQuant.EQConverter.DateToDateTime((curve as DiscountCurve).dates()[0])) + "\\";
                        if (!System.IO.Directory.Exists(path))
                        {
                            System.IO.Directory.CreateDirectory(path);
                        }

                        System.IO.File.WriteAllLines(path + s + ".csv", ret);
                    }
                    else if (curve.GetType() == typeof(EliteQuant.LinearDiscountCurve))
                    {
                        asofdate = EliteQuant.EQConverter.DateToDateTime((curve as LinearDiscountCurve).dates()[0]);
                        string[] ret = new string[(curve as LinearDiscountCurve).dates().Count];
                        for (int i = 0; i < (curve as LinearDiscountCurve).dates().Count; i++)
                        {
                            ret[i] = (curve as LinearDiscountCurve).dates()[i].serialNumber() + "," + (curve as LinearDiscountCurve).discounts()[i].ToString();
                        }

                        path = EliteQuant.ConfigManager.Instance.IRRootDir + @"Curves\"
                               + EliteQuant.EQConverter.DateTimeToString(EliteQuant.EQConverter.DateToDateTime((curve as LinearDiscountCurve).dates()[0])) + "\\";
                        if (!System.IO.Directory.Exists(path))
                        {
                            System.IO.Directory.CreateDirectory(path);
                        }

                        System.IO.File.WriteAllLines(path + s + ".csv", ret);
                    }
                }

                return(asofdate);
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return(e.Message);
            }
        }
Beispiel #19
0
        public static object eqInstCommodityVanillaSwap(
            [ExcelArgument(Description = "id of instrument ")] string ObjectId,
            [ExcelArgument(Description = "name of instrument ")] string name,
            [ExcelArgument(Description = "payer/receiver (1/0) ")] bool payer,
            [ExcelArgument(Description = "trade price ")] double fixedPrice,
            [ExcelArgument(Description = "trade quantity ")] double[] quantities,
            [ExcelArgument(Description = "start date ")] object[] startdates,
            [ExcelArgument(Description = "end date ")] object[] enddates,
            [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
            {
                //bool ispayer = string.Compare(payer.ToUpper(), "PAYER") == 0 ? true : false;
                bool ispayer = payer;
                if (startdates.Length != enddates.Length)
                {
                    return("size mismatch");
                }

                Xl.Range                 rng           = ExcelUtil.getActiveCellRange();
                CommodityIndexExt        idx           = OHRepository.Instance.getObject <CommodityIndexExt>(indexid);
                YieldTermStructureHandle discountcurve = OHRepository.Instance.getObject <YieldTermStructureHandle>(discountId);
                Date refDate = discountcurve.referenceDate();

                PricingPeriodExts pps = new PricingPeriodExts(startdates.Length);

                for (int i = 0; i < startdates.Length; i++)
                {
                    if (ExcelUtil.isNull(startdates[i]))
                    {
                        continue;
                    }

                    //EliteQuant.Date sd = Conversion.ConvertObject<EliteQuant.Date>((DateTime)startdates[i], "date");
                    //EliteQuant.Date ed = Conversion.ConvertObject<EliteQuant.Date>((DateTime)enddates[i], "date");
                    Date sd = new Date(Convert.ToInt32(startdates[i]));
                    Date ed = new Date(Convert.ToInt32(enddates[i]));

                    PricingPeriodExt pp = new PricingPeriodExt(sd, ed, sd, quantities[i]);
                    pps.Add(pp);
                }

                EnergyVanillaSwapExt evs = new EnergyVanillaSwapExt(ispayer, fixedPrice, idx, pps, name, discountcurve, discountcurve,
                                                                    Frequency.Monthly, new NullCalendar());

                // Store the futures and return its id
                string id = "Swp@" + ObjectId;
                OHRepository.Instance.storeObject(id, evs, 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!");
            }
        }
Beispiel #20
0
        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 object eqInstIRGenericSwap(
            // trade info

            /*[ExcelArgument(Description = "trade id ")] string tradeid,
             * [ExcelArgument(Description = "Entity ")] string entity,
             * [ExcelArgument(Description = "Entity ID ")] string entityid,
             * [ExcelArgument(Description = "Counterparty ")] string counterparty,
             * [ExcelArgument(Description = "Counterparty ID ")] string counterpartyid,
             * [ExcelArgument(Description = "swap type ")] string swaptype,
             * [ExcelArgument(Description = "Fixing Days ")] object fixingdays,        // use object to catch missing
             * [ExcelArgument(Description = "Trade Date ")] object tradedate,
             * [ExcelArgument(Description = "Start date ")] object startdate,
             * [ExcelArgument(Description = "Maturity date ")] object maturitydate,
             * [ExcelArgument(Description = "Tenor ")] string Tenor,
             * [ExcelArgument(Description = "is notional schedule given ")] bool isschedulegiven,*/
            [ExcelArgument(Description = "swap type ")] object[] tradeinfo,
            // first leg

            /*[ExcelArgument(Description = "id of first leg index ")] string firstlegindex,
             * [ExcelArgument(Description = "first leg frequency ")] string firstlegfreq,
             * [ExcelArgument(Description = "first leg convention ")] string firstlegconv,
             * [ExcelArgument(Description = "first leg calendar ")] string firstlegcalendar,
             * [ExcelArgument(Description = "first leg day counter ")] string firstlegdc,
             * [ExcelArgument(Description = "first leg date generation rule ")] string firstlegdgrule,
             * [ExcelArgument(Description = "first leg end of month ")] bool firstlegeom,
             * [ExcelArgument(Description = "first leg fixed rate ")] double firstlegrate,*/
            [ExcelArgument(Description = "first leg info ")] object[] firstleginfo,
            [ExcelArgument(Description = "first leg notional(s) ")] object[] firstlegnotionals,     // only object[] works
            [ExcelArgument(Description = "first leg schedule(s) ")] object[,] firstlegschedule,
            // second leg

            /*[ExcelArgument(Description = "id of second leg index ")] string secondlegindex,
             * [ExcelArgument(Description = "second leg frequency  ")] string secondlegfreq,
             * [ExcelArgument(Description = "second leg convention ")] string secondlegconv,
             * [ExcelArgument(Description = "second leg calendar ")] string secondlegcalendar,
             * [ExcelArgument(Description = "second leg day counter ")] string secondlegdc,
             * [ExcelArgument(Description = "second leg date generation rule ")] string secondlegdgrule,
             * [ExcelArgument(Description = "second leg end of month ")] bool secondlegeom,
             * [ExcelArgument(Description = "second leg spread ")] double secondlegspread,*/
            [ExcelArgument(Description = "second leg info ")] object[] secondleginfo,
            [ExcelArgument(Description = "second leg notional(s) ")] object[] secondlegnotionals,
            [ExcelArgument(Description = "second leg schedule(s) ")] object[,] secondlegschedule,
            [ExcelArgument(Description = "id of discount curve ")] string discountId
            )
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range rng = ExcelUtil.getActiveCellRange();

                EliteQuant.Instruments.InterestRateGenericSwap genswap = new EliteQuant.Instruments.InterestRateGenericSwap();

                #region parameters
                if (ExcelUtil.isNull(tradeinfo[0]))
                {
                    return("#EQ_ERR!");
                }
                else
                {
                    genswap.ContractId = (string)tradeinfo[0];
                }

                if (ExcelUtil.isNull(tradeinfo[1]))
                {
                    genswap.Entity = "NA";
                }
                else
                {
                    genswap.Entity = (string)tradeinfo[1];
                }

                if (ExcelUtil.isNull(tradeinfo[2]))
                {
                    genswap.EntityID = "NA";
                }
                else
                {
                    genswap.EntityID = (string)tradeinfo[2];
                }

                if (ExcelUtil.isNull(tradeinfo[3]))
                {
                    genswap.Counterparty = "NA";
                }
                else
                {
                    genswap.Counterparty = (string)tradeinfo[3];
                }

                if (ExcelUtil.isNull(tradeinfo[4]))
                {
                    genswap.CounterpartyID = "NA";
                }
                else
                {
                    genswap.CounterpartyID = (string)tradeinfo[4];
                }

                if (ExcelUtil.isNull(tradeinfo[5]))
                {
                    genswap.SwapType = "Payer";
                }
                else
                {
                    genswap.SwapType = (string)tradeinfo[5];
                }

                if (ExcelUtil.isNull(tradeinfo[6]))
                {
                    genswap.FixingDays = 2;
                }
                else
                {
                    genswap.FixingDays = (int)(double)tradeinfo[6];
                }

                if (ExcelUtil.isNull(tradeinfo[7]))
                {
                    genswap.TradeDate = EliteQuant.EQConverter.DateTimeToString(DateTime.Today);
                }
                else
                {
                    genswap.TradeDate = EliteQuant.EQConverter.DateTimeToString(DateTime.FromOADate((double)tradeinfo[7]));
                }

                // set it temporarily to ""
                if (ExcelUtil.isNull(tradeinfo[8]))
                {
                    genswap.SettlementDate = string.Empty;
                }
                else
                {
                    genswap.SettlementDate = EliteQuant.EQConverter.DateTimeToString(DateTime.FromOADate((double)tradeinfo[8]));
                }

                // set it temporarily to today
                if (ExcelUtil.isNull(tradeinfo[9]))
                {
                    genswap.MaturityDate = string.Empty;
                }
                else
                {
                    genswap.MaturityDate = EliteQuant.EQConverter.DateTimeToString(DateTime.FromOADate((double)tradeinfo[9]));
                }

                // set it temporarily to blank
                if (ExcelUtil.isNull(tradeinfo[10]))
                {
                    genswap.Tenor = string.Empty;
                }
                else
                {
                    genswap.Tenor = (string)tradeinfo[10];
                }

                if (ExcelUtil.isNull(tradeinfo[11]))
                {
                    genswap.IsScheduleGiven = false;
                }
                else
                {
                    //genswap.IsScheduleGiven = Convert.ToBoolean((string)tradeinfo[11]);
                    genswap.IsScheduleGiven = (bool)tradeinfo[11];
                }
                genswap.IsScheduleGiven = false;        // set to false always, amortization currently not supported

                //***************  First Leg *************************//
                if (ExcelUtil.isNull(firstleginfo[0]))
                {
                    genswap.FirstLegIndex = "FIXED";
                }
                else
                {
                    genswap.FirstLegIndex = (string)firstleginfo[0];
                }

                if (ExcelUtil.isNull(firstleginfo[1]))
                {
                    genswap.FirstLegFrequency = "SEMIANNUAL";
                }
                else
                {
                    genswap.FirstLegFrequency = (string)firstleginfo[1];
                }

                if (ExcelUtil.isNull(firstleginfo[2]))
                {
                    genswap.FirstLegConvention = "MODIFIEDFOLLOWING";
                }
                else
                {
                    genswap.FirstLegConvention = (string)firstleginfo[2];
                }

                if (ExcelUtil.isNull(firstleginfo[3]))
                {
                    genswap.FirstLegCalendar = "NYC|LON";
                }
                else
                {
                    genswap.FirstLegCalendar = (string)firstleginfo[3];
                }

                if (ExcelUtil.isNull(firstleginfo[4]))
                {
                    genswap.FirstLegDayCounter = "ACTUAL360";
                }
                else
                {
                    genswap.FirstLegDayCounter = (string)firstleginfo[4];
                }

                if (ExcelUtil.isNull(firstleginfo[5]))
                {
                    genswap.FirstLegDateGenerationRule = "BACKWARD";
                }
                else
                {
                    genswap.FirstLegDateGenerationRule = (string)firstleginfo[5];
                }

                if (ExcelUtil.isNull(firstleginfo[6]))
                {
                    genswap.FirstLegEOM = true;
                }
                else
                {
                    genswap.FirstLegEOM = (bool)firstleginfo[6];
                }

                if (ExcelUtil.isNull(firstleginfo[7]))
                {
                    genswap.FirstLegSpread = 0.0;
                }
                else
                {
                    genswap.FirstLegSpread = (double)firstleginfo[7];
                }

                if (ExcelUtil.isNull(firstlegnotionals))
                {
                    genswap.FirstLegNotionals.Clear();
                    genswap.FirstLegNotionals.Add(0);       // size = 1
                }
                else
                {
                    genswap.FirstLegNotionals.Clear();
                    foreach (var nl in firstlegnotionals)
                    {
                        if (ExcelUtil.isNull(nl))
                        {
                            continue;
                        }

                        genswap.FirstLegNotionals.Add((double)nl);
                    }
                }

                if (ExcelUtil.isNull(firstlegschedule) || (!genswap.IsScheduleGiven))
                {
                    genswap.FirstLegSchedules.Clear();
                    genswap.FirstLegSchedules.Add(genswap.SettlementDate);
                    genswap.FirstLegSchedules.Add(genswap.MaturityDate);
                }
                else
                {
                    genswap.FirstLegSchedules.Clear();
                    for (int a = 0; a < firstlegschedule.GetLength(0); a++)
                    {
                        DateTime d;
                        if (ExcelUtil.isNull(firstlegschedule[a, 0]))
                        {
                            // add one more date
                            d = DateTime.FromOADate((double)firstlegschedule[a - 1, 1]);
                            genswap.FirstLegSchedules.Add(EliteQuant.EQConverter.DateTimeToString(d));
                            break;
                        }


                        d = DateTime.FromOADate((double)firstlegschedule[a, 0]);
                        genswap.FirstLegSchedules.Add(EliteQuant.EQConverter.DateTimeToString(d));
                    }
                }

                //***************  Second Leg *************************//

                if (ExcelUtil.isNull(secondleginfo[0]))
                {
                    genswap.SecondLegIndex = "USDLIB3M";
                }
                else
                {
                    genswap.SecondLegIndex = (string)secondleginfo[0];
                }

                if (ExcelUtil.isNull(secondleginfo[1]))
                {
                    genswap.SecondLegFrequency = "QUARTERLY";
                }
                else
                {
                    genswap.SecondLegFrequency = (string)secondleginfo[1];
                }

                if (ExcelUtil.isNull(secondleginfo[2]))
                {
                    genswap.SecondLegConvention = "MODIFIEDFOLLOWING";
                }
                else
                {
                    genswap.SecondLegConvention = (string)secondleginfo[2];
                }

                if (ExcelUtil.isNull(secondleginfo[3]))
                {
                    genswap.SecondLegCalendar = "NYC|LON";           // nor NYC|LON
                }
                else
                {
                    genswap.SecondLegCalendar = (string)secondleginfo[3];
                }

                if (ExcelUtil.isNull(secondleginfo[4]))
                {
                    genswap.SecondLegDayCounter = "ACTUAL360";
                }
                else
                {
                    genswap.SecondLegDayCounter = (string)secondleginfo[4];
                }

                if (ExcelUtil.isNull(secondleginfo[5]))
                {
                    genswap.SecondLegDateGenerationRule = "BACKWARD";
                }
                else
                {
                    genswap.SecondLegDateGenerationRule = (string)secondleginfo[5];
                }

                if (ExcelUtil.isNull(secondleginfo[6]))
                {
                    genswap.SecondLegEOM = true;
                }
                else
                {
                    genswap.SecondLegEOM = (bool)secondleginfo[6];
                }

                if (ExcelUtil.isNull(secondleginfo[7]))
                {
                    genswap.SecondLegSpread = 0.0;
                }
                else
                {
                    genswap.SecondLegSpread = (double)secondleginfo[7];
                }

                if (ExcelUtil.isNull(secondlegnotionals))
                {
                    genswap.SecondLegNotionals.Clear();
                    genswap.SecondLegNotionals.Add(0);
                }
                else
                {
                    genswap.SecondLegNotionals.Clear();
                    foreach (var nl in secondlegnotionals)
                    {
                        if (ExcelUtil.isNull(nl))
                        {
                            continue;
                        }

                        genswap.SecondLegNotionals.Add((double)nl);
                    }
                }

                if (ExcelUtil.isNull(secondlegschedule) || (!genswap.IsScheduleGiven))
                {
                    genswap.SecondLegSchedules.Clear();
                    genswap.SecondLegSchedules.Add(genswap.SettlementDate);
                    genswap.SecondLegSchedules.Add(genswap.MaturityDate);
                }
                else
                {
                    genswap.SecondLegSchedules.Clear();
                    for (int a = 0; a < secondlegschedule.GetLength(0); a++)
                    {
                        DateTime d;
                        if (ExcelUtil.isNull(secondlegschedule[a, 0]))
                        {
                            // add one more date
                            d = DateTime.FromOADate((double)secondlegschedule[a - 1, 1]);
                            genswap.SecondLegSchedules.Add(EliteQuant.EQConverter.DateTimeToString(d));
                            break;
                        }


                        d = DateTime.FromOADate((double)secondlegschedule[a, 0]);
                        genswap.SecondLegSchedules.Add(EliteQuant.EQConverter.DateTimeToString(d));
                    }
                }

                #endregion

                #region convert Interest rate generic swap to swap obj
                string firstidx = genswap.FirstLegIndex, secondidx = genswap.SecondLegIndex;
                string firstidx_id  = "IDX@" + firstidx;
                string secondidx_id = "IDX@" + secondidx;
                EliteQuant.IborIndex firstidx_obj = null;
                if (!firstidx.Contains("FIXED"))
                {
                    firstidx_obj = OHRepository.Instance.getObject <EliteQuant.IborIndex>(firstidx_id);
                }

                EliteQuant.IborIndex secondidx_obj = OHRepository.Instance.getObject <EliteQuant.IborIndex>(secondidx_id);

                genswap.ConstructSwap(firstidx_obj, secondidx_obj);

                if (!discountId.Contains('@'))
                {
                    discountId = "CRV@" + discountId;
                }
                YieldTermStructure       discountcurve = OHRepository.Instance.getObject <YieldTermStructure>(discountId);
                YieldTermStructureHandle dch           = new YieldTermStructureHandle(discountcurve);

                DiscountingSwapEngine engine = new DiscountingSwapEngine(dch);
                genswap.eqswap_.setPricingEngine(engine);
                #endregion

                string id = "SWP@" + genswap.ContractId;
                OHRepository.Instance.storeObject(id, genswap, 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!");
            }
        }