📄 optionvanilla.m
字号:
function [oprice] = optionvanilla(S,E,r,T,sigma,divYield,nSims,nSteps,type, showPlot)
%OPTIONVANILLA Price European call or put using montecarlo simulation.
%
% oprice = optionvanilla(S0,Strike,rf,T,sigma,divYield,nSims,nSteps,type,showPlot)
%
% Inputs:
% S0 - Current asset price.
% Strike - Exercise price.
% rf - Risk free rate.
% T - Maturity date in years.
% sigma - Volatility of the asset price.
% divYield - Dividend yield.
% nSims - Number of simulations.
% nSteps - Number of steps.
% type - Option type: 'call' or 'put'.
% showPlot - Determines whether the MonteCarlo paths are plotted{true | [false]}.
%
% Outputs:
% oprice - Price of the vanilla option
%
% Mayeda Reyes-Kattar 2007
%------------------------------------------------
%Checking inputs
%------------------------------------------------
%Force the state of the random number generator
randn('state' , 0);
%Determine the size of the timestep
Dt = T/nSteps;
%Generate the random numbers
mat = randn(nSteps , nSims);
mat = exp( (r-divYield-sigma^2/2)*Dt + sigma*sqrt(Dt).*mat );
mat = cumprod(mat , 1);
mat = mat.*S;
if(isempty(type))
type = 'call';
end
if (nargin < 10)
showPlot = false;
end
%------------------------------------------------
%Calculate the option price
%------------------------------------------------
if strcmp(type,'call')
oFactor=1; %call
else
oFactor=-1; %put
end
V = exp(-r*T) * max(oFactor*(mat(end,:)-E) , 0);
oprice = 1/nSims*sum(V);
if(showPlot)
plot(linspace(0, T, nSteps+1)', [S*ones(1,size(mat,2)); mat]);
ylabel('Price of Underlying');
xlabel('Time');
title('Monte Carlo Paths for Option Pricing');
grid on
set(gcf, 'NumberTitle', 'off');
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -