Exemple #1
0
        public void testFixing()
        {
            Date     tradeDate      = new Date(17, Month.April, 2015);
            Calendar calendar       = new UnitedKingdom();
            Date     settlementDate = calendar.advance(tradeDate, 2, TimeUnit.Days, BusinessDayConvention.Following);
            Date     maturityDate   = calendar.advance(settlementDate, 5, TimeUnit.Years, BusinessDayConvention.Following);

            Date valueDate = new Date(20, Month.April, 2015);

            Settings.setEvaluationDate(valueDate);

            List <Date> dates = new List <Date>();

            dates.Add(valueDate);
            dates.Add(valueDate + new Period(1, TimeUnit.Years));
            dates.Add(valueDate + new Period(2, TimeUnit.Years));
            dates.Add(valueDate + new Period(5, TimeUnit.Years));
            dates.Add(valueDate + new Period(10, TimeUnit.Years));
            dates.Add(valueDate + new Period(20, TimeUnit.Years));

            List <double> rates = new List <double>();

            rates.Add(0.01);
            rates.Add(0.01);
            rates.Add(0.01);
            rates.Add(0.01);
            rates.Add(0.01);
            rates.Add(0.01);

            var      discountCurveHandle = new RelinkableHandle <YieldTermStructure>();
            var      forecastCurveHandle = new RelinkableHandle <YieldTermStructure>();
            GBPLibor index = new GBPLibor(new Period(6, TimeUnit.Months), forecastCurveHandle);
            InterpolatedZeroCurve <Linear> zeroCurve = new InterpolatedZeroCurve <Linear>(dates, rates, new Actual360(), new Linear());
            var         fixedSchedule = new Schedule(settlementDate, maturityDate, new Period(1, TimeUnit.Years), calendar, BusinessDayConvention.Following, BusinessDayConvention.Following, DateGeneration.Rule.Forward, false);
            var         floatSchedule = new Schedule(settlementDate, maturityDate, index.tenor(), calendar, BusinessDayConvention.Following, BusinessDayConvention.Following, DateGeneration.Rule.Forward, false);
            VanillaSwap swap          = new VanillaSwap(VanillaSwap.Type.Payer, 1000000, fixedSchedule, 0.01, new Actual360(), floatSchedule, index, 0, new Actual360());

            discountCurveHandle.linkTo(zeroCurve);
            forecastCurveHandle.linkTo(zeroCurve);
            var swapEngine = new DiscountingSwapEngine(discountCurveHandle, false, null);

            swap.setPricingEngine(swapEngine);

            try
            {
                double npv = swap.NPV();
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
                Console.WriteLine(ex);
            }
        }