#### Implementing a European Option class

I need implement the class Option_Pricer that encapsulates all the functions relevant to price both call and put options. The teacher is giving me a code listing (.cpp file) that I have to turn into a class.

Here is the original code:

``````#define _USE_MATH_DEFINES
#include <iostream> #include <cmath>

// Standard normal probability density function
double norm_pdf(const double& x) {
return (1.0/(pow(2*M_PI,0.5)))*exp(-0.5*x*x); }

// An approximation to the cumulative distribution function
// for the standard normal distribution
// Note: This is a recursive function
double norm_cdf(const double& x) {
double k = 1.0/(1.0 + 0.2316419*x);
double k_sum = k*(0.319381530 + k*(-0.356563782 + k*(1.781477937 + k*(-1.821255978 + 1.330274429*k))));
if (x >= 0.0) {
return (1.0 - (1.0/(pow(2*M_PI,0.5)))*exp(-0.5*x*x) * k_sum);
}
else {
return 1.0 - norm_cdf(-x);
}
}

// This calculates d_j, for j in {1,2}.
//This term appears in the closed form solution for the European call or put price

double d_j(const int& j, const double& S, const double& K, const double& r, const double& v, const double& T) {
return (log(S/K) + (r + (pow(-1,j-1))*0.5*v*v)*T)/(v*(pow(T,0.5))); }

// Calculate the European vanilla call price based on underlying S, strike K, risk-free rate r, volatility ofunderlying sigma and time to maturity T

double call_price(const double& S, const double& K, const double& r, const double& v, const double& T) {
return S * norm_cdf(d_j(1, S, K, r, v, T))-K*exp(-r*T) * norm_cdf(d_j(2, S, K, r, v, T)); }

// Calculate the European vanilla put price based on underlying S, strike K, risk-free rate r, volatility of underlying sigma and time to maturity T

double put_price(const double& S, const double& K, const double& r, const double& v, const double& T) {
return -S*norm_cdf(-d_j(1, S, K, r, v, T))+K*exp(-r*T) * norm_cdf(-d_j(2, S, K, r, v, T)); }

// First we create the parameter list
S = 100.0; // Option price
K = 100.0; // Strike price
r = 0.05; // Risk-free rate (5%)
v = 0.2; // Volatility of the underlying (20%)
T = 1.0; // One year until expiry

int main(int argc, char **argv) {
// Then we calculate the call/put values
Option_Pricer* option_call = new Option_Pricer.call_price(S, K, r, v, T);
Option_Price* option_put = new Option_Pricer.put_price(S, K, r, v, T);

// Finally we output the parameters and prices
std::cout << "Underlying: " << S << std::endl;
std::cout << "Strike: " << K << std::endl;
std::cout << "Risk-Free Rate: " << r << std::endl;
std::cout << "Volatility: "<< v << std::endl;
std::cout << "Maturity: " << T << std::endl;
std::cout << "Call price: " << option_call << std::endl;
std::cout << "Put price: " << option_put << std::endl;

return 0;

}
``````

Here is what I have done so far: I have split the code into two different files. One is called option_pricer.hpp and is used as an header for the main file option_pricer.cpp.

``````//option_pricer.hpp
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>

class Option_Pricer {

private:

void init();

public:

double S;
double K;
double r;
double v;
double T;
double x;
double j;

public:
//Constructors
double call_price() const;
double put_price() const;
double norm_pdf() const;
double norm_cdf() const;
double d_j() const;

// Assignment operator
call_price& operator = (const call_price& call);
put_price& operator = (const put_price& put);

};
``````

Here is the main file:

``````//option_pricer.cpp
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include "option_pricer.hpp"

double Option_Pricer::norm_pdf() const {
return (1.0/(pow(2*M_PI,0.5)))*exp(-0.5*x*x);
}

double Option_Pricer::norm_cdf() const {
double k = 1.0/(1.0 + 0.2316419*x);
double k_sum = k*(0.319381530 + k*(-0.356563782 + k*(1.781477937 + k*(-1.821255978 + 1.330274429*k))));

if (x >= 0.0) {
return (1.0 -(1.0/(pow(2*M_PI,0.5)))*exp(-0.5*x*x) * k_sum);
}
else {
return 1.0 - norm_cdf(-x);
}
}

double Option_Pricer::d_j() const {
return (log(S/K) + (r + (pow(-1,j 1))*0.5*v*v)*T)/(v*(pow(T,0.5)));
}

double Option_Pricer::call_price() const {
return S * norm_cdf(d_j(1, S, K, r, v, T))-K*exp(-r*T) * norm_cdf(d_j(2, S, K, r, v, T));
}

double Option_Pricer::put_price() const {
return -S*norm_cdf(-d_j(1, S, K, r, v, T))+K*exp(-r*T) * norm_cdf(-d_j(2, S, K, r, v, T));

}

int main() {

// First we create the parameter list
Option_Pricer.S = 100.0; // Option price
Option_Pricer.K = 100.0; // Strike price
Option_Pricer.r = 0.05; // Risk-free rate (5%)
Option_Pricer.v = 0.2; // Volatility of the underlying (20%)
Option_Pricer.T = 1.0; // One year until expiry

// Then we calculate the call/put values
double option_call = Option_Pricer.call_price();
double option_put = Option_Pricer.put_price();

// Finally we output the parameters and prices
std::cout << "Underlying: " << Option_Pricer.S << std::endl;
std::cout << "Strike: " << Option_Pricer.K << std::endl;
std::cout << "Risk-Free Rate: " << Option_Pricer.r << std::endl;
std::cout << "Volatility: "<< Option_Pricer.v << std::endl;
std::cout << "Maturity: " << Option_Pricer.T << std::endl;
std::cout << "Call price: " << option_call << std::endl;
std::cout << "Put price: " << option_put << std::endl;

return 0;

}
``````

However, as you can guess, my code isn’t compiling really well. I have much trouble to understand where to define my parameters (S, K, T…): should I put a void init in the hpp file? Also, I’m not sure if I implemented the functions norm_pdf, norm_cdf and d_j well before computing the call and put price. I’m fairly new to C++ (was using Python before) and therefore struggling a lot.

Help will be appreciated! Thank you!

Source: Windows Questions C++