Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

BlackScholes.cpp

Go to the documentation of this file.
00001 #include "BlackScholes.h"
00002 
00003 Real absolute(Real x){
00004         if (x>=0)
00005                 return x;
00006         else
00007                 return -x;
00008 }
00009 
00010 BlackScholes::BlackScholes(Real spot, Real volOrPrice, bool isVol, Real r, Real K, Real T,TypeOptionBS typeOption):
00011                                                         _spot(spot),
00012                                                         _r(r),
00013                                                         _K(K),
00014                                                         _T(T),
00015                                                         _type(typeOption)
00016 {
00017         // If paramter given is volatility, we compute the price
00018         if (isVol) {
00019                 _vol=volOrPrice;
00020                 d1= (log(_spot/_K)+(_r+_vol*_vol/2)*_T)/(_vol*sqrt(_T));
00021                 d2= d1-_vol*sqrt(_T);
00022         } 
00023         // If the parameter given is the price, we compute the implied volatility
00024         else {
00025                 _price=volOrPrice;
00026                 Real vol;
00027                 Real p,vega;
00028                 vol= sqrt(2*absolute(log(_spot/_K)+_r*_T)/_T);
00029                 for (int i=0;i<100;i++){
00030                         BlackScholes bs=BlackScholes(_spot,vol,true,_r,_K,_T,_type);
00031                         p=bs.getPrice();
00032                         vega=bs.getVega();
00033                         vol+=(_price-p)/vega;
00034                 }
00035                 if (!(volOrPrice>0))
00036                         volOrPrice=0;
00037                 _vol=vol;
00038                 d1= (log(_spot/_K)+(_r+_vol*_vol/2)*_T)/(_vol*sqrt(_T));
00039                 d2= d1-_vol*sqrt(_T);
00040         }
00041 }
00042 
00043 BlackScholes::BlackScholes() {
00044 }
00045 
00046 BlackScholes::~BlackScholes()
00047 {
00048 }
00049 
00050 Real BlackScholes::getDelta() {
00051         switch (_type) {
00052                         case Call:
00053                         default:
00054                                 return CumulativeNormal(d1);
00055                         case Put:
00056                                 return CumulativeNormal(d1)-1;
00057                         }
00058 }
00059 
00060 /*Real BlackScholes::getDelta() {
00061         Real delta,p_d;
00062         BlackScholes BS_d(_spot*1.01,_vol,_r,_K,_T);
00063         p_d=BS_d.getPrice();
00064         delta=(p_d-_price)/(0.01*_spot);
00065         return delta;
00066 }*/
00067 
00068 Real BlackScholes::getGamma() {
00069         return (NormalDensity(d1)/(_spot*_vol*sqrt(_T)));
00070 }
00071 
00072 Real BlackScholes::getVega() {
00073         return (_spot*sqrt(_T)*NormalDensity(d1));
00074 }
00075 
00076 /*Real BlackScholes::getVega() {
00077         Real vega,p_v;
00078         BlackScholes BS_v(_spot,_vol*1.01,_r,_K,_T);
00079         p_v=BS_v.getPrice();
00080         vega=(p_v-_price)/(0.01*_vol);
00081         return vega;
00082 }*/
00083 
00084 Real BlackScholes::getTheta() {
00085         switch (_type) {
00086                         case Call:
00087                         default:
00088                                 return ((-_spot*_vol*NormalDensity(d1))/(2*sqrt(_T))-_r*_K*exp(-_r*_T)*CumulativeNormal(d2));
00089                         case Put:
00090                                 return ((-_spot*_vol*NormalDensity(d1))/(2*sqrt(_T))+_r*_K*exp(-_r*_T)*CumulativeNormal(-d2));
00091         }
00092 }
00093 
00094 /*Real BlackScholes::getTheta() {
00095         Real theta,p_t;
00096         BlackScholes BS_t(_spot,_vol,_r,_K,_T*1.01);
00097         p_t=BS_t.getPrice();
00098         theta=(p_t-_price)/(0.01*_T);
00099         return theta;
00100 }*/
00101 
00102 Real BlackScholes::getRho() {
00103         switch (_type) {
00104                         case Call:
00105                         default:
00106                                 return (_K*_T*exp(-_r*_T)*CumulativeNormal(d2));
00107                         case Put:
00108                                 return (-_K*_T*exp(-_r*_T)*CumulativeNormal(-d2));
00109         }
00110 }
00111 
00112 /*Real BlackScholes::getRho() {
00113         Real rho,p_r;
00114         BlackScholes BS_r(_spot,_vol,_r*1.01,_K,_T);
00115         p_r=BS_r.getPrice();
00116         rho=(p_r-_price)/(0.01*_r);
00117         return rho;
00118 }*/
00119 
00120 Real BlackScholes::getVolatility() const{
00121         return _vol;
00122 }
00123 
00124 Real BlackScholes::getStrike() const{
00125         return _K;
00126 }
00127 
00128 Real BlackScholes::getRate() const{
00129         return _r;
00130 }
00131 
00132 Real BlackScholes::getSpot() const{
00133         return _spot;
00134 }
00135 
00136 Real BlackScholes::getMaturity() const{
00137         return _T;
00138 }
00139 
00140 bool BlackScholes::isCall() const{
00141         return (_type==Call);
00142 }
00143 
00144 void BlackScholes::changeRate(Real newRate) {
00145         _r=newRate;
00146         recalcInformation();
00147 }
00148 
00149 void BlackScholes::changeVol(Real newVol) {
00150         _vol=newVol;
00151         recalcInformation();
00152 }
00153 
00154 void BlackScholes::changeMaturity(Real newMat) {
00155         _T=newMat;
00156         recalcInformation();
00157 }
00158 
00159 void BlackScholes::changeSpot(Real newSpot) {
00160         _spot=newSpot;
00161         recalcInformation();
00162 }
00163 
00164 void BlackScholes::changeStrike(Real newStrike) {
00165         _K=newStrike;
00166         recalcInformation();
00167 }
00168 
00169 void BlackScholes::recalcInformation() {
00170         d1= (log(_spot/_K)+(_r+_vol*_vol/2)*_T)/(_vol*sqrt(_T));
00171         d2= d1-_vol*sqrt(_T);
00172 }

Note: Generated nightly - reload for latest version
Generated on Thu Dec 22 23:12:35 2005 for terreneuve by doxygen 1.3.6