Posted by
L.Isella on
URL: http://quantlib.414.s1.nabble.com/Simple-Code-tp4403p4414.html
On Tue, 2006-01-24 at 15:10 +0100, Luigi Ballabio wrote:
On 1/24/06, L.Isella <
[hidden email]> wrote:
> > If I understand correctly, the C++ code in the email prices plain vanilla European call options.
> > It uses the standard Black and Scholes theory for a dividend-paying stock.
> > No stochastic volatility, flat term structure and so on.
> > I am testing the results against another library (
http://finance.bi.no/~bernt/gcc_prog/).
> > I am not totally sure whether the dividends in the example are paid continuosly or not, but if I set the yield to zero, then I am in the case of a non-dividend paying stock.
> > But the output of the code in the email (I simply reset the value of the yield and print out the relevant quantities) is a bit puzzling to me:
> >
> > Black-Scholes value: 0
> >
> > whereas the other library provides a small value (about 0.08) but definitely non-zero.
> > Am I misunderstanding?
>
> No, but you might be doing something wrong. If I compile and run the
> code in the email after setting the dividendYield variable to 0.0, I
> get for the option a Black-Scholes price of 0.082. You might want to
> check your program or post it here to see what's the problem.
>
> Later,
> Luigi
>
Sorry for the previous mail I sent by mistake.
The code I am using is the following (taken from one of the previous emails):
#include <ql/quantlib.hpp>
#include <iostream>
using namespace QuantLib;
int main()
{
Option::Type type(Option::Call);
Real underlying = 7.00, strike = 8.00;
Spread dividendYield = 0.0; //only part of the code I modified
Rate riskFreeRate = 0.05;
Volatility volatility = 0.10;
Date todaysDate(15, May, 1998);
Date settlementDate(17, May, 1998), exerciseDate(17, May, 1999);
Settings::instance().evaluationDate() = todaysDate;
DayCounter dayCounter = Actual365Fixed();
Time maturity = dayCounter.yearFraction(settlementDate,exerciseDate);
boost::shared_ptr<Exercise> exercise(new EuropeanExercise(exerciseDate));
Handle<Quote> underlyingH(boost::shared_ptr<Quote>(new
SimpleQuote(underlying)));
Handle<YieldTermStructure>
flatTermStructure(boost::shared_ptr<YieldTermStructure>(
new FlatForward(settlementDate, riskFreeRate, dayCounter)));
Handle<YieldTermStructure>
flatDividendTS(boost::shared_ptr<YieldTermStructure>(
new FlatForward(settlementDate, dividendYield, dayCounter)));
Handle<BlackVolTermStructure>
flatVolTS(boost::shared_ptr<BlackVolTermStructure>(
new BlackConstantVol(settlementDate, volatility, dayCounter)));
boost::shared_ptr<StrikedTypePayoff> payoff(
new PlainVanillaPayoff(type, strike));
boost::shared_ptr<BlackScholesProcess> stochasticProcess(
new BlackScholesProcess(underlyingH, flatDividendTS,
flatTermStructure, flatVolTS));
EuropeanOption option(stochasticProcess, payoff, exercise);
option.setPricingEngine(boost::shared_ptr<PricingEngine>(
new AnalyticEuropeanEngine()));
std::cout << "Black-Scholes value: " << option.NPV() << std::endl;
return 0;
}
I then save it in a file (simple-option.cc) and compile & run it:
lorenzo@mypc:~/Temp$ g++ -o simple simple-option.cc -lQuantLib
lorenzo@mypc:~/Temp$ ./simple
Black-Scholes value: 0
However, If I use the same parameters inside the AmericanOption example, then I get the "right" value.
It is again something trivial, 100% sure, but I copied the code from the mail!
Lorenzo