I just started learning C++ for finance with the book "Introduction to C++ for Financial Engineers: An Object-Oriented Approach" by Daniel Duffy. I got through the first two chapters after some work but the third chapter on classes has me in a bit of a twist. I understand that a class consists of member data (header file in this case) and member functions (code file in this case). The book went on to talk about constructors, modifiers, selectors, and destructors. What's pasted in the header and code files below is probably coded in a way to show these features. I'm not looking to make this code more efficient I'm looking to understand what is going on. This program is supposed to give the put/call prices and their respective deltas for European Options. I kind of understand things conceptually but the syntax and structure is what is really confusing me. When I look up definitions for things like void I can't see how they fit in with the code.
I will comment in what I think is going on. I would really appreciate some help because I am totally in the dark at this point. The comments with the question marks at the end are where I need help most.
What would also help me is a resource that gives basic layman explanations for things and syntax in C++. I'm not looking to gloss over anything in this book or in learning C++. I want to fully understand every line of code.
I can upload or email the xcode project/files if needed.
Thank you in advance and I apologize for any silly mistakes.
/////////////////class header file////////////////////
#ifndef EuropeanOption_hpp //Regular header looking stuff. Not sure exactly what it means.
#define EuropeanOption_hpp
#include <string> //Include this if you are using strings.
using namespace std; //Adding in the standard namespace library. Be careful with conflits between libraries in the future.
class EuropeanOption //Looks like we are defining a class here.
{
public: //Public members of the EuropeanOption class can be accessed from anywhere.
void init(); // What is this?
void copy(const EuropeanOption& o2); //What's going on here?
//Defining some constants here that we will do calculations later to get values for. These do not change hence the "const."
double CallPrice() const;
double PutPrice() const;
double CallDelta() const;
double PutDelta() const;
double CallGamma() const;
double PutGamma() const;
double CallVega() const;
double PutVega() const;
//Stuff for the overly used and abused normal distribution that we will calculate later.
double n(double x) const;
double N(double x) const;
//Defining some parameters. These can change, no const at the end.
double r;
double sig;
double K;
double T;
double U;
double b;
string optType; // Option name (call, put)
string unam; // Name of underlying asset
public: //Public members of the EuropeanOption class can be accessed from anywhere.
EuropeanOption(); // Default call option
EuropeanOption(const EuropeanOption& option2); //What?
EuropeanOption (const string& optionType); //Who?
virtual ~EuropeanOption(); //Where?
EuropeanOption& operator = (const EuropeanOption& option2); //No idea what this is.
// Functions that calculate option price and sensitivities
double Price() const;
double Delta() const;
void toggle(); //What?
};
#endif
///////////////class code file/////////////////
#ifndef EuropeanOption_cpp //Again, not really sure what this is but I know its important.
#define EuropeanOption_cpp
#include "EuropeanOption.hpp"
#include <math.h> //Include a math library.
#include <iostream> //Include IO stuff.
//////////// Gaussian functions /////////////////////////////////
double EuropeanOption::n(double x) const //With :: we made little n's member function part of the EuropeanOption class?
{
double A = 1.0/sqrt(2.0 * 3.1415);
return A * exp(-x*x*0.5);
}
double EuropeanOption::N(double x) const //Here we used little n to make big N's member function part of the European option class?
{ // The approximation to the cumulative normal distribution
double a1 = 0.4361836;
double a2 = -0.1201676;
double a3 = 0.9372980;
double k = 1.0/(1.0 + (0.33267 * x));
if (x >= 0.0)
{
return 1.0 - n(x)* (a1*k + (a2*k*k) + (a3*k*k*k));
}
else
{
return 1.0 - N(-x);
}
}
// Black and Scholes stock option model (1973)
double EuropeanOption::CallPrice() const //Defined callprices's function as part of the EuropeanOption class?
{
double tmp = sig * sqrt(T);
double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;
double d2 = d1 - tmp;
return (U * exp((b-r)*T) * N(d1)) - (K * exp(-r * T)* N(d2));
}
double EuropeanOption::PutPrice() const //Defined putprices's function as part of the EuropeanOption class?
{
double tmp = sig * sqrt(T);
double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;
double d2 = d1 - tmp;
return (K * exp(-r * T)* N(-d2)) - (U * exp((b-r)*T) * N(-d1));
}
double EuropeanOption::CallDelta() const
{
double tmp = sig * sqrt(T);
double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;
return exp((b-r)*T) * N(d1);
}
double EuropeanOption::PutDelta() const
{
double tmp = sig * sqrt(T);
double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;
return exp((b-r)*T) * (N(d1) - 1.0);
}
/////////////////////////////////////////////////////////////////////////////////////
void EuropeanOption::init() //What is going on here?
{
r = 0.08;
sig= 0.30;
K = 65.0;
T = 0.25;
U = 60.0;
b = r;
optType = "C"; //What?
}
void EuropeanOption::copy(const EuropeanOption& o2) //What's going on here?
{
r = o2.r;
sig = o2.sig;
K = o2.K;
T = o2.T;
U = o2.U;
b = o2.b;
optType = o2.optType;
}
EuropeanOption::EuropeanOption() //Completely lost.
{
init();
}
EuropeanOption::EuropeanOption(const EuropeanOption& o2) //??
{
copy(o2);
}
EuropeanOption::EuropeanOption (const string& optionType) //??
{
init();
optType = optionType;
if (optType == "c")
optType = "C";
}
EuropeanOption::~EuropeanOption() //I've never seen the swiggly before.
{
}
EuropeanOption& EuropeanOption::operator = (const EuropeanOption& option2) //Still lost.
{
if (this == &option2) return *this;
copy (option2);
return *this;
}
double EuropeanOption::Price() const //Looks like we are returning either a call or put price based on what was entered before??
{
if (optType == "C")
{
return CallPrice();
}
else
return PutPrice();
}
double EuropeanOption::Delta() const
{
if (optType == "C")
return CallDelta();
else
return PutDelta();
}
void EuropeanOption::toggle()
{ // Change option type (C/P, P/C)??
if (optType == "C")
optType = "P";
else
optType = "C";
}
#endif
///////////main code////////////////
#include "EuropeanOption.hpp"
#include <iostream>
//I did not comment on this section because I don't understand most of it and I think once I understand the class cpp & hpp files this will start to click. I do understand basic things here like int main(), endl, cout, cin. I also left the original author's comments in from the book.
int main()
{
EuropeanOption callOption;
cout << "Call option on a stock: " << callOption.Price() << endl;
// Put option on a stock index
EuropeanOption indexOption;
indexOption.optType = "P";
indexOption.U = 100.0;
indexOption.K = 95.0;
indexOption.T = 0.5;
indexOption.r = 0.10;
indexOption.sig = 0.20;
double q = 0.05; // Dividend yield
indexOption.b = indexOption.r - q;
cout << "Put option on an index: " << indexOption.Price() << endl;
// Call and put options on a future
EuropeanOption futureOption;
futureOption.optType = "P";
futureOption.U = 19.0;
futureOption.K = 19.0;
futureOption.T = 0.75;
futureOption.r = 0.10;
futureOption.sig = 0.28;
futureOption.b = 0.0;
cout << "Put option on a future: " << futureOption.Price() << endl;
// Now change over to a call on the option
futureOption.toggle();
cout << "Call option on a future: " << futureOption.Price() << endl;
// Call option on currency
EuropeanOption currencyOption;
currencyOption.optType = "C";
currencyOption.U = 1.56;
currencyOption.K = 1.60;
currencyOption.T = 0.5;
currencyOption.r = 0.06;
currencyOption.sig = 0.12;
double rf = 0.08; // risk-free rate of foreign currency
currencyOption.b = currencyOption.r - rf;
cout << endl << "** Other pricing examples **" << endl << endl;
cout << "Call option on a currency: " << currencyOption.Price() << endl;
//////// NOW CALCULATIONS OF SENSITIVITIES //////////////////////////////////
// Call and put options on a future: Delta and Elasticity
EuropeanOption futureOption2;
futureOption2.optType = "P";
futureOption2.U = 105.0;
futureOption2.K = 100.0;
futureOption2.T = 0.5;
futureOption2.r = 0.10;
futureOption2.sig = 0.36;
futureOption2.b = 0.0;
cout << "Delta on a put future: " << futureOption2.Delta() << endl;
// Now change over to a call on the option
futureOption2.toggle();
cout << "Delta on a call future: " << futureOption2.Delta() << endl;
// Stock Option: Gamma
EuropeanOption stockOption;
stockOption.optType = "C";
stockOption.U = 55.0;
stockOption.K = 60.0;
stockOption.T = 0.75;
stockOption.r = 0.10;
stockOption.sig = 0.30;
stockOption.b = stockOption.r;
stockOption.toggle();
// Calculating theta of a European stock index
EuropeanOption indexOption2;
indexOption2.optType = "P";
indexOption2.U = 430.0;
indexOption2.K = 405.0;
indexOption2.T = 0.0833; // One month expiration
indexOption2.r = 0.07;
indexOption2.sig = 0.20;
double divYield = 0.05; // Dividend yield, 5% per annum
indexOption2.b = indexOption2.r - divYield;
// Stock Option: Rho
EuropeanOption stockOption2;
stockOption2.optType = "C";
stockOption2.U = 72.0;
stockOption2.K = 75.0;
stockOption2.T = 1.0;
stockOption2.r = 0.09;
stockOption2.sig = 0.19;
stockOption2.b = stockOption2.r;
// Calculating Cost of Carry of a European stock index
EuropeanOption indexOption3;
indexOption3.optType = "P";
indexOption3.U = 500.0;
indexOption3.K = 490.0;
indexOption3.T = 0.222225;
indexOption3.r = 0.08;
indexOption3.sig = 0.15;
double divYield3 = 0.05; // Dividend yield, 5% per annum
indexOption3.b = indexOption3.r - divYield3 ;
return 0;
}