Login  Register

European option call/put

Posted by Robert Kubrick on Jul 28, 2008; 2:46pm
URL: http://quantlib.414.s1.nabble.com/European-option-call-put-tp5988.html

I wrote a simple program to print the npv for both a call and a put  
european option with same expiration/rate/strike/underlying. The  
program returns a correct value for the put option, but the npv for  
the call option is 0 and I can't get the put greeks:

// CPPSTD
#include <iostream>

// Boost
#include <boost/shared_ptr.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>

// Quantlib
#include <ql/quantlib.hpp>

using namespace std;
using namespace boost;
using namespace boost::gregorian;
using namespace QuantLib;

#include <bs.h>

int main(int argc, char* argv[])
{
   // AAPL $162.12, Sigma XXX, Strike $170, Expire August 14, risk  
free rate 3%
   date today = day_clock::local_day();
   date expiration(2008, 8, 14);
   days days_to_expiration = expiration - today;

   const double price = 162.12;
   const double timeToExp = days_to_expiration.days()/(double)365;
   const double vol = 0.2;
   const double strike = 170;
   const double rate = 0.05;

   cout << "Calculating option price for AAPL $" << price
        << ", strike $" << strike
        << ", time to expiration " << timeToExp
        << ", volatility " << vol*100 << '%'
        << ", rate " << rate*100 << '%'
        << endl;

   double call = BlackScholes('c', price, strike, timeToExp, rate, vol);
   double put = BlackScholes('p', price, strike, timeToExp, rate, vol);

   cout << "AAPL call $" << call << ", put $" << put << endl;


   // QuantLib
   try {
     cout << endl << "======== QuantLib ========" << endl;

     date btoday = day_clock::local_day();

     Calendar calendar = TARGET();
     Date today(Day(btoday.day()), Month(btoday.month().as_enum()),  
Year(btoday.year()));
     Date settlementDate(14, August, 2008);
     Settings::instance().evaluationDate() = today;
     Real underlying = 162.12;
     Real strike = 170;
     Spread divYield = 0;
     Rate rate = 0.05;
     Volatility volatility = 0.2;
     Date maturity(14, August, 2008);
     DayCounter dayCounter = Actual365Fixed();

     cout << "Maturity = "        << maturity << endl;
     cout << "Underlying price = "        << underlying << endl;
     cout << "Strike = "                  << strike << endl;
     cout << "Risk-free interest rate = " << QuantLib::io::rate(rate)  
<< endl;
     cout << "Dividend yield = " << QuantLib::io::rate(divYield) <<  
endl;
     cout << "Volatility = " << QuantLib::io::volatility(volatility)  
<< endl;

     shared_ptr<Exercise> exercise(new EuropeanExercise(maturity));
     Handle<Quote> underlyingH(shared_ptr<Quote>(new SimpleQuote
(underlying)));
     shared_ptr<StrikedTypePayoff> payoff_call(new PlainVanillaPayoff
(Option::Call, strike));
     shared_ptr<StrikedTypePayoff> payoff_put(new PlainVanillaPayoff
(Option::Put, strike));

     // Volatility term structure
     Handle<YieldTermStructure> flatTermStructure
(shared_ptr<YieldTermStructure>(new FlatForward(settlementDate,
                                                                         
                          rate,
                                                                         
                          dayCounter)));
     Handle<BlackVolTermStructure> flatVolTS
(shared_ptr<BlackVolTermStructure>(new BlackConstantVol(settlementDate,
                                                                         
                             calendar,
                                                                         
                             volatility,
                                                                         
                             dayCounter)));
     // Process
     string method = "Black-Scholes";
     shared_ptr<BlackScholesProcess> bsmProcess(new  
BlackScholesProcess(underlyingH,
                                                                         
flatTermStructure,
                                                                         
flatVolTS));
     // Options and pricing engine
     EuropeanOption eo_call(payoff_call, exercise);
     EuropeanOption eo_put(payoff_put, exercise);
     shared_ptr<PricingEngine> pe(new AnalyticEuropeanEngine
(bsmProcess));
     eo_put.setPricingEngine(pe);
     eo_call.setPricingEngine(pe);

     Size widths[] = { 10, 35, 14 };
     cout
       << setw(widths[0]) << left << "Side"
       << setw(widths[1]) << left << "Method"
       << setw(widths[2]) << left << "European"
       << endl;

     std::cout
       << setw(widths[0]) << left << Option::Call
       << setw(widths[1]) << left << method
       << fixed << setw(widths[2]) << left << eo_call.NPV()
       << endl
       << setw(widths[0]) << left << Option::Put
       << setw(widths[1]) << left << method
       << fixed << setw(widths[2]) << left << eo_put.NPV()
       << endl;

     cout << "Delta " << eo_put.delta() << endl;
     cout << "Gamma " << eo_put.gamma() << endl;
     cout << "Theta " << eo_put.theta() << endl;

   } catch( const std::exception& e ) {

     cerr << "Error: " << e.what() << endl;

   } catch( ... ) {
     cerr << "Unknown error" << endl;
     return -1;
   }
}


The output:

Calculating option price for AAPL $162.12, strike $170, time to  
expiration 0.0465753, volatility 20%, rate 5%
AAPL call $0.546167, put $8.03074

======== QuantLib ========
Maturity = August 14th, 2008
Underlying price = 162.12
Strike = 170
Risk-free interest rate = 5.000000 %
Dividend yield = 0.000000 %
Volatility = 20.000000 %
Side      Method                             European
Call      Black-Scholes                      0.000000
Put       Black-Scholes                      7.880000
Delta nan
Gamma nan
Theta 0.000000





-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users