Instantly share code, notes, and snippets.

# nesteruk/deltahedgingSecret Created Feb 8, 2013

What would you like to do?
Simple delta hedging simulation
 %% 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