268
Appendix B: Software
B.3 Dynamic Hedging Simulation
The following script dynamic_hedging.m is used to prepare the plots in Fig. 5.3.
After initializing the parameters and allocating arrays to hold all intermediary values
in the simulation, the function black_scholes_call() is used to calculate the
initial price c 0 and hedging parameter 0 . They are used to determine how many
shares must be purchased for the hedge. The money needed for the hedge is borrowed
from the bank. Once all values are initialized, we loop over the remaining days until
maturity of the option. Each day, we calculate how many days are left until maturity,
the interest we need to pay for the borrowed money, the increase in value of the
money I received for the option and the the changed price of the share. Since the
time until maturity and the share price S have changed, we calculate the new in
a call to black_scholes_call() and use that information to update the hedge
and the money borrowed from the bank.
% dynamic_hedging.m
clear all; close all
rho=0.09;
T0=1;
% one year
S0=1;
% initial share value
K=1.0;
% strike price, K=1.2 in lower plot
rf=0.05;
% risk free rate
sig=0.3;
% annual volatility
N=252;
% trading days
S=zeros(N,1); hedge=zeros(N,1);
borrow=zeros(N,1); value=zeros(N,1); KK=K*ones(N,1);
[c0,delta0]=black_scholes_call(S0,K,T0,rf,sig);
S(1)=S0;
hedge(1)=delta0*S0;
% initial hedge
borrow(1)=hedge(1);
% borrow money for hedge
value(1)=c0;
% pocket the money for the option
for i=2:N
dt=T0*(N-i)/N;
% trading days left
interest=borrow(i-1)*rf/N;
% interest on borrowed
value(i)=value(i-1)*exp(rf/N); % money call option
S(i)=S(i-1)*exp(rho/N);%*(1+sig*randn/sqrt(N));
[c,delta]=black_scholes_call(S(i),K,dt,rf,sig);
hedge(i)=delta*S(i);
borrow(i)=borrow(i-1)+hedge(i)-hedge(i-1)+interest;
end
if S(i) > K
% in the money
cost=borrow(i)-K;
% repay borrowed, get strike price
else
% out of the money
cost=borrow(i);
Appendix B: Software
B.3 Dynamic Hedging Simulation
The following script dynamic_hedging.m is used to prepare the plots in Fig. 5.3.
After initializing the parameters and allocating arrays to hold all intermediary values
in the simulation, the function black_scholes_call() is used to calculate the
initial price c 0 and hedging parameter 0 . They are used to determine how many
shares must be purchased for the hedge. The money needed for the hedge is borrowed
from the bank. Once all values are initialized, we loop over the remaining days until
maturity of the option. Each day, we calculate how many days are left until maturity,
the interest we need to pay for the borrowed money, the increase in value of the
money I received for the option and the the changed price of the share. Since the
time until maturity and the share price S have changed, we calculate the new in
a call to black_scholes_call() and use that information to update the hedge
and the money borrowed from the bank.
% dynamic_hedging.m
clear all; close all
rho=0.09;
T0=1;
% one year
S0=1;
% initial share value
K=1.0;
% strike price, K=1.2 in lower plot
rf=0.05;
% risk free rate
sig=0.3;
% annual volatility
N=252;
% trading days
S=zeros(N,1); hedge=zeros(N,1);
borrow=zeros(N,1); value=zeros(N,1); KK=K*ones(N,1);
[c0,delta0]=black_scholes_call(S0,K,T0,rf,sig);
S(1)=S0;
hedge(1)=delta0*S0;
% initial hedge
borrow(1)=hedge(1);
% borrow money for hedge
value(1)=c0;
% pocket the money for the option
for i=2:N
dt=T0*(N-i)/N;
% trading days left
interest=borrow(i-1)*rf/N;
% interest on borrowed
value(i)=value(i-1)*exp(rf/N); % money call option
S(i)=S(i-1)*exp(rho/N);%*(1+sig*randn/sqrt(N));
[c,delta]=black_scholes_call(S(i),K,dt,rf,sig);
hedge(i)=delta*S(i);
borrow(i)=borrow(i-1)+hedge(i)-hedge(i-1)+interest;
end
if S(i) > K
% in the money
cost=borrow(i)-K;
% repay borrowed, get strike price
else
% out of the money
cost=borrow(i);
