Re: Example of a simple option with Quantlib ?

Posted by Fred Nastos on
URL: http://quantlib.414.s1.nabble.com/Example-of-a-simple-option-with-Quantlib-tp4400p4401.html

On Monday 23 January 2006 12:13, gilles herzog wrote:
> I just want to price a simple option with quantlib
> I know the maturity, the strike, s0, Sigma and r and I want to use Black
> and scholes.
> It must be very easy to do that with Quantlib but the example
> europeanOption.cpp causes a lot of problems to me.
> Could someone give me a simple example ??

Gilles,

In case it helps, here is the EuropeanOption.cpp example stripped to
its most basics:

#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.05;
  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;
}


> Thank you very much
>
> NB: in EuropeanOption, what means
> return std::exp(-r_*maturity_)
>                *PlainVanillaPayoff(type_, strike_)(s0_*std::exp(x))
>                *std::exp(-(x - nuT)*(x -nuT)/(2*sigma_*sigma_*maturity_))
>                /std::sqrt(2.0*M_PI*sigma_*sigma_*maturity_);
> in the operator().
> Why a PI ? why multiply the payoff with a exp ?