Problem making FloatingRateBond calculations in C#

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem making FloatingRateBond calculations in C#

sergvil
Hello

I'm using QuantLib wrapper for C#. I have to do some calculations based on three types of securities: Fixed Rate Bond, Zero Coupon Bond and Fixed Rate Bond. I'm getting some problems with last type. The calculations I'm implementing are the next ones:

Theoretical Clean Price
Theoretical Dirty Price
Theoretical Bond Yield
Yield To Maturity
Time To Maturity
Accrued Amount
Net Present Value
Basis-Point Sensivity
Internal Rate of Return
Modified Duration
Macaulay Duration
Duration
Convexity
Basis-Point Value
Z-Spread

When I create the bond and try to execute all calculations, I get the same error in all calculations except Time To Maturity and Basis-Point Sensivity. The thrown exception message is "empty Handle cannot be dereferenced".

Please, say me what part of the code you need to check and I'll put here.

Thank you in advance.

Sergio
Reply | Threaded
Open this post in threaded view
|

Re: Problem making FloatingRateBond calculations in C#

Luigi Ballabio
On Tue, 2011-10-25 at 03:05 -0700, sergvil wrote:
> Hello
>
> I'm using QuantLib wrapper for C#. I have to do some calculations based on
> three types of securities: Fixed Rate Bond, Zero Coupon Bond and Fixed Rate
> Bond. I'm getting some problems with last type.

I think you mean floating-rate bond?

> When I create the bond and try to execute all calculations, I get the same
> error in all calculations except Time To Maturity and Basis-Point Sensivity.
> The thrown exception message is "empty Handle cannot be dereferenced".

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?

Luigi



--

Though this be madness, yet there is method in't.
-- Hamlet, Act II, scene II



------------------------------------------------------------------------------
The demand for IT networking professionals continues to grow, and the
demand for specialized networking skills is growing even more rapidly.
Take a complimentary Learning@Cisco Self-Assessment and learn
about Cisco certifications, training, and career opportunities.
http://p.sf.net/sfu/cisco-dev2dev
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Problem making FloatingRateBond calculations in C#

sergvil
In reply to this post by sergvil
> 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

Reply | Threaded
Open this post in threaded view
|

Re: Problem making FloatingRateBond calculations in C#

sergvil
Hello

I'm still looking for a solution and I didn´t find nothing.

Can somebody help me on this issue?

Thank you very much!
Reply | Threaded
Open this post in threaded view
|

Re: Problem making FloatingRateBond calculations in C#

Luigi Ballabio
On Wed, 2011-10-26 at 18:56 -0700, sergvil wrote:
> Hello
>
> I'm still looking for a solution and I didn´t find nothing.
>
> Can somebody help me on this issue?

Can you send a self-contained program that shows the problem and that we
can execute?

Thanks,
        Luigi


--

Humphrey's Requirements Uncertainty Principle:
For a new software system, the requirements will not be
completely known until after the users have used it.



------------------------------------------------------------------------------
The demand for IT networking professionals continues to grow, and the
demand for specialized networking skills is growing even more rapidly.
Take a complimentary Learning@Cisco Self-Assessment and learn
about Cisco certifications, training, and career opportunities.
http://p.sf.net/sfu/cisco-dev2dev
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Problem making FloatingRateBond calculations in C#

sergvil
>Can you send a self-contained program that shows the problem and that we
>can execute?

When I was making the self-contained program from my code I found the error.

I was creating IborIndex without iborcurve, and that was the cause of exception.

I was doing
IborIndex index = new Euribor(tenor);
instead of
IborIndex index = new Euribor(tenor, iborcurve);
Thanks for your help!

Kind regards.

Sergio.