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
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
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
00061
00062
00063
00064
00065
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
00077
00078
00079
00080
00081
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
00095
00096
00097
00098
00099
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
00113
00114
00115
00116
00117
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 }