Exemplo n.º 1
0
        /// <summary>
        /// Add tf market data definition to an existing market.
        /// </summary>
        /// <param name="marketName">The Existing Market Name</param>
        /// <param name="tfId">The Bond futures Id</param>
        /// <param name="bondId">The Bond Id</param>
        /// <param name="quoteType">The Price quote type(Irr, Basis, NetBasis)</param>
        /// <param name="quote">The quote of bond futures</param>
        /// <param name="isOverride">if true, then add or override. if false, then ignore(default true)</param>
        /// <returns>if succeeded, return true. if failed, return error message : "Market {market object name} is not available!"</returns>
        public static object xl_MktAddTreasuryFutureData(string marketName, string tfId, string bondId, string quoteType, double quote, bool isOverride = true)
        {
            var mktObjName = tfId + "_" + bondId;
            var marketData = new TreasuryFutureMktData(mktObjName, quoteType, quote);

            return(XlManager.MergeMarketInfo(marketName, marketData, isOverride));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Return QDP bond futures qdp object.
        /// </summary>
        /// <param name="tradeId"></param>
        /// <param name="startDate"></param>
        /// <param name="settlementDate"></param>
        /// <param name="deliverableBondIds"></param>
        /// <param name="notional"></param>
        /// <param name="calendar"></param>
        /// <param name="dayCount"></param>
        /// <param name="nominalCoupon"></param>
        /// <param name="currency"></param>
        /// <returns></returns>
        public static object xl_CustomizedBondFutures(string tradeId, string startDate = null, string settlementDate = null, string[] deliverableBondIds = null, double notional = 1000000, string calendar = "chn", string dayCount = "Act365", double nominalCoupon = 0.03, string currency = "CNY")
        {
            var trade = XlManager.GetTrade(tradeId);

            if (!(trade is BondFuturesInfo))
            {
                startDate      = startDate ?? DateTime.Now.ToString("yyyy-MM-dd");
                settlementDate = settlementDate ?? new Term("3M").Next(startDate.ToDate()).ToString();
                trade          = new BondFuturesInfo(tradeId)
                {
                    StartDate            = startDate,
                    MaturityDate         = settlementDate,
                    Calendar             = calendar,
                    Currency             = currency,
                    DayCount             = dayCount,
                    DeliverableBondInfos =
                        deliverableBondIds == null
                                                        ? null
                                                        : deliverableBondIds.Select(x => (FixedRateBondInfo)XlManager.GetTrade(x)).ToArray(),
                    InstrumentType     = "BondFutures",
                    NominalCoupon      = nominalCoupon,
                    Notional           = notional,
                    ValuationParamters = new SimpleCfValuationParameters("FundingCurve", "", "FundingCurve")
                };

                XlManager.AddTrades(new[] { trade });
            }
            return(trade.ToTradeInfoInLabelData(null));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get the zero rate for a given period on a given curve.
        /// </summary>
        /// <param name="mktName">The name of the market.</param>
        /// <param name="curveName">The name of the yield curve.</param>
        /// <param name="startDate">The start date of the calculation period, i.e. 2017-01-03.</param>
        /// <param name="endDate">The end date of the calculation period, i.e. 2017-01-13.</param>
        /// <returns></returns>
        public static double GetZeroRate(string mktName, string curveName, string startDate, string endDate)
        {
            //var qdpMarket = XlManager.GetXlMarket(mktName).QdpMarket;
            //var yieldcurve = qdpMarket.GetData<CurveData>(curveName).YieldCurve;
            var yieldcurve = XlManager.GetPrebuildQdpMarket(mktName).YieldCurves[curveName];

            return(yieldcurve.ZeroRate(startDate.ToDate(), endDate.ToDate()));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Add HistoricalIndexRates market data definition to an existing market.
        /// </summary>
        /// <param name="marketName">The Existing Market Name</param>
        /// <param name="indexType">The index type(enum IndexType)</param>
        /// <param name="indexDate">The index date</param>
        /// <param name="indexRate">The rate</param>
        /// <param name="isOverride">if true, then add or override. if false, then ignore(default true)</param>
        /// <returns>if succeeded, return true. if failed, return error message : "Market {market object name} is not available!"</returns>
        public static object xl_MktAddHistoricalIndexRates(string marketName, IndexType indexType, Date indexDate, double indexRate, bool isOverride = true)
        {
            var hisDic = new Dictionary <IndexType, Dictionary <Date, double> > {
                { indexType, new Dictionary <Date, double> {
                      { indexDate, indexRate }
                  } }
            };

            return(XlManager.MergeMarketInfo(marketName, hisDic, isOverride));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Add instrument curve market data definition to an existing market.
        /// </summary>
        /// <param name="marketName">The Existing Market Name</param>
        /// <param name="curveName">The curve name</param>
        /// <param name="curveConvention">The curveConvention</param>
        /// <param name="rateDefinitions">The rateMktData array</param>
        /// <param name="trait">(enum YieldCurveTrait)</param>
        /// <param name="baseCurveDefinition">The base InstrumentCurveDefinition(default null)</param>
        /// <param name="regriddedTenors">The regriddedTenors array(default null)</param>
        /// <param name="isOverride">if true, then add or override. if false, then ignore(default true)</param>
        /// <returns>if succeeded, return true. if failed, return error message : "Market {market object name} is not available!"</returns>
        public static object xl_MktAddYieldCurve(string marketName, string curveName,
                                                 CurveConvention curveConvention,
                                                 RateMktData[] rateDefinitions,
                                                 string trait,
                                                 InstrumentCurveDefinition baseCurveDefinition = null,
                                                 string[] regriddedTenors = null,
                                                 bool isOverride          = true)
        {
            var marketData = new InstrumentCurveDefinition(curveName, curveConvention, rateDefinitions, trait, baseCurveDefinition, regriddedTenors);

            return(XlManager.MergeMarketInfo(marketName, marketData, isOverride));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Add curve convention market data definition to an existing market.
        /// </summary>
        /// <param name="marketName">The Existing Market Name</param>
        /// <param name="curveConventionName">The curve convention name</param>
        /// <param name="currency">The currency (enum CurrencyCode)</param>
        /// <param name="businessDayConvention">(enum BusinessDayConvention)</param>
        /// <param name="calendar">(enum Calendar)</param>
        /// <param name="dayCount">(enum DayCount)</param>
        /// <param name="compound">(enum Compound)</param>
        /// <param name="interpolation">(enum Interpolation)</param>
        /// <param name="isOverride">if true, then add or override. if false, then ignore(default true)</param>
        /// <returns>if succeeded, return true. if failed, return error message : "Market {market object name} is not available!"</returns>
        public static object xl_MktAddCurveConvention(string marketName, string curveConventionName,
                                                      string currency,
                                                      string businessDayConvention,
                                                      string calendar,
                                                      string dayCount,
                                                      string compound,
                                                      string interpolation,
                                                      bool isOverride = true)
        {
            var marketData = new CurveConvention(curveConventionName, currency, businessDayConvention, calendar, dayCount, compound, interpolation);

            return(XlManager.MergeMarketInfo(marketName, marketData, isOverride));
        }
Exemplo n.º 7
0
        public static Dictionary <string, PnLResultBase> DoPnLExplain(string[] tradeIds,
                                                                      PrebuiltQdpMarket t0Mkt,
                                                                      PrebuiltQdpMarket t1Mkt,
                                                                      PrebuiltQdpMarket t0MktDateRolledFwd,
                                                                      PrebuiltQdpMarket t0MktRolldownForBond,
                                                                      PrebuiltQdpMarket t0MktPriceNow    = null,
                                                                      PrebuiltQdpMarket t0MktPriceVolNow = null,
                                                                      PrebuiltQdpMarket t0MktVolNow      = null)
        {
            //TODO: add the following for more exotic product where rate/price interplay is more pronounced
            //resultRateNow,  resultPriceRateNow, resultVolRateNow, resultPriceVolRateNow

            var useRevalPnLFramework = (t0MktPriceNow != null && t0MktVolNow != null && t0MktPriceVolNow != null);
            //use reval for pnl attribution
            //PnL = PriceImpact + VolImpact + PriceVolCrossImpact + Unexplained

            var ret = new Dictionary <string, PnLResultBase>();

            var tMarketName  = t0Mkt.MarketName;
            var t1MarketName = t1Mkt.MarketName;

            var t0Vals = new Dictionary <string, IPricingResult>();
            var t0ValsRolledForward = new Dictionary <string, IPricingResult>();
            var t1Vals = new Dictionary <string, IPricingResult>();

            var t0ValsPriceNow         = new Dictionary <string, IPricingResult>();
            var t0RolldownPriceForBond = new Dictionary <string, IPricingResult>();
            var t0ValsVolNow           = new Dictionary <string, IPricingResult>();
            var t0ValsPriceVolNow      = new Dictionary <string, IPricingResult>();

            //use minimium price request to make it faster
            #region requests
            var pricingRequest =
                PricingRequest.Pv |
                PricingRequest.Cashflow |
                PricingRequest.DirtyPrice |
                PricingRequest.KeyRateDv01 |
                PricingRequest.DollarDuration |
                PricingRequest.DollarConvexity |
                PricingRequest.Delta |
                PricingRequest.Gamma |
                PricingRequest.Vega |
                PricingRequest.Theta |
                PricingRequest.Rho |
                PricingRequest.DVegaDvol | //one high order risk
                PricingRequest.ZeroSpread |
                PricingRequest.ZeroSpreadDelta |
                PricingRequest.AiEod |
                PricingRequest.Basis |
                PricingRequest.CheapestToDeliver |
                PricingRequest.Ytm |
                PricingRequest.UnderlyingPv |
                PricingRequest.Carry;
            #endregion requests

            foreach (var tradeId in tradeIds)
            {
                t0Vals[tradeId] = xl_ValueTrade(tradeId, t0Mkt, pricingRequest);
                var t1Request = PricingRequest.Pv | PricingRequest.DirtyPrice | PricingRequest.ZeroSpread | PricingRequest.AiEod
                                | PricingRequest.Ytm | PricingRequest.Basis | PricingRequest.Cashflow;
                t1Vals[tradeId] = xl_ValueTrade(tradeId, t1Mkt, t1Request);
                t0ValsRolledForward[tradeId] = xl_ValueTrade(tradeId, t0MktDateRolledFwd, PricingRequest.Pv | PricingRequest.DirtyPrice | PricingRequest.UnderlyingPv);

                //reval framework for better pnl attribution
                if (useRevalPnLFramework)
                {
                    t0ValsPriceNow[tradeId]         = xl_ValueTrade(tradeId, t0MktPriceNow, PricingRequest.Pv);
                    t0ValsVolNow[tradeId]           = xl_ValueTrade(tradeId, t0MktVolNow, PricingRequest.Pv);
                    t0ValsPriceVolNow[tradeId]      = xl_ValueTrade(tradeId, t0MktPriceVolNow, PricingRequest.Pv);
                    t0RolldownPriceForBond[tradeId] = xl_ValueTrade(tradeId, t0MktRolldownForBond, PricingRequest.Pv | PricingRequest.ZeroSpread);
                }
            }

            //For old interface:
            //var tCurves = GetXlMarket(tMarketName).MarketInfo.YieldCurveDefinitions.Select(x => x.Name)
            //            .Select(x => t0Mkt.GetData<CurveData>(x).YieldCurve).ToDictionary(x => x.Name, x => x);
            //var t1Curves = GetXlMarket(t1MarketName).MarketInfo.YieldCurveDefinitions.Select(x => x.Name)
            //    .Select(x => t1Mkt.GetData<CurveData>(x).YieldCurve).ToDictionary(x => x.Name, x => x);
            var tCurves  = t0Mkt.YieldCurves;
            var t1Curves = t1Mkt.YieldCurves;

            var curveMoveScaling = 1e4;

            foreach (var tradeId in tradeIds)
            {
                var t1cf = (t1Vals[tradeId].Cashflows != null) ?
                           t1Vals[tradeId].Cashflows.Where(x => x.PaymentDate <= t1Mkt.ReferenceDate && x.PaymentDate > t0Mkt.ReferenceDate).Sum(x => x.PaymentAmount) :
                           0.0;

                var tradeInfo = GetTrade(tradeId);
                if (tradeInfo is InterestRateSwapInfo || tradeInfo is BondInfoBase || tradeInfo is BondFuturesInfo)
                {
                    //PnL using bond discounted cash flows
                    //curve risk is between T0_{prime} and T1
                    var _tPv        = t0Vals[tradeId].Pv;
                    var _t1Pv       = t1Vals[tradeId].Pv;
                    var _tPvRecalib = t0ValsRolledForward[tradeId].Pv;
                    var curvePnL    = new Dictionary <string, CurveRisk[]>();

                    foreach (var curveRiskse in t0Vals[tradeId].KeyRateDv01)
                    {
                        var tCurve  = tCurves[curveRiskse.Key];
                        var t1Curve = t1Curves[curveRiskse.Key];

                        curvePnL[curveRiskse.Key] =
                            curveRiskse.Value.Select(x =>
                                                     new CurveRisk(
                                                         x.Tenor,
                                                         x.Risk * (t1Curve[x.Tenor] - tCurve[x.Tenor]) * curveMoveScaling
                                                         )).ToArray();
                    }

                    //include raw t1 curve risks in result
                    foreach (var curveRisks in t0Vals[tradeId].KeyRateDv01)
                    {
                        curvePnL[curveRisks.Key + "KeyRateDv01"] = curveRisks.Value;
                    }

                    var pnLCurve = new CommonPnLResult(_tPv, _t1Pv, _tPvRecalib - _tPv, t1cf, curvePnL);
                    ret[tradeId] = pnLCurve;
                }

                if (tradeInfo is InterestRateSwapInfo)
                {
                    var swap = tradeInfo as InterestRateSwapInfo;
                    //carry & roll down
                    var rollDown = t0ValsRolledForward[tradeId].Pv - t0Vals[tradeId].Pv;
                    var carry    = t0Vals[tradeId].Carry;
                    var pnlTime  = rollDown + carry;

                    var commonPnl = ret[tradeId];
                    var pnlPv01   =
                        ret.ContainsKey(tradeId) && ret[tradeId].YieldCurvePnL.Count > 0
                        ? ret[tradeId].YieldCurvePnL.First().Value.Sum(x => x.Risk)
                        : 0.0;
                    var pnl = new SwapPnLResult(commonPnl.TPv, commonPnl.T1Pv, pnlTime: pnlTime, t1Cf: t1cf, pnlPv01: pnlPv01,
                                                pnlCarry: carry, pnlRolldown: rollDown);

                    ret[tradeId + "durationConvexity"] = pnl;
                }

                if (tradeInfo is BondInfoBase)
                {
                    var bond       = tradeInfo as BondInfoBase;
                    var tPv        = t0Vals[tradeId].DirtyPrice;
                    var t1Pv       = t1Vals[tradeId].DirtyPrice;
                    var tPvRecalib = t0ValsRolledForward[tradeId].DirtyPrice;

                    //bond market PnL
                    var pnlPv01 =
                        ret.ContainsKey(tradeId) && ret[tradeId].YieldCurvePnL.Count > 0
                        ? ret[tradeId].YieldCurvePnL.First().Value.Sum(x => x.Risk)
                        : 0.0;

                    //bond specific pnl
                    var tZSpread      = t0Vals[tradeId].ZeroSpread;
                    var t1ZSpread     = t1Vals[tradeId].ZeroSpread;
                    var tZSpreadDelta = t0Vals[tradeId].ZeroSpreadDelta;
                    var zSpreadPnl    = tZSpreadDelta * (t1ZSpread - tZSpread) * curveMoveScaling;

                    var pnlCarry = t1Vals[tradeId].Ai - t0Vals[tradeId].Ai;
                    //bond roll down effect:  cashflow less, but benefit from still curve,  note that zspread also changes due to rolling down the curve
                    var pnlRolldown = t0RolldownPriceForBond[tradeId].Pv - t0Vals[tradeId].Pv +
                                      tZSpreadDelta * (t0RolldownPriceForBond[tradeId].ZeroSpread - tZSpread) * curveMoveScaling;

                    var pnlTime = pnlCarry + pnlRolldown;

                    //duration pnl is  not used in book level pnl explain
                    var pnlDuration  = t0Vals[tradeId].ModifiedDuration * (t1Vals[tradeId].Ytm - t0Vals[tradeId].Ytm) * bond.Notional;
                    var pnlConverixy = 0.5 * Math.Pow(t1Vals[tradeId].Ytm - t0Vals[tradeId].Ytm, 2.0) * t0Vals[tradeId].DollarConvexity / 100.0;

                    var explainedPriceImpact = pnlPv01 + zSpreadPnl + pnlConverixy + pnlTime;

                    var pnl = new BondPnLResult(tPv, t1Pv, pnlTime: pnlTime, t1Cf: t1cf, pnlPv01: pnlPv01, pnlZspread: zSpreadPnl, pnlCarry: pnlCarry, pnlRolldown: pnlRolldown,
                                                pnlDuration: pnlDuration, pnlConvexity: pnlConverixy);
                    ret[tradeId + "durationConvexity"] = pnl;
                }
                if (tradeInfo is BondFuturesInfo)
                {
                    var tPv  = t0Vals[tradeId].DirtyPrice;
                    var t1Pv = t1Vals[tradeId].DirtyPrice;

                    //curve pnl
                    var pnlPv01 =
                        ret.ContainsKey(tradeId) && ret[tradeId].YieldCurvePnL.Count > 0
                        ? ret[tradeId].YieldCurvePnL.First().Value.Sum(x => x.Risk)
                        : 0.0;

                    //zspread pnl from CTD, converted to future equivalen t
                    var zspreadT0  = t0Vals[tradeId].ZeroSpread;
                    var zspreadT1  = t1Vals[tradeId].ZeroSpread;
                    var zspreadPnl = (zspreadT1 - zspreadT0) * curveMoveScaling * t0Vals[tradeId].ZeroSpreadDelta;

                    //basis pnl from  CTD/cf - Future
                    var basisT0       = t0Vals[tradeId].Basis;
                    var basisT1       = t1Vals[tradeId].Basis;
                    var bondFut       = XlManager.GetTrade(tradeId) as BondFuturesInfo;
                    var futPosScaling = 1.0 / 100.0 * bondFut.Notional;
                    var basisPnL      = (basisT1 - basisT0) * futPosScaling;

                    //convexity pnl from CTD
                    var ctdId        = t0Vals[tradeId].CheapestToDeliver;
                    var pnlConvexity = 0.0;
                    var bondMktData  = t0Mkt.BondPrices[ctdId];
                    if (bondMktData != null)
                    {
                        var ctdCleanPriceT0 = bondMktData.CleanPrice;
                        var ctdInfo         = bondFut.DeliverableBondInfos.Where(x => x.BondId == ctdId).First();
                        var ctdResultT0     = XlUdf.BondEngineCalc(ctdId, t0Mkt.ReferenceDate.ToString(),
                                                                   PriceQuoteType.Clean, ctdCleanPriceT0, PricingRequest.DirtyPrice,
                                                                   fixedBond: ctdInfo) as PricingResult;
                        pnlConvexity = 0.5 * Math.Pow(t1Vals[tradeId].Ytm - t0Vals[tradeId].Ytm, 2.0) * t0Vals[tradeId].DollarConvexity / 100.0;
                    }

                    //time pnl from CTD
                    var timePnL = t0ValsRolledForward[tradeId].UnderlyingPv - t0Vals[tradeId].UnderlyingPv;

                    var pnl = new BondFuturesPnLResult(tPv, t1Pv, pnlPv01: pnlPv01, pnlZspread: zspreadPnl, pnlBasis: basisPnL,
                                                       pnlTime: timePnL, pnlConvexity: pnlConvexity, curveRisks: null);

                    ret[tradeId] = pnl;
                }
                if (tradeInfo is VanillaOptionInfo || tradeInfo is BinaryOptionInfo || GetTrade(tradeId) is BarrierOptionInfo || tradeInfo is AsianOptionInfo)
                {
                    OptionValuationParameters valuationParameters = null;
                    Date   exerciseDate = null;
                    double strike       = 0.0;

                    var trade = tradeInfo as OptionInfoBase;
                    valuationParameters = trade.ValuationParamter;
                    TradeUtil.GenerateOptionDates(trade, out Date[] exerciseDates, out Date[] obsDates, out DayGap settlementGap);
Exemplo n.º 8
0
        /// <summary>
        /// Add rate market data definition to an existing market.
        /// </summary>
        /// <param name="marketName">The Existing Market Name</param>
        /// <param name="curveName">The curve name</param>
        /// <param name="indexType">The index type(enum IndexType)</param>
        /// <param name="instrumentType">The InstrumentType(enum InstrumentType)</param>
        /// <param name="tenor">The curve rate tenor</param>
        /// <param name="rate">The rate value</param>
        /// <param name="isOverride">if true, then add or override. if false, then ignore(default true)</param>
        /// <returns>if succeeded, return true. if failed, return error message : "Market {market object name} is not available!"</returns>
        public static object xl_MktAddRateData(string marketName, string curveName, string indexType, string instrumentType, string tenor, double rate, bool isOverride = true)
        {
            var marketData = new RateMktData(tenor, rate, indexType, instrumentType, curveName);

            return(XlManager.MergeMarketInfo(marketName, marketData, isOverride));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Add bondfutures market data definition to an existing market.
        /// </summary>
        /// <param name="marketName">The Existing Market Name</param>
        /// <param name="tfId">The Bond futures Id</param>
        /// <param name="quote">The quote of bond futures price</param>
        /// <param name="isOverride">if true, then add or override. if false, then ignore(default true)</param>
        /// <returns>if succeeded, return true. if failed, return error message : "Market {market object name} is not available!"</returns>
        public static object xl_MktAddFuturesData(string marketName, string tfId, double quote, bool isOverride = true)
        {
            var marketData = new FuturesMktData(tfId, quote);

            return(XlManager.MergeMarketInfo(marketName, marketData, isOverride));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Add bond market data definition to an existing market.
        /// </summary>
        /// <param name="marketName">The Existing Market Name</param>
        /// <param name="bondId">The Bond Id</param>
        /// <param name="quoteType">The Price quote type(Clean, Dirty, Ytm, YtmExecution)</param>
        /// <param name="quote">The quote of bond</param>
        /// <param name="isOverride">if true, then add or override. if false, then ignore(default true)</param>
        /// <returns>if succeeded, return true. if failed, return error message : "Market {market object name} is not available!"</returns>
        public static object xl_MktAddBondData(string marketName, string bondId, string quoteType, double quote, bool isOverride = true)
        {
            var marketData = new BondMktData(bondId, quoteType, quote);

            return(XlManager.MergeMarketInfo(marketName, marketData, isOverride));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Convert tow column array {label, value} paris to TradeInfo.
        /// </summary>
        /// <param name="labelValue">Label value pairs.</param>
        /// <returns>Return a QDP trade info object given two column array of {label, value} pairs.</returns>
        public static TradeInfoBase ToTradeInfoBase(this object[,] labelValue)
        {
            var row = labelValue.GetLength(0);
            var col = labelValue.GetLength(1);

            var deliverableBondIds = new List <string>();
            var instrumentType     = InstrumentType.None;

            for (var i = 0; i < row; ++i)
            {
                if ((string)labelValue[i, 0] == "InstrumentType")
                {
                    instrumentType = ((string)labelValue[i, 1]).ToInstrumentType();
                }
                //if ((string) labelValue[i, 0] == "DeliverableBondInfos")
                //{
                //	deliverableBondIds.AddRange(labelValue[i, 1] != null ? ((string)labelValue[i, 1]).Split(',').ToList() : null);
                //}
            }

            if (instrumentType == InstrumentType.BondFutures)
            {
                var tf = labelValue.TransposeRowsAndColumns().ToArrayObj <BondFuturesInfo>().Single();
                tf.DeliverableBondInfos = deliverableBondIds.Select(x => (FixedRateBondInfo)XlManager.GetTrade(x)).ToArray();
                return(tf);
            }
            if (instrumentType == InstrumentType.InterestRateSwap)
            {
                return(labelValue.TransposeRowsAndColumns().ToArrayObj <InterestRateSwapInfo>().Single());
            }
            if (instrumentType == InstrumentType.FixedRateBond)
            {
                return(labelValue.TransposeRowsAndColumns().ToArrayObj <FixedRateBondInfo>().Single());
            }
            if (instrumentType == InstrumentType.FloatingRateBond)
            {
                return(labelValue.TransposeRowsAndColumns().ToArrayObj <FloatingRateBondInfo>().Single());
            }
            if (instrumentType == InstrumentType.FixedDateCouonAdjustedBond)
            {
                return(labelValue.TransposeRowsAndColumns().ToArrayObj <FixedDateCouonAdjustedBondInfo>().Single());
            }
            return(null);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Return a interest rate swap.
        /// </summary>
        /// <param name="tradeId"></param>
        /// <param name="startDate"></param>
        /// <param name="maturityDate"></param>
        /// <param name="tenor"></param>
        /// <param name="notional"></param>
        /// <param name="currency"></param>
        /// <param name="swapDirection"></param>
        /// <param name="calendar"></param>
        /// <param name="fixedLegDayCount"></param>
        /// <param name="fixedLegFrequency"></param>
        /// <param name="fixedLegBusinessDayConvention"></param>
        /// <param name="fixedLegStub"></param>
        /// <param name="fixedLegCoupon"></param>
        /// <param name="floatingLegDayCount"></param>
        /// <param name="floatingLegFrequency"></param>
        /// <param name="floatingLegBusinessDayConvention"></param>
        /// <param name="floatingLegStub"></param>
        /// <param name="index"></param>
        /// <param name="resetTerm"></param>
        /// <param name="resetStub"></param>
        /// <param name="resetBusinessDayConvention"></param>
        /// <param name="resetToFixingGap"></param>
        /// <param name="resetCompound"></param>
        /// <returns></returns>
        public static object xl_InterestRateSwap(string tradeId
                                                 , string startDate                        = null
                                                 , string maturityDate                     = null
                                                 , string tenor                            = "1Y"
                                                 , double notional                         = 100.0
                                                 , string currency                         = "CNY"
                                                 , string swapDirection                    = "Payer"
                                                 , string calendar                         = "chn_ib"
                                                 , string fixedLegDayCount                 = "Act365"
                                                 , string fixedLegFrequency                = "Quarterly"
                                                 , string fixedLegBusinessDayConvention    = "ModifiedFollowing"
                                                 , string fixedLegStub                     = "ShortEnd"
                                                 , double fixedLegCoupon                   = 0.03
                                                 , string floatingLegDayCount              = "Act365"
                                                 , string floatingLegFrequency             = "Quarterly"
                                                 , string floatingLegBusinessDayConvention = "ModifiedFollowing"
                                                 , string floatingLegStub                  = "ShortEnd"
                                                 , string index                            = "Fr007"
                                                 , string resetTerm                        = "1W"
                                                 , string resetStub                        = "ShortEnd"
                                                 , string resetBusinessDayConvention       = "None"
                                                 , string resetToFixingGap                 = "+1BD"
                                                 , string resetCompound                    = "Compounded"
                                                 )
        {
            var interestRateSwapInfo = XlManager.GetTrade(tradeId);

            if (!(interestRateSwapInfo is InterestRateSwapInfo))
            {
                startDate = startDate ?? DateTime.Now.ToString("yyyy-MM-dd");
                if (maturityDate == null && tenor != null)
                {
                    maturityDate = new Term(tenor).Next(startDate.ToDate()).ToString();
                }
                else
                {
                    maturityDate = maturityDate ?? new Term("1Y").Next(startDate.ToDate()).ToString();
                }
                string curveName = "";
                switch (index)
                {
                case "Fr007":
                    curveName = "Fr007SwapCurve";
                    break;

                case "Shibor3M":
                    curveName = "Shibor3MSwapCurve";
                    break;

                case "Shibor1D":
                    curveName = "ShiborONSwapCurve";
                    break;

                case "Depo1Y":
                    curveName = "Depo1YSwapCurve";
                    break;
                }

                interestRateSwapInfo = new InterestRateSwapInfo(tradeId)
                {
                    StartDate          = startDate,
                    MaturityDate       = maturityDate,
                    Tenor              = tenor,
                    Notional           = notional,
                    Currency           = currency,
                    SwapDirection      = swapDirection,
                    Calendar           = calendar,
                    FixedLegDC         = fixedLegDayCount,
                    FixedLegFreq       = fixedLegFrequency,
                    FixedLegBD         = fixedLegBusinessDayConvention,
                    FixedLegStub       = fixedLegStub,
                    FixedLegCoupon     = fixedLegCoupon,
                    FloatingLegDC      = floatingLegDayCount,
                    FloatingLegFreq    = floatingLegFrequency,
                    FloatingLegBD      = floatingLegBusinessDayConvention,
                    FloatingLegStub    = floatingLegStub,
                    Index              = index,
                    ResetTerm          = resetTerm,
                    ResetStub          = resetStub,
                    ResetBD            = resetBusinessDayConvention,
                    ResetToFixingGap   = resetToFixingGap,
                    ResetCompound      = resetCompound,
                    ValuationParamters = new SimpleCfValuationParameters(curveName, curveName, null)
                };

                XlManager.AddTrades(new[] { interestRateSwapInfo });
            }
            return(interestRateSwapInfo.ToTradeInfoInLabelData(null));
        }
Exemplo n.º 13
0
        public static object BondEngineCalc(string bondId, string calcDate, PriceQuoteType priceQuote, double quote, PricingRequest request, FixedRateBondInfo fixedBond = null)
        {
            var bond = fixedBond ?? XlManager.GetTrade(bondId);

            if (bond == null)
            {
                return(string.Format("Cannot find bond {0}.", bondId));
            }

            var vf             = new BondVf((BondInfoBase)bond);
            var bondInstrument = vf.GenerateInstrument();

            var valueDate   = calcDate.ToDate();
            var fixingCurve = new YieldCurve(
                "中债国债收收益率曲线",
                valueDate,
                new[]
            {
                new Tuple <Date, double>(valueDate, 0.0),
                new Tuple <Date, double>(new Term("10Y").Next(valueDate), 0.0)
            },
                BusinessDayConvention.ModifiedFollowing,
                new Act365(),
                CalendarImpl.Get("Chn_ib"),
                CurrencyCode.CNY,
                Compound.Continuous,
                Interpolation.ForwardFlat,
                YieldCurveTrait.ForwardCurve
                );
            var market = new MarketCondition(
                x => x.ValuationDate.Value = valueDate,
                x => x.FixingCurve.Value   = fixingCurve,
                x =>
                x.MktQuote.Value =
                    new Dictionary <string, Tuple <PriceQuoteType, double> > {
                { bondId, Tuple.Create(priceQuote, quote) }
            },
                x => x.HistoricalIndexRates.Value = HistoricalIndexRates
                );

            if (bond is FloatingRateBondInfo)
            {
                var fixingTuple = bondInstrument.Coupon.GetPrimeCoupon(HistoricalIndexRates, fixingCurve, valueDate);
                var keyTenors   = new string[fixingCurve.GetKeyTenors().Length];
                fixingCurve.GetKeyTenors().CopyTo(keyTenors, 0);
                for (var i = 0; i < keyTenors.Length; ++i)
                {
                    market = (MarketCondition)market.UpdateCondition(new UpdateMktConditionPack <IYieldCurve>(x => x.FixingCurve, market.FixingCurve.Value.BumpKeyRate(i, fixingTuple.Item2)));
                }
                if (request.Equals(PricingRequest.None))
                {
                    return(fixingTuple);
                }
            }

            var engine = vf.GenerateEngine();
            var result = engine.Calculate(bondInstrument, market, request);

            if (!result.Succeeded)
            {
                return(string.Format("Failed to Calculate bond {0}:{1}", bondId, result.ErrorMessage));
            }

            return(result);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Return QDP bond qdp object.
        /// </summary>
        /// <param name="tradeId"></param>
        /// <param name="startDate"></param>
        /// <param name="maturityDate"></param>
        /// <param name="notional"></param>
        /// <param name="calendar"></param>
        /// <param name="currency"></param>
        /// <param name="accrualDayCount"></param>
        /// <param name="accrualBusinessDayConvention"></param>
        /// <param name="paymentDayCount"></param>
        /// <param name="paymentFrequency"></param>
        /// <param name="paymentStub"></param>
        /// <param name="paymentBusinessDayConvention"></param>
        /// <param name="settlement"></param>
        /// <param name="settlementCoupon"></param>
        /// <param name="issuePrice"></param>
        /// <param name="firstPaymentDate"></param>
        /// <param name="issueRate"></param>
        /// <param name="amoritzationInIndex"></param>
        /// <param name="renormalizeAfterAmoritzation"></param>
        /// <param name="compensationRate"></param>
        /// <param name="optionToCall"></param>
        /// <param name="optionToPut"></param>
        /// <param name="optionToAssPut"></param>
        /// <param name="fixedCoupon"></param>
        /// <param name="index"></param>
        /// <param name="resetDayCount"></param>
        /// <param name="resetCompound"></param>
        /// <param name="resetStub"></param>
        /// <param name="resetBusinessDayConvention"></param>
        /// <param name="resetToFixingGap"></param>
        /// <param name="resetTerm"></param>
        /// <param name="resetAverageDays"></param>
        /// <param name="resetRateDigits"></param>
        /// <param name="spread"></param>
        /// <param name="floatingRateMultiplier"></param>
        /// <param name="stickToEom"></param>
        /// <returns></returns>
        private static object xl_Bond(
            string tradeId,
            string startDate       = null,
            string maturityDate    = null,
            double notional        = 100,
            string calendar        = "chn_ib",
            string currency        = "CNY",
            string accrualDayCount = "Act365",
            string accrualBusinessDayConvention = "ModifiedFollowing",
            string paymentDayCount              = "Act365",
            string paymentFrequency             = "SemiAnnual",
            string paymentStub                  = "ShortStart",
            string paymentBusinessDayConvention = "ModifiedFollowing",
            string settlement       = "+0D",
            double settlementCoupon = double.NaN,
            double issuePrice       = double.NaN,
            string firstPaymentDate = null,
            double issueRate        = double.NaN,
            Dictionary <int, double> amoritzationInIndex = null,
            bool renormalizeAfterAmoritzation            = false,
            Dictionary <int, double> compensationRate    = null,
            Dictionary <string, double> optionToCall     = null,
            Dictionary <string, double> optionToPut      = null,
            Dictionary <string, double> optionToAssPut   = null,
            double fixedCoupon   = double.NaN,
            string index         = null,
            string resetDayCount = null,
            string resetCompound = null,
            string resetStub     = null,
            string resetBusinessDayConvention = null,
            string resetToFixingGap           = null,
            string resetTerm              = null,
            int resetAverageDays          = 1,
            int resetRateDigits           = 12,
            double spread                 = double.NaN,
            double floatingRateMultiplier = double.NaN,
            bool stickToEom               = false)
        {
            var tradeInfo = XlManager.GetTrade(tradeId);

            if (!(tradeInfo is BondInfoBase))
            {
                startDate    = startDate ?? DateTime.Now.ToString("yyyy-MM-dd");
                maturityDate = maturityDate ?? new Term("1Y").Next(startDate.ToDate()).ToString();
                BondInfoBase bondInfo = null;
                if (string.IsNullOrWhiteSpace(index))
                {
                    bondInfo = new FixedRateBondInfo(tradeId)
                    {
                        FixedCoupon = double.IsNaN(fixedCoupon) ? 0.03 : fixedCoupon
                    };
                }
                else
                {
                    bondInfo = new FloatingRateBondInfo(tradeId)
                    {
                        Index                  = index ?? "Shibor3M",
                        ResetDC                = resetDayCount ?? "Act365",
                        ResetCompound          = resetCompound ?? "Simple",
                        ResetStub              = resetStub ?? "ShortStart",
                        ResetBD                = resetBusinessDayConvention ?? "ModifiedFollowing",
                        ResetToFixingGap       = resetToFixingGap ?? "-1BD",
                        ResetTerm              = resetTerm ?? "3M",
                        Spread                 = double.IsNaN(spread) ? 0.0 : spread,
                        ResetAverageDays       = resetAverageDays,
                        ResetRateDigits        = resetRateDigits,
                        FloatingRateMultiplier = double.IsNaN(floatingRateMultiplier) ? 1.0 : floatingRateMultiplier,
                        FloatingCalc           = "ZzFrn",
                        CapRate                = 100,
                        FloorRate              = -100
                    };
                }
                bondInfo.StartDate        = startDate;
                bondInfo.MaturityDate     = maturityDate;
                bondInfo.Calendar         = calendar;
                bondInfo.PaymentFreq      = paymentFrequency;
                bondInfo.StickToEom       = stickToEom;
                bondInfo.PaymentStub      = paymentStub;
                bondInfo.Notional         = notional;
                bondInfo.Currency         = currency;
                bondInfo.AccrualDC        = accrualDayCount;
                bondInfo.DayCount         = paymentDayCount;
                bondInfo.AccrualBD        = accrualBusinessDayConvention;
                bondInfo.PaymentBD        = paymentBusinessDayConvention;
                bondInfo.Settlement       = settlement;
                bondInfo.SettlementCoupon = settlementCoupon;
                bondInfo.TradingMarket    = calendar == "chn_ib"
                                        ? TradingMarket.ChinaInterBank.ToString()
                                        : TradingMarket.ChinaExShe.ToString();
                bondInfo.IsZeroCouponBond    = !double.IsNaN(issuePrice);
                bondInfo.IssuePrice          = issuePrice;
                bondInfo.FirstPaymentDate    = firstPaymentDate;
                bondInfo.AmortizationType    = "None";
                bondInfo.AmoritzationInIndex = amoritzationInIndex;
                bondInfo.RenormAmortization  = renormalizeAfterAmoritzation;
                bondInfo.CompensationRate    = compensationRate;
                bondInfo.IssueRate           = issueRate;
                bondInfo.OptionToCall        = optionToCall;
                bondInfo.OptionToPut         = optionToPut;
                bondInfo.OptionToAssPut      = optionToAssPut;
                bondInfo.ValuationParamters  = new SimpleCfValuationParameters("中债国债收益率曲线", "", "中债国债收益率曲线");

                XlManager.AddTrades(new[] { bondInfo });
                tradeInfo = bondInfo;
            }
            return(tradeInfo.ToTradeInfoInLabelData(null));
        }