Re: Java wrapper test program using Eclipse 3.1.1 -- BS European Option Sample Java Code]

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: Java wrapper test program using Eclipse 3.1.1 -- BS European Option Sample Java Code]

jerryji
"Just think it could benefit someone" --

Dear Johan,

It is my great pleasure to be of any help to you, no matter how small it
is. I thank you for
taking the words from a novice like me seriously.

As for the sample Java program, I cracked my head today to arrive at the
following simple
BS analytical European call option code translated from (a stripped-down
version of) QuantLib
site's European Option example --

package org.quantlib.examples;

import org.quantlib.*;

public class EuropeanOption {

    /**
     * @param args
     */
    public static void main(String[] args) {
        double underlying = 7.00, strike = 8.00;
        double dividendYield = 0.05;
        double riskFreeRate = 0.05;
        double volatility = 0.10;
       
        // TODO Auto-generated method stub
        try {          
            System.loadLibrary("QuantLibCppWrapper");
            System.out.println("QuantLibCppWrapper loaded");
           
            Option.Type type = Option.Type.Call;
            Date todaysDate = new Date(15, Month.May, 1998);
            Date settlementDate = new Date(17, Month.May, 1998);
            Date exerciseDate = new Date(17, Month.May, 1999);
            Settings.instance().setEvaluationDate(todaysDate);
            DayCounter dayCounter = new Actual365Fixed();
           
            EuropeanExercise exercise = new EuropeanExercise(exerciseDate);
            QuoteHandle underlyingH =
                new QuoteHandle(new SimpleQuote(underlying));
            YieldTermStructureHandle flatTermStructure =
                new YieldTermStructureHandle(
                    new FlatForward(settlementDate, riskFreeRate,
dayCounter));
            YieldTermStructureHandle flatDividendTS =
                new YieldTermStructureHandle(
                    new FlatForward(settlementDate, dividendYield,
dayCounter));
            BlackVolTermStructureHandle flatVolTS =
                new BlackVolTermStructureHandle(
                    new BlackConstantVol(settlementDate, volatility,
dayCounter));
            PlainVanillaPayoff payoff = new PlainVanillaPayoff(type,
strike);
            BlackScholesProcess stochasticProcess =
                new BlackScholesProcess(underlyingH, flatDividendTS,
                    flatTermStructure, flatVolTS);
            EuropeanOption option =
                new EuropeanOption(stochasticProcess, payoff, exercise);
            option.setPricingEngine(new AnalyticEuropeanEngine());
            System.out.println("Value: " + option.NPV());
        } catch (Throwable t) {
            System.out.println("QuantLibCppWrapper load failed");
            t.printStackTrace();
        }
    }
}

And its output is --

QuantLibCppWrapper loaded
Value: 0.030023778624485974

BTW, credit has to be given to Ken for his recent post in this mailing
list last month --

From: Ken Anderson <lists <at> anderhome.com>
Subject: Problem pricing options in the past?
<http://news.gmane.org/find-root.php?message_id=%3c03CD3808%2d9914%2d4C13%2d8BB8%2d1B81FB190BEA%40anderhome.com%3e>
Newsgroups: gmane.comp.finance.quantlib.user
<http://news.gmane.org/gmane.comp.finance.quantlib.user>
Date: 2006-02-17 19:53:58 GMT (5 weeks, 20 hours and 18 minutes ago)
I'm using the SWIG for Java of the latest download, and I have a problem
with pricing options in the past.  If I use current or future dates, it
works fine, but the value is always zero in the past.  Here's the code:

Date todaysDate = new Date(17,Month.February, 2006);
Date settlementDate = new Date(21, Month.February, 2006);
Date exerciseDate = new Date(24, Month.May, 2006);

DayCounter fixed365 = new Actual365Fixed();
EuropeanExercise exercise = new EuropeanExercise(exerciseDate);
PlainVanillaPayoff payoff = new PlainVanillaPayoff(Option.Type.Call, 58.0);
SimpleQuote underlyingQuote = new SimpleQuote(60.0);
FlatForward flatDividentTS = new FlatForward(settlementDate, 0.0,
fixed365);
FlatForward flatTermStructure = new FlatForward(settlementDate, .06,
fixed365);
BlackConstantVol flatVolTS = new BlackConstantVol(settlementDate, .20,
fixed365);

BlackScholesProcess process = new BlackScholesProcess(new
QuoteHandle(underlyingQuote),
new YieldTermStructureHandle(flatDividentTS),
new YieldTermStructureHandle(flatTermStructure),
new BlackVolTermStructureHandle(flatVolTS));

VanillaOption euroOption = new VanillaOption(process, payoff, exercise,
new AnalyticEuropeanEngine());

System.out.println("Value: "+euroOption.NPV());
System.out.println("Delta: "+euroOption.delta());

The code above works OK, but if you change the dates to be 2005, it
fails with 0.0 NPV and 0.0 delta.  Any ideas?

Thanks,
Ken

And Lugi's reply --

> Ken,
> just declaring a variable called todaysDate to be in the past does  
> not change QuantLib's today's date. You have to set it with a line  
> that in C++ would be
>
> Settings::instance().evaluationDate() = todaysDate;
>
> I'm not sure how that would spell in Java, though. It's probably  
> something like
>
> Settings.instance().setEvaluationDate(todaysDate);
>
> Hope this helps,
> Luigi

for enlightening me on some key understandings. Just that I believe my
code represents a
more literal line-to-line/variable-to-variable translation of the
European Option C++ example
code.

Nice weekend.

--
Jerry

Johan Witters wrote:

> I've updated my website.
>  
> Jerry,
>  
> many thanks you for helping me!
>  
> I don't have a simple European or American option java test code. C++
> examples are available at
> http://www.quantlib.org/reference/examples.html. You could translate
> one of these to java. If you do (or someone else), please send me a
> copy so that I can put it on my website.
>  
> cheers,
>  
> Johan