does anyone use java+swig bindings?

Posted by Tito Ingargiola on
URL: http://quantlib.414.s1.nabble.com/does-anyone-use-java-swig-bindings-tp9752.html


Hi,

I've been playing with QuantLib for the last month or so and have more
or less fallen in love.  In c++, everything I've tried works pretty
much as expected.

Unfortunately, I have an extensive gui written in java and when I try
to bridge into QuantLib via the SWIG interfaces I've run into endless
difficulties.  Given the lack of traffic on this list for java users
except the odd "I tried but it didn't work..." post, I wonder if
anyone is actually using the SWIG interfaces for java integration?  If
so, could anyone kindly point me to a non trivial example of java
accessing QuantLib functionality?  The sample provided with the swig
extension is laughably simple and doesn't demonstrate any kind of
parameter passing.

I'm frustrated at this point as things which are simple to do in c++
just don't work via the swig interfaces and I'm getting close to
dumping the swig interfaces and writing my own JNI interfaces for the
relatively limited areas of functionality I'm interested in, but I'd
prefer not to do this if a reasonable alternative exists.  

In order to illustrate the kinds of difficulties I'm running into, I
enclose below a simple sample program based on
Quantlib/Examples/EquityOption which uses the SWIG interfaces and
compiles fine but will crash in several different places when run.
Even such a simple thing as Dates don't seem to work.  That is:


       Date goodDate = Date.todaysDate(); // ok
       goodDate.add(17); // ok
// boom when date is referenced
       Date badDate = new Date( 1, Month.October, 2007);

I have similar experiences with simple objects like quotes or
handles... which is why I question that anyone is actually using these
things.

Any insights, pointers or suggestions will be very much appreciated!
Thanks and regards,

    Tito.

--- the java program I mentioned follows ---




package test;

import org.quantlib.Actual365Fixed;
import org.quantlib.AmericanExercise;
import org.quantlib.BaroneAdesiWhaleyEngine;
import org.quantlib.BlackConstantVol;
import org.quantlib.BlackScholesMertonProcess;
import org.quantlib.BlackVolTermStructureHandle;
import org.quantlib.Date;
import org.quantlib.DayCounter;
import org.quantlib.FlatForward;
import org.quantlib.Month;
import org.quantlib.Option;
import org.quantlib.Payoff;
import org.quantlib.PlainVanillaPayoff;
import org.quantlib.QuoteHandle;
import org.quantlib.SimpleQuote;
import org.quantlib.StochasticProcess;
import org.quantlib.VanillaOption;
import org.quantlib.YieldTermStructureHandle;

/**
 * Test app - simplified version of QuantLib/Examples/EquityOption to test
 * use of Quantlib through supplied SWIG interface...
 *
 * @author Tito Ingargiola
 */
public class EquityOption {
    static {
        /* You need to run this thing with something like:
         * -Djava.library.path=/usr/local/lib
         */
        System.loadLibrary("QuantLib-0.8.1");
        System.loadLibrary("QuantLibJNI");
    }
    
    public static void main(String[] args) throws Exception {
        System.out.println("starting...");
        //QuantLib ql = new QuantLib();
        //Thread.sleep(500);

        Option.Type type = Option.Type.Put;
        double strike = 14.7;
        double underlying = 14.76;
        double riskFreeRate = 0.055;
        double divYield = riskFreeRate; // for futures
        double volatility = .22;
        System.out.println("creating dates...");
        Date settle = Date.todaysDate();        // Explicitly setting date
        Date expiry = Date.todaysDate().add(13);//  causes crash...on use(?)
        Date expiry2 = new Date( 20, Month.September, 2007 ); // e.g.
        System.out.println(settle+" -> "+expiry);
        
        AmericanExercise exercise = new AmericanExercise(settle, expiry,true);
        // using the below will cause crash
       // AmericanExercise exercise2 = new AmericanExercise(settle, expiry2,true);
        
        // this is fine ... unless you use it...
        DayCounter dayc = new Actual365Fixed();
        // this is fine ... unless you use it...
        SimpleQuote under = new SimpleQuote(underlying);
        // this instead blows up on construction...
        QuoteHandle underh = new QuoteHandle(under);
        
        /// the rest is pie-in-the-sky stuff...
        
        YieldTermStructureHandle  flatTS = new YieldTermStructureHandle
            (new FlatForward(settle,riskFreeRate, dayc));
        
        YieldTermStructureHandle flatDivTS = new YieldTermStructureHandle
            (new FlatForward(settle,divYield, dayc));

        BlackVolTermStructureHandle flatVolTS = new BlackVolTermStructureHandle
            (new BlackConstantVol(settle,volatility,dayc));
        
        
        StochasticProcess stochp = new BlackScholesMertonProcess
            (underh,flatDivTS,flatTS,flatVolTS);
        Payoff payoff = new PlainVanillaPayoff(type, strike) ;

        VanillaOption option = new VanillaOption
            (stochp, payoff,exercise);
        
        option.setPricingEngine(new BaroneAdesiWhaleyEngine());
        
        System.out.println("Option:   \t"+option);
        System.out.println("NPV:      \t"+option.NPV());
        
        System.out.println("\ndone.");
        
    }
}



    


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev