Exemplo n.º 1
0
        static void Main(string[] args)
        {
            DateTime timer = DateTime.Now;

            Date        repoSettlementDate     = new Date(14, Month.February, 2000);;
            Date        repoDeliveryDate       = new Date(15, Month.August, 2000);
            double      repoRate               = 0.05;
            DayCounter  repoDayCountConvention = new Actual360();
            int         repoSettlementDays     = 0;
            Compounding repoCompounding        = Compounding.Simple;
            Frequency   repoCompoundFreq       = Frequency.Annual;

            // assume a ten year bond- this is irrelevant
            Date      bondIssueDate       = new Date(15, Month.September, 1995);
            Date      bondDatedDate       = new Date(15, Month.September, 1995);
            Date      bondMaturityDate    = new Date(15, Month.September, 2005);
            double    bondCoupon          = 0.08;
            Frequency bondCouponFrequency = Frequency.Semiannual;
            // unknown what calendar fincad is using
            Calendar   bondCalendar           = new NullCalendar();
            DayCounter bondDayCountConvention = new Thirty360(Thirty360.Thirty360Convention.BondBasis);
            // unknown what fincad is using. this may affect accrued calculation
            int bondSettlementDays = 0;
            BusinessDayConvention bondBusinessDayConvention = BusinessDayConvention.Unadjusted;
            double bondCleanPrice = 89.97693786;
            double bondRedemption = 100.0;
            double faceAmount     = 100.0;


            Settings.setEvaluationDate(repoSettlementDate);

            RelinkableHandle <YieldTermStructure> bondCurve = new RelinkableHandle <YieldTermStructure>();

            bondCurve.linkTo(new FlatForward(repoSettlementDate,
                                             .01,                                               // dummy rate
                                             bondDayCountConvention,
                                             Compounding.Compounded,
                                             bondCouponFrequency));

            /*
             * boost::shared_ptr<FixedRateBond> bond(
             *                         new FixedRateBond(faceAmount,
             *                                                               bondIssueDate,
             *                                                               bondDatedDate,
             *                                                               bondMaturityDate,
             *                                                               bondSettlementDays,
             *                                                               std::vector<Rate>(1,bondCoupon),
             *                                                               bondCouponFrequency,
             *                                                               bondCalendar,
             *                                                               bondDayCountConvention,
             *                                                               bondBusinessDayConvention,
             *                                                               bondBusinessDayConvention,
             *                                                               bondRedemption,
             *                                                               bondCurve));
             */

            Schedule bondSchedule = new Schedule(bondDatedDate, bondMaturityDate,
                                                 new Period(bondCouponFrequency),
                                                 bondCalendar, bondBusinessDayConvention,
                                                 bondBusinessDayConvention,
                                                 DateGeneration.Rule.Backward, false);
            FixedRateBond bond = new FixedRateBond(bondSettlementDays,
                                                   faceAmount,
                                                   bondSchedule,
                                                   new List <double>()
            {
                bondCoupon
            },
                                                   bondDayCountConvention,
                                                   bondBusinessDayConvention,
                                                   bondRedemption,
                                                   bondIssueDate);

            bond.setPricingEngine(new DiscountingBondEngine(bondCurve));

            bondCurve.linkTo(new FlatForward(repoSettlementDate,
                                             bond.yield(bondCleanPrice,
                                                        bondDayCountConvention,
                                                        Compounding.Compounded,
                                                        bondCouponFrequency),
                                             bondDayCountConvention,
                                             Compounding.Compounded,
                                             bondCouponFrequency));

            Position.Type fwdType     = Position.Type.Long;
            double        dummyStrike = 91.5745;

            RelinkableHandle <YieldTermStructure> repoCurve = new RelinkableHandle <YieldTermStructure>();

            repoCurve.linkTo(new FlatForward(repoSettlementDate,
                                             repoRate,
                                             repoDayCountConvention,
                                             repoCompounding,
                                             repoCompoundFreq));


            FixedRateBondForward bondFwd = new FixedRateBondForward(repoSettlementDate,
                                                                    repoDeliveryDate,
                                                                    fwdType,
                                                                    dummyStrike,
                                                                    repoSettlementDays,
                                                                    repoDayCountConvention,
                                                                    bondCalendar,
                                                                    bondBusinessDayConvention,
                                                                    bond,
                                                                    repoCurve,
                                                                    repoCurve);


            Console.WriteLine("Underlying bond clean price: " + bond.cleanPrice());
            Console.WriteLine("Underlying bond dirty price: " + bond.dirtyPrice());
            Console.WriteLine("Underlying bond accrued at settlement: "
                              + bond.accruedAmount(repoSettlementDate));
            Console.WriteLine("Underlying bond accrued at delivery:   "
                              + bond.accruedAmount(repoDeliveryDate));
            Console.WriteLine("Underlying bond spot income: "
                              + bondFwd.spotIncome(repoCurve));
            Console.WriteLine("Underlying bond fwd income:  "
                              + bondFwd.spotIncome(repoCurve) /
                              repoCurve.link.discount(repoDeliveryDate));
            Console.WriteLine("Repo strike: " + dummyStrike);
            Console.WriteLine("Repo NPV:    " + bondFwd.NPV());
            Console.WriteLine("Repo clean forward price: "
                              + bondFwd.cleanForwardPrice());
            Console.WriteLine("Repo dirty forward price: "
                              + bondFwd.forwardPrice());
            Console.WriteLine("Repo implied yield: "
                              + bondFwd.impliedYield(bond.dirtyPrice(),
                                                     dummyStrike,
                                                     repoSettlementDate,
                                                     repoCompounding,
                                                     repoDayCountConvention));
            Console.WriteLine("Market repo rate:   "
                              + repoCurve.link.zeroRate(repoDeliveryDate,
                                                        repoDayCountConvention,
                                                        repoCompounding,
                                                        repoCompoundFreq));

            Console.WriteLine("\nCompare with example given at \n"
                              + "http://www.fincad.com/support/developerFunc/mathref/BFWD.htm");
            Console.WriteLine("Clean forward price = 88.2408");
            Console.WriteLine("\nIn that example, it is unknown what bond calendar they are\n"
                              + "using, as well as settlement Days. For that reason, I have\n"
                              + "made the simplest possible assumptions here: NullCalendar\n"
                              + "and 0 settlement days.\n");


            Console.WriteLine("nRun completed in {0}", DateTime.Now - timer);

            Console.Write("Press any key to continue ...");
            Console.ReadKey();
        }
