Skip to content

Instantly share code, notes, and snippets.

@cdfpaz
Created October 5, 2015 17:08
Show Gist options
  • Save cdfpaz/b39c37a3f60ab3ecfa88 to your computer and use it in GitHub Desktop.
Save cdfpaz/b39c37a3f60ab3ecfa88 to your computer and use it in GitHub Desktop.
Calculating the greeks
#include <stdio.h>
#include <math.h>
const double Pi = 3.14159265359;
// Standard Normal probability density function
// Normal PDF(x) = exp(-x*x/2)/{sigma * sqrt(2 * Pi) }
double Normal_PDF(const double & x) {
return (1.0/(double)pow(2 * Pi, 0.5)) * exp(-0.5 * x * x);
}
// Standard Normal cumulative distribution function
double Normal_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*Pi,0.5)))*exp(-0.5*x*x) * k_sum);
}
else {
return 1.0 - Normal_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((double)-1,j-1))*0.5*v*v)*T)/(v*(pow(T,0.5)));
}
// Calculate the Black Scholes European call option Gamma
// Parameters: (S = Current Stock Price, K = Strike Price, r = Risk-Free Rate, v = Volatility of Stock Price, T = Time to Maturity)
double BS_Call_Option_Gamma(double S, double K, double r, double v, double T) {
return Normal_PDF(d_j(1, S, K, r, v, T))/(S * v * sqrt(T));
}
// Calculate the Black Scholes European put option Gamma
// Parameters: (S = Current Stock Price, K = Strike Price, r = Risk-Free Rate, v = Volatility of Stock Price, T = Time to Maturity)
double BS_Put_Option_Gamma(double S, double K, double r, double v, double T) {
return BS_Call_Option_Gamma(S, K, r, v, T);
}
// Calculate the Black Scholes European call option Delta
// Parameters: (S = Current Stock Price, K = Strike Price, r = Risk-Free Rate, v = Volatility of Stock Price, T = Time to Maturity)
double BS_Call_Option_Delta(double S, double K, double r, double v, double T) {
return Normal_CDF(d_j(1, S, K, r, v, T));
}
// Calculate the Black Scholes European put option Delta
// Parameters: (S = Current Stock Price, K = Strike Price, r = Risk-Free Rate, v = Volatility of Stock Price, T = Time to Maturity)
double BS_Put_Option_Delta(double S, double K, double r, double v, double T) {
return Normal_CDF(d_j(1, S, K, r, v, T)) - 1;
}
// Calculate the Black Scholes European call Rho
// Parameters: (S = Current Stock Price, K = Strike Price, r = Risk-Free Rate, v = Volatility of Stock Price, T = Time to Maturity)
double BS_Call_Option_Rho(double S, double K, double r, double v, double T) {
return K * T * exp(-r * T) * Normal_CDF(d_j(2, S, K, r, v, T));
}
// Calculate the Black Scholes European put option Rho
// Parameters: (S = Current Stock Price, K = Strike Price, r = Risk-Free Rate, v = Volatility of Stock Price, T = Time to Maturity)
double BS_Put_Option_Rho(double S, double K, double r, double v, double T) {
return -T*K*exp(-r*T)*Normal_CDF(-d_j(2, S, K, r, v, T));
}
// Calculate the Black Scholes European call option Vega
// Parameters: (S = Current Stock Price, K = Strike Price, r = Risk-Free Rate, v = Volatility of Stock Price, T = Time to Maturity)
double BS_Call_Option_Vega(double S, double K, double r, double v, double T) {
return S * Normal_PDF(d_j(1, S, K, r, v, T)) * sqrt(T);
}
// Calculate the Black Scholes European put option Vega
// Parameters: (S = Current Stock Price, K = Strike Price, r = Risk-Free Rate, v = Volatility of Stock Price, T = Time to Maturity)
double BS_Put_Option_Vega(double S, double K, double r, double v, double T){
return BS_Call_Option_Vega(S, K, r, v, T);
}
// Calculate the Black Scholes European call option Theta
// Parameters: (S = Current Stock Price, K = Strike Price, r = Risk-Free Rate, v = Volatility of Stock Price, T = Time to Maturity)
double BS_Call_Option_Theta(double S, double K, double r, double v, double T) {
return -(S * Normal_PDF(d_j(1, S, K, r, v, T)) * v)/(2 * sqrt(T)) - r * K * exp(-r * T) * Normal_CDF(d_j(2, S, K, r, v, T));
}
// Calculate the Black Scholes European put option Theta
// Parameters: (S = Current Stock Price, K = Strike Price, r = Risk-Free Rate, v = Volatility of Stock Price, T = Time to Maturity)
double BS_Put_Option_Theta(double S, double K, double r, double v, double T) {
return -(S*Normal_PDF(d_j(1, S, K, r, v, T))*v)/(2*sqrt(T)) + r*K*exp(-r*T)*Normal_CDF(-d_j(2, S, K, r, v, T));
}
int main(int argc, char **argv) {
// First we create the parameter list
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
// Finally we output the parameters and prices
printf("Underlying: %f\r\n", S);
printf("Strike: %f\r\n", K);
printf("Risk-Free Rate: %f\r\n", r);
printf("Volatility: %f\r\n", v);
printf("Maturity: %f\r\n", T);
printf("\r\n");
printf("Call Delta: %f\r\n", BS_Call_Option_Delta(S, K, r, v, T));
printf("Call Gamma: %f\r\n", BS_Call_Option_Gamma(S, K, r, v, T));
printf("Call Vega: %f\r\n", BS_Call_Option_Vega(S, K, r, v, T));
printf("Call Theta: %f\r\n", BS_Call_Option_Theta(S, K, r, v, T));
printf("Call Rho: %f\r\n", BS_Call_Option_Rho(S, K, r, v, T));
printf("\r\n");
printf("Put Delta: %f\r\n", BS_Put_Option_Delta(S, K, r, v, T));
printf("Put Gamma: %f\r\n", BS_Put_Option_Gamma(S, K, r, v, T));
printf("Put Vega: %f\r\n", BS_Put_Option_Vega(S, K, r, v, T));
printf("Put Theta: %f\r\n", BS_Put_Option_Theta(S, K, r, v, T));
printf("Put Rho: %f\r\n", BS_Put_Option_Rho(S, K, r, v, T));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment