[quantlib/SWIG/Java]No outputs when pricing a swap

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

[quantlib/SWIG/Java]No outputs when pricing a swap

thysdrus
Hi,

I am trying to price a vanilla swap using quantlib mapped to java through SWIG wrappers.
Unfortunately, getting the faire spread (or any other output) throws an exception because it is null.

Can you please help solving this issue ? Hereunder the java class I am launching. It is adapted from the C++ examples.

package org.quantlib.examples;

import org.quantlib.Actual360;
import org.quantlib.ActualActual;
import org.quantlib.BusinessDayConvention;
import org.quantlib.Calendar;
import org.quantlib.Date;
import org.quantlib.DateGeneration;
import org.quantlib.DateVector;
import org.quantlib.DayCounter;
import org.quantlib.DepositRateHelper;
import org.quantlib.DiscountingSwapEngine;
import org.quantlib.Euribor6M;
import org.quantlib.Frequency;
import org.quantlib.IborIndex;
import org.quantlib.Month;
import org.quantlib.Period;
import org.quantlib.PiecewiseFlatForward;
import org.quantlib.PricingEngine;
import org.quantlib.QuoteHandle;
import org.quantlib.QuoteHandleVector;
import org.quantlib.RateHelper;
import org.quantlib.RateHelperVector;
import org.quantlib.Schedule;
import org.quantlib.Settings;
import org.quantlib.SimpleQuote;
import org.quantlib.SwapRateHelper;
import org.quantlib.TARGET;
import org.quantlib.Thirty360;
import org.quantlib.TimeUnit;
import org.quantlib.VanillaSwap;
import org.quantlib.YieldTermStructure;
import org.quantlib.YieldTermStructureHandle;
import org.quantlib._VanillaSwap;

public class Swap {

        static { // Load QuantLib
                try {
                        System.loadLibrary("QuantLibCppWrapper");
                } catch (RuntimeException e) {
                        e.printStackTrace();
                }
        }

        public static void main(String[] args) {

                /*********************
                 *** MARKET DATA ***
                 *********************/

                Calendar calendar = new TARGET();
                Date settlementDate = new Date(22, Month.September, 2004);
                // must be a business day
                settlementDate = calendar.adjust(settlementDate);

                Integer fixingDays = 2;
                Date todaysDate = calendar.advance(settlementDate, -fixingDays,
                                TimeUnit.Days);
                // nothing to do with Date::todaysDate
                Settings.instance().setEvaluationDate(todaysDate);

                todaysDate = Settings.instance().getEvaluationDate();
                System.out.println("Today: " + todaysDate.weekday() + ", " + todaysDate
                                + "\n");

                System.out.println("Settlement date: " + settlementDate.weekday()
                                + ", " + settlementDate + "\n");

                // deposits
                double d1wQuote = 0.0382;
                double d1mQuote = 0.0372;
                double d3mQuote = 0.0363;
                double d6mQuote = 0.0353;
                double d9mQuote = 0.0348;
                double d1yQuote = 0.0345;
                // swaps
                double s2yQuote = 0.037125;
                double s3yQuote = 0.0398;
                double s5yQuote = 0.0443;
                double s10yQuote = 0.05165;
                double s15yQuote = 0.055175;

                /********************
                 *** QUOTES ***
                 ********************/

                // deposits
                SimpleQuote d1wRate = new SimpleQuote(d1wQuote);
                SimpleQuote d1mRate = new SimpleQuote(d1mQuote);
                SimpleQuote d3mRate = new SimpleQuote(d3mQuote);
                SimpleQuote d6mRate = new SimpleQuote(d6mQuote);
                SimpleQuote d9mRate = new SimpleQuote(d9mQuote);
                SimpleQuote d1yRate = new SimpleQuote(d1yQuote);
                // swaps
                SimpleQuote s2yRate = new SimpleQuote(s2yQuote);
                SimpleQuote s3yRate = new SimpleQuote(s3yQuote);
                SimpleQuote s5yRate = new SimpleQuote(s5yQuote);
                SimpleQuote s10yRate = new SimpleQuote(s10yQuote);
                SimpleQuote s15yRate = new SimpleQuote(s15yQuote);

                /*********************
                 *** RATE HELPERS ***
                 *********************/

                // deposits
                DayCounter depositDayCounter = new Actual360();

                RateHelper d1w = new DepositRateHelper(new QuoteHandle(d1wRate),
                                new Period(1, TimeUnit.Weeks), fixingDays, calendar,
                                BusinessDayConvention.ModifiedFollowing, true,
                                depositDayCounter);

                RateHelper d1m = new DepositRateHelper(new QuoteHandle(d1mRate),
                                new Period(1, TimeUnit.Months), fixingDays, calendar,
                                BusinessDayConvention.ModifiedFollowing, true,
                                depositDayCounter);

                RateHelper d3m = new DepositRateHelper(new QuoteHandle(d3mRate),
                                new Period(3, TimeUnit.Months), fixingDays, calendar,
                                BusinessDayConvention.ModifiedFollowing, true,
                                depositDayCounter);

                RateHelper d6m = new DepositRateHelper(new QuoteHandle(d6mRate),
                                new Period(6, TimeUnit.Months), fixingDays, calendar,
                                BusinessDayConvention.ModifiedFollowing, true,
                                depositDayCounter);

                RateHelper d9m = new DepositRateHelper(new QuoteHandle(d9mRate),
                                new Period(9, TimeUnit.Months), fixingDays, calendar,
                                BusinessDayConvention.ModifiedFollowing, true,
                                depositDayCounter);

                RateHelper d1y = new DepositRateHelper(new QuoteHandle(d1yRate),
                                new Period(1, TimeUnit.Years), fixingDays, calendar,
                                BusinessDayConvention.ModifiedFollowing, true,
                                depositDayCounter);

                // setup swaps
                Frequency swFixedLegFrequency = Frequency.Annual;
                BusinessDayConvention swFixedLegConvention = BusinessDayConvention.Unadjusted;
                DayCounter swFixedLegDayCounter = new Thirty360(
                                Thirty360.Convention.European);
                IborIndex swFloatingLegIndex = new Euribor6M();

                RateHelper s2y = new SwapRateHelper(new QuoteHandle(s2yRate),
                                new Period(2, TimeUnit.Years), calendar, swFixedLegFrequency,
                                swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);

                RateHelper s3y = new SwapRateHelper(new QuoteHandle(s3yRate),
                                new Period(3, TimeUnit.Years), calendar, swFixedLegFrequency,
                                swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
                RateHelper s5y = new SwapRateHelper(new QuoteHandle(s5yRate),
                                new Period(5, TimeUnit.Years), calendar, swFixedLegFrequency,
                                swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
                RateHelper s10y = new SwapRateHelper(new QuoteHandle(s10yRate),
                                new Period(10, TimeUnit.Years), calendar, swFixedLegFrequency,
                                swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
                RateHelper s15y = new SwapRateHelper(new QuoteHandle(s15yRate),
                                new Period(15, TimeUnit.Years), calendar, swFixedLegFrequency,
                                swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);

                /*********************
                 ** CURVE BUILDING **
                 *********************/

                // Any DayCounter would be fine.
                // ActualActual::ISDA ensures that 30 years is 30.0
                DayCounter termStructureDayCounter = new ActualActual(
                                ActualActual.Convention.ISDA);

                double tolerance = 1.0e-15;

                // A depo-swap curve
                RateHelperVector depoSwapInstruments = new RateHelperVector();
                depoSwapInstruments.add(d1w);
                depoSwapInstruments.add(d1m);
                depoSwapInstruments.add(d3m);
                depoSwapInstruments.add(d6m);
                depoSwapInstruments.add(d9m);
                depoSwapInstruments.add(d1y);
                depoSwapInstruments.add(s2y);
                depoSwapInstruments.add(s3y);
                depoSwapInstruments.add(s5y);
                depoSwapInstruments.add(s10y);
                depoSwapInstruments.add(s15y);

                YieldTermStructure depoSwapTermStructure = new PiecewiseFlatForward(
                                settlementDate, depoSwapInstruments, termStructureDayCounter,
                                new QuoteHandleVector(), new DateVector(), tolerance);

                YieldTermStructureHandle curve = new YieldTermStructureHandle(
                                depoSwapTermStructure);

                /*********************
                 * SWAPS TO BE PRICED *
                 **********************/

                // constant nominal 1,000,000 Euro
                double nominal = 1000000.0;
                // fixed leg
                Frequency fixedLegFrequency = Frequency.Annual;
                BusinessDayConvention fixedLegConvention = BusinessDayConvention.Unadjusted;
                BusinessDayConvention floatingLegConvention = BusinessDayConvention.ModifiedFollowing;
                DayCounter fixedLegDayCounter = new Thirty360(
                                Thirty360.Convention.European);
                double fixedRate = 0.04;
                DayCounter floatingLegDayCounter = new Actual360();

                // floating leg
                Frequency floatingLegFrequency = Frequency.Semiannual;
                IborIndex euriborIndex = new Euribor6M(curve);

                double spread = 0.0;

                Integer lenghtInYears = 5;
                _VanillaSwap.Type swapType = VanillaSwap.Payer;

                Date maturity = settlementDate.add(new Period(lenghtInYears,
                                TimeUnit.Years));
                Schedule fixedSchedule = new Schedule(settlementDate, maturity,
                                new Period(fixedLegFrequency), calendar, fixedLegConvention,
                                fixedLegConvention, DateGeneration.Rule.Forward, false);

                Schedule floatSchedule = new Schedule(settlementDate, maturity,
                                new Period(floatingLegFrequency), calendar,
                                floatingLegConvention, floatingLegConvention,
                                DateGeneration.Rule.Forward, false);
                VanillaSwap spot5YearSwap = new VanillaSwap(swapType, nominal,
                                fixedSchedule, fixedRate, fixedLegDayCounter, floatSchedule,
                                euriborIndex, spread, floatingLegDayCounter);

                PricingEngine swapEngine = new DiscountingSwapEngine(curve);

                spot5YearSwap.setPricingEngine(swapEngine);

                System.out.println("NPV = " + spot5YearSwap.NPV());
                System.out.println("fairSpread = " + spot5YearSwap.fairSpread());
                System.out.println("fairRate = " + spot5YearSwap.fairRate());

        }
}
Reply | Threaded
Open this post in threaded view
|

Re: [quantlib/SWIG/Java]No outputs when pricing a swap

Bojan Nikolic

This works for me. Are you sure your JNI library is getting loaded
correctly?

Best,
Bojan

--
Bojan Nikolic          ||          http://www.bnikolic.co.uk

------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: [quantlib/SWIG/Java]No outputs when pricing a swap

thysdrus
Hello,

I used Eclipse CDT and the plugin skwash to generate the JNI and the java files.
There is no errors and no warnings.
when launching the pricing, all works fine (dates are properly shifted, yield curves properly bootstrapped),  despite the swap pricing.
I think (but not sure yet) that the issue comes from the setPricingEngine method.

I noticed also that the equity options pricing example provided with the SWIG files, returns null values.
The example on the monte carlo pricing works fine ! 

Bojan Nikolic wrote
This works for me. Are you sure your JNI library is getting loaded
correctly?

Best,
Bojan

--
Bojan Nikolic          ||          http://www.bnikolic.co.uk

------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
QuantLib-users mailing list
QuantLib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: [quantlib/SWIG/Java]No outputs when pricing a swap

Luigi Ballabio
In reply to this post by thysdrus

Thysdrus,
        I get a spread printed out on my machine.  Just to check, are you sure
the code you sent is the same you're running?

Luigi


On Tue, 2011-08-30 at 07:23 -0700, thysdrus wrote:

> Hi,
>
> I am trying to price a vanilla swap using quantlib mapped to java through
> SWIG wrappers.
> Unfortunately, getting the faire spread (or any other output) throws an
> exception because it is null.
>
> Can you please help solving this issue ? Hereunder the java class I am
> launching. It is adapted from the C++ examples.
>
> package org.quantlib.examples;
>
> import org.quantlib.Actual360;
> import org.quantlib.ActualActual;
> import org.quantlib.BusinessDayConvention;
> import org.quantlib.Calendar;
> import org.quantlib.Date;
> import org.quantlib.DateGeneration;
> import org.quantlib.DateVector;
> import org.quantlib.DayCounter;
> import org.quantlib.DepositRateHelper;
> import org.quantlib.DiscountingSwapEngine;
> import org.quantlib.Euribor6M;
> import org.quantlib.Frequency;
> import org.quantlib.IborIndex;
> import org.quantlib.Month;
> import org.quantlib.Period;
> import org.quantlib.PiecewiseFlatForward;
> import org.quantlib.PricingEngine;
> import org.quantlib.QuoteHandle;
> import org.quantlib.QuoteHandleVector;
> import org.quantlib.RateHelper;
> import org.quantlib.RateHelperVector;
> import org.quantlib.Schedule;
> import org.quantlib.Settings;
> import org.quantlib.SimpleQuote;
> import org.quantlib.SwapRateHelper;
> import org.quantlib.TARGET;
> import org.quantlib.Thirty360;
> import org.quantlib.TimeUnit;
> import org.quantlib.VanillaSwap;
> import org.quantlib.YieldTermStructure;
> import org.quantlib.YieldTermStructureHandle;
> import org.quantlib._VanillaSwap;
>
> public class Swap {
>
> static { // Load QuantLib
> try {
> System.loadLibrary("QuantLibCppWrapper");
> } catch (RuntimeException e) {
> e.printStackTrace();
> }
> }
>
> public static void main(String[] args) {
>
> /*********************
> *** MARKET DATA ***
> *********************/
>
> Calendar calendar = new TARGET();
> Date settlementDate = new Date(22, Month.September, 2004);
> // must be a business day
> settlementDate = calendar.adjust(settlementDate);
>
> Integer fixingDays = 2;
> Date todaysDate = calendar.advance(settlementDate, -fixingDays,
> TimeUnit.Days);
> // nothing to do with Date::todaysDate
> Settings.instance().setEvaluationDate(todaysDate);
>
> todaysDate = Settings.instance().getEvaluationDate();
> System.out.println("Today: " + todaysDate.weekday() + ", " + todaysDate
> + "\n");
>
> System.out.println("Settlement date: " + settlementDate.weekday()
> + ", " + settlementDate + "\n");
>
> // deposits
> double d1wQuote = 0.0382;
> double d1mQuote = 0.0372;
> double d3mQuote = 0.0363;
> double d6mQuote = 0.0353;
> double d9mQuote = 0.0348;
> double d1yQuote = 0.0345;
> // swaps
> double s2yQuote = 0.037125;
> double s3yQuote = 0.0398;
> double s5yQuote = 0.0443;
> double s10yQuote = 0.05165;
> double s15yQuote = 0.055175;
>
> /********************
> *** QUOTES ***
> ********************/
>
> // deposits
> SimpleQuote d1wRate = new SimpleQuote(d1wQuote);
> SimpleQuote d1mRate = new SimpleQuote(d1mQuote);
> SimpleQuote d3mRate = new SimpleQuote(d3mQuote);
> SimpleQuote d6mRate = new SimpleQuote(d6mQuote);
> SimpleQuote d9mRate = new SimpleQuote(d9mQuote);
> SimpleQuote d1yRate = new SimpleQuote(d1yQuote);
> // swaps
> SimpleQuote s2yRate = new SimpleQuote(s2yQuote);
> SimpleQuote s3yRate = new SimpleQuote(s3yQuote);
> SimpleQuote s5yRate = new SimpleQuote(s5yQuote);
> SimpleQuote s10yRate = new SimpleQuote(s10yQuote);
> SimpleQuote s15yRate = new SimpleQuote(s15yQuote);
>
> /*********************
> *** RATE HELPERS ***
> *********************/
>
> // deposits
> DayCounter depositDayCounter = new Actual360();
>
> RateHelper d1w = new DepositRateHelper(new QuoteHandle(d1wRate),
> new Period(1, TimeUnit.Weeks), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> RateHelper d1m = new DepositRateHelper(new QuoteHandle(d1mRate),
> new Period(1, TimeUnit.Months), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> RateHelper d3m = new DepositRateHelper(new QuoteHandle(d3mRate),
> new Period(3, TimeUnit.Months), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> RateHelper d6m = new DepositRateHelper(new QuoteHandle(d6mRate),
> new Period(6, TimeUnit.Months), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> RateHelper d9m = new DepositRateHelper(new QuoteHandle(d9mRate),
> new Period(9, TimeUnit.Months), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> RateHelper d1y = new DepositRateHelper(new QuoteHandle(d1yRate),
> new Period(1, TimeUnit.Years), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> // setup swaps
> Frequency swFixedLegFrequency = Frequency.Annual;
> BusinessDayConvention swFixedLegConvention =
> BusinessDayConvention.Unadjusted;
> DayCounter swFixedLegDayCounter = new Thirty360(
> Thirty360.Convention.European);
> IborIndex swFloatingLegIndex = new Euribor6M();
>
> RateHelper s2y = new SwapRateHelper(new QuoteHandle(s2yRate),
> new Period(2, TimeUnit.Years), calendar, swFixedLegFrequency,
> swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
>
> RateHelper s3y = new SwapRateHelper(new QuoteHandle(s3yRate),
> new Period(3, TimeUnit.Years), calendar, swFixedLegFrequency,
> swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
> RateHelper s5y = new SwapRateHelper(new QuoteHandle(s5yRate),
> new Period(5, TimeUnit.Years), calendar, swFixedLegFrequency,
> swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
> RateHelper s10y = new SwapRateHelper(new QuoteHandle(s10yRate),
> new Period(10, TimeUnit.Years), calendar, swFixedLegFrequency,
> swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
> RateHelper s15y = new SwapRateHelper(new QuoteHandle(s15yRate),
> new Period(15, TimeUnit.Years), calendar, swFixedLegFrequency,
> swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
>
> /*********************
> ** CURVE BUILDING **
> *********************/
>
> // Any DayCounter would be fine.
> // ActualActual::ISDA ensures that 30 years is 30.0
> DayCounter termStructureDayCounter = new ActualActual(
> ActualActual.Convention.ISDA);
>
> double tolerance = 1.0e-15;
>
> // A depo-swap curve
> RateHelperVector depoSwapInstruments = new RateHelperVector();
> depoSwapInstruments.add(d1w);
> depoSwapInstruments.add(d1m);
> depoSwapInstruments.add(d3m);
> depoSwapInstruments.add(d6m);
> depoSwapInstruments.add(d9m);
> depoSwapInstruments.add(d1y);
> depoSwapInstruments.add(s2y);
> depoSwapInstruments.add(s3y);
> depoSwapInstruments.add(s5y);
> depoSwapInstruments.add(s10y);
> depoSwapInstruments.add(s15y);
>
> YieldTermStructure depoSwapTermStructure = new PiecewiseFlatForward(
> settlementDate, depoSwapInstruments, termStructureDayCounter,
> new QuoteHandleVector(), new DateVector(), tolerance);
>
> YieldTermStructureHandle curve = new YieldTermStructureHandle(
> depoSwapTermStructure);
>
> /*********************
> * SWAPS TO BE PRICED *
> **********************/
>
> // constant nominal 1,000,000 Euro
> double nominal = 1000000.0;
> // fixed leg
> Frequency fixedLegFrequency = Frequency.Annual;
> BusinessDayConvention fixedLegConvention =
> BusinessDayConvention.Unadjusted;
> BusinessDayConvention floatingLegConvention =
> BusinessDayConvention.ModifiedFollowing;
> DayCounter fixedLegDayCounter = new Thirty360(
> Thirty360.Convention.European);
> double fixedRate = 0.04;
> DayCounter floatingLegDayCounter = new Actual360();
>
> // floating leg
> Frequency floatingLegFrequency = Frequency.Semiannual;
> IborIndex euriborIndex = new Euribor6M(curve);
>
> double spread = 0.0;
>
> Integer lenghtInYears = 5;
> _VanillaSwap.Type swapType = VanillaSwap.Payer;
>
> Date maturity = settlementDate.add(new Period(lenghtInYears,
> TimeUnit.Years));
> Schedule fixedSchedule = new Schedule(settlementDate, maturity,
> new Period(fixedLegFrequency), calendar, fixedLegConvention,
> fixedLegConvention, DateGeneration.Rule.Forward, false);
>
> Schedule floatSchedule = new Schedule(settlementDate, maturity,
> new Period(floatingLegFrequency), calendar,
> floatingLegConvention, floatingLegConvention,
> DateGeneration.Rule.Forward, false);
> VanillaSwap spot5YearSwap = new VanillaSwap(swapType, nominal,
> fixedSchedule, fixedRate, fixedLegDayCounter, floatSchedule,
> euriborIndex, spread, floatingLegDayCounter);
>
> PricingEngine swapEngine = new DiscountingSwapEngine(curve);
>
> spot5YearSwap.setPricingEngine(swapEngine);
>
> System.out.println("NPV = " + spot5YearSwap.NPV());
> System.out.println("fairSpread = " + spot5YearSwap.fairSpread());
> System.out.println("fairRate = " + spot5YearSwap.fairRate());
>
> }
> }
>

--

Use every man after his desert, and who shall scape whipping?
-- Hamlet, Act II, scene II



------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: [quantlib/SWIG/Java]No outputs when pricing a swap

thysdrus
Hello Luigi,

Yes. It is the same. At last, I compiled the project using Visual Studio 2008 and the example worked fine !
So the issue seems to be due to the compiler (with Eclipse CDT, I used cygwin c++ compiler).

Thanks,
Thysdrus

Luigi Ballabio wrote
Thysdrus,
        I get a spread printed out on my machine.  Just to check, are you sure
the code you sent is the same you're running?

Luigi


On Tue, 2011-08-30 at 07:23 -0700, thysdrus wrote:
> Hi,
>
> I am trying to price a vanilla swap using quantlib mapped to java through
> SWIG wrappers.
> Unfortunately, getting the faire spread (or any other output) throws an
> exception because it is null.
>
> Can you please help solving this issue ? Hereunder the java class I am
> launching. It is adapted from the C++ examples.
>
> package org.quantlib.examples;
>
> import org.quantlib.Actual360;
> import org.quantlib.ActualActual;
> import org.quantlib.BusinessDayConvention;
> import org.quantlib.Calendar;
> import org.quantlib.Date;
> import org.quantlib.DateGeneration;
> import org.quantlib.DateVector;
> import org.quantlib.DayCounter;
> import org.quantlib.DepositRateHelper;
> import org.quantlib.DiscountingSwapEngine;
> import org.quantlib.Euribor6M;
> import org.quantlib.Frequency;
> import org.quantlib.IborIndex;
> import org.quantlib.Month;
> import org.quantlib.Period;
> import org.quantlib.PiecewiseFlatForward;
> import org.quantlib.PricingEngine;
> import org.quantlib.QuoteHandle;
> import org.quantlib.QuoteHandleVector;
> import org.quantlib.RateHelper;
> import org.quantlib.RateHelperVector;
> import org.quantlib.Schedule;
> import org.quantlib.Settings;
> import org.quantlib.SimpleQuote;
> import org.quantlib.SwapRateHelper;
> import org.quantlib.TARGET;
> import org.quantlib.Thirty360;
> import org.quantlib.TimeUnit;
> import org.quantlib.VanillaSwap;
> import org.quantlib.YieldTermStructure;
> import org.quantlib.YieldTermStructureHandle;
> import org.quantlib._VanillaSwap;
>
> public class Swap {
>
> static { // Load QuantLib
> try {
> System.loadLibrary("QuantLibCppWrapper");
> } catch (RuntimeException e) {
> e.printStackTrace();
> }
> }
>
> public static void main(String[] args) {
>
> /*********************
> *** MARKET DATA ***
> *********************/
>
> Calendar calendar = new TARGET();
> Date settlementDate = new Date(22, Month.September, 2004);
> // must be a business day
> settlementDate = calendar.adjust(settlementDate);
>
> Integer fixingDays = 2;
> Date todaysDate = calendar.advance(settlementDate, -fixingDays,
> TimeUnit.Days);
> // nothing to do with Date::todaysDate
> Settings.instance().setEvaluationDate(todaysDate);
>
> todaysDate = Settings.instance().getEvaluationDate();
> System.out.println("Today: " + todaysDate.weekday() + ", " + todaysDate
> + "\n");
>
> System.out.println("Settlement date: " + settlementDate.weekday()
> + ", " + settlementDate + "\n");
>
> // deposits
> double d1wQuote = 0.0382;
> double d1mQuote = 0.0372;
> double d3mQuote = 0.0363;
> double d6mQuote = 0.0353;
> double d9mQuote = 0.0348;
> double d1yQuote = 0.0345;
> // swaps
> double s2yQuote = 0.037125;
> double s3yQuote = 0.0398;
> double s5yQuote = 0.0443;
> double s10yQuote = 0.05165;
> double s15yQuote = 0.055175;
>
> /********************
> *** QUOTES ***
> ********************/
>
> // deposits
> SimpleQuote d1wRate = new SimpleQuote(d1wQuote);
> SimpleQuote d1mRate = new SimpleQuote(d1mQuote);
> SimpleQuote d3mRate = new SimpleQuote(d3mQuote);
> SimpleQuote d6mRate = new SimpleQuote(d6mQuote);
> SimpleQuote d9mRate = new SimpleQuote(d9mQuote);
> SimpleQuote d1yRate = new SimpleQuote(d1yQuote);
> // swaps
> SimpleQuote s2yRate = new SimpleQuote(s2yQuote);
> SimpleQuote s3yRate = new SimpleQuote(s3yQuote);
> SimpleQuote s5yRate = new SimpleQuote(s5yQuote);
> SimpleQuote s10yRate = new SimpleQuote(s10yQuote);
> SimpleQuote s15yRate = new SimpleQuote(s15yQuote);
>
> /*********************
> *** RATE HELPERS ***
> *********************/
>
> // deposits
> DayCounter depositDayCounter = new Actual360();
>
> RateHelper d1w = new DepositRateHelper(new QuoteHandle(d1wRate),
> new Period(1, TimeUnit.Weeks), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> RateHelper d1m = new DepositRateHelper(new QuoteHandle(d1mRate),
> new Period(1, TimeUnit.Months), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> RateHelper d3m = new DepositRateHelper(new QuoteHandle(d3mRate),
> new Period(3, TimeUnit.Months), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> RateHelper d6m = new DepositRateHelper(new QuoteHandle(d6mRate),
> new Period(6, TimeUnit.Months), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> RateHelper d9m = new DepositRateHelper(new QuoteHandle(d9mRate),
> new Period(9, TimeUnit.Months), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> RateHelper d1y = new DepositRateHelper(new QuoteHandle(d1yRate),
> new Period(1, TimeUnit.Years), fixingDays, calendar,
> BusinessDayConvention.ModifiedFollowing, true,
> depositDayCounter);
>
> // setup swaps
> Frequency swFixedLegFrequency = Frequency.Annual;
> BusinessDayConvention swFixedLegConvention =
> BusinessDayConvention.Unadjusted;
> DayCounter swFixedLegDayCounter = new Thirty360(
> Thirty360.Convention.European);
> IborIndex swFloatingLegIndex = new Euribor6M();
>
> RateHelper s2y = new SwapRateHelper(new QuoteHandle(s2yRate),
> new Period(2, TimeUnit.Years), calendar, swFixedLegFrequency,
> swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
>
> RateHelper s3y = new SwapRateHelper(new QuoteHandle(s3yRate),
> new Period(3, TimeUnit.Years), calendar, swFixedLegFrequency,
> swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
> RateHelper s5y = new SwapRateHelper(new QuoteHandle(s5yRate),
> new Period(5, TimeUnit.Years), calendar, swFixedLegFrequency,
> swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
> RateHelper s10y = new SwapRateHelper(new QuoteHandle(s10yRate),
> new Period(10, TimeUnit.Years), calendar, swFixedLegFrequency,
> swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
> RateHelper s15y = new SwapRateHelper(new QuoteHandle(s15yRate),
> new Period(15, TimeUnit.Years), calendar, swFixedLegFrequency,
> swFixedLegConvention, swFixedLegDayCounter, swFloatingLegIndex);
>
> /*********************
> ** CURVE BUILDING **
> *********************/
>
> // Any DayCounter would be fine.
> // ActualActual::ISDA ensures that 30 years is 30.0
> DayCounter termStructureDayCounter = new ActualActual(
> ActualActual.Convention.ISDA);
>
> double tolerance = 1.0e-15;
>
> // A depo-swap curve
> RateHelperVector depoSwapInstruments = new RateHelperVector();
> depoSwapInstruments.add(d1w);
> depoSwapInstruments.add(d1m);
> depoSwapInstruments.add(d3m);
> depoSwapInstruments.add(d6m);
> depoSwapInstruments.add(d9m);
> depoSwapInstruments.add(d1y);
> depoSwapInstruments.add(s2y);
> depoSwapInstruments.add(s3y);
> depoSwapInstruments.add(s5y);
> depoSwapInstruments.add(s10y);
> depoSwapInstruments.add(s15y);
>
> YieldTermStructure depoSwapTermStructure = new PiecewiseFlatForward(
> settlementDate, depoSwapInstruments, termStructureDayCounter,
> new QuoteHandleVector(), new DateVector(), tolerance);
>
> YieldTermStructureHandle curve = new YieldTermStructureHandle(
> depoSwapTermStructure);
>
> /*********************
> * SWAPS TO BE PRICED *
> **********************/
>
> // constant nominal 1,000,000 Euro
> double nominal = 1000000.0;
> // fixed leg
> Frequency fixedLegFrequency = Frequency.Annual;
> BusinessDayConvention fixedLegConvention =
> BusinessDayConvention.Unadjusted;
> BusinessDayConvention floatingLegConvention =
> BusinessDayConvention.ModifiedFollowing;
> DayCounter fixedLegDayCounter = new Thirty360(
> Thirty360.Convention.European);
> double fixedRate = 0.04;
> DayCounter floatingLegDayCounter = new Actual360();
>
> // floating leg
> Frequency floatingLegFrequency = Frequency.Semiannual;
> IborIndex euriborIndex = new Euribor6M(curve);
>
> double spread = 0.0;
>
> Integer lenghtInYears = 5;
> _VanillaSwap.Type swapType = VanillaSwap.Payer;
>
> Date maturity = settlementDate.add(new Period(lenghtInYears,
> TimeUnit.Years));
> Schedule fixedSchedule = new Schedule(settlementDate, maturity,
> new Period(fixedLegFrequency), calendar, fixedLegConvention,
> fixedLegConvention, DateGeneration.Rule.Forward, false);
>
> Schedule floatSchedule = new Schedule(settlementDate, maturity,
> new Period(floatingLegFrequency), calendar,
> floatingLegConvention, floatingLegConvention,
> DateGeneration.Rule.Forward, false);
> VanillaSwap spot5YearSwap = new VanillaSwap(swapType, nominal,
> fixedSchedule, fixedRate, fixedLegDayCounter, floatSchedule,
> euriborIndex, spread, floatingLegDayCounter);
>
> PricingEngine swapEngine = new DiscountingSwapEngine(curve);
>
> spot5YearSwap.setPricingEngine(swapEngine);
>
> System.out.println("NPV = " + spot5YearSwap.NPV());
> System.out.println("fairSpread = " + spot5YearSwap.fairSpread());
> System.out.println("fairRate = " + spot5YearSwap.fairRate());
>
> }
> }
>

--

Use every man after his desert, and who shall scape whipping?
-- Hamlet, Act II, scene II



------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
QuantLib-users mailing list
QuantLib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/quantlib-users