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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#include <iostream> //I/O stuff like cout, cin
#include "Option.h"
#include <math.h>
#include <conio.h>
#ifndef pi
#define pi 3.141592653589793238462643
#endif
using namespace std ;
const int PriceStepNb=200;
const int TimeStepNb=200 ;
const double Precision =0.01;// wofür
const int DaysInYearNb=360;
double Date;
long Amount;
typedef double vecteur[PriceStepNb];
double n(double x)
{ // Gaussian (normal) distribution function
double K = 1.0/sqrt(2.0 * pi);
return K * exp(-pow(x,2)*0.5);
};
double N( double x){
const double c[]={ 0.196854, 0.115194, 0.000344, 0.019527};
if(x >= 0.0) {
double y1=1+x*(c[0]+x*(c[1] + x*(c[2] + c[3]*x)));
double z1=1/y1;
return (1-0.5*pow(z1,4));
}
else {
return 1-N(-x);
}
};
// Kernel Functions
double Option::Euro_PutPrice(double S, struct AmericanPut option, struct Modele modele) const
{
double tmp = modele.sigma * sqrt(option.MaturityDate-option.ContractDate);
double d1 = ( log(S/K) + (modele.r+ (modele.sigma*modele.sigma)*0.5 ) * option.MaturityDate )/ tmp;
double d2 = d1 - tmp;
return (option.StrikePrice * exp(-modele.r * option.MaturityDate)* N(-d2)) - (S * N(-d1));
};
double Option::Ame_PutPrice_Johnson(double S, struct AmericanPut &option, struct Modele modele) const {
const double a0 = 3.9649;
const double a1 = 0.032325;
const double b0 = 1.04083;
const double b1 = 0.00963;
double tau=option.MaturityDate-option.ContractDate;
double gam=modele.sigma*modele.sigma*tau/(b0*modele.sigma*modele.sigma*tau+b1);
double S_f=option.StrikePrice*pow((2*modele.r/(modele.sigma*modele.sigma+2*modele.r)),gam);
double betha=log(S/S_f)/log(option.StrikePrice/S_f);
double alfa=pow(modele.r*tau/(a0*r*tau+a1),betha);
double V_l=Euro_PutPrice( S, option, modele );
AmericanPut cp_option(option);
raz(cp_option);
double V_u=Euro_PutPrice(S , cp_option, modele );
return alfa*V_l+(1-alfa)*V_u;
};
void Option::init()
{ // Initialise all default values
// Default values
r = 0.1;
sig= 0.30;
K = 50.0;
T = 0.4166;
S = 50.0;
optType = "C"; // European Call Option (the default type)
};
void Option::copy(const Option& o2)
{
r = o2.r;
sig = o2.sig;
K = o2.K;
T = o2.T;
S = o2.S;
optType = o2.optType;
};
Option::Option()
{ // Default call option
init();
};
Option::Option(const Option& o2)
{ // Copy constructor
copy(o2);
};
Option::Option (const string& optionType)
{ // Create option type
init();
optType = optionType;
if (optType == "c")optType = "C";
};
Option::~Option()
{ // Destructor
};
Option& Option::operator = (const Option& opt2)
{ // Assignment operator (deep copy)
if (this == &opt2) return *this;
copy (opt2);
return *this;
};
// Functions that calculate EuroOption price
double Option::Black_Schol_Price() const
{
if (optType == "C")return Euro_CallPrice();
else return Euro_PutPrice();
};
double num_eva_Price() const;// Brenann&Schwartz as numeric solution
{
if (optType == "C")return Euro_CallPrice()
else Ame_PutPrice_BS ();
};
double analy_eva_Price() const;// Johnson as anlytic solution
{
return Ame_PutPrice_Johnson ();
};
void option::raz()
{
StikePrice=StikePrice*exp(MaturityDate-ContractDate);
};
void Option::toggle()
{ // Change option type (C/P, P/C)
if (optType == "C")optType = "P";
else optType = "C";
};
int main()
{
Option::AmericanPut Test={ 0 , 150 , 50 };
Option::Modele modele ={ 0.1 , 0.4 , 50 };
Option europut;
europut.optType = "P";
cout << "European Put option : " << "" <<europut.Black_Schol_Price() << endl;
cout << "Johnson : " << "" << Ame_PutPrice_Johnson( 50 , 0 , 50 , 0.1 , 0.4 , 0.4666)<< endl;
getch();
}; |
Partager