Exemplo n.º 1
0
        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!");
            }
        }
Exemplo n.º 2
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!");
            }
        }
Exemplo n.º 3
0
        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!");
            }
        }
Exemplo n.º 4
0
        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!");
            }
        }