secret
Created

Simple delta hedging simulation

  • Download Gist
deltahedging
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
%% random stock prices and volatility
count = 252;
ds = normrnd(0,1,count,1);
s = zeros(count,1);
s(count) = 100;
for i=(count-1):-1:1; s(i)=s(i+1)+ds(i); end
 
dv = normrnd(0,0.01,count,1);
v = zeros(count,1);
v(count) = 0.1;
for i=(count-1):-1:1; v(i)=v(i+1)+dv(i); end
 
strike = 100;
 
c = zeros(count,1);
p = zeros(count,1);
parfor t=1:count
[c(t),p(t)] = blsprice(s(t),strike,0,t/count,v(t),0);
end
 
cd = zeros(count,1);
pd = zeros(count,1);
gamma = zeros(count,1);
for t=1:count
[cd(t),pd(t)] = blsdelta(s(t),strike,0,t/count,v(t),0);
gamma(t) = blsgamma(s(t),strike,0,t/count,v(t),0);
end
 
 
%%
figure
 
t=1:size(c);
subplot(2,2,1)
set(gca,'xdir','reverse')
hold on
plot(t,s,'color','black')
plot(t,c,'color','blue')
plot(t,p,'color','red')
xlabel('Days to expiration')
ylabel('Price')
%legend('stock','call','put','location','southwest')
xlim([1 count])
line([1 count],[100 100],'color',[0.7 0.7 0.7])
hold off
 
subplot(2,2,2)
hold on
plot(t,v,'color','black')
plot(t,cd,'color','blue');
plot(t,pd,'color','red');
set(gca,'xdir','reverse')
xlim([1 count])
hold off
 
% delta hedging
portfolio.delta = zeros(count,1);
portfolio.baseCount = zeros(count,1);
portfolio.position = zeros(count,1);
portfolio.basePrice = zeros(count,1);
optCount = 100;
isCall = -1;
baseCount = 0;
basePrice = 0;
isLong = sign(optCount);
 
%assume options are non-variational,so
if isCall == 1
optionPremium = c(count);
else
optionPremium = p(count);
end
portfolio.cash = -isLong * optionPremium * optCount;
 
for t=count:-1:1
disp(['WELCOME TO t=' num2str(t) '=============='])
disp(['reminder: s(t) = ' num2str(t)])
if isCall == 1
optDelta = optCount * cd(t);
else
optDelta = optCount * pd(t);
end
disp(['option delta = ' num2str(optDelta)])
fullDelta = optDelta + baseCount;
disp(['portfolio delta = ' num2str(fullDelta)])
if abs(fullDelta) >= 1
amountToBuy = -isLong * round(fullDelta);
disp(['hedging with ' num2str(amountToBuy) ' of underlying'])
if (baseCount + amountToBuy) == 0
basePrice = s(t);
else
% if direction is identical, increase and reprice
if sign(amountToBuy) == sign(baseCount) || baseCount == 0
basePrice = (baseCount * basePrice + amountToBuy * s(t)) ...
/ (baseCount + amountToBuy);
disp(['s = ' num2str(s(t)) ', underlying priced at ' num2str(basePrice)])
else
% otherwise, convert to cash
% for example, if we sold 10 at 50 and buy out 2 @ 40
% then amt=-2, (50 - 40) * -2
cashProfit = -sign(baseCount) * (basePrice - s(t)) * amountToBuy;
portfolio.cash = portfolio.cash + cashProfit;
disp(['realized for ' num2str(cashProfit) ' entry price was ' ...
num2str(basePrice) ', s(t)=' num2str(s(t))])
end
end
baseCount = baseCount + amountToBuy;
disp(['we have ' num2str(baseCount) ' underlyings'])
end
portfolio.delta(t) = fullDelta;
portfolio.baseCount(t) = baseCount;
portfolio.basePrice(t) = basePrice;
%optPos is what those options are worth right now
if isCall == 1
optPos = optCount * c(t) * isLong;
else
optPos = optCount * p(t) * isLong;
end
basePos = baseCount * (s(t)-basePrice);
portfolio.position(t) = optPos + basePos + portfolio.cash;
disp(['underlying is worth ' num2str(basePos)])
disp(['options are worth ' num2str(optPos)])
disp(['and available cash ' num2str(portfolio.cash)])
disp(' ')
end
 
t=1:count;
subplot(2,2,3);
hold on
plot(t, portfolio.delta,'color','black');
plot(t, portfolio.position,'color','green');
plot(t, portfolio.baseCount, 'color',[0.5 0.5 0.5]);
plot(t, portfolio.basePrice,'color','red');
set(gca,'xdir','reverse');
xlim([1 count]);
xlabel(['Final balance = ' num2str(portfolio.position(1))]);
hold off
 
subplot(2,2,4);
hold on
plot(t,gamma,'color','black');
set(gca,'xdir','reverse');
xlim([1 count]);
hold off

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.