next up previous contents
Next: About this document ... Up: Code Previous: quotes.phtml   Contents

opm.cpp

#include <fstream.h> #include <math.h> #include <stdlib.h> #include <iomanip.h> #include <string.h>

#define pi 3.141592653

class option public: //**FUNCTIONS**//

void DisplayInfo(); void defineOption(ifstream myFile); void BlackScholes(); float nd(float d1); void sdeviation(ifstream myFile);

//**IMPORTANT VARIABLES**//

char name[5]; //ticker symbol float stock; //stock price float strike; //strike price float expiration; //years until expiration float rate; //interest rate float volat; //volatility

//**AUXILLARY VARIABLES**// float mean; //mean of stock data float sd; //sd of stock data float BSprice; //black-scholes option price ;

void option::DisplayInfo() cout«"Stock Name: "«name«endl; cout«"Stock Price: "«stock«endl; cout«"Expiration Date: "«expiration«endl; cout«"Interest Rate: "«rate«endl; cout«"Volatility: "«volat«endl; cout«"Black-Scholes Price: "«BSprice«endl;

void option::defineOption(ifstream myFile) myFile » name » stock » strike » expiration » rate » volat;

void option::BlackScholes() float d1 = (log(stock / strike) + (rate + pow(volat,2) / 2) * expiration)/(volat*sqrt(expiration)); float d2 = d1 - volat * sqrt(expiration); //cout«d1«" "«d2«endl; BSprice = stock * nd(d1) - strike * exp( -1.0 * rate * expiration) * nd(d2);

float option::nd(float d1) //the standard normal cumulative distribution function float score; //loop variable (z-score) float CDF = 0.0; //value of the standard normal cumulative distribution function for(float score=-3.0; score<d1; score+=.00001) //integral, -3 to z-score of normal curve CDF += .00001* 1/sqrt(2*pi*1) * exp(-1*(score)*(score)/(2*1*1)); //sd = 1 return CDF + .0013499672; //intergral from -infinity to -3 = .015

void option::sdeviation(ifstream myFile) //standard deviation of the 30-day stock data int x; //for loop variable float sum_of_array = 0.0; //sum of 30-day stock data float sum_of_residuals = 0.0; //sum of residuals for sd calculation float price_array[30]; //array of stock data mean = 0.0; //mean, for sd calculation for(x=0; x<30;x++) myFile » price_array[x]; sum_of_array += price_array[x]; mean = sum_of_array / 30.0; for (x=0; x<30; x++) float difference = mean - price_array[x]; if (difference < 0) //absolute value difference = difference * -1.0; sum_of_residuals += difference * difference; //add residual^2 sd = sqrt(sum_of_residuals / 30.0); volat = sd/stock; //volatility = standard deviation/stock price

int main() option a; cout « "Input values: "; cin » a.stock » a.strike » a.expiration » a.rate » a.volat; a.BlackScholes(); //calculates the Black-Scholes option value cout «"Option Price: "« a.BSprice « endl; return 0;


next up previous contents
Next: About this document ... Up: Code Previous: quotes.phtml   Contents
Charles Vu 2003-06-12