Can you help me with my codes? I am having an error LNK2005. I am computing the option price for put and call using BS

tarantula

New Member
C++:
***************************************************************************************

class StatUtility
{
public:
double normalCalc(double d);
private:
double d;

};

*****************************************************************************************
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include "StatUtility.h"
using namespace std;
#define PI  3.1415926535897932384626433832795028841968
double StatUtility::normalCalc(double d)
{
const double a1 = 0.319381530;
const double a2 = -0.356563782;
const double a3 = 1.781477937;
const double a4 = -1.821255978;
const double a5 = 1.330274429;
const double gamma = 0.2316419;
const double k1 = 1 / (1 + gamma * d);
const double k2 = 1 / (1 - gamma*d);
const double normalprime = (1 / (sqrt(2 * PI)))*exp(-d * d / 2);
double value = 0.0;
double h = 0.0;
if (d >= 0)
  value = 1 - normalprime * (a1*k1 + a2 * pow(k1, 2) + a3 * pow(k1, 3) + a4 * pow(k1, 4) +
   a5 * pow(k1, 5));
else
  value = normalprime * (a1*k2 + a2 * pow(k2, 2) + a3 * pow(k2, 3) + a4 * pow(k2, 4) +
   a5 * pow(k2, 5));
return value;
}

**********************************************************************************************************************

#pragma once

class BlackScholesModel
{
public:
double calcBSPutPrice(double vol, double rate, double div,
  double strike, double price, double T);
private:
double vol;
double rate;
double div;
double strike;
double price;
double T;
};

******************************************************************************************

#pragma once

class BlackScholesModel
{
public:
double calcBSPutPrice(double vol, double rate, double div,
  double strike, double price, double T);
private:
double vol;
double rate;
double div;
double strike;
double price;
double T;
};

**************************************************************************************************

#include "statUtility.h"
#include "normalCalc.h"
#include "put.h"

using namespace std;
StatUtility util2;
StatUtility util3;
double BlackScholesModel::calcBSPutPrice(double vol, double rate, double div,
double strike, double price, double T)
{
double prob1;
double prob2;
double putprice;
double d1, d2;
d1 = (log(price / strike) + (rate - div + (vol)*(vol) / 2)*T) / (vol*sqrt(T));
d2 = d1 - vol*sqrt(T);
prob1 = util2.normalCalc(-d1);
prob2 = util3.normalCalc(-d2);
putprice = strike * exp(-rate * T)*prob2 - price*exp(-div * T)*prob1;
return putprice;
}
***************************************************************************************************



#include "StatUtility.h"
#include "normalCalc.h"
#include "call.h"
using namespace std;

double BlackScholesModel1::calcBSCallPrice(double vol, double rate, double div,
double strike, double price, double T)
{
StatUtility util;
StatUtility util1;
double prob1;
double prob2;
double d1, d2;
double callprice;
d1 = (log(price / strike) + (rate - div + (vol)*(vol) / 2)*T) / (vol*sqrt(T));
d2 = d1 - vol*sqrt(T);
prob1 = util.normalCalc(d1);
prob2 = util1.normalCalc(d2);
callprice = price * exp(-div * T)*prob1 - strike*exp(-rate * T)*prob2;
return callprice;
}


**********************************************************************************************

#include "StatUtility.h"
#include "normalCalc.h"
#include "call.h"
using namespace std;

double BlackScholesModel1::calcBSCallPrice(double vol, double rate, double div,
double strike, double price, double T)
{
StatUtility util;
StatUtility util1;
double prob1;
double prob2;
double d1, d2;
double callprice;
d1 = (log(price / strike) + (rate - div + (vol)*(vol) / 2)*T) / (vol*sqrt(T));
d2 = d1 - vol * sqrt(T);
prob1 = util.normalCalc(d1);
prob2 = util1.normalCalc(d2);
callprice = price * exp(-div * T)*prob1 - strike * exp(-rate * T)*prob2;
return callprice;
}
**********************************************************************************************

#pragma once

class BlackScholesModel
{
public:
  double calcBSCallPrice(double vol, double rate, double div,
  double strike, double price, double T);
private:
 double vol;
 double rate;
 double div;
 double strike;
 double price;
 double T;


};
 
Last edited:

tarantula

New Member
I am not sure why but it gives me now error c2011 BlackScholesModel class type redefinition.
 

Daniel Duffy

C++ author, trainer
I am not sure why I get c2011 error BlackScholesModel class type redefinition
That's because you have still to learn the basics..
This is all basic C stuff in reality. Are you just random copying (code) and pasting and hoping for the best? I
Pardon my bluntness.

In judo you grade white -> yellow -> green -> blue -> brown -> black 1... black5 -> red and white -> ...
 
Last edited:

tarantula

New Member
I am not just random copying but I defined the classes and have other codes that joined to make it work. It doesn't compile.
 

tarantula

New Member
That's because you have still to learn the basics..
This is all basic C stuff in reality. Are you just random copying (code) and pasting and hoping for the best? I
Pardon my bluntness.
I am not just random copying but I defined the classes and have other codes that joined to make it work. It doesn't compile.
 

tarantula

New Member
I know the error is about the BSM class from the call.h. I have never had before that's why I am asking. Please find my codes attached.
 

Attachments

Daniel Duffy

C++ author, trainer
No, you should solve this problem yourself. Are you new to C/C++ (white belt)?
Of course, putting code in doc files is not a good idea.. Use inset file of code option and make it a bit more reader-friendly.

You are doing something wrong. Structure the code a bit better. Use .hpp and .cpp for each thingy and and use #ifndef stuff in the former.
 
Last edited:

tarantula

New Member
No, you should solve this problem yourself. Are you new to C/C++ (white belt)?
Of course, putting code in doc files is not a good idea.. Use inset file of code option and make it a bit more reader-friendly.

You are doing something wrong. Structure the code a bit better. Use .hpp and .cpp for each thingy and and use #ifndef stuff in the former.
I am not new to C++. I consider myself as yellow belt. I value returning and reference parameter. I started working with classes too.
 

Daniel Duffy

C++ author, trainer
Did you take my advice on .hpp and .cpp? (.h files are C and old style).I
Looking at the confusing code, it's missing preprocessor directives as well.
 
Last edited:

tarantula

New Member
Yes, I did look at it and change the .h file to .hpp. I also use the the endnif, define and endif. it still gives me error C2011 and C2334
 

Daniel Duffy

C++ author, trainer
Thanks for the cryptic names, I had to look them up!

error C2011: '' : 'class' type redefinition

Compiler Error C2334

I do think you need to do something with the quality of these posts. Your code is not inviting to read.

C++:
class BlackScholesModel
{
public:
double calcBSPutPrice(double vol, double rate, double div,
double strike, double price, double T);
private:
double vol;
double rate;
double div;
double strike;
double price;
double T;
};
 
Last edited:

Daniel Duffy

C++ author, trainer
The following code is very inefficient and outdated (Abramowitz and Stegun 26.2.16) and has 1.0e-5 accuracy Use C++11.
BTW did you pluck it from the internet?

C++:
if (d >= 0)
        value = 1 - normalprime * (a1*k1 + a2 * pow(k1, 2) + a3 * pow(k1, 3) + a4 * pow(k1, 4) +
            a5 * pow(k1, 5));
    else
        value = normalprime * (a1*k2 + a2 * pow(k2, 2) + a3 * pow(k2, 3) + a4 * pow(k2, 4) +
            a5 * pow(k2, 5));
 

tarantula

New Member
I also define a main
The following code is very inefficient and outdated (Abramowitz and Stegun 26.2.16) and has 1.0e-5 accuracy Use C++11.
BTW did you pluck it from the internet?

C++:
if (d >= 0)
        value = 1 - normalprime * (a1*k1 + a2 * pow(k1, 2) + a3 * pow(k1, 3) + a4 * pow(k1, 4) +
            a5 * pow(k1, 5));
    else
        value = normalprime * (a1*k2 + a2 * pow(k2, 2) + a3 * pow(k2, 3) + a4 * pow(k2, 4) +
            a5 * pow(k2, 5));
I got it from Modelling Derivatives using C++
 

Daniel Duffy

C++ author, trainer
More bad news, this class is wrong. It should have static member functions

C++:
class StatUtility
{
public:
    double normalCalc(double d);

private:

    double d;


};
 

Daniel Duffy

C++ author, trainer
It looks like you copied all the code (including error and sloppy style) verbatim from Justin London's book. Meshuggah.
We have a phrase in Ireland: "chancing your arm"
 
Last edited:
Top