Login  Register

Re: Problem making FloatingRateBond calculations in C#

Posted by sergvil on Oct 25, 2011; 5:17pm
URL: http://quantlib.414.s1.nabble.com/Problem-making-FloatingRateBond-calculations-in-C-tp6279p6280.html

> I think you mean floating-rate bond?

Yes, I'm sorry.

> My guess is that you're passing to the bond constructor an Euribor or
> Libor index without linking it to a yield term structure for forecasting
> index fixings.  How are you creating the index and the bond?

Here it is:

/First I load the Index curve (Euribor3M)

            Date date = new Date(fecha.ToShortDateString(), "dd/MM/yyyy"); //This is proccess date (22/09/2011)

            int settlementDays = 2;

            DateVector vtFechas = new DateVector();
            DoubleVector vtTipos = new DoubleVector();
            Calendar calendario = new TARGET();
            Date settlementDate = date;
            DayCounter zcsDayCounter = new ActualActual();

            vtFechas.Add(settlementDate);
            vtTipos.Add(0.0);
            ...  //Here I charge the vectors with index curve data stored in DB.

            YieldTermStructure yti = new ZeroCurve(vtFechas, vtTipos, zcsDayCounter, calendario);
           
            // I set the handle for iborcurve.
            YieldTermStructureHandle iborcurve = new YieldTermStructureHandle(yti);
           
            //I create the iborindex adding its fixings

            Period tenor = new Period(3, TimeUnit.Months)

            IborIndex index = new Euribor(tenor, iborcurve);
           
            //Here I have a loop to add all fixings stored in DB.
            foreach (FIXINGS fixing in fixings)
            {
                index.addFixing(new Date(fixing.FECHA.ToShortDateString(), "ddMMyyyy"), Convert.ToDouble(fixing.FIXING)/100);
            }
             
            // I create the schedule. All data came from DB and is correct.
            Schedule schedule = new Schedule(settlementdate, maturitydate, Securityperiod, calendar, busdayconv, busdayconvend, generationrule, boolEndOfMonth);

            // Finally I create the Floating Bond

            DoubleVector dvCoupon = new DoubleVector();

            dvCoupon.Add(Convert.ToDouble(producto.CUPON)/100); //From DB, it's stored in % format

            DayCounter contador = new ActualActual();

            BusinessDayConvention busdayconv =  BusinessDayConvention.Unadjusted;

            Calendar calendario = new TARGET();
         
            DoubleVector gearings = new DoubleVector();

            gearings.Add(1);

            DoubleVector spreads = new DoubleVector();

            spreads.Add(0.04);

            DoubleVector caps = new DoubleVector();
            //No CAP            

            DoubleVector floors = new DoubleVector();
            //No FLOOR
           
            settlementdate = new Date(Convert.ToDateTime(producto.FECHAEMISION).ToShortDateString(), "dd/MM/yyyy")  // This is 25/03/2005

            FloatingRateBond frb= new FloatingRateBond(2, 100, schedule, iborindex, contador, busdayconv, 2, gearings, spreads, caps, floors, true, 100, settlementdate);

            //Now i'm going to create the pricer

            double volatility = 0; //This is because CAP and FLOOR are both 0.
   
            ConstantOptionletVolatility cvol = new ConstantOptionletVolatility(2, calendario, busdayconv, volatility, contador);

            OptionletVolatilityStructureHandle vol = new OptionletVolatilityStructureHandle(cvol);

            IborCouponPricer pricer = new BlackIborCouponPricer(vol);

            //Setting the pricer to the Bond Leg (Thanks to other QuantLib List question solved by you :) )
            NQuantLibc.setCouponPricer(frb.cashflows(), pricer);

            //Ok at this point I only have to create discountcurve and load the pricingengine with it.

             // I load the discount curve (TDI-1Y)

             Date date = new Date(fecha.ToShortDateString(), "dd/MM/yyyy");

            int settlementDays = producto.DIASALIQUIDACION;

            DateVector vtFechas = new DateVector();
            DoubleVector vtTipos = new DoubleVector();
            Calendar calendario = new TARGET();
            Date settlementDate = date;
            DayCounter zcsDayCounter = new ActualActual();

            vtFechas.Add(settlementDate);
            vtTipos.Add(0.0);
            ...  //Here I charge the vectors with curve data stored in DB.

            YieldTermStructure ytd = new ZeroCurve(vtFechas, vtTipos, zcsDayCounter, calendario);
           
            // I set the handle for discountcurve.
            YieldTermStructureHandle discounturve = new YieldTermStructureHandle(ytd);
           
            //Setting the curve to BondEngine
            PricingEngine pe = new DiscountingBondEngine(discountcurve);
            frb.setPricingEngine(pe);

            //Now we have the bond ready to make calculations
This is all the processes I make to create the bond, some parts can be a little confused because DB related code (LINQ2SQL) has been omitted and the code is separated in several methods, I put all together for better reading understanding.  I think that thrown exception has relation with iborcurve handle.

Thank you very much for your help!!

Sergio