00001 #ifndef stringify_defined
00002 #define stringify_defined
00003 #include <iostream>
00004 #include <sstream>
00005 #include <string>
00006 #include <stdexcept>
00007
00008 inline std::string stringify(
00009 const char *funcname,
00010 const char* _file,
00011 const char *_error_msg,
00012 int _line,
00013 int _erno)
00014 {
00015 std::ostringstream o;
00016 o << _error_msg
00017 << " in function "
00018 << funcname
00019 << " at line "
00020 << _line <<
00021 " of file "
00022 << _file;
00023
00024 return o.str();
00025 }
00026 #endif
00027
00028 #ifndef SIMPOPT_H
00029 #define SIMPOPT_H
00030
00031 #if defined( WIN32) && !defined(__NT__)
00032 #define __NT__
00033 #endif
00034
00035 #if defined( __NT__) && !defined(WIN32)
00036 #define WIN32
00037 #endif
00038
00039 #if defined(ALLSTATIC) && defined(SIMPOPTDLL)
00040 #error ALLSTATIC and SIMPOPTDLL are mutually exclusive
00041 #endif
00042
00043 #if defined(ALLSTATIC) || defined(SIMPOPTSTATIC)
00044 #undef SIMPOPTPTR
00045 #define SIMPOPTPTR
00046 #define SIMPOPTFNC
00047 #elif !defined(SIMPOPTPTR)
00048 #ifdef __NT__
00049 #ifdef SIMPOPTDLL
00050 #define SIMPOPTPTR __declspec(dllexport)
00051 #define SIMPOPTFNC __declspec(dllexport)
00052 #else
00053 #define SIMPOPTPTR __declspec(dllimport)
00054 #define SIMPOPTFNC __declspec(dllimport)
00055 #endif
00056 #else
00057 #define SIMPOPTFNC
00058 #define SIMPOPTPTR
00059 #endif
00060 #endif
00061
00062 #ifdef __GNUC__
00063 #ifdef SIMPOPTFNC
00064 #undef SIMPOPTFNC
00065 #endif
00066 #define SIMPOPTFNC
00067 #endif
00068
00069 #ifdef WIN32
00070 #define STDCALL __stdcall
00071 #else
00072 #define STDCALL
00073 #endif
00074
00075 #ifdef WIN32
00076 #pragma warning(disable : 4251) //4251=needs to have dll-interface
00077 #endif
00078
00079 #include <math.h>
00080
00081 #include "optionpair.h"
00082
00083 using namespace std;
00084
00098
00099 class SIMPOPTPTR european_option_pair: public option_pair {
00100 protected:
00101
00102
00103
00104
00105
00106 mutable double _mu;
00107 mutable double _sqrt_tau;
00108 mutable double _sigma_sqrt_tau;
00109 mutable double _log_S_K;
00110 mutable double _Zk;
00111 mutable double _dZk_dtau;
00112 mutable double _h;
00113 mutable double _dh_dtau;
00114 mutable double _N_h;
00115 mutable double _N_Zk;
00116 mutable double _exp_alpha_r_t;
00117 mutable double _exp_r_t;
00118 mutable double _S_exp_alpha_r_t;
00119 mutable double _K_exp_r_t;
00120 mutable double _d3C_dS2_dtau;
00121 mutable double _d3P_dS2_dtau;
00122 mutable double _dh_dsigma;
00123 mutable double _dZ_dsigma;
00124 mutable double _d2C_dsigma2;
00125 mutable double _d2P_dsigma2;
00126 mutable double _dC_dtau;
00127 mutable double _dP_dtau;
00128 mutable double _d2C_dtau2;
00129 mutable double _d2P_dtau2;
00130 mutable double _d2C_dS_dtau;
00131 mutable double _d2P_dS_dtau;
00132 mutable double _P_h;
00133
00134
00135 static const char *_error_msg;
00136 static int _erno;
00137
00138 public:
00139
00140 static const char *get_error_msg() { return _error_msg;}
00141 static int get_erno() { return _erno;}
00142 virtual const char *get_class_name() { return "european_option_pair";}
00143
00144 static int check_attributes(
00145 double S,
00146 double K,
00147 double Tau,
00148 double Alpha,
00149 double R,
00150 double Sigma);
00151
00152 void set_null() {
00153 option_pair::set_null();
00154 _mu = 0;
00155 _sqrt_tau = 0;
00156 _sigma_sqrt_tau = 0;
00157 _log_S_K = 0;
00158 _Zk = 0;
00159 _dZk_dtau = 0;
00160 _h = 0;
00161 _dh_dtau = 0;
00162 _N_h = 0;
00163 _N_Zk = 0;
00164 _exp_alpha_r_t = 0;
00165 _exp_r_t = 0;
00166 _S_exp_alpha_r_t = 0;
00167 _K_exp_r_t = 0;
00168 _d3C_dS2_dtau = 0;
00169 _d3P_dS2_dtau = 0;
00170 _dh_dsigma = 0;
00171 _dZ_dsigma = 0;
00172 _d2C_dsigma2 = 0;
00173 _d2P_dsigma2 = 0;
00174 _dC_dtau = 0;
00175 _dP_dtau = 0;
00176 _d2C_dtau2 = 0;
00177 _d2P_dtau2 = 0;
00178 _d2C_dS_dtau = 0;
00179 _d2P_dS_dtau = 0;
00180 _P_h = 0;
00181 }
00182
00183 virtual void init_calc_derived_attributes() const;
00184
00185 void init( const european_option_pair& rhs) {
00186 _S = rhs._S;
00187 _K = rhs._K;
00188 _tau = rhs._tau;
00189 _alpha = rhs._alpha;
00190 _r = rhs._r;
00191 _sigma = rhs._sigma;
00192 _C = rhs._C;
00193 _P = rhs._P;
00194 _dC_dS = rhs._dC_dS;
00195 _dP_dS = rhs._dP_dS;
00196 _d2C_dS2 = rhs._d2C_dS2;
00197 _d2P_dS2 = rhs._d2P_dS2;
00198 _dC_dtau = rhs._dC_dtau;
00199 _dP_dtau = rhs._dP_dtau;
00200 _dC_dsigma = rhs._dC_dsigma;
00201 _dP_dsigma = rhs._dP_dsigma;
00202 _calc_call = rhs._calc_call;
00203 _calc_put = rhs._calc_put;
00204 _input_call_price = rhs._input_call_price;
00205 _input_put_price = rhs._input_put_price;
00206 _C_implied_sigma = rhs._C_implied_sigma;
00207 _P_implied_sigma = rhs._P_implied_sigma;
00208 _mu = rhs._mu;
00209 _sqrt_tau = rhs._sqrt_tau;
00210 _sigma_sqrt_tau = rhs._sigma_sqrt_tau;
00211 _log_S_K = rhs._log_S_K;
00212 _Zk = rhs._Zk;
00213 _dZk_dtau = rhs._dZk_dtau;
00214 _h = rhs._h;
00215 _dh_dtau = rhs._dh_dtau;
00216 _N_h = rhs._N_h;
00217 _N_Zk = rhs._N_Zk;
00218 _exp_alpha_r_t = rhs._exp_alpha_r_t;
00219 _exp_r_t = rhs._exp_r_t;
00220 _S_exp_alpha_r_t = rhs._S_exp_alpha_r_t;
00221 _K_exp_r_t = rhs._K_exp_r_t;
00222 _d3C_dS2_dtau = rhs._d3C_dS2_dtau;
00223 _d3P_dS2_dtau = rhs._d3P_dS2_dtau;
00224 _dh_dsigma = rhs._dh_dsigma;
00225 _dZ_dsigma = rhs._dZ_dsigma;
00226 _d2C_dsigma2 = rhs._d2C_dsigma2;
00227 _d2P_dsigma2 = rhs._d2P_dsigma2;
00228 _dC_dtau = rhs._dC_dtau;
00229 _dP_dtau = rhs._dP_dtau;
00230 _d2C_dtau2 = rhs._d2C_dtau2;
00231 _d2P_dtau2 = rhs._d2P_dtau2;
00232 _d2C_dS_dtau = rhs._d2C_dS_dtau;
00233 _d2P_dS_dtau = rhs._d2P_dS_dtau;
00234 _P_h = rhs._P_h;
00235 }
00236
00237 european_option_pair& operator = ( const european_option_pair& rhs) {
00238 init( rhs);
00239 return *this;
00240 }
00241
00242 european_option_pair( const european_option_pair& rhs) {
00243 init( rhs);
00244 }
00245
00246 void init(
00247 double S,
00248 double K,
00249 double Tau,
00250 double Alpha = 0,
00251 double R = 0.05,
00252 double Sigma = 0.2)
00253 {
00254 const char* funcname = "european_option_pair::init";
00255
00256 check_attributes(
00257 S,
00258 K,
00259 Tau,
00260 Alpha,
00261 R,
00262 Sigma);
00263
00264 if (_erno) {
00265 cerr << _error_msg << std::endl;
00266 throw std::domain_error( stringify(
00267 funcname,
00268 __FILE__,
00269 _error_msg,
00270 __LINE__,
00271 _erno ));
00272 }
00273
00274
00275 _S = S;
00276 _K = K;
00277 _tau = Tau;
00278
00279
00280 _alpha = Alpha;
00281 _r = R;
00282 _sigma = Sigma;
00283
00284 init_calc_derived_attributes();
00285 }
00286
00287 european_option_pair(
00288 double S = 0,
00289 double K = 0,
00290 double Tau = 0,
00291 double Alpha = 0,
00292 double R = 0.05,
00293 double Sigma = 0.2)
00294 {
00295 set_null();
00296
00297 init(
00298 S,
00299 K,
00300 Tau,
00301 Alpha,
00302 R,
00303 Sigma);
00304 }
00305
00306 void deinit() { set_null();}
00307
00308 virtual ~european_option_pair() { deinit();}
00309
00310
00311
00312 double get_mu() const { return _mu;}
00313 double get_sqrt_tau() const { return _sqrt_tau;}
00314 double get_sigma_sqrt_tau() const { return _sigma_sqrt_tau;}
00315 double get_log_S_K() const { return _log_S_K;}
00316 double get_Zk() const { return _Zk;}
00317 double get_dZk_dtau() const { return _dZk_dtau;}
00318 double get_h() const { return _h;}
00319 double get_dh_dtau() const { return _dh_dtau;}
00320 double get_N_h() const { return _N_h;}
00321 double get_N_Zk() const { return _N_Zk;}
00322 double get_exp_alpha_r_t() const { return _exp_alpha_r_t;}
00323 double get_exp_r_t() const { return _exp_r_t;}
00324 double get_S_exp_alpha_r_t() const { return _S_exp_alpha_r_t;}
00325 double get_K_exp_r_t() const { return _K_exp_r_t;}
00326 double get_d3C_dS2_dtau() const { return _d3C_dS2_dtau;}
00327 double get_d3P_dS2_dtau() const { return _d3P_dS2_dtau;}
00328 double get_dh_dsigma() const { return _dh_dsigma;}
00329 double get_dZ_dsigma() const { return _dZ_dsigma;}
00330 double get_d2C_dsigma2() const { return _d2C_dsigma2;}
00331 double get_d2P_dsigma2() const { return _d2P_dsigma2;}
00332 double get_dC_dtau() const { return _dC_dtau;}
00333 double get_dP_dtau() const { return _dP_dtau;}
00334 double get_d2C_dtau2() const { return _d2C_dtau2;}
00335 double get_d2P_dtau2() const { return _d2P_dtau2;}
00336 double get_d2C_dS_dtau() const { return _d2C_dS_dtau;}
00337 double get_d2P_dS_dtau() const { return _d2P_dS_dtau;}
00338 double get_P_h() const { return _P_h;}
00339
00340 void init_simple_attributes( const european_option_pair& rhs) {
00341 _mu = rhs._mu;
00342 _sqrt_tau = rhs._sqrt_tau;
00343 _sigma_sqrt_tau = rhs._sigma_sqrt_tau;
00344 _log_S_K = rhs._log_S_K;
00345 _Zk = rhs._Zk;
00346 _dZk_dtau = rhs._dZk_dtau;
00347 _h = rhs._h;
00348 _dh_dtau = rhs._dh_dtau;
00349 _N_h = rhs._N_h;
00350 _N_Zk = rhs._N_Zk;
00351 _exp_alpha_r_t = rhs._exp_alpha_r_t;
00352 _exp_r_t = rhs._exp_r_t;
00353 _S_exp_alpha_r_t = rhs._S_exp_alpha_r_t;
00354 _K_exp_r_t = rhs._K_exp_r_t;
00355 _d3C_dS2_dtau = rhs._d3C_dS2_dtau;
00356 _d3P_dS2_dtau = rhs._d3P_dS2_dtau;
00357 _dh_dsigma = rhs._dh_dsigma;
00358 _dZ_dsigma = rhs._dZ_dsigma;
00359 _d2C_dsigma2 = rhs._d2C_dsigma2;
00360 _d2P_dsigma2 = rhs._d2P_dsigma2;
00361 _dC_dtau = rhs._dC_dtau;
00362 _dP_dtau = rhs._dP_dtau;
00363 _d2C_dtau2 = rhs._d2C_dtau2;
00364 _d2P_dtau2 = rhs._d2P_dtau2;
00365 _d2C_dS_dtau = rhs._d2C_dS_dtau;
00366 _d2P_dS_dtau = rhs._d2P_dS_dtau;
00367 _P_h = rhs._P_h;
00368 }
00369
00370 virtual double call_intrinsic_value() const { double tmp = _S_exp_alpha_r_t - _K_exp_r_t; return (tmp>0)? tmp : 0;}
00371 virtual double put_intrinsic_value() const { double tmp = _K_exp_r_t - _S_exp_alpha_r_t; return (tmp>0)? tmp : 0;}
00372 friend class american_option_approximation;
00373 friend SIMPOPTFNC ostream& STDCALL operator << (ostream& os, const european_option_pair& rhs);
00374 void init_call_values( const european_option_pair& rhs);
00375 void init_put_values( const european_option_pair& rhs);
00376 double dC_dt_tau( double tau);
00377 double d2C_dt2_tau( double tau);
00378 double dP_dt_tau( double tau);
00379 double d2P_dt2_tau( double tau);
00380 double C_tau( double tau);
00381 double P_tau( double tau);
00382 double dC_dsigma( double sigma);
00383 double d2C_dsigma2( double sigma);
00384 double dP_dsigma( double sigma);
00385 double d2P_dsigma2( double sigma);
00386 void set_call_immediate_exercise() const;
00387 void set_put_immediate_exercise() const;
00388 };
00389
00390
00391
00392 #endif