Advertisement
Nam_Hoang_Waw

Delta Gamma hedging

Mar 2nd, 2019
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include "normdist.h"
  4. #include <cmath>
  5. #include <fstream>
  6. #include<ctime>
  7. #include<cstdlib>
  8.  
  9. // We hedge an European call option with a strike price K of 100 on stock S with initial price S0 = 100
  10. // and drift =0.15. The implied volatility sigma1 = 0.2 is at the purchase.
  11. // For the purpose of designing a delta hedge, we 1 year option on the stock with a strike price of 90
  12. // and implied volatility sigma2 = 0.3. The interest rate is 0.05.
  13.  
  14.  
  15. using namespace std;
  16. class CallOption {
  17.  
  18. public:
  19.  
  20.     double S, X, sigma, r, time;
  21.     double CallOptionPrice(double S, double X,double sigma,double r,double time);
  22.     double GetDelta(double S, double X,double sigma,double r,double time);
  23. };
  24.  
  25. double CallOption::CallOptionPrice(double S,double X,double sigma,double r,double time) {
  26.   double d1=(log(S/X)+r*time)/(sigma*sqrt(time))+0.5*sigma*sqrt(time);
  27.   double d2=d1-(sigma*sqrt(time));
  28.   double c=S* N(d1) - X*exp(-r*time)*N(d2);
  29.  
  30.   return c;
  31. }
  32. double CallOption::GetDelta(double S,double X,double sigma,double r,double time){
  33. double d1=(log(S/X)+r*time)/(sigma*sqrt(time))+0.5*sigma*sqrt(time);
  34. return N(d1);
  35. }
  36.  
  37. double GetOneGaussianByBoxMuller ( ) {
  38. double result , x , y , SizeSquared ;
  39. do {
  40. x = 2.0*rand()/static_cast<double>(RAND_MAX)-1;
  41. y = 2.0*rand()/static_cast<double>(RAND_MAX)-1;
  42. SizeSquared = x*x+y*y;
  43. }
  44. while ( SizeSquared >= 1.0);
  45. result = x*sqrt(-2*log(SizeSquared)/SizeSquared);
  46. return result;
  47. }
  48.  
  49. int main(){
  50. std::cout.setf(std::ios_base::fixed);
  51. std::cout.setf(std::ios_base::showpoint);
  52. std::cout.precision(4);
  53. std::ofstream fileOutput("Delta_63.csv", std::ios::app);
  54. srand(time(NULL));
  55.     CallOption myOption;
  56. for (int j=1;j<1000;j++)
  57. {
  58.     double S=100;
  59.     double X=100;
  60.     double sigma=0.2;
  61.     double r=0.05;
  62.     double time=1.0;
  63.     double trend=0.15;
  64.     double pnl=0;
  65.     double loan=myOption.CallOptionPrice(S,X,sigma,r,time);
  66.     double deposit=myOption.GetDelta(S,X,sigma,r,time)*S;
  67.     double drift=(trend/63.0-0.5*pow(sigma/sqrt(63),2));
  68.  
  69. for( double i = 1; i < 63; i++ )
  70.    {
  71.     double Delta0=myOption.GetDelta(S,X,sigma,r,time);
  72.     loan=loan*exp(r/63.0);
  73.     time=time-(1.0/63.0);
  74.     S=S*exp((drift+GetOneGaussianByBoxMuller()*(sigma/sqrt(63))));
  75.     double Delta1=myOption.GetDelta(S,X,sigma,r,time);
  76.     double sell=Delta0*S+(Delta1-Delta0)*S;
  77.     deposit=deposit*exp(r/63.0);
  78.     deposit=deposit+(Delta1-Delta0)*S;
  79.     pnl=myOption.CallOptionPrice(S,X,sigma,r,time)-loan-sell+deposit;
  80.    }
  81. cout<<pnl<<endl;
  82. fileOutput<<pnl<<endl;
  83. }
  84. fileOutput.close();
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement