I am attempting to build a basic windows forms application that takes several parameters and calculates a price when the button is clicked. I am using the TryParse method to convert the input strings into doubles and implemented some functions to calculate the price of the option but I keep getting an error that says the following:
"1>c:\users\rodrigo\documents\visual studio 2010\projects\optionscalc\optionscalc\Form1.h(271): error C2082: redefinition of formal parameter 'e'
1>c:\users\rodrigo\documents\visual studio 2010\projects\optionscalc\optionscalc\Form1.h(297): error C2601: 'callPrice' : local function definitions are illegal
1>c:\users\rodrigo\documents\visual studio 2010\projects\optionscalc\optionscalc\Form1.h(303): error C2601: 'N' : local function definitions are illegal
1>c:\users\rodrigo\documents\visual studio 2010\projects\optionscalc\optionscalc\Form1.h(325): error C2601: 'd1' : local function definitions are illegal
1>c:\users\rodrigo\documents\visual studio 2010\projects\optionscalc\optionscalc\Form1.h(330): error C2601: 'd2' : local function definitions are illegal"
along with some other errors claiming that the identifiers for my variables are undeclared.
My other issue is that I'm not 100% sure where to include the math library in my code i.e. at the top of all the visual code generated or right before my button click event.
Below is my code...PLEASE HELP!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
private: System::Void btCalc_Click(System::Object^ sender, System::EventArgs^ e) {
#include <math.h>
double callPrice();
double d1();
double d2();
double dblStock;
double dblVol;
double dblStrike;
double dblRate;
double temp;
long double e = 2.71828182845904523536;
System::TimeSpan daysdiff;
double yearsDiff;
daysdiff = this->dtpExpire->Value - this->dtpCurrent->Value;
yearsDiff = ((double) daysdiff.Days/ 365.0);
//Boolean variable is used to validate user inputs and becomes true if parse is Successful
bool gotStock=false;
bool gotVol=false;
bool gotStrike=false;
bool gotRate=false;
//establishing methods by which inputs will be validated/converted
gotStock = Double::TryParse(tbStock->Text, dblStock);
gotVol = Double::TryParse(tbVol->Text, dblVol);
gotStrike = Double::TryParse(tbStrike->Text, dblStrike);
gotRate = Double::TryParse(tbRate->Text, dblRate);
//calculation of options price
if((gotStock) && (gotVol) && (gotStrike) && (gotRate))
{
tbCall->Text = callPrice().ToString(".00");
}
double callPrice()
{
return dblStock*N(d1())-dblStrike*pow(e, (-dblRate*yearsDiff))*N(d2());
}
//
double N(double z)
{
if (z>6.0){return 1.0; }
if (z<-6.0){return 0.0; }
const double b1 = 0.319381530;
const double b2 = -0.356563782;
const double b3 = 1.781477937;
const double b4 = -1.821255978;
const double b5 = 1.330274429;
const double p = 0.2316419;
const double c2 = 0.39894228;
double a=fabs(z);
double t=1.0/(1.0+a*p);
double b = c2*exp((-z)*(z/2.0));
double n = ((((b5*t+b4)*t+b3)*t+b2)*t+b1)*t;
n = 1.0=b*n;
if (z<0.0) n = 1.0 - n;
return n
}
double d1()
{
return (log(dblStock/dblStrike) + (dblRate + pow(dblVol,2)/2)*(yearsDiff))/dblVol*sqrt(yearsDiff);
}
double d2()
{
return d1()-dblVol*sqrt(yearsDiff);
}
}
};
}
|