Created
August 6, 2015 03:16
-
-
Save mtmoses/1830cb6573ba1e2cd4b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The Greeks Calculator for European vanilla options | |
//by Todd Moses | |
#include <iostream> | |
#include <cmath> | |
//standard normal probability density function | |
double PDF(const double d) { | |
return (1.0/(pow(2*M_PI,0.5)))*exp(-0.5*d*d); | |
} | |
//cumulative normal distribution function | |
static double CDF(const double d) | |
{ | |
const double A1 = 0.31938153; | |
const double A2 = -0.356563782; | |
const double A3 = 1.781477937; | |
const double A4 = -1.821255978; | |
const double A5 = 1.330274429; | |
const double RSQRT2PI = 0.39894228040143267793994605993438; | |
double K = 1.0 / (1.0 + 0.2316419 * fabs(d)); | |
double cnd = RSQRT2PI * exp(- 0.5 * d * d) * (K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5))))); | |
if (d > 0) { | |
cnd = 1.0 - cnd; | |
} | |
return cnd; | |
} | |
// 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 Delta | |
// option price S, strike price K, risk-free rate r, volatility of underlying v, and time to maturity T | |
double call_delta(const double S, const double K, const double r, const double v, const double T) | |
{ | |
return CDF(d_j(1, S, K, r, v, T)); | |
} | |
//calculate the European vanilla call Gamma | |
// option price S, strike price K, risk-free rate r, volatility of underlying v, and time to maturity T | |
double call_gamma(const double S, const double K, const double r, const double v, const double T) | |
{ | |
return PDF(d_j(1, S, K, r, v, T))/(S*v*sqrt(T)); | |
} | |
//calculate the European vanilla call Vega | |
// option price S, strike price K, risk-free rate r, volatility of underlying v, and time to maturity T | |
double call_vega(const double S, const double K, const double r, const double v, const double T) | |
{ | |
return S*PDF(d_j(1, S, K, r, v, T))*sqrt(T); | |
} | |
//calculate the European vanilla call Theta | |
// option price S, strike price K, risk-free rate r, volatility of underlying v, and time to maturity T | |
double call_theta(const double S, const double K, const double r, const double v, const double T) | |
{ | |
return -(S*PDF(d_j(1, S, K, r, v, T))*v)/(2*sqrt(T)) - r*K*exp(-r*T)*CDF(d_j(2, S, K, r, v, T)); | |
} | |
//calculate the European vanilla call Rho | |
// option price S, strike price K, risk-free rate r, volatility of underlying v, and time to maturity T | |
double call_rho(const double S, const double K, const double r, const double v, const double T) | |
{ | |
return K*T*exp(-r*T)*CDF(d_j(2, S, K, r, v, T)); | |
} | |
//calculate the European vanilla put Delta | |
// option price S, strike price K, risk-free rate r, volatility of underlying v, and time to maturity T | |
double put_delta(const double S, const double K, const double r, const double v, const double T) | |
{ | |
return CDF(d_j(1, S, K, r, v, T)) - 1; | |
} | |
//calculate the European vanilla put Gamma | |
// option price S, strike price K, risk-free rate r, volatility of underlying v, and time to maturity T | |
double put_gamma(const double S, const double K, const double r, const double v, const double T) | |
{ | |
return call_gamma(S, K, r, v, T); // Identical to call by put-call parity | |
} | |
//calculate the European vanilla put Vega | |
// option price S, strike price K, risk-free rate r, volatility of underlying v, and time to maturity T | |
double put_vega(const double S, const double K, const double r, const double v, const double T) | |
{ | |
return call_vega(S, K, r, v, T); // Identical to call by put-call parity | |
} | |
//calculate the European vanilla put Theta | |
// option price S, strike price K, risk-free rate r, volatility of underlying v, and time to maturity T | |
double put_theta(const double S, const double K, const double r, const double v, const double T) | |
{ | |
return -(S*PDF(d_j(1, S, K, r, v, T))*v)/(2*sqrt(T)) + r*K*exp(-r*T)*CDF(-d_j(2, S, K, r, v, T)); | |
} | |
// calculate the European vanilla put Rho | |
// option price S, strike price K, risk-free rate r, volatility of underlying v, and time to maturity T | |
double put_rho(const double S, const double K, const double r, const double v, const double T) | |
{ | |
return -T*K*exp(-r*T)*CDF(-d_j(2, S, K, r, v, T)); | |
} | |
//main for testing | |
int main() | |
{ | |
std::cout << "The Greeks Calculator" << std::endl << std::endl; | |
double S = 100.0; // Option price | |
double K = 100.0; // Strike price | |
double r = 0.05; // Risk-free rate (5%) | |
double v = 0.2; // Volatility of the underlying (20%) | |
double T = 1.0; // One year until expiry | |
double call_delta_v = call_delta(S, K, r, v, T); | |
double call_gamma_v = call_gamma(S, K, r, v, T); | |
double call_vega_v = call_vega(S, K, r, v, T); | |
double call_theta_v = call_theta(S, K, r, v, T); | |
double call_rho_v = call_rho(S, K, r, v, T); | |
double put_delta_v = put_delta(S, K, r, v, T); | |
double put_gamma_v = put_gamma(S, K, r, v, T); | |
double put_vega_v = put_vega(S, K, r, v, T); | |
double put_theta_v = put_theta(S, K, r, v, T); | |
double put_rho_v = put_rho(S, K, r, v, T); | |
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::endl; | |
std::cout << "Call Delta: " << call_delta_v << std::endl; | |
std::cout << "Call Gamma: " << call_gamma_v << std::endl; | |
std::cout << "Call Vega: " << call_vega_v << std::endl; | |
std::cout << "Call Theta: " << call_theta_v << std::endl; | |
std::cout << "Call Rho: " << call_rho_v << std::endl << std::endl; | |
std::cout << "Put Delta: " << put_delta_v << std::endl; | |
std::cout << "Put Gamma: " << put_gamma_v << std::endl; | |
std::cout << "Put Vega: " << put_vega_v << std::endl; | |
std::cout << "Put Theta: " << put_theta_v << std::endl; | |
std::cout << "Put Rho: " << put_rho_v << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment