PDA

View Full Version : Black Scholes Calculator


beginner123
Mar 31, 2009, 09:26 AM
Hi,

My professor gave an assignment whereby we have to create a Black Scholes option pricing calculator to give the call and put premium. He never gave us the formula, but stated we should search online. He also stated that we can use any source as long as we document it. I found a website which gives the code, but I'm having some difficulty deciphering it because it will not work as is. I am new to programming and would like some assistance please.

Here is a link to the website which has the formula and the code:
Espen Haug (http://www.espenhaug.com/black_scholes.html)

There are more websites out there but I can't seem to find out what I'm doing wrong. This is what I have so far:


//Code to calculate date difference

System::TimeSpan daysdiff;
double yearsdiff;
daysdiff = this->dtpExpirationDate->Value -
this->dtpCurrentDate->Value;
yearsdiff = ((double) daysdiff.Days / 365.0);
tbYearsDifference->Text = yearsdiff.ToString(".00");

if (this->dtpExpirationDate->Value < this->dtpCurrentDate->Value) {
MessageBox::Show("Expiration Date needs to be later than
Current Date!");
}


// Code to calculate Call Premium

double stock, strike, risk, volatility, call, sqrt;
double d1, d2;
double log;
stock = Convert::ToDouble(tbStockPrice->Text);
strike = Convert::ToDouble(tbStrikePrice->Text);
yearsdiff = Convert::ToDouble(tbYearsDifference->Text);
risk = Convert::ToDouble(tbRiskFreeRate->Text);
volatility = Convert::ToDouble(tbVolatility->Text);
d1 = (log(stock / strike) + (risk + (volatility *volatility) / 2) * yearsdiff)
/ (volatility * sqrt(yearsdiff));
d2 = d1 - volatility * sqrt(yearsdiff);
call = stock * CND(d1) - strike * exp(-risk * yearsdiff) * CND(d2);
tbCallPremium->Text = Convert::ToString(call);
tbCallPremium->Text = call.ToString(".00");

//Code for cumulative normal distribution

double CND (double strike), L, K, w, pow;
double sqrt;
double const a1 = 0.31938153, a2 = -0.356563782, a3 = 1.781477937,
a4 = -1.821255978, a5 = 1.330274429;
L = fabs(strike);
K = 1.0 / (1.0 + 0.2316419 * L);
w = 1.0 - 1.0 / sqrt(2 * Pi) * exp(-L *L / 2) * (a1 * K + a2 * K *K + a3 *
pow(K,3) + a4 * pow(K,4) + a5 * pow(K,5));

We have to use a date time picker to calculate the date, that's why it's included.

I've made a couple changes, but I'm sure you can find tons of mistakes.

PLEASE HELP :)

Thank You.

Perito
Apr 2, 2009, 05:20 AM
I think the first thing to do, before generating code, is to figure out how to do the calculation manually. Until you understand it at that level, there's no way that you can generate code to do it.

You're probably not going to find someone who is willing to figure that out for you. If there's a specific problem that you're having, and you know how to do it manually, post and I'll help you out.