%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                        %
%                Option pricing for european call options                %
%                                                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

close all
clc

%%% Parameters
K = 100;            % strike price of the option
sigma = 0.2;        % volatility of the underlying

%%% Option pricing formula
C = @(pi,T) pi.*normcdf((log(pi/K) + sigma^2*T/2)./(sigma*sqrt(T)))...
    - K*normcdf((log(pi/K) - sigma^2*T/2)./(sigma*sqrt(T)));

%%% Discretisation in time and space
T = 0:0.01:1;           % different times to maturity
pi = 70:0.5:130;        % range for initial values/prices of the underlying
[X,Y] = ndgrid(pi,T);   % grid for calculation and 3D plot

%%% Calculation
Cmat = C(X,Y);
payout = (pi - K).*(pi>K);    % payout at maturity
Cmat(:,1) = payout;

%%% Plot
set(figure,'units','normalized','outerposition',[0,0,1,1])% maximize figure

subplot(2,2,[1;3])
surf(X,Y,Cmat)
title('Option Price European Call with $K = 100$ and $\sigma = 0.2$',...
    'interpreter','latex','FontSize',16)
xlabel('$\pi^1$','interpreter','latex','FontSize',16)
ylabel('$T$','interpreter','latex','FontSize',16)
zlabel('$\pi^C(\pi^1,T)$','interpreter','latex','FontSize',16)

subplot(2,2,2)
plot(pi,payout,'LineWidth',1.5)
for k = 0.1:0.2:0.9
    hold on
    plot(pi,C(pi,k),'LineWidth',1.5)
end
title('Option Price European Call with $K = 100$ and $\sigma = 0.2$',...
    'interpreter','latex','FontSize',16)
xlabel('$\pi^1$','interpreter','latex','FontSize',16)
ylabel('$\pi^C(\pi^1,T)$','interpreter','latex','FontSize',16)
legend({'$T = 0$','$T = 0.1$','$T = 0.3$','$T = 0.5$',...
    '$T = 0.7$','$T = 0.9$'},'Location','northwest',...
    'interpreter','latex','FontSize',14)

subplot(2,2,4)
T = 0.1:0.1:10;
for k = 80:10:120
    plot(T,C(k,T),'LineWidth',1.5)
    hold on
end
hold off
title('Option Price European Call with $K = 100$ and $\sigma = 0.2$',...
    'interpreter','latex','FontSize',16)
xlabel('$T$','interpreter','latex','FontSize',16)
ylabel('$\pi^C(\pi^1,T)$','interpreter','latex','FontSize',16)
legend({'$\pi^1 = 80$','$\pi^1 = 90$','$\pi^1 = 100$',...
    '$\pi^1 = 110$','$\pi^1 = 120$'},'Location','northwest',...
    'interpreter','latex','FontSize',14)