Ejemplo n.º 1
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);
        }