Showing posts with label hedging-strategy. Show all posts
Showing posts with label hedging-strategy. Show all posts

Sunday, October 20, 2019

How to find the optimal hedging strategy

How to find the optimal hedging strategy for a call or a put option.

 hedging strategy for a call or a put option

> hedging.iid(R,T,K,r,put,n,m,minS,maxS)
Error in hedging.iid(R, T, K, r, put, n, m, minS, maxS) :
  object 'R' not found
>
> R
Error: object 'R' not found
> Simulated iid excess periodic returns for the first period.
Error: unexpected symbol in "Simulated iid"
> T
[1] TRUE
> Maturity of the option (in years).
Error: unexpected symbol in "Maturity of"
> K
Error: object 'K' not found
> Strike price.
Error: unexpected symbol in "Strike price."
> r
Error: object 'r' not found
> Annual (continuous) interest rate.
Error: unexpected symbol in "Annual (continuous) interest"
> put
Error: object 'put' not found
> 1 (default) for a put and 0 for a call.
Error: unexpected 'for' in "1 (default) for"
> n
[1] 10
> Number of hedging periods.
Error: unexpected symbol in "Number of"
> m
Error: object 'm' not found
> Number of points of the grid.
Error: unexpected symbol in "Number of"
> minS
Error: object 'minS' not found
> Minimum value of the grid.
Error: unexpected symbol in "Minimum value"
> maxS
Error: object 'maxS' not found
> Maximum value of the grid
Error: unexpected symbol in "Maximum value"
> n = 5;         # number of hedging periods
> m = 5001;      # number of points of the grid
> minS = 80.0;   # minimum value of the grid
> maxS = 120.0;  # maximum value of the grid
> S0 = 100.0;    # initial value
> K  = 100.0;    # strike price
> T  = 1.0;      # maturity of the option
> r  = 0.05;     # annual (continuous) rate
> put = 1;       # Put = 0 implies call!
> sigma = 0.06;  # annual volatility of the returns
> mu = 0.09;     # annual mean of the returns
> Tp = T/n;
> rp = r*Tp;
> sigmap = sigma*sqrt(Tp);
> Kp = K*exp(-r*T);
> mup = mu*Tp-0.5*sigmap*sigmap
> N = 10000;
> R = mup -rp +sigmap*rnorm(N);
> out0 = hedging.iid(R,T,K,r,put,n,m,minS,maxS)
> C = out0$C;
> a = out0$a;
> rho = out0$rho;
> S = out0$S;
> phi1 = out0$phi1;
> C0 = interpol1d(S0,C[1,],minS,maxS);
> phi = (interpol1d(S0,a[1,],minS,maxS)-C0*rho)/S0;
> par(mfrow=c(2,1))
> plot(S,C[1,],type='s',main=bquote('Put values ' * C[0] * ' at time 0  for n' ==.(n)  ))
>
>
> plot(S,phi1,type='s',main=expression('Number of shares ' *phi[1] * ' at start'))
>
> par(new=TRUE)
> C0
[1] 0.6048564
> phi
[1] -0.1522512
> F0 = c(1:10)
> minS = 1;
> maxS = 10;
> out = interpol1d(2.45,F0,1,10)
> using optimal hedging and simulations
Error: unexpected symbol in "using optimal"
> library("fOptions", lib.loc="~/R/win-library/3.6")
> TW <- TurnbullWakemanAsianApproxOption(
+     TypeFlag = "c", S = 100, SA = 100, X = 100,
+     Time = 1/12, time = 1/12, tau = 0 , r = 0.1,
+     b = 0.1, sigma = 0.4)$price
Error in TurnbullWakemanAsianApproxOption(TypeFlag = "c", S = 100, SA = 100,  :
  could not find function "TurnbullWakemanAsianApproxOption"
> TW <- TurnbullWakemanAsianApproxOption(
+     TypeFlag = "c", S = 100, SA = 100, X = 100,
+     Time = 1/12, time = 1/12, tau = 0 , r = 0.1,
+     b = 0.1, sigma = 0.4)$price
Error in TurnbullWakemanAsianApproxOption(TypeFlag = "c", S = 100, SA = 100,  :
  could not find function "TurnbullWakemanAsianApproxOption"
> print(TW)
Error in print(TW) : object 'TW' not found
> } else
Error: unexpected '}' in "}"
> if(FALSE) { #   ... requires(fExoticOptions)
+     TW <- TurnbullWakemanAsianApproxOption(
+         TypeFlag = "c", S = 100, SA = 100, X = 100,
+         Time = 1/12, time = 1/12, tau = 0 , r = 0.1,
+         b = 0.1, sigma = 0.4)$price
+     print(TW)
+ }
> TW <- 2.859122
> abline(h = TW, col = 2)
> RollGeskeWhaleyOption(S = 80, X = 82, time1 = 1/4,Time2 = 1/3, r = 0.06, D = 4, sigma = 0.30)

Title:
 Roll Geske Whaley Option

Call:
 RollGeskeWhaleyOption(S = 80, X = 82, time1 = 1/4, Time2 = 1/3,
     r = 0.06, D = 4, sigma = 0.3)

Parameters:
       Value:         
 S     80             
 X     82             
 time1 0.25           
 Time2 0.333333333333333
 r     0.06           
 D     4             
 sigma 0.3           

Option Price:
 4.38603

Description:
 Sun Oct 20 00:05:25 2019

> BAWAmericanApproxOption(TypeFlag = "p", S = 100, X = 100, Time = 0.5, r = 0.10, b = 0, sigma = 0.25)

Title:
 BAW American Approximated Option

Call:
 BAWAmericanApproxOption(TypeFlag = "p", S = 100, X = 100, Time = 0.5,
     r = 0.1, b = 0, sigma = 0.25)

Parameters:
          Value:
 TypeFlag p   
 S        100 
 X        100 
 Time     0.5 
 r        0.1 
 b        0   
 sigma    0.25

Option Price:
 6.801362

Description:
 Sun Oct 20 00:08:30 2019

> Black76Option(TypeFlag = "c", FT = 100, X = 100,
+               Time = 0.5, r = 0.10, sigma = 0.25)

Title:
 Black 76 Option Valuation

Call:
 Black76Option(TypeFlag = "c", FT = 100, X = 100, Time = 0.5,
     r = 0.1, sigma = 0.25)

Parameters:
          Value:
 TypeFlag c   
 FT       100 
 X        100 
 Time     0.5 
 r        0.1 
 sigma    0.25

Option Price:
 6.699709

Description:
 Sun Oct 20 00:08:30 2019

> BAWAmericanApproxOption(TypeFlag = "p", S = 100,
+ X = 100, Time = 0.5, r = 0.10, b = 0, sigma = 0.25)

Title:
 BAW American Approximated Option

Call:
 BAWAmericanApproxOption(TypeFlag = "p", S = 100, X = 100, Time = 0.5,
     r = 0.1, b = 0, sigma = 0.25)

Parameters:
          Value:
 TypeFlag p   
 S        100 
 X        100 
 Time     0.5 
 r        0.1 
 b        0   
 sigma    0.25

Option Price:
 6.801362

Description:
 Sun Oct 20 00:09:38 2019

> BSAmericanApproxOption(TypeFlag = "c", S = 42, X = 40, Time = 0.75, r = 0.04, b = 0.04-0.08, sigma = 0.35)

Title:
 BS American Approximated Option

Call:
 BSAmericanApproxOption(TypeFlag = "c", S = 42, X = 40, Time = 0.75,
     r = 0.04, b = 0.04 - 0.08, sigma = 0.35)

Parameters:
             Value:       
 TypeFlag    c             
 S           42           
 X           40           
 Time        0.75         
 r           0.04         
 b           -0.04         
 sigma       0.35         
 TrigerPrice 57.5994499306841

Option Price:
 5.270405

Description:
 Sun Oct 20 00:10:12 2019

> CRRBinomialTreeOption(TypeFlag = "pa", S = 50, X = 50, Time = 5/12, r = 0.1, b = 0.1, sigma = 0.4, n = 5)

Title:
 CRR Binomial Tree Option

Call:
 CRRBinomialTreeOption(TypeFlag = "pa", S = 50, X = 50, Time = 5/12,
     r = 0.1, b = 0.1, sigma = 0.4, n = 5)

Parameters:
          Value:         
 TypeFlag pa             
 S        50             
 X        50             
 Time     0.416666666666667
 r        0.1           
 b        0.1           
 sigma    0.4           
 n        5             

Option Price:
 4.488459

Description:
 Sun Oct 20 00:12:39 2019

> CRRBinomialTreeOption(TypeFlag = "pa", S = 100, X = 95, Time = 0.5, r = 0.08, b = 0.08, sigma = 0.3, n = 5)

Title:
 CRR Binomial Tree Option

Call:
 CRRBinomialTreeOption(TypeFlag = "pa", S = 100, X = 95, Time = 0.5,
     r = 0.08, b = 0.08, sigma = 0.3, n = 5)

Parameters:
          Value:
 TypeFlag pa 
 S        100 
 X        95 
 Time     0.5 
 r        0.08
 b        0.08
 sigma    0.3 
 n        5   

Option Price:
 4.919211

Description:
 Sun Oct 20 00:13:14 2019

> CRRBinomialTreeOption(TypeFlag = "ce", S = 100, X = 100, Time = 1, r = 0.1, b = 0.1, sigma = 0.25, n = 50)

Title:
 CRR Binomial Tree Option

Call:
 CRRBinomialTreeOption(TypeFlag = "ce", S = 100, X = 100, Time = 1,
     r = 0.1, b = 0.1, sigma = 0.25, n = 50)

Parameters:
          Value:
 TypeFlag ce    
 S        100   
 X        100   
 Time     1     
 r        0.1   
 b        0.1   
 sigma    0.25  
 n        50    

Option Price:
 14.92529 

Description:
 Sun Oct 20 00:13:46 2019

> GBSOption(TypeFlag = "c", S = 100, X = 100, Time = 1, r = 0.1, b = 0.1, sigma = 0.25)@price
[1] 14.9758
> CRRTree = BinomialTreeOption(TypeFlag = "pa", S = 50, X = 50, Time = 0.4167, r = 0.1, b = 0.1, sigma = 0.4, n = 5)
> BinomialTreePlot(CRRTree, dy = 1, cex = 0.8, ylim = c(-6, 7),xlab = "n", ylab = "Option Value")
> title(main = "Option Tree")
> par(mfrow = c(2, 1), cex = 0.7)
> steps = 50
> CRROptionValue =  JROptionValue = TIANOptionValue =
+     rep(NA, times = steps)
> for (n in 3:steps) {
+     CRROptionValue[n] = CRRBinomialTreeOption(TypeFlag = "pa", S = 50,
+                                               X = 50, Time = 0.4167, r = 0.1, b = 0.1, sigma = 0.4, n = n)@price
+     JROptionValue[n] = JRBinomialTreeOption(TypeFlag = "pa", S = 50,
+ X = 50, Time = 0.4167, r = 0.1, b = 0.1, sigma = 0.4, n = n)@price
+     TIANOptionValue[n] = TIANBinomialTreeOption(TypeFlag = "pa", S = 50,
+ X = 50, Time = 0.4167, r = 0.1, b = 0.1, sigma = 0.4, n = n)@price
+ }
> plot(CRROptionValue[3:steps], type = "l", col = "red", ylab = "Option Value")
> lines(JROptionValue[3:steps], col = "green")
> lines(TIANOptionValue[3:steps], col = "blue")
> BAWValue =  BAWAmericanApproxOption(TypeFlag = "p", S = 50, X = 50,
+ Time = 0.4167, r = 0.1, b = 0.1, sigma = 0.4)@price
> abline(h = BAWValue, lty = 3)
> title(main = "Convergence")
> data.frame(CRROptionValue, JROptionValue, TIANOptionValue)
   CRROptionValue JROptionValue TIANOptionValue
1              NA            NA              NA
2              NA            NA              NA
3        4.644221      4.658215        4.250039
4        4.138064      4.147067        4.436720
5        4.488599      4.498538        4.118174
6        4.174600      4.182133        4.420335
7        4.440592      4.447394        4.153362
8        4.208285      4.213683        4.376841
9        4.404249      4.407809        4.219837
10       4.220215      4.224464        4.356222
11       4.383327      4.387160        4.265722
12       4.232302      4.239263        4.327229
13       4.370110      4.372616        4.288170
14       4.240825      4.248576        4.304847
15       4.357657      4.359191        4.306384
16       4.245087      4.253081        4.288338
17       4.348818      4.349603        4.314806
18       4.249080      4.257725        4.272137
19       4.342130      4.343514        4.315981
20       4.252509      4.262715        4.255790
21       4.336482      4.338156        4.318543
22       4.255462      4.265471        4.244420
23       4.331761      4.333151        4.319010
24       4.257848      4.267585        4.244907
25       4.328345      4.329232        4.316251
26       4.260138      4.269382        4.254040
27       4.325388      4.325936        4.311505
28       4.262036      4.271401        4.261820
29       4.322574      4.323182        4.309124
30       4.263565      4.272796        4.271949
31       4.320010      4.320505        4.306223
32       4.264729      4.274120        4.279511
33       4.317817      4.318179        4.302398
34       4.265834      4.275367        4.284229
35       4.315989      4.316346        4.297096
36       4.266951      4.276686        4.287908
37       4.314409      4.314752        4.292959
38       4.268021      4.277666        4.291718
39       4.312930      4.313071        4.289362
40       4.268896      4.278398        4.295093
41       4.311560      4.311546        4.285607
42       4.269617      4.278981        4.297283
43       4.310265      4.310177        4.281393
44       4.270281      4.279516        4.298161
45       4.309224      4.308935        4.276739
46       4.271009      4.280033        4.298265
47       4.308268      4.307800        4.272384
48       4.271626      4.280648        4.299010
49       4.307313      4.306856        4.268842
50       4.272160      4.281069        4.299600

Black-Scholes formula-R

 Black-Scholes formula-R > BlackScholes <- function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma) { TypeFla...