Dec 28, 2017 at 7:43pm UTC
Hi guys!
I'm studying the book 'C++ for Quantitative Finance' and implementing the first example Vanilla Option, but in the function to calculate call / put option values, the IDE always reports a error: 'N was not declared in this scope'.
I bet the N stands for the calculation of CDF of a standard normal distribution, using <cmath>. I checked <cmath> documentation and found out that the library does not include function N.
Could u please give me some solution for this ?
Thanks a lot!!!
Here are the codes:
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
#ifndef VANILLAOPTION_H
#define VANILLAOPTION_H
class vanillaoption
{
public :
vanillaoption();
vanillaoption(const double & _K,
const double & _r,
const double & _T,
const double & _S,
const double & _sigma);
vanillaoption(const vanillaoption& rhs);
vanillaoption& operator =(const vanillaoption& rhs);
virtual ~vanillaoption();
// selector (getter) methods for our option parameters
double getK() const ;
double getr() const ;
double getT() const ;
double getS() const ;
double getsigma() const ;
double calcalloption() const ;
double calputoption() const ;
protected :
private :
void init();
void copy(const vanillaoption& rhs);
double K;
double r;
double T;
double S;
double sigma;
};
#endif // VANILLAOPTION_H
#include <iostream>
#include "vanillaoption.h"
#include <cmath>
using namespace std;
void vanillaoption::init(){
K = 100.0;
r = 0.05;
T = 1.0;
S = 100.0;
sigma = 0.2;
}
vanillaoption::vanillaoption()
{
init();
}
vanillaoption::vanillaoption(const double & _K, const double & _r, const double & _T, const double & _S, const double & _sigma)
{
K = _K;
r = _r;
T = _T;
S = _S;
sigma = _sigma;
}
//copy constructor
vanillaoption::vanillaoption(const vanillaoption& rhs)
{
copy(rhs);
}
//assignment operator
vanillaoption& vanillaoption::operator =(const vanillaoption& rhs)
{
if (this == &rhs) return *this ;
copy(rhs);
return *this ; /
}
void vanillaoption::copy(const vanillaoption& rhs){
K = rhs.getK();
r = rhs.getr();
T = rhs.getT();
S = rhs.getS();
sigma = rhs.getsigma();
}
vanillaoption::~vanillaoption()
{
//empty
}
double vanillaoption::getK() const {
return K;
}
double vanillaoption::getr() const {
return r;
}
double vanillaoption::getT() const {
return T;
}
double vanillaoption::getS() const {
return S;
}
double vanillaoption::getsigma() const {
return sigma;
}
double vanillaoption::calcalloption() const
{
double sigma_squrt_T = sigma * sqrt(T);
double d_1 = ( log(S/K) + sigma * sigma * 0.5 * T) / sigma_squrt_T;
double d_2 = d_1 - sigma_squrt_T;
return (S * exp(-r*T) * N(d_1)) - (K * exp(-r*T) * N(d_2)); // problem occurred in this line
};
double vanillaoption::calputoption() const
{
double sigma_squrt_T = sigma * sqrt(T);
double d_1 = ( log(S/K) + sigma * sigma * 0.5 * T) / sigma_squrt_T;
double d_2 = d_1 - sigma_squrt_T;
return (K * exp(-r*T) * N(-d_2)) - (S * exp(-r*T) * N(-d_1));
};
Thanks!
Last edited on Dec 28, 2017 at 7:44pm UTC
Dec 28, 2017 at 8:19pm UTC
Line 89: Extraneous /
Line 132, 139: N appears to be a function call, but I see no such function.
Dec 28, 2017 at 8:38pm UTC
I have not fooled with probability since the 90s so forgive me... but a quick google provided this function for N:
double normalCFD(double value)
{
return 0.5 * erfc(-value * M_SQRT1_2);
}
TEST it to see if it is correct and giving yuo what you expect.
erfc is in <cmath> as is the constant. It should flat out work if the formula is correct.
Last edited on Dec 28, 2017 at 8:40pm UTC
Dec 28, 2017 at 10:10pm UTC
Thanks a lot, AbstractionAnon and jonnin, using the function indicated by jonnin, the problem has been solved!!!I'm so happy with this, my friends!