Exemplo n.º 2
0
        public void testInArrears()
        {
            // Testing in-arrears swap calculation
            CommonVars vars = new CommonVars();

            /* See Hull, 4th ed., page 550
             * Note: the calculation in the book is wrong (work out the adjustment and you'll get 0.05 + 0.000115 T1) */
            Date     maturity = vars.today + new Period(5, TimeUnit.Years);
            Calendar calendar = new NullCalendar();
            Schedule schedule = new Schedule(vars.today, maturity, new Period(Frequency.Annual), calendar,
                                             BusinessDayConvention.Following, BusinessDayConvention.Following,
                                             DateGeneration.Rule.Forward, false);
            DayCounter dayCounter = new SimpleDayCounter();

            List <double> nominals = new List <double>()
            {
                100000000.0
            };

            IborIndex index = new IborIndex("dummy", new Period(1, TimeUnit.Years), 0, new EURCurrency(), calendar,
                                            BusinessDayConvention.Following, false, dayCounter, vars.termStructure);
            double oneYear = 0.05;
            double r       = Math.Log(1.0 + oneYear);

            vars.termStructure.linkTo(Utilities.flatRate(vars.today, r, dayCounter));

            List <double> coupons = new List <double>()
            {
                oneYear
            };
            List <CashFlow> fixedLeg = new FixedRateLeg(schedule)
                                       .withCouponRates(coupons, dayCounter)
                                       .withNotionals(nominals);

            List <double> gearings   = new List <double>();
            List <double> spreads    = new List <double>();
            int           fixingDays = 0;

            double capletVolatility = 0.22;
            var    vol = new Handle <OptionletVolatilityStructure>(
                new ConstantOptionletVolatility(vars.today, new NullCalendar(),
                                                BusinessDayConvention.Following, capletVolatility, dayCounter));
            IborCouponPricer pricer = new BlackIborCouponPricer(vol);

            List <CashFlow> floatingLeg = new IborLeg(schedule, index)
                                          .withPaymentDayCounter(dayCounter)
                                          .withFixingDays(fixingDays)
                                          .withGearings(gearings)
                                          .withSpreads(spreads)
                                          .inArrears()
                                          .withNotionals(nominals);

            Utils.setCouponPricer(floatingLeg, pricer);

            Swap swap = new Swap(floatingLeg, fixedLeg);

            swap.setPricingEngine(new DiscountingSwapEngine(vars.termStructure));

            double storedValue = -144813.0;
            double tolerance   = 1.0;

            if (Math.Abs(swap.NPV() - storedValue) > tolerance)
            {
                QAssert.Fail("Wrong NPV calculation:\n"
                             + "    expected:   " + storedValue + "\n"
                             + "    calculated: " + swap.NPV());
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            // boost::timer timer;

            Date today = new Date(16, Month.October, 2007);

            Settings.setEvaluationDate(today);

            Console.WriteLine();
            Console.WriteLine("Pricing a callable fixed rate bond using");
            Console.WriteLine("Hull White model w/ reversion parameter = 0.03");
            Console.WriteLine("BAC4.65 09/15/12  ISIN: US06060WBJ36");
            Console.WriteLine("roughly five year tenor, quarterly coupon and call dates");
            Console.WriteLine("reference date is : " + today.ToLongDateString());
            Console.WriteLine("");

            /* Bloomberg OAS1: "N" model (Hull White)
             * varying volatility parameter
             *
             * The curve entered into Bloomberg OAS1 is a flat curve,
             * at constant yield = 5.5%, semiannual compounding.
             * Assume here OAS1 curve uses an ACT/ACT day counter,
             * as documented in PFC1 as a "default" in the latter case.
             */

            // set up a flat curve corresponding to Bloomberg flat curve

            double       bbCurveRate  = 0.055;
            DayCounter   bbDayCounter = new ActualActual(ActualActual.Convention.Bond);
            InterestRate bbIR         = new InterestRate(bbCurveRate, bbDayCounter, Compounding.Compounded, Frequency.Semiannual);

            Handle <YieldTermStructure> termStructure = new Handle <YieldTermStructure>(flatRate(today,
                                                                                                 bbIR.rate(),
                                                                                                 bbIR.dayCounter(),
                                                                                                 bbIR.compounding(),
                                                                                                 bbIR.frequency()));
            // set up the call schedule

            CallabilitySchedule callSchedule = new CallabilitySchedule();
            double callPrice         = 100.0;
            int    numberOfCallDates = 24;
            Date   callDate          = new Date(15, Month.September, 2006);

            for (int i = 0; i < numberOfCallDates; i++)
            {
                Calendar nullCalendar = new NullCalendar();

                Callability.Price myPrice = new Callability.Price(callPrice, Callability.Price.Type.Clean);
                callSchedule.Add(new Callability(myPrice, Callability.Type.Call, callDate));
                callDate = nullCalendar.advance(callDate, 3, TimeUnit.Months);
            }

            // set up the callable bond

            Date      dated          = new Date(16, Month.September, 2004);
            Date      issue          = dated;
            Date      maturity       = new Date(15, Month.September, 2012);
            int       settlementDays = 3; // Bloomberg OAS1 settle is Oct 19, 2007
            Calendar  bondCalendar   = new UnitedStates(UnitedStates.Market.GovernmentBond);
            double    coupon         = .0465;
            Frequency frequency      = Frequency.Quarterly;
            double    redemption     = 100.0;
            double    faceAmount     = 100.0;

            /* The 30/360 day counter Bloomberg uses for this bond cannot
             * reproduce the US Bond/ISMA (constant) cashflows used in PFC1.
             * Therefore use ActAct(Bond)
             */
            DayCounter bondDayCounter = new ActualActual(ActualActual.Convention.Bond);

            // PFC1 shows no indication dates are being adjusted
            // for weekends/holidays for vanilla bonds
            BusinessDayConvention accrualConvention = BusinessDayConvention.Unadjusted;
            BusinessDayConvention paymentConvention = BusinessDayConvention.Unadjusted;

            Schedule sch = new Schedule(dated, maturity, new Period(frequency), bondCalendar,
                                        accrualConvention, accrualConvention,
                                        DateGeneration.Rule.Backward, false);

            int    maxIterations      = 1000;
            double accuracy           = 1e-8;
            int    gridIntervals      = 40;
            double reversionParameter = .03;

            // output price/yield results for varying volatility parameter

            double sigma = Const.QL_EPSILON; // core dumps if zero on Cygwin

            ShortRateModel hw0 = new HullWhite(termStructure, reversionParameter, sigma);

            IPricingEngine engine0 = new TreeCallableFixedRateBondEngine(hw0, gridIntervals, termStructure);

            CallableFixedRateBond callableBond = new CallableFixedRateBond(settlementDays, faceAmount, sch,
                                                                           new InitializedList <double>(1, coupon),
                                                                           bondDayCounter, paymentConvention,
                                                                           redemption, issue, callSchedule);

            callableBond.setPricingEngine(engine0);

            Console.WriteLine("sigma/vol (%) = {0:0.00}", (100.0 * sigma));

            Console.WriteLine("QLNet price/yld (%)  {0:0.00} / {1:0.00} ",
                              callableBond.cleanPrice(),
                              100.0 * callableBond.yield(bondDayCounter,
                                                         Compounding.Compounded,
                                                         frequency,
                                                         accuracy,
                                                         maxIterations));
            Console.WriteLine("Bloomberg price/yld (%) 96,50 / 5,47");
            Console.WriteLine("");
            //

            sigma = .01;

            Console.WriteLine("sigma/vol (%) = {0:0.00}", (100.0 * sigma));

            ShortRateModel hw1 = new HullWhite(termStructure, reversionParameter, sigma);

            IPricingEngine engine1 = new TreeCallableFixedRateBondEngine(hw1, gridIntervals, termStructure);

            callableBond.setPricingEngine(engine1);

            Console.WriteLine("QLNet price/yld (%)  {0:0.00} / {1:0.00} ",
                              callableBond.cleanPrice(),
                              100.0 * callableBond.yield(bondDayCounter,
                                                         Compounding.Compounded,
                                                         frequency,
                                                         accuracy,
                                                         maxIterations));

            Console.WriteLine("Bloomberg price/yld (%)  95,68 / 5,66");
            Console.WriteLine("");

            //

            sigma = .03;

            Console.WriteLine("sigma/vol (%) = {0:0.00}", (100.0 * sigma));

            ShortRateModel hw2 = new HullWhite(termStructure, reversionParameter, sigma);

            IPricingEngine engine2 = new TreeCallableFixedRateBondEngine(hw2, gridIntervals, termStructure);

            callableBond.setPricingEngine(engine2);

            Console.WriteLine("QLNet price/yld (%)  {0:0.00} / {1:0.00} ",
                              callableBond.cleanPrice(),
                              100.0 * callableBond.yield(bondDayCounter,
                                                         Compounding.Compounded,
                                                         frequency,
                                                         accuracy,
                                                         maxIterations));

            Console.WriteLine("Bloomberg price/yld (%) 92,34 / 6,49");
            Console.WriteLine("");
            //

            sigma = .06;

            Console.WriteLine("sigma/vol (%) = {0:0.00}", (100.0 * sigma));

            ShortRateModel hw3 = new HullWhite(termStructure, reversionParameter, sigma);

            IPricingEngine engine3 = new TreeCallableFixedRateBondEngine(hw3, gridIntervals, termStructure);

            callableBond.setPricingEngine(engine3);

            Console.WriteLine("QLNet price/yld (%)  {0:0.00} / {1:0.00} ",
                              callableBond.cleanPrice(),
                              100.0 * callableBond.yield(bondDayCounter,
                                                         Compounding.Compounded,
                                                         frequency,
                                                         accuracy,
                                                         maxIterations));

            Console.WriteLine("Bloomberg price/yld (%) 87,16 / 7,83");
            Console.WriteLine("");
            //

            sigma = .12;

            Console.WriteLine("sigma/vol (%) = {0:0.00}", (100.0 * sigma));

            ShortRateModel hw4 = new HullWhite(termStructure, reversionParameter, sigma);

            IPricingEngine engine4 = new TreeCallableFixedRateBondEngine(hw4, gridIntervals, termStructure);

            callableBond.setPricingEngine(engine4);

            Console.WriteLine("QLNet price/yld (%)  {0:0.00} / {1:0.00} ",
                              callableBond.cleanPrice(),
                              100.0 * callableBond.yield(bondDayCounter,
                                                         Compounding.Compounded,
                                                         frequency,
                                                         accuracy,
                                                         maxIterations));

            Console.WriteLine("Bloomberg price/yld (%) 77,31 / 10,65");
        }
Exemplo n.º 4
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(NullCalendar obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
Exemplo n.º 5
0
        public void testCached()
        {
            // ("Testing bond price/yield calculation against cached values...");

            CommonVars vars = new CommonVars();

            // with implicit settlement calculation:
            Date today = new Date(22, Month.November, 2004);

            Settings.setEvaluationDate(today);

            Calendar   bondCalendar   = new NullCalendar();
            DayCounter bondDayCount   = new ActualActual(ActualActual.Convention.ISMA);
            int        settlementDays = 1;

            var discountCurve = new Handle <YieldTermStructure>(Utilities.flatRate(today, new SimpleQuote(0.03), new Actual360()));

            // actual market values from the evaluation date
            Frequency freq = Frequency.Semiannual;
            Schedule  sch1 = new Schedule(new Date(31, Month.October, 2004), new Date(31, Month.October, 2006), new Period(freq),
                                          bondCalendar, BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                          DateGeneration.Rule.Backward, false);

            FixedRateBond bond1 = new FixedRateBond(settlementDays, vars.faceAmount, sch1, new List <double>()
            {
                0.025
            },
                                                    bondDayCount, BusinessDayConvention.ModifiedFollowing, 100.0, new Date(1, Month.November, 2004));

            IPricingEngine bondEngine = new DiscountingBondEngine(discountCurve);

            bond1.setPricingEngine(bondEngine);

            double marketPrice1 = 99.203125;
            double marketYield1 = 0.02925;

            Schedule sch2 = new Schedule(new Date(15, Month.November, 2004), new Date(15, Month.November, 2009), new Period(freq),
                                         bondCalendar, BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                         DateGeneration.Rule.Backward, false);

            FixedRateBond bond2 = new FixedRateBond(settlementDays, vars.faceAmount, sch2, new List <double>()
            {
                0.035
            },
                                                    bondDayCount, BusinessDayConvention.ModifiedFollowing,
                                                    100.0, new Date(15, Month.November, 2004));

            bond2.setPricingEngine(bondEngine);

            double marketPrice2 = 99.6875;
            double marketYield2 = 0.03569;

            // calculated values
            double cachedPrice1a = 99.204505, cachedPrice2a = 99.687192;
            double cachedPrice1b = 98.943393, cachedPrice2b = 101.986794;
            double cachedYield1a = 0.029257, cachedYield2a = 0.035689;
            double cachedYield1b = 0.029045, cachedYield2b = 0.035375;
            double cachedYield1c = 0.030423, cachedYield2c = 0.030432;

            // check
            double tolerance = 1.0e-6;
            double price, yield;

            price = bond1.cleanPrice(marketYield1, bondDayCount, Compounding.Compounded, freq);
            if (Math.Abs(price - cachedPrice1a) > tolerance)
            {
                Assert.Fail("failed to reproduce cached price:"
                            + "\n    calculated: " + price
                            + "\n    expected:   " + cachedPrice1a
                            + "\n    tolerance:  " + tolerance
                            + "\n    error:      " + (price - cachedPrice1a));
            }

            price = bond1.cleanPrice();
            if (Math.Abs(price - cachedPrice1b) > tolerance)
            {
                Assert.Fail("failed to reproduce cached price:"
                            + "\n    calculated: " + price
                            + "\n    expected:   " + cachedPrice1b
                            + "\n    tolerance:  " + tolerance
                            + "\n    error:      " + (price - cachedPrice1b));
            }

            yield = bond1.yield(marketPrice1, bondDayCount, Compounding.Compounded, freq);
            if (Math.Abs(yield - cachedYield1a) > tolerance)
            {
                Assert.Fail("failed to reproduce cached compounded yield:"
                            + "\n    calculated: " + yield
                            + "\n    expected:   " + cachedYield1a
                            + "\n    tolerance:  " + tolerance
                            + "\n    error:      " + (yield - cachedYield1a));
            }

            yield = bond1.yield(marketPrice1, bondDayCount, Compounding.Continuous, freq);
            if (Math.Abs(yield - cachedYield1b) > tolerance)
            {
                Assert.Fail("failed to reproduce cached continuous yield:"
                            + "\n    calculated: " + yield
                            + "\n    expected:   " + cachedYield1b
                            + "\n    tolerance:  " + tolerance
                            + "\n    error:      " + (yield - cachedYield1b));
            }

            yield = bond1.yield(bondDayCount, Compounding.Continuous, freq);
            if (Math.Abs(yield - cachedYield1c) > tolerance)
            {
                Assert.Fail("failed to reproduce cached continuous yield:"
                            + "\n    calculated: " + yield
                            + "\n    expected:   " + cachedYield1c
                            + "\n    tolerance:  " + tolerance
                            + "\n    error:      " + (yield - cachedYield1c));
            }


            price = bond2.cleanPrice(marketYield2, bondDayCount, Compounding.Compounded, freq);
            if (Math.Abs(price - cachedPrice2a) > tolerance)
            {
                Assert.Fail("failed to reproduce cached price:"
                            + "\n    calculated: " + price
                            + "\n    expected:   " + cachedPrice2a
                            + "\n    tolerance:  " + tolerance
                            + "\n    error:      " + (price - cachedPrice2a));
            }

            price = bond2.cleanPrice();
            if (Math.Abs(price - cachedPrice2b) > tolerance)
            {
                Assert.Fail("failed to reproduce cached price:"
                            + "\n    calculated: " + price
                            + "\n    expected:   " + cachedPrice2b
                            + "\n    tolerance:  " + tolerance
                            + "\n    error:      " + (price - cachedPrice2b));
            }

            yield = bond2.yield(marketPrice2, bondDayCount, Compounding.Compounded, freq);
            if (Math.Abs(yield - cachedYield2a) > tolerance)
            {
                Assert.Fail("failed to reproduce cached compounded yield:"
                            + "\n    calculated: " + yield
                            + "\n    expected:   " + cachedYield2a
                            + "\n    tolerance:  " + tolerance
                            + "\n    error:      " + (yield - cachedYield2a));
            }

            yield = bond2.yield(marketPrice2, bondDayCount, Compounding.Continuous, freq);
            if (Math.Abs(yield - cachedYield2b) > tolerance)
            {
                Assert.Fail("failed to reproduce cached continuous yield:"
                            + "\n    calculated: " + yield
                            + "\n    expected:   " + cachedYield2b
                            + "\n    tolerance:  " + tolerance
                            + "\n    error:      " + (yield - cachedYield2b));
            }

            yield = bond2.yield(bondDayCount, Compounding.Continuous, freq);
            if (Math.Abs(yield - cachedYield2c) > tolerance)
            {
                Assert.Fail("failed to reproduce cached continuous yield:"
                            + "\n    calculated: " + yield
                            + "\n    expected:   " + cachedYield2c
                            + "\n    tolerance:  " + tolerance
                            + "\n    error:      " + (yield - cachedYield2c));
            }

            // with explicit settlement date:
            Schedule sch3 = new Schedule(new Date(30, Month.November, 2004), new Date(30, Month.November, 2006), new Period(freq),
                                         new UnitedStates(UnitedStates.Market.GovernmentBond), BusinessDayConvention.Unadjusted,
                                         BusinessDayConvention.Unadjusted, DateGeneration.Rule.Backward, false);

            FixedRateBond bond3 = new FixedRateBond(settlementDays, vars.faceAmount, sch3, new List <double>()
            {
                0.02875
            },
                                                    new ActualActual(ActualActual.Convention.ISMA),
                                                    BusinessDayConvention.ModifiedFollowing, 100.0, new Date(30, Month.November, 2004));

            bond3.setPricingEngine(bondEngine);

            double marketYield3 = 0.02997;

            Date   settlementDate = new Date(30, Month.November, 2004);
            double cachedPrice3   = 99.764874;

            price = bond3.cleanPrice(marketYield3, bondDayCount, Compounding.Compounded, freq, settlementDate);
            if (Math.Abs(price - cachedPrice3) > tolerance)
            {
                Assert.Fail("failed to reproduce cached price:"
                            + "\n    calculated: " + price + ""
                            + "\n    expected:   " + cachedPrice3 + ""
                            + "\n    error:      " + (price - cachedPrice3));
            }

            // this should give the same result since the issue date is the
            // earliest possible settlement date
            Settings.setEvaluationDate(new Date(22, Month.November, 2004));

            price = bond3.cleanPrice(marketYield3, bondDayCount, Compounding.Compounded, freq);
            if (Math.Abs(price - cachedPrice3) > tolerance)
            {
                Assert.Fail("failed to reproduce cached price:"
                            + "\n    calculated: " + price + ""
                            + "\n    expected:   " + cachedPrice3 + ""
                            + "\n    error:      " + (price - cachedPrice3));
            }
        }
Exemplo n.º 6
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(NullCalendar obj) {
   return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
 }
Exemplo n.º 7
0
        public void testYYIndex()
        {
            // Testing year-on-year inflation indices

            SavedSettings backup = new SavedSettings();
            //IndexHistoryCleaner cleaner = new IndexHistoryCleaner();

            YYEUHICP yyeuhicp = new YYEUHICP(true);

            if (yyeuhicp.name() != "EU YY_HICP" ||
                yyeuhicp.frequency() != Frequency.Monthly ||
                yyeuhicp.revised() ||
                !yyeuhicp.interpolated() ||
                yyeuhicp.ratio() ||
                yyeuhicp.availabilityLag() != new Period(1, TimeUnit.Months))
            {
                Assert.Fail("wrong year-on-year EU HICP data ("
                            + yyeuhicp.name() + ", "
                            + yyeuhicp.frequency() + ", "
                            + yyeuhicp.revised() + ", "
                            + yyeuhicp.interpolated() + ", "
                            + yyeuhicp.ratio() + ", "
                            + yyeuhicp.availabilityLag() + ")");
            }

            YYEUHICPr yyeuhicpr = new YYEUHICPr(true);

            if (yyeuhicpr.name() != "EU YYR_HICP" ||
                yyeuhicpr.frequency() != Frequency.Monthly ||
                yyeuhicpr.revised() ||
                !yyeuhicpr.interpolated() ||
                !yyeuhicpr.ratio() ||
                yyeuhicpr.availabilityLag() != new Period(1, TimeUnit.Months))
            {
                Assert.Fail("wrong year-on-year EU HICPr data ("
                            + yyeuhicpr.name() + ", "
                            + yyeuhicpr.frequency() + ", "
                            + yyeuhicpr.revised() + ", "
                            + yyeuhicpr.interpolated() + ", "
                            + yyeuhicpr.ratio() + ", "
                            + yyeuhicpr.availabilityLag() + ")");
            }

            YYUKRPI yyukrpi = new YYUKRPI(false);

            if (yyukrpi.name() != "UK YY_RPI" ||
                yyukrpi.frequency() != Frequency.Monthly ||
                yyukrpi.revised() ||
                yyukrpi.interpolated() ||
                yyukrpi.ratio() ||
                yyukrpi.availabilityLag() != new Period(1, TimeUnit.Months))
            {
                Assert.Fail("wrong year-on-year UK RPI data ("
                            + yyukrpi.name() + ", "
                            + yyukrpi.frequency() + ", "
                            + yyukrpi.revised() + ", "
                            + yyukrpi.interpolated() + ", "
                            + yyukrpi.ratio() + ", "
                            + yyukrpi.availabilityLag() + ")");
            }

            YYUKRPIr yyukrpir = new YYUKRPIr(false);

            if (yyukrpir.name() != "UK YYR_RPI" ||
                yyukrpir.frequency() != Frequency.Monthly ||
                yyukrpir.revised() ||
                yyukrpir.interpolated() ||
                !yyukrpir.ratio() ||
                yyukrpir.availabilityLag() != new Period(1, TimeUnit.Months))
            {
                Assert.Fail("wrong year-on-year UK RPIr data ("
                            + yyukrpir.name() + ", "
                            + yyukrpir.frequency() + ", "
                            + yyukrpir.revised() + ", "
                            + yyukrpir.interpolated() + ", "
                            + yyukrpir.ratio() + ", "
                            + yyukrpir.availabilityLag() + ")");
            }


            // Retrieval test.
            //----------------
            // make sure of the evaluation date
            Date evaluationDate = new Date(13, Month.August, 2007);

            evaluationDate = new UnitedKingdom().adjust(evaluationDate);
            Settings.setEvaluationDate(evaluationDate);

            // fixing data
            Date     from        = new Date(1, Month.January, 2005);
            Date     to          = new Date(13, Month.August, 2007);
            Schedule rpiSchedule = new MakeSchedule().from(from).to(to)
                                   .withTenor(new Period(1, TimeUnit.Months))
                                   .withCalendar(new UnitedKingdom())
                                   .withConvention(BusinessDayConvention.ModifiedFollowing).value();

            double[] fixData = { 189.9, 189.9, 189.6, 190.5, 191.6, 192.0,
                                 192.2, 192.2, 192.6, 193.1, 193.3, 193.6,
                                 194.1, 193.4, 194.2, 195.0, 196.5, 197.7,
                                 198.5, 198.5, 199.2, 200.1, 200.4, 201.1,
                                 202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
                                 207.3 };

            bool     interp = false;
            YYUKRPIr iir    = new YYUKRPIr(interp);
            YYUKRPIr iirYES = new YYUKRPIr(true);

            for (int i = 0; i < fixData.Length; i++)
            {
                iir.addFixing(rpiSchedule[i], fixData[i]);
                iirYES.addFixing(rpiSchedule[i], fixData[i]);
            }

            Date todayMinusLag             = evaluationDate - iir.availabilityLag();
            KeyValuePair <Date, Date> lim0 = Utils.inflationPeriod(todayMinusLag, iir.frequency());

            todayMinusLag = lim0.Value + 1 - 2 * new Period(iir.frequency());

            double eps = 1.0e-8;

            // Interpolation tests
            //--------------------
            // (no TS so can't forecast).
            for (int i = 13; i < rpiSchedule.Count; i++)
            {
                KeyValuePair <Date, Date> lim    = Utils.inflationPeriod(rpiSchedule[i], iir.frequency());
                KeyValuePair <Date, Date> limBef = Utils.inflationPeriod(rpiSchedule[i - 12], iir.frequency());
                for (Date d = lim.Key; d <= lim.Value; d++)
                {
                    if (d < todayMinusLag)
                    {
                        double expected   = fixData[i] / fixData[i - 12] - 1.0;
                        double calculated = iir.fixing(d);
                        Assert.IsTrue(Math.Abs(calculated - expected) < eps,
                                      "Non-interpolated fixings not constant within a period: "
                                      + calculated
                                      + ", should be "
                                      + expected);

                        double dp    = lim.Value + 1 - lim.Key;
                        double dpBef = limBef.Value + 1 - limBef.Key;
                        double dl    = d - lim.Key;
                        // potentially does not work on 29th Feb
                        double dlBef = new NullCalendar().advance(d, -new Period(1, TimeUnit.Years),
                                                                  BusinessDayConvention.ModifiedFollowing) - limBef.Key;

                        double linearNow     = fixData[i] + (fixData[i + 1] - fixData[i]) * dl / dp;
                        double linearBef     = fixData[i - 12] + (fixData[i + 1 - 12] - fixData[i - 12]) * dlBef / dpBef;
                        double expectedYES   = linearNow / linearBef - 1.0;
                        double calculatedYES = iirYES.fixing(d);
                        Assert.IsTrue(Math.Abs(expectedYES - calculatedYES) < eps,
                                      "Error in interpolated fixings: expect " + expectedYES
                                      + " see " + calculatedYES
                                      + " flat " + calculated
                                      + ", data: " + fixData[i - 12] + ", " + fixData[i + 1 - 12]
                                      + ", " + fixData[i] + ", " + fixData[i + 1]
                                      + ", fac: " + dp + ", " + dl
                                      + ", " + dpBef + ", " + dlBef
                                      + ", to: " + linearNow + ", " + linearBef
                                      );
                    }
                }
            }
        }