November 2011 |
|
January 2012 |
cvs up -r B4_2_X (bla)then check in
cvs ci -m"Updates blablabla" blathen tag.
cvs tag V00-05-06-12 (bla)
#include<iostream> #include<cmath> // The cumulative normal distribution function double CND( double X ) { return 0.5 * erfc(-X * M_SQRT1_2); } // The Black-Scholes formula: computes the price of a European style option double BlackScholes(char CallPutFlag, double S, double X, double T, double r, double v) { double d1 = (log(S/X) + (r+v*v/2)*T) / (v*sqrt(T)); double d2 = d1 - v * sqrt(T); if(CallPutFlag == 'c') return S *CND(d1)-X * exp(-r*T)*CND(d2); else if(CallPutFlag == 'p') return X * exp(-r * T) * CND(-d2) - S * CND(-d1); else return -1; } int main() { // Here is an example double stock_price = 30.0; double strike = 32.0; double rate = 0.01; // annual risk-free interest rate double time = 30.0/365; // 30 days double v = 0.2; // 20% annual std::cout << "Call option price = " << BlackScholes('c', stock_price, strike, time, rate, v) << std::endl; std::cout << "Put option price = " << BlackScholes('p', stock_price, strike, time, rate, v) << std::endl; return 0; }Output:
Call option price = 0.00406919 Put option price = 1.97778
// Function to compute Greeks void Greeks(char CallPutFlag, double S, double X, double T, double r, double v, double& delta, double& gamma, double& vega, double& theta, double& rho) { double d1 = (log(S/X) + (r+v*v/2)*T) / (v*sqrt(T)); double d2 = d1 - v * sqrt(T); delta = CND(d1); if(CallPutFlag == 'p') delta -= 1.0; gamma = norm_pdf(d1) / (S * v *sqrt(T)); vega = norm_pdf(d1) * S * sqrt(T); theta = -norm_pdf(d1) * S * v/(2*sqrt(T)) - r * X * exp(-r*T) * CND(d2); if(CallPutFlag == 'p') theta = -norm_pdf(d1) * S * v/(2*sqrt(T)) + r * X * exp(-r*T) * CND(-d2); rho = X*T*exp(-r*T)*CND(d2); if(CallPutFlag == 'p') rho = -X*T*exp(-r*T)*CND(-d2); } int main() { double stock_price = 30.0; double strike = 32.0; double rate = 0.01; // annual risk-free interest rate double time = 30.0/365; // 30 days double v = 0.2; // 20% annual // Compute Greeks double delta, gamma, vega, theta, rho; Greeks('c', stock_price, strike, time, rate, v, delta, gamma, vega, theta, rho); std::cout << "Call option" << std::endl; std::cout << "Delta = " << delta << ", Gamma = " << gamma << ", Vega = " << vega << ", Theta = " << theta << ", Rho = " << rho << std::endl; Greeks('p', stock_price, strike, time, rate, v, delta, gamma, vega, theta, rho); std::cout << "Put option" << std::endl; std::cout << "Delta = " << delta << ", Gamma = " << gamma << ", Vega = " << vega << ", Theta = " << theta << ", Rho = " << rho << std::endl; return 0; }Output:
Call option Delta = 0.139499, Gamma = 0.129079, Vega = 1.90966, Theta = -2.36408, Rho = 0.334178 Put option Delta = -0.860501, Gamma = 0.129079, Vega = 1.90966, Theta = -2.04435, Rho = -2.2938